Browse Source

fixed sql extension

master
jkpete 1 month ago
parent
commit
3190224018
  1. 146
      addons/EGFramework/Module/Extension/EGSqlExtension.cs
  2. 15
      addons/EGFramework/Module/GenerateTools/Templete/Variant/EGVariantGenerator.cs
  3. 18
      addons/EGFramework/Module/SaveTools/Data/EGDapper.cs
  4. 21
      addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs

146
addons/EGFramework/Module/Extension/EGSqlExtension.cs

@ -0,0 +1,146 @@ @@ -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<string, Type> 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<string,Type> tableParam,string tableName)
{
string keySet = "";
foreach(KeyValuePair<string,Type> key in tableParam){
keySet += key.ToCreateTableSQL();
}
keySet = keySet.TrimEnd(',');
string createSql = @"CREATE TABLE "+tableName+" ("+
"`ID` INTEGER PRIMARY KEY AUTOINCREMENT, "+keySet
+");";
return createSql;
}
}
}

15
addons/EGFramework/Module/GenerateTools/Templete/Variant/EGVariantGenerator.cs

@ -36,6 +36,21 @@ namespace EGFramework{ @@ -36,6 +36,21 @@ namespace EGFramework{
}
return result;
}
public static Dictionary<string, Type> EGenerateTypeDictiontaryByType(this Type self)
{
PropertyInfo[] propertyNames = self.GetProperties();
FieldInfo[] fieldNames = self.GetFields();
Dictionary<string, Type> result = new Dictionary<string, Type>();
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<string, object> EGenerateDictiontaryByObject<T>(this T self)
{

18
addons/EGFramework/Module/SaveTools/Data/EGDapper.cs

@ -8,7 +8,7 @@ using Dapper; @@ -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 @@ -220,5 +220,21 @@ namespace EGFramework
return Connection;
}
public void CreateTable<TData>(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<string, object> tableParam)
{
throw new NotImplementedException();
}
}
}

21
addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs

@ -10,32 +10,39 @@ namespace EGFramework @@ -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<TData>(string dataKey);
void CreateTable(string dataKey,Dictionary<string, object> tableParam);
}
#endregion
#region Object
public interface IEGSaveObjectReadOnly{
public interface IEGSaveObjectReadOnly
{
TObject GetObject<TObject>(string objectKey) where TObject : new();
IEnumerable<string> GetKeys();
bool ContainsKey(string objectKey);

Loading…
Cancel
Save