diff --git a/Example/SaveSystem/Script/ViewSaveSystem.cs b/Example/SaveSystem/Script/ViewSaveSystem.cs index 15461a5..6bed41b 100644 --- a/Example/SaveSystem/Script/ViewSaveSystem.cs +++ b/Example/SaveSystem/Script/ViewSaveSystem.cs @@ -30,9 +30,29 @@ namespace EGFramework.Examples.Test { public void TestMySQL() { - EGMysqlSave mysqlSave = this.EGSave().Load("server=" + "localhost" + ";port=" + "3306" + ";uid=" + "root" + ";pwd=" + "root" + ";database=" + "Test3" + ";"); + EGDapper mysqlSave = this.EGSave().Load("server=" + "localhost" + ";port=" + "3306" + ";uid=" + "root" + ";pwd=" + "root" + ";database=" + "Test3" + ";"); + bool isExist = mysqlSave.ContainsKey("DataStudent"); + GD.Print(isExist); // mysqlSave.CreateTable("DataStudent"); - // mysqlSave.AddData("DataStudent",new DataStudent("Bob",12)); + // DataStudent stuData = new DataStudent("Bob", 12); + // stuData.Path = new EGPathSelect(){Path = "AA"}; + // mysqlSave.AddData("DataStudent",stuData); + // DataStu stu1 = new DataStu("Anti", 20,"London"); + // mysqlSave.AddData("DataStudent",stu1); + // DataStu stu2 = new DataStu("CC", 23,"NewYork"){Age = 19}; + // DataStu stu3 = new DataStu("Rocket", 24,"Paris"){Age = 26}; + // List stuList = new List(); + // stuList.Add(stu2); + // stuList.Add(stu3); + // mysqlSave.AddData("DataStudent",stuList); + // mysqlSave.RemoveData("DataStudent",2); + // IEnumerable findStudent = mysqlSave.FindData("DataStudent", e => e.Name == "CC"); + IEnumerable findStudent = mysqlSave.FindData("DataStudent","Name","CC"); + GD.Print(findStudent.Count() +" data has been find!"); + foreach (DataStu stu in findStudent) + { + GD.Print(stu.Path); + } } public void TestDialog() @@ -132,8 +152,24 @@ namespace EGFramework.Examples.Test { Path = new EGPathSelect(); } } + + public struct DataStu + { + public int ID; + public string Name { get; set; } + public int Age { set; get; } + public string Path { set; get; } + public DataStu(string name, int age,string path) + { + Name = name; + Age = age; + ID = 0; + Path = path; + } + } - public struct DataPerson{ + public struct DataPerson + { public string id { get; set; } public string namee { set; get; } public string workPlace { set; get; } diff --git a/Manual/SaveTools.md b/Manual/SaveTools.md index 514cad0..647e7b1 100644 --- a/Manual/SaveTools.md +++ b/Manual/SaveTools.md @@ -74,9 +74,143 @@ GD.Print(jsonManage.GetObject("CPU")); 针对Data型的存储类型,可以简单实现数据库的增删改查,同时可使用Database的建表操作完成数据库的初始化。 +首先我们定义两个结构类型,其一是符合类型,带相关对象的: + +```csharp +public struct DataStudent +{ + public int ID; + public string Name { get; set; } + public int Age; + public EGPathSelect Path { set; get; } + public DataStudent(string name, int age) + { + Name = name; + Age = age; + ID = 0; + Path = new EGPathSelect(); + } +} +``` + +其二是只包含原始类型(IsPrimitive)和string类型的结构,其中类结构里面的所有属性(除了主键ID自增外)皆为属性类型。 + +```csharp +public struct DataStu +{ + public int ID; + public string Name { get; set; } + public int Age { set; get; } + public string Path { set; get; } + public DataStu(string name, int age,string path) + { + Name = name; + Age = age; + ID = 0; + Path = path; + } +} +``` + +后面我们会讲到两种类型兼容方面的差异,第一种仅支持单项操作,复数操作无法很好的兼容,第二种则支持复数操作(适用于数据批量导入,备份功能的定制)。 + +以MySQL为例: + +### 数据库连接 + +```csharp +EGDapper mysqlSave = this.EGSave().Load("server=" + "localhost" + ";port=" + "3306" + ";uid=" + "root" + ";pwd=" + "root" + ";database=" + "Test3" + ";"); +``` + +或者 + +```csharp +EGMysqlSave mysqlSave = this.EGSave().Load("server=" + "localhost" + ";port=" + "3306" + ";uid=" + "root" + ";pwd=" + "root" + ";database=" + "Test3" + ";"); +``` + +### 查询表名是否存在 + +```csharp +bool isExist = mysqlSave.ContainsKey("DataStudent"); +GD.Print(isExist); +``` + +### 初始化表 + +如果包含复合对象的,比如其它类对象的声明(例如上面的 EGPathSelect),此选项创建的字段为varchar(255),同String。 + +```csharp +mysqlSave.CreateTable("DataStudent"); +``` + +### 增加数据 + +关于Path属性,会根据该类的ToString()方法,插入到表格对应的Path字段中。 + +```csharp +DataStudent stuData = new DataStudent("Bob", 12); +stuData.Path = new EGPathSelect(){Path = "AA"}; +mysqlSave.AddData("DataStudent",stuData); +``` + +或者 + +```csharp +DataStu stu1 = new DataStu("Anti", 20,"London"); +mysqlSave.AddData("DataStudent",stu1); +``` + +### 批量增加数据 + +注意,批量增加数据时,需要保证对象结构必须包含表字段的所有非空属性(此框架提供的建表方法字段默认非空)。其中类结构里面的所有属性(除了主键ID自增外)皆为属性类型。 + +```csharp +DataStu stu2 = new DataStu("CC", 23,"NewYork"){Age = 19}; +DataStu stu3 = new DataStu("Rocket", 24,"Paris"){Age = 26}; +List stuList = new List(); +stuList.Add(stu2); +stuList.Add(stu3); +mysqlSave.AddData("DataStudent",stuList); +``` + +### 删除数据(通过ID) + +删除ID为2的一条数据(可配合FindData找到要删除的内容的ID,实现批量删除) + +```csharp +mysqlSave.RemoveData("DataStudent",2); +``` + +### 查找数据(通过表达式) + +查找Name为CC的数据 + +```csharp +IEnumerable findStudent = mysqlSave.FindData("DataStudent", e => e.Name == "CC"); +GD.Print(findStudent.Count() +" data has been find!"); +foreach (DataStu stu in findStudent) +{ + GD.Print(stu.Path); +} +``` + +### 查找数据(通过关键词) + +查找Name包含C的数据,注意:通过关键词查找的数据大小写不敏感,包含小写c也会被检索出来 + ```csharp +IEnumerable findStudent = mysqlSave.FindData("DataStudent","Name","C"); +GD.Print(findStudent.Count() +" data has been find!"); +foreach (DataStu stu in findStudent) +{ + GD.Print(stu.Path); +} ``` +### 修改数据 + + + ## 开发计划(随版本更新) diff --git a/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGDataStruct.cs b/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGDataStruct.cs index 8985e3f..dfee6d8 100644 --- a/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGDataStruct.cs +++ b/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGDataStruct.cs @@ -37,6 +37,18 @@ namespace EGFramework { return Path; } + public EGPathSelect() { Path = ""; IsDir = false; } + + public EGPathSelect(string path) + { + this.Path = path; + this.IsDir = false; + } + public EGPathSelect(string path, bool isDir) + { + this.Path = path; + this.IsDir = isDir; + } } public interface IEGReadOnlyString diff --git a/addons/EGFramework/Module/SaveTools/Data/EGDapper.cs b/addons/EGFramework/Module/SaveTools/Data/EGDapper.cs index e53e4b5..d73d5df 100644 --- a/addons/EGFramework/Module/SaveTools/Data/EGDapper.cs +++ b/addons/EGFramework/Module/SaveTools/Data/EGDapper.cs @@ -78,19 +78,57 @@ namespace EGFramework // throw new System.NotImplementedException(); Type DataType = typeof(TData); var properties = DataType.GetProperties(); + var fields = DataType.GetFields(); string keySet = ""; string keySetParam = ""; foreach (PropertyInfo key in properties) { - keySet += key.Name + ","; - keySetParam += "@" + key.Name + ","; + if (key.Name == "ID" || key.Name == "Id" || key.Name == "id") + { + continue; + } + if (DataType.IsPrimitive) + { + keySet += key.Name + ","; + keySetParam += "@" + key.Name + ","; + } + else + { + keySet += key.Name + ","; + keySetParam += "'" + key.GetValue(data).ToString() + "',"; + } + } + + foreach (FieldInfo key in fields) + { + if (key.Name == "ID" || key.Name == "Id" || key.Name == "id") + { + continue; + } + if (DataType.IsPrimitive) + { + keySet += key.Name + ","; + keySetParam += "@" + key.Name + ","; + } + else + { + keySet += key.Name + ","; + keySetParam += "'" + key.GetValue(data).ToString() + "',"; + } } + keySet = keySet.TrimEnd(','); keySetParam = keySetParam.TrimEnd(','); int count = Connection.Execute(@"insert " + dataKey + "(" + keySet + ") values(" + keySetParam + ")", data); //EG.Print("count:" + count); } - + + /// + /// TData Must be all member to type of properties. such as 'public string Name { set; get; }'. Properties cannot be except string, primitive + /// + /// + /// + /// public void AddData(string dataKey, IEnumerable data) { Type DataType = typeof(TData); @@ -99,12 +137,18 @@ namespace EGFramework string keySetParam = ""; foreach (PropertyInfo key in properties) { + if (key.Name == "ID" || key.Name == "Id" || key.Name == "id") + { + continue; + } keySet += key.Name + ","; keySetParam += "@" + key.Name + ","; } + keySet = keySet.TrimEnd(','); keySetParam = keySetParam.TrimEnd(','); string sql = @"insert " + dataKey + "(" + keySet + ") values(" + keySetParam + ")"; + EG.Print("sql:" + sql); int count = Connection.Execute(sql, data); //EG.Print("count:" + count); }