Commit 556f69da authored by 潘栩锋's avatar 潘栩锋 🚴

Merge remote-tracking branch 'remotes/origin/master' into dev6.0

parents abd54323 b3f2bc58
...@@ -9,8 +9,6 @@ namespace SQLite ...@@ -9,8 +9,6 @@ namespace SQLite
public class DBTable<T> : IDBTable public class DBTable<T> : IDBTable
where T : new() where T : new()
{ {
public List<SQLiteHelper.ArrayFieldTypeInfo> ArrayFieldTypeInfos = new List<SQLiteHelper.ArrayFieldTypeInfo>();
public string TableName { get; private set; } public string TableName { get; private set; }
private long freeID = 0; private long freeID = 0;
...@@ -32,10 +30,7 @@ namespace SQLite ...@@ -32,10 +30,7 @@ namespace SQLite
{ {
get get
{ {
if (ArrayFieldTypeInfos.Count() == 0) return ddl;
return ddl;
else
return SQLiteHelper.GetCreateTableCommandText(typeof(T), ArrayFieldTypeInfos.ToArray());
} }
} }
...@@ -81,7 +76,7 @@ namespace SQLite ...@@ -81,7 +76,7 @@ namespace SQLite
sql += " " + condition; sql += " " + condition;
DataTable dataTable = sqliteHelper.ExecuteReader(sql); DataTable dataTable = sqliteHelper.ExecuteReader(sql);
return SQLiteHelper.ToObjs<T>(dataTable, ArrayFieldTypeInfos.ToArray()); return SQLiteHelper.ToObjs<T>(dataTable);
} }
/// <summary> /// <summary>
......
...@@ -11,15 +11,6 @@ namespace SQLite ...@@ -11,15 +11,6 @@ namespace SQLite
} }
} }
public class BortherAttribute : Attribute
{
}
public class ChildAttribute : Attribute
{
}
public class PropertyIndexAttribute : Attribute public class PropertyIndexAttribute : Attribute
{ {
......
...@@ -109,24 +109,6 @@ namespace SQLite ...@@ -109,24 +109,6 @@ namespace SQLite
return null; return null;
} }
} }
public class ArrayFieldTypeInfo
{
/// <summary>
/// 属性名
/// </summary>
public string PropertyName { get; set; }
/// <summary>
/// 数组大小
/// </summary>
public int Length { get; set; }
public ArrayFieldTypeInfo(string propertyname, int length)
{
PropertyName = propertyname;
Length = length;
}
}
public static string GetTableName(Type type) public static string GetTableName(Type type)
...@@ -142,7 +124,7 @@ namespace SQLite ...@@ -142,7 +124,7 @@ namespace SQLite
} }
} }
static string GetCreateTableCommandText_fieldText(Type type, IEnumerable<ArrayFieldTypeInfo> arrayFieldTypeInfos) static string GetCreateTableCommandText_fieldText(Type type)
{ {
string total_fieldtext = ""; string total_fieldtext = "";
...@@ -161,51 +143,15 @@ namespace SQLite ...@@ -161,51 +143,15 @@ namespace SQLite
if (propertyIndex != null) if (propertyIndex != null)
fieldText.index = propertyIndex.Index;//默认index=0 fieldText.index = propertyIndex.Index;//默认index=0
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
string text = "";
text += string.Format("{0} {1}", propertyInfo.Name, fieldTypeInfo.FieldType);
//这个属性 下面的全部属性,是同一个表 //主键
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0) if (propertyInfo.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0)
{ text += " PRIMARY KEY";
//从arrayFieldTypeInfos 提取 fieldText.fieldtext = text;
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.fieldtext = GetCreateTableCommandText_fieldText(propertyInfo.PropertyType, aftis);
continue;
}
if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小
{
int length = arrayFieldTypeInfos.First((a) => a.PropertyName == propertyInfo.Name).Length;
Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
string text = "";
for (int i = 0; i < length; i++)
{
if (i != 0)
text += ",";
text += string.Format("{0}{1} {2}",
propertyInfo.Name,
i,
fieldTypeInfo.FieldType);
}
fieldText.fieldtext = text;
}
else
{
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
string text = "";
text += string.Format("{0} {1}", propertyInfo.Name, fieldTypeInfo.FieldType);
//主键
if (propertyInfo.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0)
text += " PRIMARY KEY";
fieldText.fieldtext = text;
}
} }
//从小到大排序 //从小到大排序
fieldTexts.Sort((fieldTextIndex0, fieldTextIndex1) => fieldTexts.Sort((fieldTextIndex0, fieldTextIndex1) =>
...@@ -227,11 +173,8 @@ namespace SQLite ...@@ -227,11 +173,8 @@ namespace SQLite
} }
return total_fieldtext; return total_fieldtext;
} }
public static string GetCreateTableCommandText(Type type, params ArrayFieldTypeInfo[] arrayFieldTypeInfos) public static string GetCreateTableCommandText(Type type)
{ {
if (arrayFieldTypeInfos == null)
arrayFieldTypeInfos = new ArrayFieldTypeInfo[0];
//CREATE TABLE table_name( //CREATE TABLE table_name(
//column1 datatype PRIMARY KEY, //column1 datatype PRIMARY KEY,
//column2 datatype, //column2 datatype,
...@@ -240,7 +183,7 @@ namespace SQLite ...@@ -240,7 +183,7 @@ namespace SQLite
// columnN datatype, // columnN datatype,
//) //)
string tablename = GetTableName(type); string tablename = GetTableName(type);
string fieldtext = GetCreateTableCommandText_fieldText(type, arrayFieldTypeInfos); string fieldtext = GetCreateTableCommandText_fieldText(type);
string commandText = string.Format("CREATE TABLE {0} ({1})", tablename, fieldtext); string commandText = string.Format("CREATE TABLE {0} ({1})", tablename, fieldtext);
return commandText; return commandText;
} }
...@@ -270,37 +213,10 @@ namespace SQLite ...@@ -270,37 +213,10 @@ namespace SQLite
object o = propertyInfo.GetValue(cell, null); object o = propertyInfo.GetValue(cell, null);
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
//这个属性 下面的全部属性,是同一个表 fieldText.fieldtext = fieldTypeInfo.PtoS(o);
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
fieldText.fieldtext = GetInsertCommandText_fieldText(o);
continue;
}
if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小
{
Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
Array a = o as Array;
string text = "";
for (int i = 0; i < a.Length; i++)
{
if (i != 0)
text += ",";
text += fieldTypeInfo.PtoS(a.GetValue(i));
}
fieldText.fieldtext = text;
}
else
{
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
fieldText.fieldtext = fieldTypeInfo.PtoS(o);
}
} }
//从小到大排序 //从小到大排序
...@@ -364,36 +280,12 @@ namespace SQLite ...@@ -364,36 +280,12 @@ namespace SQLite
object o = propertyInfo.GetValue(cell, null); object o = propertyInfo.GetValue(cell, null);
//这个属性 下面的全部属性,是同一个表
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0)
{
fieldtext += GetUpdateCommandText_fieldText(o);
continue;
}
if (propertyInfo.PropertyType.IsArray)//是数组,需要明确大小
{
Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
Array a = o as Array; SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
for (int i = 0; i < a.Length; i++)
{
if (i != 0)
fieldtext += ",";
fieldtext += string.Format("{0}{1} = {2}", propertyInfo.Name, i, fieldTypeInfo.PtoS(a.GetValue(i)));
}
}
else
{
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == propertyInfo.PropertyType);
fieldtext += string.Format("{0} = {1}", propertyInfo.Name, fieldTypeInfo.PtoS(o)); fieldtext += string.Format("{0} = {1}", propertyInfo.Name, fieldTypeInfo.PtoS(o));
}
} }
return fieldtext; return fieldtext;
...@@ -421,19 +313,18 @@ namespace SQLite ...@@ -421,19 +313,18 @@ namespace SQLite
return commandText; return commandText;
} }
public static List<T> ToObjs<T>(DataTable dataTable, params ArrayFieldTypeInfo[] arrayFieldTypeInfos) public static List<T> ToObjs<T>(DataTable dataTable)
where T : new() where T : new()
{ {
Type type = typeof(T);
List<T> list = new List<T>(); List<T> list = new List<T>();
foreach (DataRow dataRow in dataTable.Rows) foreach (DataRow dataRow in dataTable.Rows)
{ {
list.Add(ToObj<T>(dataRow, arrayFieldTypeInfos)); list.Add(ToObj<T>(dataRow));
} }
return list; return list;
} }
static void SetObj(object t, DataRow dataRow, IEnumerable<ArrayFieldTypeInfo> arrayFieldTypeInfos) static void SetObj(object t, DataRow dataRow)
{ {
Type type = t.GetType(); Type type = t.GetType();
...@@ -447,49 +338,17 @@ namespace SQLite ...@@ -447,49 +338,17 @@ namespace SQLite
Type ptype = propertyInfo.PropertyType; Type ptype = propertyInfo.PropertyType;
//这个属性 下面的全部属性,是同一个表 SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == ptype);
if (propertyInfo.GetCustomAttributes(typeof(BortherAttribute), false).Count() > 0) object o = fieldTypeInfo.StoP(dataRow[propertyInfo.Name]);
{ propertyInfo.SetValue(t, o, null);
//从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)//是数组,需要明确大小
{
Type elementType = propertyInfo.PropertyType.GetElementType();
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == elementType);
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);
for (int i = 0; i < length; i++)
{
object o = fieldTypeInfo.StoP(dataRow[propertyInfo.Name + i]);
array.SetValue(o, i);
}
propertyInfo.SetValue(t, array, null);
}
else
{
SQLiteFieldTypeInfo fieldTypeInfo = FieldTypeInfo.Find((fti) => fti.PropertyType == ptype);
object o = fieldTypeInfo.StoP(dataRow[propertyInfo.Name]);
propertyInfo.SetValue(t, o, null);
}
} }
} }
public static T ToObj<T>(DataRow dataRow, params ArrayFieldTypeInfo[] arrayFieldTypeInfos) public static T ToObj<T>(DataRow dataRow)
where T : new() where T : new()
{ {
Type type = typeof(T);
T t = new T(); T t = new T();
SetObj(t, dataRow, arrayFieldTypeInfos); SetObj(t, dataRow);
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