From 82c7425044a25219650bfe6a59be2ead4fb927e3 Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Mon, 14 Apr 2025 17:33:25 +0800 Subject: [PATCH] fixed Dapper Mysql,changed interface IEGSaveData and all tools --- addons/EGFramework/Module/EGSave.cs | 4 +- .../Module/SaveTools/EGByteSave.cs | 2 +- .../EGFramework/Module/SaveTools/EGCsvSave.cs | 98 +++++++++++++- .../EGFramework/Module/SaveTools/EGDapper.cs | 39 ------ .../Module/SaveTools/EGJsonSave.cs | 4 +- .../Module/SaveTools/EGLiteDBSave.cs | 35 ++++- .../Module/SaveTools/EGMysqlSave.cs | 128 ++++++++++++++++++ .../Module/SaveTools/SaveToolsInterface.cs | 7 +- 8 files changed, 268 insertions(+), 49 deletions(-) delete mode 100644 addons/EGFramework/Module/SaveTools/EGDapper.cs create mode 100644 addons/EGFramework/Module/SaveTools/EGMysqlSave.cs diff --git a/addons/EGFramework/Module/EGSave.cs b/addons/EGFramework/Module/EGSave.cs index a7cabad..3517891 100644 --- a/addons/EGFramework/Module/EGSave.cs +++ b/addons/EGFramework/Module/EGSave.cs @@ -37,7 +37,7 @@ namespace EGFramework #region Load Data or Object and Unload public void LoadDataFile(string path) where TSaveData:IEGSaveData,IEGSave,new(){ TSaveData saveData = new TSaveData(); - saveData.InitSaveFile(path); + saveData.InitSave(path); if(!DataBaseFiles.ContainsKey(path)){ DataBaseFiles.Add(path,saveData); }else{ @@ -67,7 +67,7 @@ namespace EGFramework public void LoadObjectFile(string path) where TSaveObject:IEGSaveObject,IEGSave,new(){ TSaveObject saveObject = new TSaveObject(); - saveObject.InitSaveFile(path); + saveObject.InitSave(path); if(!ObjectFiles.ContainsKey(path)){ ObjectFiles.Add(path, saveObject); }else{ diff --git a/addons/EGFramework/Module/SaveTools/EGByteSave.cs b/addons/EGFramework/Module/SaveTools/EGByteSave.cs index a900ea6..3e20286 100644 --- a/addons/EGFramework/Module/SaveTools/EGByteSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGByteSave.cs @@ -58,7 +58,7 @@ namespace EGFramework } } - public void InitSaveFile(string path) + public void InitSave(string path) { ReadDataBlock(path); } diff --git a/addons/EGFramework/Module/SaveTools/EGCsvSave.cs b/addons/EGFramework/Module/SaveTools/EGCsvSave.cs index de1249e..1896037 100644 --- a/addons/EGFramework/Module/SaveTools/EGCsvSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGCsvSave.cs @@ -9,6 +9,9 @@ using System.Reflection; namespace EGFramework { + /// + /// CSV Save tools, support read and write CSV file.The dataKey param is not use.please use "" or any string. + /// public class EGCsvSave : IEGSaveData, IEGSave, IEGSaveReadOnly { public bool IsReadOnly { get; set; } @@ -20,7 +23,7 @@ namespace EGFramework private string ReadText { set; get; } - public void InitSaveFile(string path) + public void InitSave(string path) { ReadDataBlock(path); } @@ -214,6 +217,99 @@ namespace EGFramework } return sourceList.Where(expression.Compile()); } + + public void AddData(string dataKey, TData data) + { + if(IsReadOnly){ + throw new Exception("This file is readonly! can't set any data to file."); + } + string[] csvSet = new string[CsvDataHeader.Keys.Count()]; + foreach(PropertyInfo property in data.GetType().GetProperties()){ + CsvParamAttribute csvParam = property.GetCustomAttribute(); + if(csvParam != null && CsvDataHeader.ContainsKey(csvParam._name)){ + csvSet[CsvDataHeader[csvParam._name]] = property.GetValue(data).ToString(); + } + } + CsvDataBlock.Add(csvSet); + this.WriteDataBlock(DefaultPath); + } + + public void AddData(string dataKey, IEnumerable dataSet) + { + if(IsReadOnly){ + throw new Exception("This file is readonly! can't set any data to file."); + } + foreach(TData data in dataSet){ + string[] csvSet = new string[CsvDataHeader.Keys.Count()]; + foreach(PropertyInfo property in data.GetType().GetProperties()){ + CsvParamAttribute csvParam = property.GetCustomAttribute(); + if(csvParam != null && CsvDataHeader.ContainsKey(csvParam._name)){ + csvSet[CsvDataHeader[csvParam._name]] = property.GetValue(data).ToString(); + } + } + CsvDataBlock.Add(csvSet); + } + this.WriteDataBlock(DefaultPath); + } + + public void RemoveData(string dataKey, object id) + { + if(IsReadOnly){ + throw new Exception("This file is readonly! can't set any data to file."); + } + bool IsAdd = false; + int dataID = 0; + if(id.GetType()==typeof(int)){ + dataID = (int)id; + }else if(int.TryParse(id.ToString() ,out dataID)){ + throw new Exception("Id cannot be convert to int!"); + } + if(dataID>=CsvDataBlock.Count() || dataID < 0){ + IsAdd = true; + } + if(IsAdd){ + return; + }else{ + CsvDataBlock.RemoveAt(dataID); + } + this.WriteDataBlock(DefaultPath); + } + + public void UpdateData(string dataKey, TData data, object id) + { + if(IsReadOnly){ + throw new Exception("This file is readonly! can't set any data to file."); + } + bool IsAdd = false; + int dataID = 0; + if(id.GetType()==typeof(int)){ + dataID = (int)id; + }else if(int.TryParse(id.ToString() ,out dataID)){ + throw new Exception("Id cannot be convert to int!"); + } + if(dataID>=CsvDataBlock.Count() || dataID < 0){ + IsAdd = true; + } + string[] csvSet = new string[CsvDataHeader.Keys.Count()]; + foreach(PropertyInfo property in data.GetType().GetProperties()){ + CsvParamAttribute csvParam = property.GetCustomAttribute(); + if(csvParam != null && CsvDataHeader.ContainsKey(csvParam._name)){ + csvSet[CsvDataHeader[csvParam._name]] = property.GetValue(data).ToString(); + } + } + if(!IsAdd){ + CsvDataBlock[dataID] = csvSet; + }else{ + throw new Exception("Data not found!"); + } + this.WriteDataBlock(DefaultPath); + } + + public IEnumerable GetKeys() + { + return CsvDataHeader.Keys; + } + } [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] diff --git a/addons/EGFramework/Module/SaveTools/EGDapper.cs b/addons/EGFramework/Module/SaveTools/EGDapper.cs deleted file mode 100644 index 91ef747..0000000 --- a/addons/EGFramework/Module/SaveTools/EGDapper.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Data.Common; -using System.Linq.Expressions; - -//ORM Save tools. First support SQLite and MySQL,In future we will support other Database who implement DBConnection. -namespace EGFramework{ - public class EGDapper : IEGSave, IEGSaveData - { - /// - /// - /// - /// files conn Str or address ip port,username and passwd - public void InitSaveFile(string conn) - { - throw new System.NotImplementedException(); - } - - public IEnumerable FindData(string dataKey, Expression> expression) where TData : new() - { - throw new NotImplementedException(); - } - - public IEnumerable GetAll(string dataKey) where TData : new() - { - throw new NotImplementedException(); - } - - public TData GetData(string dataKey, object id) where TData : new() - { - throw new NotImplementedException(); - } - - public void SetData(string dataKey, TData data, object id) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGJsonSave.cs b/addons/EGFramework/Module/SaveTools/EGJsonSave.cs index 749170e..4eaa7f2 100644 --- a/addons/EGFramework/Module/SaveTools/EGJsonSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGJsonSave.cs @@ -16,7 +16,7 @@ namespace EGFramework private JObject SaveObject{ get { if(_SaveObject == null){ - InitSaveFile(DefaultPath); + InitSave(DefaultPath); } return _SaveObject; } @@ -25,7 +25,7 @@ namespace EGFramework /// /// Init a new save data file or load an other file with json suffix, if you want to load other save data, please use this function to reload; /// - public void InitSaveFile(string path) + public void InitSave(string path) { DefaultPath = path; if(!File.Exists(path)){ diff --git a/addons/EGFramework/Module/SaveTools/EGLiteDBSave.cs b/addons/EGFramework/Module/SaveTools/EGLiteDBSave.cs index 8f59939..6aba9ce 100644 --- a/addons/EGFramework/Module/SaveTools/EGLiteDBSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGLiteDBSave.cs @@ -15,13 +15,13 @@ namespace EGFramework private LiteDatabase Database{ get { if(_Database == null){ - InitSaveFile(DefaultPath); + InitSave(DefaultPath); } return _Database; } } - public void InitSaveFile(string path) + public void InitSave(string path) { DefaultPath = path; if (!Directory.Exists(Path.GetDirectoryName(DefaultPath))) @@ -60,6 +60,35 @@ namespace EGFramework return collection.Find(expression); } - + public void AddData(string dataKey, TData data) + { + LiteCollection collection = (LiteCollection)Database.GetCollection(dataKey); + collection.Insert(data); + } + + public void AddData(string dataKey, IEnumerable data) + { + LiteCollection collection = (LiteCollection)Database.GetCollection(dataKey); + collection.Insert(data); + } + + public void RemoveData(string dataKey,object id) + { + LiteCollection collection = (LiteCollection)Database.GetCollection(dataKey); + if(collection.FindById((BsonValue)id)==null){ + collection.Delete((BsonValue)id); + } + } + + public void UpdateData(string dataKey, TData data, object id) + { + LiteCollection collection = (LiteCollection)Database.GetCollection(dataKey); + collection.Update((BsonValue)id,data); + } + public IEnumerable GetKeys() + { + return Database.GetCollectionNames(); + } + } } \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs b/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs new file mode 100644 index 0000000..995252e --- /dev/null +++ b/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Data.Common; +using System.Linq.Expressions; +using MySql.Data.MySqlClient; +using Dapper; +using System.Reflection; +using System.Linq; + +//ORM Save tools. First support SQLite and MySQL,In future we will support other Database who implement DBConnection. +namespace EGFramework{ + /// + /// This Class used Dapper for operate MySQL database. + /// + public class EGMysqlSave : IEGSave, IEGSaveData + { + private string Conn { set; get; } + public MySqlConnection Connection { set; get; } + public bool IsInit { set; get; } + /// + /// "server="+Address+";port="+Port+";uid="+UserName+";pwd="+Password+";database="+DataBase+";" + /// + /// files conn Str or address ip port,username and passwd + public void InitSave(string conn) + { + try + { + Connection = new MySqlConnection(conn); + IsInit = true; + } + catch (System.Exception e) + { + EG.Print("e:" + e); + } + } + + + + public IEnumerable GetAll(string dataKey) where TData : new() + { + IEnumerable result = Connection.Query("select * from "+dataKey); + 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}); + return result; + } + + public IEnumerable FindData(string dataKey, Expression> expression) where TData : new() + { + IEnumerable sourceList = Connection.Query("select * from "+dataKey); + return sourceList.Where(expression.Compile()); + } + + public void SetData(string dataKey, TData data, object id) + { + throw new NotImplementedException(); + } + + public void AddData(string dataKey, TData data) + { + // throw new System.NotImplementedException(); + Type DataType = typeof(TData); + var properties = DataType.GetProperties(); + string keySet = ""; + string keySetParam = ""; + foreach(PropertyInfo key in properties){ + keySet += key.Name + ","; + keySetParam += "@" + key.Name + ","; + } + keySet = keySet.TrimEnd(','); + keySetParam = keySetParam.TrimEnd(','); + int count = Connection.Execute(@"insert "+dataKey+"("+keySet+") values("+keySetParam+")",data); + //EG.Print("count:" + count); + } + + public void AddData(string dataKey, IEnumerable data) + { + Type DataType = typeof(TData); + var properties = DataType.GetProperties(); + string keySet = ""; + string keySetParam = ""; + foreach(PropertyInfo key in properties){ + keySet += key.Name + ","; + keySetParam += "@" + key.Name + ","; + } + keySet = keySet.TrimEnd(','); + keySetParam = keySetParam.TrimEnd(','); + string sql = @"insert "+dataKey+"("+keySet+") values("+keySetParam+")"; + int count = Connection.Execute(sql,data); + //EG.Print("count:" + count); + } + + public void RemoveData(string dataKey, object id) + { + Type DataType = typeof(TData); + int count = Connection.Execute(@"delete from "+dataKey+" where ID = @ID",new {ID = id}); + //EG.Print("count:" + count); + } + + public void UpdateData(string dataKey, TData data, object id) + { + Type DataType = typeof(TData); + EG.Print("----"+DataType.Name); + var properties = DataType.GetProperties(); + string keyMap = ""; + foreach(PropertyInfo key in properties){ + if(key.Name=="ID"){ + continue; + } + keyMap += key.Name + " = @"+key.Name +","; + } + keyMap = keyMap.TrimEnd(','); + string sql = @"update "+DataType.Name+" set "+ keyMap +" where ID = " + id; + EG.Print(sql); + int count = Connection.Execute(sql,data); + //EG.Print("count:" + count); + } + + public IEnumerable GetKeys() + { + throw new NotImplementedException(); + } + + } +} \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs index 8d0747b..79000b9 100644 --- a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs +++ b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs @@ -8,7 +8,7 @@ namespace EGFramework { #region Sync_Interface public interface IEGSave{ - void InitSaveFile(string path); + void InitSave(string path); } public interface IEGSaveReadOnly{ @@ -24,6 +24,7 @@ namespace EGFramework TData GetData(string dataKey,object id) where TData : new(); IEnumerable GetAll(string dataKey) where TData : new(); IEnumerable FindData(string dataKey,Expression> expression) where TData : new(); + IEnumerable GetKeys(); } public interface IEGSaveObject : IEGSaveObjectReadOnly{ @@ -41,6 +42,10 @@ namespace EGFramework public interface IEGSaveData : IEGSaveDataReadOnly{ void SetData(string dataKey,TData data,object id); + void AddData(string dataKey,TData data); + void AddData(string dataKey,IEnumerable data); + void RemoveData(string dataKey,object id); + void UpdateData(string dataKey,TData data,object id); } #endregion