Commit d8728c2c authored by 潘栩锋's avatar 潘栩锋 🚴

sqlitehelper 支持 borther属性

parent 8d83f1b8
...@@ -10,4 +10,13 @@ namespace FLY.OBJComponents.Common ...@@ -10,4 +10,13 @@ namespace FLY.OBJComponents.Common
} }
} }
public class BortherAttribute : Attribute
{
}
public class ChildAttribute : Attribute
{
}
} }
\ No newline at end of file
...@@ -130,17 +130,8 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -130,17 +130,8 @@ namespace FLY.OBJComponents.Common.SQLite
} }
} }
public static string GetCreateTableCommandText(Type type, params ArrayFieldTypeInfo[] arrayFieldTypeInfos) static string GetCreateTableCommandText_fieldText(Type type, IEnumerable<ArrayFieldTypeInfo> arrayFieldTypeInfos)
{ {
//CREATE TABLE table_name(
//column1 datatype PRIMARY KEY,
//column2 datatype,
// column3 datatype,
// .....
// columnN datatype,
//)
string tablename = GetTableName(type);
string fieldtext = ""; string fieldtext = "";
PropertyInfo[] propertyInfos = type.GetProperties(); PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos) foreach (var propertyInfo in propertyInfos)
...@@ -154,6 +145,18 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -154,6 +145,18 @@ namespace FLY.OBJComponents.Common.SQLite
fieldtext += ","; fieldtext += ",";
} }
//这个属性 下面的全部属性,是同一个表
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
//从arrayFieldTypeInfos 提取
string startswith = propertyInfo.Name + ".";
var aftis = from afti in arrayFieldTypeInfos
where afti.PropertyName.StartsWith(startswith)
select new ArrayFieldTypeInfo(afti.PropertyName.Substring(startswith.Length), afti.PropertyName.Length);
fieldtext += GetCreateTableCommandText_fieldText(propertyInfo.PropertyType, aftis);
continue;
}
if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小 if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小
{ {
int length = arrayFieldTypeInfos.First((a) => a.PropertyName == propertyInfo.Name).Length; int length = arrayFieldTypeInfos.First((a) => a.PropertyName == propertyInfo.Name).Length;
...@@ -161,7 +164,7 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -161,7 +164,7 @@ namespace FLY.OBJComponents.Common.SQLite
Type elementType = propertyInfo.PropertyType.GetElementType(); Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType); SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
if (i != 0) if (i != 0)
...@@ -184,22 +187,28 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -184,22 +187,28 @@ namespace FLY.OBJComponents.Common.SQLite
fieldtext += " PRIMARY KEY"; fieldtext += " PRIMARY KEY";
} }
} }
return fieldtext;
}
public static string GetCreateTableCommandText(Type type, params ArrayFieldTypeInfo[] arrayFieldTypeInfos)
{
//CREATE TABLE table_name(
//column1 datatype PRIMARY KEY,
//column2 datatype,
// column3 datatype,
// .....
// columnN datatype,
//)
string tablename = GetTableName(type);
string fieldtext = GetCreateTableCommandText_fieldText(type, arrayFieldTypeInfos);
string commandText = string.Format("CREATE TABLE {0} ({1})", tablename, fieldtext); string commandText = string.Format("CREATE TABLE {0} ({1})", tablename, fieldtext);
return commandText; return commandText;
} }
public static string GetInsertCommandText(object cell)
{
//不用
//INSERT INTO TABLE_NAME[(column1, column2, column3,...columnN)]
//VALUES(value1, value2, value3,...valueN);
//使用
//INSERT INTO TABLE_NAME VALUES(value1, value2, value3,...valueN);
static string GetInsertCommandText_fieldText(object cell)
{
Type type = cell.GetType(); Type type = cell.GetType();
string tablename = GetTableName(type);
string fieldtext = ""; string fieldtext = "";
PropertyInfo[] propertyInfos = type.GetProperties(); PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos) foreach (var propertyInfo in propertyInfos)
...@@ -213,13 +222,23 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -213,13 +222,23 @@ namespace FLY.OBJComponents.Common.SQLite
fieldtext += ","; fieldtext += ",";
} }
object o = propertyInfo.GetValue(cell, null);
//这个属性 下面的全部属性,是同一个表
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
fieldtext += GetInsertCommandText_fieldText(o);
continue;
}
if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小 if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小
{ {
Type elementType = propertyInfo.PropertyType.GetElementType(); Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType); SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
Array a = propertyInfo.GetValue(cell, null) as Array; Array a = o as Array;
for (int i = 0; i < a.Length; i++) for (int i = 0; i < a.Length; i++)
{ {
...@@ -233,22 +252,29 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -233,22 +252,29 @@ namespace FLY.OBJComponents.Common.SQLite
{ {
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType); SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
object o = propertyInfo.GetValue(cell, null);
fieldtext += fieldTypeInfo.PtoS(o); fieldtext += fieldTypeInfo.PtoS(o);
} }
} }
return fieldtext;
}
public static string GetInsertCommandText(object cell)
{
//不用
//INSERT INTO TABLE_NAME[(column1, column2, column3,...columnN)]
//VALUES(value1, value2, value3,...valueN);
//使用
//INSERT INTO TABLE_NAME VALUES(value1, value2, value3,...valueN);
Type type = cell.GetType();
string tablename = GetTableName(type);
string fieldtext = GetInsertCommandText_fieldText(cell);
string commandText = string.Format("INSERT INTO {0} VALUES({1})", tablename, fieldtext); string commandText = string.Format("INSERT INTO {0} VALUES({1})", tablename, fieldtext);
return commandText; return commandText;
} }
/// <summary>
/// condition 为 "WHERE ......" static string GetUpdateCommandText_fieldText(object cell)
/// </summary>
/// <param name="cell"></param>
/// <param name="condition"></param>
/// <returns></returns>
public static string GetUpdateCommandText(object cell, string condition)
{ {
//UPDATE table_name //UPDATE table_name
//SET column1 = value1, column2 = value2...., columnN = valueN //SET column1 = value1, column2 = value2...., columnN = valueN
...@@ -270,33 +296,62 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -270,33 +296,62 @@ namespace FLY.OBJComponents.Common.SQLite
fieldtext += ","; fieldtext += ",";
} }
object o = propertyInfo.GetValue(cell, null);
//这个属性 下面的全部属性,是同一个表
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
fieldtext += GetUpdateCommandText_fieldText(o);
continue;
}
if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小 if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小
{ {
Type elementType = propertyInfo.PropertyType.GetElementType(); Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType); SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
Array a = propertyInfo.GetValue(cell, null) as Array; Array a = o as Array;
for (int i = 0; i < a.Length; i++) for (int i = 0; i < a.Length; i++)
{ {
if (i != 0) if (i != 0)
fieldtext += ","; fieldtext += ",";
fieldtext += string.Format("{0}{1} = {2}", propertyInfo.PropertyType.Name, i, fieldTypeInfo.PtoS(a.GetValue(i))); fieldtext += string.Format("{0}{1} = {2}", propertyInfo.Name, i, fieldTypeInfo.PtoS(a.GetValue(i)));
} }
} }
else else
{ {
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType); SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
object o = propertyInfo.GetValue(cell, null);
fieldtext += string.Format("{0} = {1}", propertyInfo.Name, fieldTypeInfo.PtoS(o));
fieldtext += string.Format("{0} = {1}", propertyInfo.PropertyType.Name, fieldTypeInfo.PtoS(o));
} }
} }
string commandText = string.Format("UPDATE {0} SET {1} {2}", tablename, fieldtext, condition); return fieldtext;
}
/// <summary>
/// condition 为 "WHERE ......"
/// </summary>
/// <param name="cell"></param>
/// <param name="condition"></param>
/// <returns></returns>
public static string GetUpdateCommandText(object cell, string condition)
{
//UPDATE table_name
//SET column1 = value1, column2 = value2...., columnN = valueN
//WHERE[condition];
Type type = cell.GetType();
string tablename = GetTableName(type);
string fieldtext = GetUpdateCommandText_fieldText(cell);
string commandText = $"UPDATE {tablename} SET {fieldtext}";
if (!string.IsNullOrEmpty(condition))
commandText += $" {condition}";
return commandText; return commandText;
} }
...@@ -312,21 +367,39 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -312,21 +367,39 @@ namespace FLY.OBJComponents.Common.SQLite
return list; return list;
} }
public static T ToObj<T>(DataRow dataRow, params ArrayFieldTypeInfo[] arrayFieldTypeInfos) static void SetObj(object t, DataRow dataRow, IEnumerable<ArrayFieldTypeInfo> arrayFieldTypeInfos)
where T : new()
{ {
Type type = typeof(T); Type type = t.GetType();
T t = new T();
PropertyInfo[] propertyInfos = type.GetProperties(); PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos) foreach (var propertyInfo in propertyInfos)
{ {
//忽略
if (propertyInfo.GetCustomAttributes(typeof(IgnoreAttribute), false).Count() > 0)
continue;
Type ptype = propertyInfo.PropertyType; Type ptype = propertyInfo.PropertyType;
//这个属性 下面的全部属性,是同一个表
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
//从arrayFieldTypeInfos 提取
string startswith = propertyInfo.Name + ".";
var aftis = from afti in arrayFieldTypeInfos
where afti.PropertyName.StartsWith(startswith)
select new ArrayFieldTypeInfo(afti.PropertyName.Substring(startswith.Length), afti.PropertyName.Length);
object obj = propertyInfo.GetValue(t, null);
SetObj(obj, dataRow, aftis);
continue;
}
if (ptype.IsArray)//是数组,需要明确大小 if (ptype.IsArray)//是数组,需要明确大小
{ {
Type elementType = propertyInfo.PropertyType.GetElementType(); Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType); SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
int length = arrayFieldTypeInfos.First((a) => a.PropertyName == propertyInfo.Name).Length; int length = arrayFieldTypeInfos.First((a) => a.PropertyName == propertyInfo.Name).Length;
Array array = (Array)ptype.Assembly.CreateInstance(ptype.FullName, false, BindingFlags.CreateInstance, null, new object[] { length }, null, null); Array array = (Array)ptype.Assembly.CreateInstance(ptype.FullName, false, BindingFlags.CreateInstance, null, new object[] { length }, null, null);
...@@ -344,6 +417,13 @@ namespace FLY.OBJComponents.Common.SQLite ...@@ -344,6 +417,13 @@ namespace FLY.OBJComponents.Common.SQLite
propertyInfo.SetValue(t, o, null); propertyInfo.SetValue(t, o, null);
} }
} }
}
public static T ToObj<T>(DataRow dataRow, params ArrayFieldTypeInfo[] arrayFieldTypeInfos)
where T : new()
{
Type type = typeof(T);
T t = new T();
SetObj(t, dataRow, arrayFieldTypeInfos);
return t; return t;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment