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; private static readonly Dictionary _lock = []; 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 object GetLock(int uid) { if (!_lock.TryGetValue(uid, out object? value)) { value = new(); _lock[uid] = value; } return value; } 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 { lock (GetLock((int)uid)) { return sqlSugarScope?.Queryable().Where(it => (it as BaseDatabaseData)!.Uid == uid).First(); } } catch (Exception e) { logger.Error("Unsupported type", e); return null; } } public T GetInstanceOrCreateNew(int uid) where T : class, new() { var instance = GetInstance(uid); if (instance == null) { instance = new(); (instance as BaseDatabaseData)!.Uid = uid; SaveInstance(instance); } return instance; } public List? GetAllInstance() where T : class, new() { try { return sqlSugarScope?.Queryable().ToList(); } catch(Exception e) { logger.Error("Unsupported type", e); return null; } } public void SaveInstance(T instance) where T : class, new() { lock (GetLock((instance as BaseDatabaseData)!.Uid)) { sqlSugarScope?.Insertable(instance).ExecuteCommand(); } } public void UpdateInstance(T instance) where T : class, new() { lock (GetLock((instance as BaseDatabaseData)!.Uid)) { sqlSugarScope?.Updateable(instance).ExecuteCommand(); } } public void DeleteInstance(T instance) where T : class, new() { lock (GetLock((instance as BaseDatabaseData)!.Uid)) { sqlSugarScope?.Deleteable(instance).ExecuteCommand(); } } } }