diff --git a/addons/EGFramework/Module/Extension/EGSqlExtension.cs b/addons/EGFramework/Module/Extension/EGSqlExtension.cs new file mode 100644 index 0000000..accad60 --- /dev/null +++ b/addons/EGFramework/Module/Extension/EGSqlExtension.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace EGFramework +{ + public static class EGSqlExtension + { + public static string ToCreateTableSQL(this PropertyInfo property) + { + string sqlCommand; + if (property.Name == "ID" || property.Name == "id" || property.Name == "Id") + { + return ""; + } + if (property.PropertyType == typeof(int) || property.PropertyType.IsEnum) + { + sqlCommand = "`" + property.Name + "` INTEGER" + " NOT NULL,"; + } + else if (property.PropertyType == typeof(double) || property.PropertyType == typeof(float)) + { + sqlCommand = "`" + property.Name + "` REAL" + " NOT NULL,"; + } + else if (property.PropertyType == typeof(bool)) + { + sqlCommand = "`" + property.Name + "` REAL" + " NOT NULL,"; + } + else if (property.PropertyType == typeof(long)) + { + sqlCommand = "`" + property.Name + "` BIGINT(20)" + " NOT NULL,"; + } + else if (property.PropertyType == typeof(string)) + { + sqlCommand = "`" + property.Name + "` VARCHAR(255)" + " NOT NULL,"; + } + else + { + sqlCommand = "`" + property.Name + "` VARCHAR(255)" + " NOT NULL,"; + } + return sqlCommand; + } + + public static string ToCreateTableSQL(this FieldInfo field) + { + string sqlCommand; + if (field.Name == "ID" || field.Name == "id" || field.Name == "Id") + { + return ""; + } + if (field.FieldType == typeof(int) || field.FieldType.IsEnum) + { + sqlCommand = "`" + field.Name + "` INTEGER" + " NOT NULL,"; + } + else if (field.FieldType == typeof(double) || field.FieldType == typeof(float)) + { + sqlCommand = "`" + field.Name + "` REAL" + " NOT NULL,"; + } + else if (field.FieldType == typeof(bool)) + { + sqlCommand = "`" + field.Name + "` REAL" + " NOT NULL,"; + } + else if (field.FieldType == typeof(long)) + { + sqlCommand = "`" + field.Name + "` BIGINT(20)" + " NOT NULL,"; + } + else if (field.FieldType == typeof(string)) + { + sqlCommand = "`" + field.Name + "` VARCHAR(255)" + " NOT NULL,"; + } + else + { + sqlCommand = "`" + field.Name + "` VARCHAR(255)" + " NOT NULL,"; + } + return sqlCommand; + } + + public static string ToCreateTableSQL(this KeyValuePair param) + { + string sqlCommand; + if (param.Key == "ID" || param.Key == "id" || param.Key == "Id") + { + return ""; + } + if (param.Value == typeof(int) || param.Value.IsEnum) + { + sqlCommand = "`" + param.Key + "` INTEGER" + " NOT NULL,"; + } + else if (param.Value == typeof(double) || param.Value == typeof(float)) + { + sqlCommand = "`" + param.Key + "` REAL" + " NOT NULL,"; + } + else if (param.Value == typeof(bool)) + { + sqlCommand = "`" + param.Key + "` REAL" + " NOT NULL,"; + } + else if (param.Value == typeof(long)) + { + sqlCommand = "`" + param.Key + "` BIGINT(20)" + " NOT NULL,"; + } + else if (param.Value == typeof(string)) + { + sqlCommand = "`" + param.Key + "` VARCHAR(255)" + " NOT NULL,"; + } + else + { + sqlCommand = "`" + param.Key + "` VARCHAR(255)" + " NOT NULL,"; + } + return sqlCommand; + } + + public static string ToCreateTableSQL(this Type type, string tableName) + { + var properties = type.GetProperties(); + FieldInfo[] fields = type.GetFields(); + string keySet = ""; + foreach (PropertyInfo key in properties) + { + keySet += key.ToCreateTableSQL(); + } + foreach (FieldInfo key in fields) + { + keySet += key.ToCreateTableSQL(); + } + keySet = keySet.TrimEnd(','); + string createSql = @"CREATE TABLE " + tableName + " (" + + "`ID` INTEGER PRIMARY KEY AUTOINCREMENT, " + keySet + + ");"; + return createSql; + } + + public static string ToCreateTableSQL(this Dictionary tableParam,string tableName) + { + + string keySet = ""; + foreach(KeyValuePair key in tableParam){ + keySet += key.ToCreateTableSQL(); + } + keySet = keySet.TrimEnd(','); + string createSql = @"CREATE TABLE "+tableName+" ("+ + "`ID` INTEGER PRIMARY KEY AUTOINCREMENT, "+keySet + +");"; + return createSql; + } + + } +} \ No newline at end of file diff --git a/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGVariantGenerator.cs b/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGVariantGenerator.cs index 570f01b..ae83a40 100644 --- a/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGVariantGenerator.cs +++ b/addons/EGFramework/Module/GenerateTools/Templete/Variant/EGVariantGenerator.cs @@ -36,6 +36,21 @@ namespace EGFramework{ } return result; } + public static Dictionary EGenerateTypeDictiontaryByType(this Type self) + { + PropertyInfo[] propertyNames = self.GetProperties(); + FieldInfo[] fieldNames = self.GetFields(); + Dictionary result = new Dictionary(); + foreach (PropertyInfo pName in propertyNames) + { + result.Add(pName.Name, pName.PropertyType); + } + foreach (FieldInfo fName in fieldNames) + { + result.Add(fName.Name, fName.FieldType); + } + return result; + } public static Dictionary EGenerateDictiontaryByObject(this T self) { diff --git a/addons/EGFramework/Module/SaveTools/Data/EGDapper.cs b/addons/EGFramework/Module/SaveTools/Data/EGDapper.cs index 56ab638..a8d3840 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 + public abstract class EGDapper : IEGSave, IEGSaveData, IEGCanGetDBConnection,IEGDataBase { public DbConnection Connection { get; set; } public string ExceptionMsg; @@ -220,5 +220,21 @@ namespace EGFramework return Connection; } + public void CreateTable(string dataKey) + { + // throw new System.NotImplementedException(); + if (this.ContainsKey(dataKey)) + { + //Drop table if has. + Connection.Execute(@"DROP TABLE IF EXISTS "+dataKey+""); + } + string createSql = typeof(TData).ToCreateTableSQL(dataKey); + int count = Connection.Execute(createSql); + } + + public void CreateTable(string dataKey,Dictionary tableParam) + { + 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 e7f9f76..5e6bf61 100644 --- a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs +++ b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs @@ -10,32 +10,39 @@ namespace EGFramework { #region SaveInit - public interface IEGSave{ + public interface IEGSave { void InitSave(string path); } - public interface IEGSaveReadOnly{ + public interface IEGSaveReadOnly { void InitReadOnly(string data); void InitReadOnly(byte[] data); } - public interface IEGSaveAsync{ + public interface IEGSaveAsync { Task InitSaveFileAsync(string path); } - public interface IEGSaveReadOnlyAsync{ + public interface IEGSaveReadOnlyAsync { Task InitReadOnlyAsync(string data); Task InitReadOnlyAsync(byte[] data); } #endregion - + #region DBConnection - public interface IEGCanGetDBConnection{ + public interface IEGCanGetDBConnection + { DbConnection GetConnection(); } + public interface IEGDataBase : IEGCanGetDBConnection + { + void CreateTable(string dataKey); + void CreateTable(string dataKey,Dictionary tableParam); + } #endregion #region Object - public interface IEGSaveObjectReadOnly{ + public interface IEGSaveObjectReadOnly + { TObject GetObject(string objectKey) where TObject : new(); IEnumerable GetKeys(); bool ContainsKey(string objectKey);