using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Threading.Tasks; using Dapper; using Microsoft.Data.Sqlite; using Serilog; using SharpRss.Models; namespace SharpRss { public static class DbAccess { private static bool _isInitialized; private static readonly string ConnectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};"; private const string GroupTable = "group_data"; private const string FeedTable = "feed_data"; private const string FeedItemTable = "feed_item_data"; // Groups public static async Task> GetGroupsAsync(string? groupId = null) { CheckInitialized(); await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); await using SqliteCommand cmd = new SqliteCommand(groupId != null ? $"SELECT * FROM {GroupTable} WHERE id=@gId;" : $"SELECT * FROM {GroupTable}", dbc) { Parameters = { new SqliteParameter("gId", groupId) } }; await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); HashSet groups = new HashSet(); await using SqliteCommand cmdFeedCount = new SqliteCommand($"SELECT COUNT(*) FROM {FeedTable} WHERE group_id=@groupId", dbc); while (reader.Read()) { cmdFeedCount.Parameters.Clear(); cmdFeedCount.Parameters.Add(new SqliteParameter("groupId", reader["id"].ToString())); using SqliteDataReader countReader = await cmdFeedCount.ExecuteReaderAsync(); int count = countReader.Read() ? countReader.GetInt32(0) : 0; groups.Add(new GroupModel() { Name = reader["name"].ToString(), FeedCount = count, HexColor = reader["hex_color"].ToString(), Icon = reader["icon"].ToString(), Id = reader["id"].ToString() }); } return groups; } public static async Task SetGroupAsync(GroupModel groupModel) { bool result = false; await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {GroupTable} (id, hex_color, icon, name) VALUES (IFNULL((SELECT id FROM {GroupTable} WHERE name=@name), @id), @hexColor, @icon, @name)", dbc) { Parameters = { new SqliteParameter("id", groupModel.Id), new SqliteParameter("hexColor", groupModel.HexColor), new SqliteParameter("icon", groupModel.Icon), new SqliteParameter("name", groupModel.Name) } }; int affected = await cmd.ExecuteNonQueryAsync(); if (affected != 0) result = true; return result; } public static async Task RemoveGroupAsync(GroupModel groupModel) { bool result = false; await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); // Remove the group and remove the feeds that were part of the group. await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {GroupTable} WHERE id=@id; UPDATE {FeedTable} SET group_id=NULL WHERE group_id=@id", dbc) { Parameters = { new SqliteParameter("id", groupModel.Id) } }; int affected = await cmd.ExecuteNonQueryAsync(); if (affected != 0) result = true; return result; } // Feeds /// /// /// /// Empty = ungrouped feeds | null = all feeds | id = grouped feeds /// public static async Task> GetFeedsAsync(string? groupId = null) { HashSet feeds = new HashSet(); await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); await using SqliteCommand cmd = new SqliteCommand(groupId != null ? $"SELECT * FROM {FeedTable} WHERE group_id=@groupId" : $"SELECT * FROM {FeedTable}", dbc) { Parameters = { new SqliteParameter("groupId", groupId ?? string.Empty) } }; await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); while (!reader.IsClosed && reader.Read()) feeds.Add(await ReaderToFeedModel(reader)); return feeds; } public static async Task GetFeedAsync(string feedId) { await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); FeedModel? feed = null; await using SqliteCommand cmd = new SqliteCommand($"SELECT * FROM {FeedTable} WHERE id=@id", dbc) { Parameters = { new SqliteParameter("id", feedId) } }; await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); if (reader.Read()) feed = await ReaderToFeedModel(reader); return feed; } public static async Task SetFeedsAsync(IEnumerable feedModels) { FeedModel? resultModel = null; await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); await using SqliteTransaction transaction = dbc.BeginTransaction(); foreach (var feedModel in feedModels) { //await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {_feedTable} (id, url, title, group_id, feed_type, description, language, copyright, date_added, last_updated, image_url, original_document) VALUES (IFNULL((SELECT id FROM {_feedTable} WHERE url=@url), @id), @url, @title, @groupId, @feedType, @description, @language, @copyright, IFNULL((SELECT date_added FROM {_feedTable} WHERE id=@id), @dateAdded), @lastUpdated, @imageUrl, @originalDoc); SELECT * FROM {_feedTable} WHERE url=@url", dbc) await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {FeedTable} (id, url, title, group_id, feed_type, description, language, copyright, date_added, last_updated, image_url, original_document) VALUES (IFNULL((SELECT id FROM {FeedTable} WHERE url=@url), @id), @url, @title, @groupId, @feedType, @description, @language, @copyright, IFNULL((SELECT date_added FROM {FeedTable} WHERE id=@id), @dateAdded), @lastUpdated, @imageUrl, @originalDoc)", dbc) { Parameters = { new SqliteParameter("id", feedModel.Id ?? string.Empty), new SqliteParameter("url", feedModel.Url ?? string.Empty), new SqliteParameter("title", feedModel.Title ?? string.Empty), new SqliteParameter("groupId", feedModel.GroupId ?? string.Empty), new SqliteParameter("feedType", feedModel.FeedType ?? string.Empty), new SqliteParameter("description", feedModel.Description ?? string.Empty), new SqliteParameter("language", feedModel.Language ?? string.Empty), new SqliteParameter("copyright", feedModel.Copyright ?? string.Empty), new SqliteParameter("dateAdded", feedModel.DateAdded?.ToUnixTimeMilliseconds()), new SqliteParameter("lastUpdated", feedModel.LastUpdated?.ToUnixTimeMilliseconds()), new SqliteParameter("imageUrl", feedModel.ImageUrl ?? string.Empty), new SqliteParameter("originalDoc", feedModel.OriginalDocument ?? string.Empty) } }; await cmd.ExecuteNonQueryAsync(); } await transaction.CommitAsync(); return resultModel; } public static async Task RemoveFeedAsync(FeedModel feedModel) { bool result = false; await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {FeedTable} WHERE id=@id; UPDATE {FeedItemTable} SET feed_id=NULL WHERE feed_id=@id", dbc) { Parameters = { new SqliteParameter("id", feedModel.Id) } }; int affected = await cmd.ExecuteNonQueryAsync(); if (affected != 0) result = true; return result; } // Feed items public static async Task> GetFeedItemsAsync(string[]? feedIds = null) { List? formattedIds = feedIds?.Select(s => $"'{s}'").ToList(); HashSet feedItems = new HashSet(); await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); await using SqliteCommand cmd = new SqliteCommand( formattedIds != null ? $"SELECT * FROM {FeedItemTable} WHERE feed_id IN ({string.Join(", ", formattedIds)})" : $"SELECT * FROM {FeedItemTable}", dbc); await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); while (reader.Read()) { FeedItemModel feedItemModel = new FeedItemModel() { Id = reader["id"].ToString(), FeedId = reader["feed_id"].ToString(), Read = int.TryParse(reader["read"].ToString(), out int parsedValue) && parsedValue != 0, Title = reader["title"].ToString(), Description = reader["description"].ToString(), Link = reader["link"].ToString(), LastUpdated = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(reader["last_updated"].ToString())), PublishingDate = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(reader["publishing_date"].ToString())), Author = reader["author"].ToString(), Categories = reader["categories"].ToString().Split(','), Content = reader["content"].ToString() }; if (feedItemModel is { FeedId: { } }) { FeedModel? feedModel = await GetFeedAsync(feedItemModel.FeedId); feedItemModel.Feed = feedModel; } feedItems.Add(feedItemModel); } return feedItems; } public static async Task SetFeedItemsAsync(HashSet items) { int result = 0; await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); await using SqliteTransaction transaction = dbc.BeginTransaction(); await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {FeedItemTable} (id, feed_id, read, title, description, link, last_updated, publishing_date, author, categories, content)" + $"VALUES (IFNULL((SELECT id FROM {FeedItemTable} WHERE link=@link), @id), @feedId, @read, @title, @description, @link, @lastUpdated, @publishingDate, @author, @categories, @content)", dbc); foreach (FeedItemModel item in items) { cmd.Parameters.Clear(); cmd.Parameters.Add(new SqliteParameter("id", item.Id ?? string.Empty)); cmd.Parameters.Add(new SqliteParameter("feedId", item.FeedId ?? string.Empty)); cmd.Parameters.Add(new SqliteParameter("read", item.Read ? 1 : 0)); cmd.Parameters.Add(new SqliteParameter("type", item.Type ?? string.Empty)); cmd.Parameters.Add(new SqliteParameter("title", item.Title ?? string.Empty)); cmd.Parameters.Add(new SqliteParameter("description", item.Description ?? string.Empty)); cmd.Parameters.Add(new SqliteParameter("link", item.Link ?? string.Empty)); cmd.Parameters.Add(new SqliteParameter("lastUpdated", item.LastUpdated?.ToUnixTimeMilliseconds())); cmd.Parameters.Add(new SqliteParameter("publishingDate", item.PublishingDate?.ToUnixTimeMilliseconds() ?? 0)); cmd.Parameters.Add(new SqliteParameter("author", item.Author ?? string.Empty)); cmd.Parameters.Add(new SqliteParameter("categories", item.Categories != null ? string.Join(',', item.Categories) : string.Empty)); cmd.Parameters.Add(new SqliteParameter("content", item.Content ?? string.Empty)); if (dbc.State != ConnectionState.Open) dbc.Open(); int affected = await cmd.ExecuteNonQueryAsync(); if (affected == 0) Log.Verbose("Could not set feed item: {FeedLink}", item.Link); else result += affected; } transaction.Commit(); return result; } public static async Task RemoveFeedItemAsync(FeedItemModel itemModel) { bool result = false; await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {FeedItemTable} WHERE id=@id", dbc) { Parameters = { new SqliteParameter("id", itemModel.Id) } }; int affected = await cmd.ExecuteNonQueryAsync(); if (affected != 0) result = true; return result; } public static async Task GetGroupFromFeedItemAsync(FeedItemModel feedItem) { GroupModel? result = null; await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); await using SqliteCommand cmd = new SqliteCommand($"SELECT * FROM {GroupTable} WHERE id=(SELECT group_id FROM {FeedTable} WHERE id=@fId)", dbc) { Parameters = { new SqliteParameter ("fId", feedItem.Id) } }; await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); HashSet? groups = null; if (reader.Read()) groups = await GetGroupsAsync(reader["group_id"].ToString()); if (groups != null && groups.Any()) result = groups.FirstOrDefault(); return result; } private static async Task ReaderToFeedModel(SqliteDataReader reader) { FeedModel fetchedFeed = new FeedModel(reader["url"].ToString()) { Id = reader["id"].ToString(), Title = reader["title"].ToString(), GroupId = reader["group_id"].ToString(), FeedType = reader["feed_type"].ToString(), Description = reader["description"].ToString(), Language = reader["language"].ToString(), Copyright = reader["copyright"].ToString(), DateAdded = DateTimeOffset.FromUnixTimeMilliseconds(long.TryParse(reader["date_added"].ToString(), out long parsedVal) ? parsedVal : 0), LastUpdated = DateTimeOffset.FromUnixTimeMilliseconds(long.TryParse(reader["last_updated"].ToString(), out long lastUpdated) ? lastUpdated : 0), ImageUrl = reader["image_url"].ToString(), OriginalDocument = reader["original_document"].ToString() }; var groupFetch = await GetGroupsAsync(fetchedFeed.GroupId); if (groupFetch.Any()) fetchedFeed.Group = groupFetch.First(); else Log.Warning("Could not get group from feed: {FeedId}", fetchedFeed.Id); return fetchedFeed; } //=== private static void CheckInitialized() { if (!_isInitialized) throw new TypeInitializationException(nameof(DbAccess), null); } public static async void Initialize() { if (_isInitialized) return; Log.Verbose("Checking database..."); HashSet failed = new HashSet(); await using SqliteConnection dbc = new SqliteConnection(ConnectionString); dbc.Open(); Log.Verbose("Checking table: {Table}", GroupTable); var queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS {GroupTable} (name STRING NOT NULL, hex_color STRING NOT NULL, icon STRING, id STRING PRIMARY KEY)"); if (queryResponse.Any()) failed.Add("category_data"); Log.Verbose("Checking table: {Table}", FeedTable); queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS {FeedTable} (id STRING PRIMARY KEY, url STRING NOT NULL, title STRING, group_id STRING, feed_type STRING, description STRING, language STRING, copyright STRING, date_added INT, last_updated INT, image_url STRING, original_document STRING)"); if (queryResponse.Any()) failed.Add("feed_data"); Log.Verbose("Checking table: {Table}", FeedItemTable); queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS {FeedItemTable} (id STRING PRIMARY KEY, feed_id STRING, read INT, title STRING, description STRING, link STRING, last_updated INT, publishing_date INT, author STRING, categories STRING, content STRING)"); if (queryResponse.Any()) failed.Add("feed_item_data"); if (failed.Any()) { var joined = string.Join(',', failed); Log.Error("Failed to initialize table(s): {TableNames}", joined); } else Log.Verbose("Checking database done!"); _isInitialized = true; } } }