From 9181ac94c4e800227846996971bc5341ea31a9ae Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Fri, 4 Jul 2025 14:51:22 +0800 Subject: [PATCH] add drop table by key --- .../Module/SaveTools/Data/EGDapper.cs | 126 +++++++++++------- addons/EGFramework/Module/SaveTools/EGSave.cs | 12 +- .../Module/SaveTools/SaveToolsInterface.cs | 3 +- 3 files changed, 89 insertions(+), 52 deletions(-) diff --git a/addons/EGFramework/Module/SaveTools/Data/EGDapper.cs b/addons/EGFramework/Module/SaveTools/Data/EGDapper.cs index a8d3840..e361746 100644 --- a/addons/EGFramework/Module/SaveTools/Data/EGDapper.cs +++ b/addons/EGFramework/Module/SaveTools/Data/EGDapper.cs @@ -8,7 +8,7 @@ using Dapper; //ORM Save tools. First support SQLite and MySQL,In future we will support other Database who implement DBConnection. namespace EGFramework { - public abstract class EGDapper : IEGSave, IEGSaveData, IEGCanGetDBConnection,IEGDataBase + public abstract class EGDapper : IEGSave, IEGSaveData, IEGCanGetDBConnection, IEGDataBase { public DbConnection Connection { get; set; } public string ExceptionMsg; @@ -16,42 +16,48 @@ namespace EGFramework public abstract void InitSave(string conn); public IEnumerable GetAll(string dataKey) where TData : new() { - IEnumerable result = Connection.Query("select * from "+dataKey); + IEnumerable result = Connection.Query("select * from " + dataKey); return result; } public IEnumerable GetPage(string dataKey, int pageIndex, int pageSize) where TData : new() { - if(pageIndex <= 0){ + if (pageIndex <= 0) + { pageIndex = 1; } int startPointer = (pageIndex - 1) * pageSize; - IEnumerable result = Connection.Query("select * from "+dataKey+" limit "+startPointer+","+pageSize); + IEnumerable result = Connection.Query("select * from " + dataKey + " limit " + startPointer + "," + pageSize); return result; } public TData GetData(string dataKey, object id) where TData : new() { - TData result = Connection.QuerySingle("select * from "+dataKey+" where ID = @ID",new {ID = id}); + TData result = Connection.QuerySingle("select * from " + dataKey + " where ID = @ID", new { ID = id }); return result; } public IEnumerable FindData(string dataKey, Expression> expression) where TData : new() { - IEnumerable sourceList = Connection.Query("select * from "+dataKey); + IEnumerable sourceList = Connection.Query("select * from " + dataKey); return sourceList.Where(expression.Compile()); } public void SetData(string dataKey, TData data, object id) { - if(data == null) + if (data == null) { throw new ArgumentNullException(nameof(data)); - }else{ - if(this.ContainsData(dataKey,id)){ - UpdateData(dataKey,data,id); - }else{ - AddData(dataKey,data); + } + else + { + if (this.ContainsData(dataKey, id)) + { + UpdateData(dataKey, data, id); + } + else + { + AddData(dataKey, data); } //EG.Print("data:" + data); } @@ -59,7 +65,7 @@ namespace EGFramework public void AddData(string dataKey, TData data) { - if(data == null) + if (data == null) { throw new ArgumentNullException(nameof(data)); } @@ -68,13 +74,14 @@ namespace EGFramework var properties = DataType.GetProperties(); string keySet = ""; string keySetParam = ""; - foreach(PropertyInfo key in properties){ + foreach (PropertyInfo key in properties) + { keySet += key.Name + ","; - keySetParam += "@" + key.Name + ","; + keySetParam += "@" + key.Name + ","; } keySet = keySet.TrimEnd(','); keySetParam = keySetParam.TrimEnd(','); - int count = Connection.Execute(@"insert "+dataKey+"("+keySet+") values("+keySetParam+")",data); + int count = Connection.Execute(@"insert " + dataKey + "(" + keySet + ") values(" + keySetParam + ")", data); //EG.Print("count:" + count); } @@ -84,26 +91,29 @@ namespace EGFramework var properties = DataType.GetProperties(); string keySet = ""; string keySetParam = ""; - foreach(PropertyInfo key in properties){ + foreach (PropertyInfo key in properties) + { keySet += key.Name + ","; - keySetParam += "@" + key.Name + ","; + keySetParam += "@" + key.Name + ","; } keySet = keySet.TrimEnd(','); keySetParam = keySetParam.TrimEnd(','); - string sql = @"insert "+dataKey+"("+keySet+") values("+keySetParam+")"; - int count = Connection.Execute(sql,data); + string sql = @"insert " + dataKey + "(" + keySet + ") values(" + keySetParam + ")"; + int count = Connection.Execute(sql, data); //EG.Print("count:" + count); } - public void AddData(string dataKey,Dictionary data) + public void AddData(string dataKey, Dictionary data) { - if(data == null) + if (data == null) { throw new ArgumentNullException(nameof(data)); } string keySet = ""; string keySetParam = ""; - foreach(KeyValuePair param in data){ - if (param.Key == "ID" || param.Key == "Id" || param.Key == "id"){ + foreach (KeyValuePair param in data) + { + if (param.Key == "ID" || param.Key == "Id" || param.Key == "id") + { continue; } keySet += param.Key + ","; @@ -118,8 +128,8 @@ namespace EGFramework } keySet = keySet.TrimEnd(','); keySetParam = keySetParam.TrimEnd(','); - EG.Print("insert into "+dataKey+"("+keySet+") values("+keySetParam+")"); - int count = Connection.Execute("insert into "+dataKey+"("+keySet+") values("+keySetParam+")"); + EG.Print("insert into " + dataKey + "(" + keySet + ") values(" + keySetParam + ")"); + int count = Connection.Execute("insert into " + dataKey + "(" + keySet + ") values(" + keySetParam + ")"); } public int RemoveData(string dataKey, object id) @@ -131,23 +141,25 @@ namespace EGFramework public void UpdateData(string dataKey, TData data, object id) { - if(data == null) + if (data == null) { throw new ArgumentNullException(nameof(data)); } Type DataType = typeof(TData); var properties = DataType.GetProperties(); string keyMap = ""; - foreach(PropertyInfo key in properties){ - if(key.Name=="ID" || key.Name == "id" || key.Name == "Id"){ + foreach (PropertyInfo key in properties) + { + if (key.Name == "ID" || key.Name == "id" || key.Name == "Id") + { continue; } - keyMap += key.Name + " = @"+key.Name +","; + keyMap += key.Name + " = @" + key.Name + ","; } keyMap = keyMap.TrimEnd(','); - string sql = @"update "+dataKey+" set "+ keyMap +" where ID = " + id; + string sql = @"update " + dataKey + " set " + keyMap + " where ID = " + id; EG.Print(sql); - int count = Connection.Execute(sql,data); + int count = Connection.Execute(sql, data); //EG.Print("count:" + count); } @@ -158,17 +170,19 @@ namespace EGFramework throw new ArgumentNullException(nameof(data)); } string keyMap = ""; - foreach (KeyValuePair param in data) { - if (param.Key == "ID" || param.Key == "Id" || param.Key == "id"){ + foreach (KeyValuePair param in data) + { + if (param.Key == "ID" || param.Key == "Id" || param.Key == "id") + { continue; } if (param.Value is string) { - keyMap += param.Key + " = '"+param.Value +"',"; + keyMap += param.Key + " = '" + param.Value + "',"; } else { - keyMap += param.Key + " = "+param.Value +","; + keyMap += param.Key + " = " + param.Value + ","; } } keyMap = keyMap.TrimEnd(','); @@ -176,9 +190,9 @@ namespace EGFramework { id = "'" + id + "'"; } - string sql = @"update "+dataKey+" set "+ keyMap +" where ID = " + id; + string sql = @"update " + dataKey + " set " + keyMap + " where ID = " + id; EG.Print(sql); - int count = Connection.Execute(sql,data); + int count = Connection.Execute(sql, data); } public IEnumerable GetKeys() @@ -196,10 +210,13 @@ namespace EGFramework { try { - var result = Connection.QuerySingle("select * from "+dataKey+" where ID = @ID",new {ID = id}); - if(result == null){ + var result = Connection.QuerySingle("select * from " + dataKey + " where ID = @ID", new { ID = id }); + if (result == null) + { return false; - }else{ + } + else + { return true; } } @@ -225,16 +242,35 @@ namespace EGFramework // throw new System.NotImplementedException(); if (this.ContainsKey(dataKey)) { - //Drop table if has. - Connection.Execute(@"DROP TABLE IF EXISTS "+dataKey+""); + EG.Print("Table " + dataKey + " has been created!"); + return; } string createSql = typeof(TData).ToCreateTableSQL(dataKey); int count = Connection.Execute(createSql); } - public void CreateTable(string dataKey,Dictionary tableParam) + public void CreateTable(string dataKey, Dictionary tableParam) + { + if (this.ContainsKey(dataKey)) + { + EG.Print("Table " + dataKey + " has been created!"); + return; + } + Dictionary tableType = new Dictionary(); + foreach (KeyValuePair pair in tableParam) + { + tableType.Add(pair.Key, pair.Value.GetType()); + } + string createSql = tableType.ToCreateTableSQL(dataKey); + int count = Connection.Execute(createSql); + } + + public void DropTable(string dataKey) { - throw new NotImplementedException(); + if (this.ContainsKey(dataKey)) + { + Connection.Execute(@"DROP TABLE IF EXISTS "+dataKey+""); + } } } } \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGSave.cs b/addons/EGFramework/Module/SaveTools/EGSave.cs index ed518df..78279a9 100644 --- a/addons/EGFramework/Module/SaveTools/EGSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGSave.cs @@ -36,7 +36,7 @@ namespace EGFramework LoadObjectFile("SaveData/DefaultJsonSave.json"); } #region Load Data or Object and Unload - public IEGSaveData LoadDataFile(string path) where TSaveData : IEGSaveData, IEGSave, new() + public TSaveData LoadDataFile(string path) where TSaveData : IEGSaveData, IEGSave, new() { TSaveData saveData = new TSaveData(); saveData.InitSave(path); @@ -51,7 +51,7 @@ namespace EGFramework return saveData; } - public IEGSaveDataReadOnly ReadData(string key, string data) where TReadOnlyData : IEGSaveDataReadOnly, IEGSaveReadOnly, new() + public TReadOnlyData ReadData(string key, string data) where TReadOnlyData : IEGSaveDataReadOnly, IEGSaveReadOnly, new() { TReadOnlyData readOnlyData = new TReadOnlyData(); readOnlyData.InitReadOnly(data); @@ -66,7 +66,7 @@ namespace EGFramework return readOnlyData; } - public IEGSaveDataReadOnly ReadData(string key, byte[] data) where TReadOnlyData : IEGSaveDataReadOnly, IEGSaveReadOnly, new() + public TReadOnlyData ReadData(string key, byte[] data) where TReadOnlyData : IEGSaveDataReadOnly, IEGSaveReadOnly, new() { TReadOnlyData readOnlyData = new TReadOnlyData(); readOnlyData.InitReadOnly(data); @@ -81,7 +81,7 @@ namespace EGFramework return readOnlyData; } - public IEGSaveObject LoadObjectFile(string path) where TSaveObject:IEGSaveObject,IEGSave,new(){ + public TSaveObject LoadObjectFile(string path) where TSaveObject:IEGSaveObject,IEGSave,new(){ TSaveObject saveObject = new TSaveObject(); saveObject.InitSave(path); if(!ObjectFiles.ContainsKey(path)){ @@ -92,7 +92,7 @@ namespace EGFramework return saveObject; } - public IEGSaveObjectReadOnly ReadObject(string key, string data) where TReadOnlyObject : IEGSaveObjectReadOnly, IEGSaveReadOnly, new() + public TReadOnlyObject ReadObject(string key, string data) where TReadOnlyObject : IEGSaveObjectReadOnly, IEGSaveReadOnly, new() { TReadOnlyObject readOnlyObject = new TReadOnlyObject(); readOnlyObject.InitReadOnly(data); @@ -107,7 +107,7 @@ namespace EGFramework return readOnlyObject; } - public IEGSaveObjectReadOnly ReadObject(string key,byte[] data) where TReadOnlyObject:IEGSaveObjectReadOnly,IEGSaveReadOnly,new(){ + public TReadOnlyObject ReadObject(string key,byte[] data) where TReadOnlyObject:IEGSaveObjectReadOnly,IEGSaveReadOnly,new(){ TReadOnlyObject readOnlyObject = new TReadOnlyObject(); readOnlyObject.InitReadOnly(data); if(!ObjectReadOnly.ContainsKey(key)){ diff --git a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs index 5e6bf61..f11cab3 100644 --- a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs +++ b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs @@ -36,7 +36,8 @@ namespace EGFramework public interface IEGDataBase : IEGCanGetDBConnection { void CreateTable(string dataKey); - void CreateTable(string dataKey,Dictionary tableParam); + void CreateTable(string dataKey, Dictionary tableParam); + void DropTable(string dataKey); } #endregion