using EggLink.DanhengServer.Configuration; using EggLink.DanhengServer.Database.Account; using EggLink.DanhengServer.Util; using Microsoft.Data.Sqlite; using SqlSugar; using System.Reflection; namespace EggLink.DanhengServer.Database { public class DatabaseHelper { public Logger logger = new("Database"); public ConfigContainer config = ConfigManager.Config; public static SqlSugarScope? sqlSugarScope; public static DatabaseHelper? Instance; public DatabaseHelper() { var f = new FileInfo(config.Path.DatabasePath + "/" + config.Database.DatabaseName); if (!f.Exists && f.Directory != null) { f.Directory.Create(); } sqlSugarScope = new(new ConnectionConfig() { ConnectionString = $"Data Source={f.FullName};", DbType = DbType.Sqlite, IsAutoCloseConnection = true, }); Instance = this; } public void Initialize() { logger.Info("Initializing database..."); switch (config.Database.DatabaseType) { case "sqlite": InitializeSqlite(); break; default: logger.Error("Unsupported database type"); break; } } public static void InitializeSqlite() { var baseType = typeof(BaseDatabaseData); var assembly = typeof(BaseDatabaseData).Assembly; var types = assembly.GetTypes().Where(t => t.IsSubclassOf(baseType)); foreach (var type in types) { typeof(DatabaseHelper).GetMethod("InitializeSqliteTable")?.MakeGenericMethod(type).Invoke(null, null); } } public static void InitializeSqliteTable() where T : class, new() { try { sqlSugarScope?.Queryable().ToList(); } catch { sqlSugarScope?.CodeFirst.InitTables(); } } public T? GetInstance(long uid) where T : class, new() { try { return sqlSugarScope?.Queryable().Where(it => (it as BaseDatabaseData).Uid == uid).First(); } catch { logger.Error("Unsupported type"); return null; } } public List? GetAllInstance() where T : class, new() { try { return sqlSugarScope?.Queryable().ToList(); } catch { logger.Error("Unsupported type"); return null; } } public void SaveInstance(T instance) where T : class, new() { sqlSugarScope?.Insertable(instance).ExecuteCommand(); } public void UpdateInstance(T instance) where T : class, new() { sqlSugarScope?.Updateable(instance).ExecuteCommand(); } public void DeleteInstance(T instance) where T : class, new() { sqlSugarScope?.Deleteable(instance).ExecuteCommand(); } } }