SharpRSS/SharpRss/DbAccess_Old.cs

417 lines
22 KiB
C#
Raw Normal View History

2023-05-18 01:27:11 +02:00
using System;
using System.Collections.Generic;
using System.Data;
2023-05-18 01:27:11 +02:00
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Data.Sqlite;
using Serilog;
using SharpRss.Models;
2023-05-29 16:14:26 +02:00
namespace SharpRss
2023-05-18 01:27:11 +02:00
{
public static class DbAccess_Old
2023-05-18 01:27:11 +02:00
{
2023-06-02 15:45:10 +02:00
//TODO: Rename group => category.
//TODO: Reworking feed => model/db implementation.
2023-05-29 16:14:26 +02:00
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";
2023-05-18 01:27:11 +02:00
2023-05-22 15:55:21 +02:00
// Groups
2023-06-02 15:45:10 +02:00
public static async Task<HashSet<CategoryModel>> GetCategoriesAsync(string? categoryId = null)
2023-05-20 00:04:45 +02:00
{
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
2023-06-02 15:45:10 +02:00
await using SqliteCommand cmd = new SqliteCommand(categoryId != null ? $"SELECT * FROM {GroupTable} WHERE id=@gId;" : $"SELECT * FROM {GroupTable}", dbc)
2023-05-21 21:56:37 +02:00
{
Parameters =
{
2023-06-02 15:45:10 +02:00
new SqliteParameter("gId", categoryId)
2023-05-21 21:56:37 +02:00
}
};
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
2023-06-02 15:45:10 +02:00
HashSet<CategoryModel> categories = new HashSet<CategoryModel>();
2023-05-29 16:14:26 +02:00
await using SqliteCommand cmdFeedCount = new SqliteCommand($"SELECT COUNT(*) FROM {FeedTable} WHERE group_id=@groupId", dbc);
2023-05-21 21:56:37 +02:00
while (reader.Read())
{
2023-05-23 15:04:02 +02:00
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;
2023-06-02 15:45:10 +02:00
categories.Add(new CategoryModel()
2023-05-21 21:56:37 +02:00
{
Name = reader["name"].ToString(),
2023-05-23 15:04:02 +02:00
FeedCount = count,
2023-05-21 21:56:37 +02:00
HexColor = reader["hex_color"].ToString(),
Icon = reader["icon"].ToString(),
Id = reader["id"].ToString()
});
}
2023-06-02 15:45:10 +02:00
return categories;
2023-05-20 00:04:45 +02:00
}
2023-06-02 15:45:10 +02:00
public static async Task<bool> SetCategoryAsync(CategoryModel categoryModel)
2023-05-18 01:27:11 +02:00
{
2023-05-21 21:56:37 +02:00
bool result = false;
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
2023-05-29 16:14:26 +02:00
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)
2023-05-18 01:27:11 +02:00
{
2023-05-21 21:56:37 +02:00
Parameters =
{
2023-06-02 15:45:10 +02:00
new SqliteParameter("id", categoryModel.Id),
new SqliteParameter("hexColor", categoryModel.HexColor),
new SqliteParameter("icon", categoryModel.Icon),
new SqliteParameter("name", categoryModel.Name)
2023-05-21 21:56:37 +02:00
}
};
int affected = await cmd.ExecuteNonQueryAsync();
if (affected != 0)
result = true;
2023-05-18 01:27:11 +02:00
return result;
}
2023-06-02 15:45:10 +02:00
public static async Task<bool> RemoveCategoryAsync(CategoryModel categoryModel)
2023-05-18 01:27:11 +02:00
{
2023-05-21 21:56:37 +02:00
bool result = false;
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
2023-06-02 10:56:30 +02:00
// Remove the group and remove the feeds that were part of the group.
2023-05-29 16:14:26 +02:00
await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {GroupTable} WHERE id=@id; UPDATE {FeedTable} SET group_id=NULL WHERE group_id=@id", dbc)
2023-05-18 01:27:11 +02:00
{
2023-05-21 21:56:37 +02:00
Parameters =
{
2023-06-02 15:45:10 +02:00
new SqliteParameter("id", categoryModel.Id)
2023-05-21 21:56:37 +02:00
}
};
int affected = await cmd.ExecuteNonQueryAsync();
if (affected != 0)
result = true;
2023-05-18 01:27:11 +02:00
return result;
}
2023-05-22 15:55:21 +02:00
// Feeds
2023-05-26 23:59:02 +02:00
/// <summary>
///
/// </summary>
/// <param name="groupId">Empty = ungrouped feeds | null = all feeds | id = grouped feeds</param>
/// <returns></returns>
2023-05-29 16:14:26 +02:00
public static async Task<HashSet<FeedModel>> GetFeedsAsync(string? groupId = null)
2023-05-18 01:27:11 +02:00
{
2023-05-21 21:56:37 +02:00
HashSet<FeedModel> feeds = new HashSet<FeedModel>();
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
2023-05-29 16:14:26 +02:00
await using SqliteCommand cmd = new SqliteCommand(groupId != null ? $"SELECT * FROM {FeedTable} WHERE group_id=@groupId" : $"SELECT * FROM {FeedTable}", dbc)
2023-05-20 00:04:45 +02:00
{
2023-05-21 21:56:37 +02:00
Parameters =
{
2023-05-26 23:59:02 +02:00
new SqliteParameter("groupId", groupId ?? string.Empty)
2023-05-21 21:56:37 +02:00
}
};
2023-05-22 15:55:21 +02:00
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
2023-05-26 23:59:02 +02:00
while (!reader.IsClosed && reader.Read())
feeds.Add(await ReaderToFeedModel(reader));
2023-05-21 21:56:37 +02:00
return feeds;
2023-05-18 01:27:11 +02:00
}
public static async Task<FeedModel?> GetFeedAsync(string url)
2023-05-18 01:27:11 +02:00
{
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
FeedModel? feed = null;
//TODO: Use dapper to simplify this query.
await using SqliteCommand cmd = new SqliteCommand($"SELECT * FROM {FeedTable} WHERE url=@Url", dbc)
{
Parameters = { new SqliteParameter("Url", url) }
};
2023-06-02 10:56:30 +02:00
try
{
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
if (reader.Read())
feed = await ReaderToFeedModel(reader);
}
catch (Exception ex)
{
Log.Error(ex, "Error while fetching feed from db.");
}
2023-05-26 23:59:02 +02:00
return feed;
}
2023-06-02 10:56:30 +02:00
public static async Task<FeedModel?> SetFeedAsync(FeedModel feedModel)
{
2023-06-02 10:56:30 +02:00
FeedModel? feed = null;
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
2023-06-02 11:56:48 +02:00
await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {FeedTable} (url, title, group_id, feed_type, description, language, copyright, date_added, last_updated, image_url, original_document) VALUES (@url, @title, @groupId, @feedType, @description, @language, @copyright, @dateAdded, @lastUpdated, @imageUrl, @originalDoc); SELECT * FROM {FeedTable} WHERE url=@url", dbc)
2023-05-20 00:04:45 +02:00
{
2023-06-02 10:56:30 +02:00
Parameters =
2023-05-21 21:56:37 +02:00
{
new SqliteParameter("url", feedModel.EncodedUrl ?? string.Empty),
2023-06-02 10:56:30 +02:00
new SqliteParameter("title", feedModel.Title ?? string.Empty),
2023-06-04 19:00:46 +02:00
new SqliteParameter("groupId", feedModel.CategoryId ?? string.Empty),
2023-06-02 10:56:30 +02:00
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.PublicationDate?.ToUnixTimeMilliseconds() ?? 0),
2023-06-02 11:56:48 +02:00
new SqliteParameter("lastUpdated", feedModel.LastUpdated?.ToUnixTimeMilliseconds() ?? 0),
new SqliteParameter("imageUrl", feedModel.ImageUrl ?? string.Empty)
2023-06-02 10:56:30 +02:00
}
};
try
{
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
feed = await ReaderToFeedModel(reader);
}
catch (Exception ex)
{
Log.Error(ex, "Database error, adding feed model to database failed!");
return feed;
}
return feed;
/*await using SqliteTransaction transaction = dbc.BeginTransaction();
try
{
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)
2023-05-29 16:14:26 +02:00
{
2023-06-02 10:56:30 +02:00
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)
},
Transaction = transaction
};
await cmd.ExecuteNonQueryAsync();
}
await transaction.CommitAsync();
2023-05-29 16:14:26 +02:00
}
2023-06-02 10:56:30 +02:00
catch (Exception ex)
{
await transaction.RollbackAsync();
Log.Error(ex, "Error on inserting feeds to db.");
return false;
}*/
2023-05-18 01:27:11 +02:00
}
public static async Task FetchFeedItemsAsync(string[]? feedUrls = null)
{
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open();
HashSet<FeedModel> dbFeeds = new HashSet<FeedModel>();
string query = $"SELECT * FROM {FeedTable}";
if (feedUrls != null)
{
List<string>? feedUrlsFormatted = feedUrls.Select(s => $"'{s}'").ToList();
query = $"SELECT * FROM {FeedTable} WHERE url IN(({string.Join(", ", feedUrlsFormatted)}))";
}
await using SqliteCommand fetchCmd = new SqliteCommand(query, dbc);
await using SqliteDataReader reader = await fetchCmd.ExecuteReaderAsync();
while (reader.Read())
{
dbFeeds.Add(await ReaderToFeedModel(reader));
}
HashSet<FeedItemModel> feedItems = new HashSet<FeedItemModel>();
foreach (var dbFeed in dbFeeds)
{
/*GenericSyndicationFeed syndication = new GenericSyndicationFeed();
syndication.Load(dbFeed.OriginalDocument);*/
2023-06-02 10:56:30 +02:00
//TODO: Get items and add to db
}
}
2023-05-29 16:14:26 +02:00
public static async Task<bool> RemoveFeedAsync(FeedModel feedModel)
2023-05-22 13:26:27 +02:00
{
bool result = false;
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
2023-06-02 10:56:30 +02:00
await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {FeedTable} WHERE url=@Url; UPDATE {FeedItemTable} SET feed_id=NULL WHERE feed_id=@Url", dbc)
2023-05-22 13:26:27 +02:00
{
Parameters =
{
new SqliteParameter("Url", feedModel.EncodedUrl)
2023-05-22 13:26:27 +02:00
}
};
int affected = await cmd.ExecuteNonQueryAsync();
if (affected != 0)
result = true;
return result;
}
2023-05-22 15:55:21 +02:00
// Feed items
2023-05-29 16:14:26 +02:00
public static async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string[]? feedIds = null)
2023-05-22 13:26:27 +02:00
{
List<string>? formattedIds = feedIds?.Select(s => $"'{s}'").ToList();
2023-05-24 19:27:22 +02:00
HashSet<FeedItemModel> feedItems = new HashSet<FeedItemModel>();
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
2023-05-24 19:27:22 +02:00
await using SqliteCommand cmd = new SqliteCommand(
formattedIds != null
2023-05-29 16:14:26 +02:00
? $"SELECT * FROM {FeedItemTable} WHERE feed_id IN ({string.Join(", ", formattedIds)})"
: $"SELECT * FROM {FeedItemTable}", dbc);
2023-05-24 19:27:22 +02:00
2023-05-22 13:26:27 +02:00
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
while (reader.Read())
{
2023-05-26 23:59:02 +02:00
FeedItemModel feedItemModel = new FeedItemModel()
2023-05-22 13:26:27 +02:00
{
Id = reader["id"].ToString(),
EncodedFeedUrl = reader["feed_url"].ToString(),
2023-05-24 19:27:22 +02:00
Read = int.TryParse(reader["read"].ToString(), out int parsedValue) && parsedValue != 0,
2023-05-22 13:26:27 +02:00
Title = reader["title"].ToString(),
Description = reader["description"].ToString(),
Link = reader["link"].ToString(),
2023-05-26 23:59:02 +02:00
LastUpdated =
DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(reader["last_updated"].ToString())),
PublishingDate =
DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(reader["publishing_date"].ToString())),
Authors = reader["authors"].ToString().ToString().Split(','),
2023-05-22 13:26:27 +02:00
Categories = reader["categories"].ToString().Split(','),
Content = reader["content"].ToString()
2023-05-26 23:59:02 +02:00
};
feedItems.Add(feedItemModel);
2023-05-22 13:26:27 +02:00
}
2023-05-24 19:27:22 +02:00
return feedItems;
2023-05-22 13:26:27 +02:00
}
2023-05-29 18:02:24 +02:00
2023-05-29 16:14:26 +02:00
public static async Task<int> SetFeedItemsAsync(HashSet<FeedItemModel> items)
2023-05-22 13:26:27 +02:00
{
int result = 0;
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
await using SqliteTransaction transaction = dbc.BeginTransaction();
await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {FeedItemTable} (id, feed_url, read, title, description, link, last_updated, publishing_date, authors, categories, content)" +
$"VALUES (IFNULL((SELECT id FROM {FeedItemTable} WHERE link=@link), @id), @feedUrl, @read, @title, @description, @link, @lastUpdated, @publishingDate, @authors, @categories, @content)", dbc)
2023-05-29 21:26:37 +02:00
{
Transaction = transaction
};
2023-05-22 13:26:27 +02:00
foreach (FeedItemModel item in items)
{
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqliteParameter("id", item.Id ?? string.Empty));
cmd.Parameters.Add(new SqliteParameter("feedUrl", item.EncodedFeedUrl ?? string.Empty));
2023-05-22 13:26:27 +02:00
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));
2023-05-24 19:27:22 +02:00
cmd.Parameters.Add(new SqliteParameter("lastUpdated", item.LastUpdated?.ToUnixTimeMilliseconds()));
2023-05-22 13:26:27 +02:00
cmd.Parameters.Add(new SqliteParameter("publishingDate", item.PublishingDate?.ToUnixTimeMilliseconds() ?? 0));
cmd.Parameters.Add(new SqliteParameter("authors", item.Authors != null ? string.Join(',', item.Authors) : 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));
2023-05-26 23:59:02 +02:00
if (dbc.State != ConnectionState.Open)
dbc.Open();
2023-05-22 13:26:27 +02:00
int affected = await cmd.ExecuteNonQueryAsync();
if (affected == 0)
2023-06-02 10:56:30 +02:00
Log.Verbose("Could not set feed item: {FeedLink}", item.Link);
2023-05-22 13:26:27 +02:00
else
2023-06-02 10:56:30 +02:00
result += affected;
}
transaction.Commit();
2023-05-29 16:14:26 +02:00
return result;
2023-05-22 13:26:27 +02:00
}
2023-05-29 16:14:26 +02:00
public static async Task<bool> RemoveFeedItemAsync(FeedItemModel itemModel)
2023-05-22 13:26:27 +02:00
{
bool result = false;
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
2023-05-29 16:14:26 +02:00
await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {FeedItemTable} WHERE id=@id", dbc)
2023-05-22 13:26:27 +02:00
{
Parameters =
{
new SqliteParameter("id", itemModel.Id)
}
};
int affected = await cmd.ExecuteNonQueryAsync();
if (affected != 0)
result = true;
return result;
}
public static async Task<CategoryModel?> GetGroupFromFeedItemAsync(FeedItemModel feedItem)
{
CategoryModel? result = null;
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
2023-05-29 16:14:26 +02:00
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)
}
};
2023-05-24 19:27:22 +02:00
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
HashSet<CategoryModel>? groups = null;
2023-06-04 19:00:46 +02:00
/*if (reader.Read())
groups = await GetCategoriesAsync(reader["group_id"].ToString());*/
if (groups != null && groups.Any())
result = groups.FirstOrDefault();
return result;
}
2023-05-29 16:14:26 +02:00
private static async Task<FeedModel> ReaderToFeedModel(SqliteDataReader reader)
2023-05-26 23:59:02 +02:00
{
2023-06-02 10:56:30 +02:00
FeedModel fetchedFeed = new FeedModel()
2023-05-26 23:59:02 +02:00
{
EncodedUrl = reader["url"].ToString(),
2023-05-26 23:59:02 +02:00
Title = reader["title"].ToString(),
2023-06-04 19:00:46 +02:00
CategoryId = reader["group_id"].ToString(),
2023-05-26 23:59:02 +02:00
FeedType = reader["feed_type"].ToString(),
Description = reader["description"].ToString(),
Language = reader["language"].ToString(),
Copyright = reader["copyright"].ToString(),
PublicationDate = DateTimeOffset.FromUnixTimeMilliseconds(long.TryParse(reader["date_added"].ToString(), out long parsedVal) ? parsedVal : 0),
2023-05-26 23:59:02 +02:00
LastUpdated = DateTimeOffset.FromUnixTimeMilliseconds(long.TryParse(reader["last_updated"].ToString(), out long lastUpdated) ? lastUpdated : 0),
ImageUrl = reader["image_url"].ToString()
2023-05-26 23:59:02 +02:00
};
2023-06-02 10:56:30 +02:00
//TODO: Set group on insert
/*var groupFetch = await GetGroupsAsync(fetchedFeed.GroupId);
2023-05-26 23:59:02 +02:00
if (groupFetch.Any())
fetchedFeed.Group = groupFetch.First();
else
2023-06-02 10:56:30 +02:00
Log.Warning("Could not get group from feed: {FeedId}", fetchedFeed.Id);*/
2023-05-26 23:59:02 +02:00
return fetchedFeed;
}
2023-06-02 10:56:30 +02:00
2023-05-29 16:14:26 +02:00
//===
public static async void Initialize()
{
if (_isInitialized) return;
2023-05-18 01:27:11 +02:00
Log.Verbose("Checking database...");
HashSet<string> failed = new HashSet<string>();
2023-05-29 16:14:26 +02:00
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
2023-05-26 23:59:02 +02:00
dbc.Open();
Log.Verbose("Checking table: {Table}", "category");
var queryResponse = await dbc.QueryAsync("CREATE TABLE IF NOT EXISTS category (name STRING NOT NULL, hex_color STRING NOT NULL, icon STRING, id STRING PRIMARY KEY)");
if (queryResponse.Any()) failed.Add("category");
2023-06-02 10:56:30 +02:00
Log.Verbose("Checking table: {Table}", "feed");
queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS feed (url STRING PRIMARY KEY, 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");
Log.Verbose("Checking table: {Table}", "feed_item");
queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS feed_item (id STRING PRIMARY KEY, feed_url STRING, read INT, title STRING, description STRING, link STRING, last_updated INT, publishing_date INT, authors STRING, categories STRING, content STRING)");
if (queryResponse.Any()) failed.Add("feed_item");
2023-06-02 10:56:30 +02:00
if (failed.Any())
2023-05-18 01:27:11 +02:00
{
var joined = string.Join(',', failed);
Log.Error("Failed to initialize table(s): {TableNames}", joined);
2023-05-18 01:27:11 +02:00
}
else
Log.Verbose("Checking database done!");
2023-05-29 16:14:26 +02:00
_isInitialized = true;
2023-05-18 01:27:11 +02:00
}
}
}