Reworking DbAccess 'GetFeedItemsAsync' function

This commit is contained in:
Max 2023-06-10 19:43:30 +02:00
parent 4ff0f7c0c3
commit 13fd44bc72

View File

@ -76,50 +76,6 @@ namespace SharpRss
{
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open();
/*await using SqliteCommand cmd = new SqliteCommand(@"INSERT OR REPLACE INTO feed
(url,
title,
category_id,
feed_type,
feed_version,
description,
language,
copyright,
publication_date,
last_updated,
categories,
image_url)
VALUES (
@Url,
@Title,
@CategoryId,
@FeedType,
@FeedVersion,
@Description,
@Language,
@Copyright,
@PublicationDate,
@LastUpdated,
@Categories,
@ImageUrl)", dbc)
{
Parameters =
{
new SqliteParameter("Url", feed.OriginalUrl ?? string.Empty),
new SqliteParameter("Title", feed.Title ?? string.Empty),
new SqliteParameter("CategoryId", feed.CategoryId ?? string.Empty),
new SqliteParameter("FeedType", feed.FeedType ?? string.Empty),
new SqliteParameter("FeedVersion", feed.FeedVersion ?? string.Empty),
new SqliteParameter("Description", feed.Description ?? string.Empty),
new SqliteParameter("Language", feed.Language ?? string.Empty),
new SqliteParameter("Copyright", feed.Copyright ?? string.Empty),
new SqliteParameter("PublicationDate", feed.PublicationDate?.ToUnixTimeMilliseconds() ?? 0),
new SqliteParameter("LastUpdated", feed.LastUpdated?.ToUnixTimeMilliseconds() ?? 0),
new SqliteParameter("Categories", feed.Categories != null && feed.Categories.Any() ? string.Join(',', feed.Categories) : string.Empty),
new SqliteParameter("ImageUrl", feed.ImageUrl ?? string.Empty)
}
};
int exec = await cmd.ExecuteNonQueryAsync();*/
int affected = await dbc.ExecuteAsync(@"INSERT OR REPLACE INTO feed
(url,
title,
@ -200,25 +156,6 @@ namespace SharpRss
await using SqliteTransaction dbTransaction = dbc.BeginTransaction();
foreach (FeedItemModel item in items)
{
/*await using SqliteCommand cmd = new SqliteCommand(@"INSERT OR REPLACE INTO feed_item (id, feed_url, read, title, description, link, last_updated, publishing_date, authors, categories, content)
VALUES (@Id, @FeedUrl, @Read, @Title, @Description, @Link, @LastUpdated, @PublishingDate, @Authors, @Categories, @Content)", dbc, dbTransaction)
{
Parameters =
{
new SqliteParameter("Id", item.Id ?? string.Empty),
new SqliteParameter("FeedUrl", item.FeedUrl ?? string.Empty),
new SqliteParameter("Read", item.Read.ToString()),
new SqliteParameter("Title", item.Title),
new SqliteParameter("Description", item.Description ?? string.Empty),
new SqliteParameter("Link", item.Link ?? string.Empty),
new SqliteParameter("LastUpdated", item.LastUpdated?.ToUnixTimeMilliseconds() ?? 0),
new SqliteParameter("PublishingDate", item.PublishingDate?.ToUnixTimeMilliseconds() ?? 0),
new SqliteParameter("Authors", item.Authors.Any() ? string.Join(',', item.Authors) : string.Empty),
new SqliteParameter("Categories", item.Categories.Any() ? string.Join(',', item.Categories) : string.Empty),
new SqliteParameter("Content", item.Content ?? string.Empty)
}
};
int exec = await cmd.ExecuteNonQueryAsync();*/
int affected = await dbc.ExecuteAsync(@"INSERT OR REPLACE INTO feed_item (id, feed_url, read, title, description, link, last_updated, publishing_date, authors, categories, content)
VALUES (@Id, @FeedUrl, @Read, @Title, @Description, @Link, @LastUpdated, @PublishingDate, @Authors, @Categories, @Content)",
transaction: dbTransaction,
@ -240,12 +177,20 @@ namespace SharpRss
}
dbTransaction.Commit();
}
private static string FormatParametersFromArray(string[] dbParams)
{
string[] formatted = dbParams.Select(s => $"'{s}'").ToArray();
return string.Join(", ", formatted);
}
public static async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string[]? feedUrls)
{
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open();
HashSet<FeedItemModel> items = new HashSet<FeedItemModel>();
await using DbDataReader reader = await dbc.ExecuteReaderAsync(feedUrls == null ? "SELECT * FROM feed_item" : "SELECT * FROM feed_item WHERE feed_item.feed_url IN(@Urls)", new { Urls = feedUrls });
await using DbDataReader reader = await dbc.ExecuteReaderAsync(feedUrls == null ? "SELECT * FROM feed_item" : $"SELECT * FROM feed_item WHERE feed_url IN({FormatParametersFromArray(feedUrls)})");
//NOTE(dbg): While debugging this part of the function the code below (Reader.ReadAsync) will return 0, because the debugger already reads the items from the reader.
while (await reader.ReadAsync())
{
FeedItemModel feedItemModel = new FeedItemModel()
@ -264,6 +209,7 @@ namespace SharpRss
};
items.Add(feedItemModel);
}
Log.Debug("Fetching feed items resulted: {ItemCount} item(s)", items.Count);
return items;
}