diff --git a/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs b/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs index 0b599e1..2834805 100644 --- a/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Data.Common; using System.Linq.Expressions; using MySql.Data.MySqlClient; using Dapper; @@ -160,7 +159,6 @@ namespace EGFramework{ { return false; } - } } } \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGSqliteSave.cs b/addons/EGFramework/Module/SaveTools/EGSqliteSave.cs new file mode 100644 index 0000000..5f565a1 --- /dev/null +++ b/addons/EGFramework/Module/SaveTools/EGSqliteSave.cs @@ -0,0 +1,176 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using Dapper; +using Microsoft.Data.Sqlite; + +namespace EGFramework{ + public class EGSqliteSave : IEGSave, IEGSaveData + { + private string Conn { set; get; } + public SqliteConnection Connection { set; get; } + public bool IsInit { set; get; } + public string ExceptionMsg; + + /// + /// If path not exist, create it. + /// + /// + public void InitSave(string path) + { + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + InitDatabase(path); + } + + /// + /// Init database with path. + /// + /// name is the file path. + public void InitDatabase(string dataBaseName) + { + Connection = new SqliteConnection("Data Source="+dataBaseName+";Mode=ReadWriteCreate;"); // Open the connection: + try + { + Connection.Open(); + } + catch (Exception ex) + { + ExceptionMsg = ex.ToString(); + } + } + + 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) + { + if(data == null) + { + throw new ArgumentNullException(nameof(data)); + }else{ + if(this.ContainsData(dataKey,id)){ + UpdateData(dataKey,data,id); + }else{ + AddData(dataKey,data); + } + //EG.Print("data:" + data); + } + } + + public void AddData(string dataKey, TData data) + { + if(data == null) + { + throw new ArgumentNullException(nameof(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) + { + 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) + { + if(data == null) + { + throw new ArgumentNullException(nameof(data)); + } + 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() + { + IEnumerable result = Connection.Query("show tables"); + return result; + } + + public bool ContainsKey(string dataKey) + { + return GetKeys().Contains(dataKey); + } + + public bool ContainsData(string dataKey, object id) + { + try + { + var result = Connection.QuerySingle("select * from "+dataKey+" where ID = @ID",new {ID = id}); + if(result == null){ + return false; + }else{ + return true; + } + } + catch (System.Exception) + { + return false; + } + } + } +} \ No newline at end of file