/// <summary> /// 根据索引获取一条数据 /// </summary> public T QueryData <T>(IDbAccessorFactory dbAcsFty, object key) where T : class, new() { try { var clsDesc = GetClassDesc(typeof(T)); var dbAccessor = dbAcsFty.GetDbAccessor(clsDesc.TableName); IDataReader dbReader = null; if (clsDesc.KeyAttribute == null) { dbReader = dbAccessor.Query(GetQueryString(clsDesc.TableName, "", null)); } else { object localKey = key; if (clsDesc.KeyAttribute.CustomType != null && localKey != null) { localKey = ConfigDataBase.Instance.CustomDbClass.ParseText(clsDesc.KeyAttribute.CustomType, localKey); } dbReader = dbAccessor.Query(GetQueryString(clsDesc.TableName, clsDesc.KeyAttribute.ColumnName, localKey)); } T result = ReadOneItem <T>(dbReader, clsDesc, dbAcsFty, key); dbAccessor.CloseDbReader(); return(result); } catch (Exception e) { var sb = new StringBuilder(); sb.AppendLine(e.Message); sb.AppendLine(e.StackTrace); Debug.LogError(sb.ToString()); return(null); } }
/// <summary> /// 解析一条数据 /// </summary> private T ReadOneItem <T>(IDataReader dr, ClassDesc clsDesc, IDbAccessorFactory dbAcsFty, object key) where T : class, new() { while (dr.Read()) { return(CreateOneItem(typeof(T), dr, clsDesc, dbAcsFty, key) as T); } return(null); }
public void Release <T>(IDbAccessorFactory dbAcsFty) { if (clsDescDic.ContainsKey(typeof(T))) { dbAcsFty.Release(clsDescDic[typeof(T)].TableName); clsDescDic.Remove(typeof(T)); } }
public void SetDbAccessorFactory(IDbAccessorFactory factory) { if (factory == null) { throw new ArgumentNullException("factory"); } _dbAccessorFactory = factory; }
/// <summary> /// 从已加载的数据中获取对应类型的List<>, 如果对应类型不存在, 加载并返回加载后的结果 /// </summary> private IList GetSubItemList(Type type, IDbAccessorFactory dbAcsFty, object key) { ClassDesc clsDesc = GetClassDesc(type); IDbAccessor dbAccessor = dbAcsFty.GetDbAccessor(clsDesc.TableName); IDataReader reader = dbAccessor.Query(GetQueryString(clsDesc.TableName, clsDesc.RootKeyAttribute.ColumnName, key)); IList list = ReadItems(type, reader, clsDesc, dbAcsFty, key); dbAccessor.CloseDbReader(); return(list); }
/// <summary> /// 解析所有数据 /// </summary> private IList ReadItems(Type type, IDataReader dr, ClassDesc clsDesc, IDbAccessorFactory dbAcsFty, object key) { IList list = Activator.CreateInstance(typeof(List <>).MakeGenericType(type)) as IList; while (dr.Read()) { list.Add(CreateOneItem(type, dr, clsDesc, dbAcsFty, key)); } return(list); }
/// <summary> /// 解析所有数据 /// </summary> private List <T> ReadItems <T>(IDataReader dr, ClassDesc clsDesc, IDbAccessorFactory dbAcsFty, object key) where T : class, new() { List <T> list = new List <T>(); while (dr.Read()) { list.Add(CreateOneItem(typeof(T), dr, clsDesc, dbAcsFty, key) as T); } return(list); }
public void Initialize(IDbAccessorFactory dbAccessorFactory, string subPath) { if (inited) { return; } inited = true; defaultPath = FileManager.GetPersistentDataPath(subPath); ConstructConfigDbPath(); SetAccessorFactory(dbAccessorFactory); if (customDbClass == null) { customDbClass = new CustomDbClass(); } RegisterEnumerationType(customDbClass); ConstValue.Initialize(); }
/// <summary> /// 获取整个表中的所有数据 /// </summary> public List <T> QueryAllData <T>(IDbAccessorFactory dbAcsFty) where T : class, new() { try { ClassDesc clsDesc = GetClassDesc(typeof(T)); IDbAccessor dbAccessor = dbAcsFty.GetDbAccessor(clsDesc.TableName); IDataReader reader = dbAccessor.Query(GetQueryString(clsDesc.TableName, clsDesc.KeyAttribute.ColumnName, null)); List <T> result = ReadItems <T>(reader, clsDesc, dbAcsFty, null); dbAccessor.CloseDbReader(); return(result); } catch (Exception e) { var sb = new StringBuilder(); sb.AppendLine(e.Message); sb.AppendLine(e.StackTrace); Debug.LogError(sb.ToString()); return(new List <T>()); } }
public void SetAccessorFactory(IDbAccessorFactory factory) { dbAccessorFactory = factory; }
/// <summary> /// 从数据流获取数据类, 支持外键 /// </summary> private object CreateOneItem(Type type, IDataReader dr, ClassDesc clsDesc, IDbAccessorFactory dbAcsFty, object key) { // 创建具体类型 var model = Activator.CreateInstance(type); object localKey = null; // 填充表数据 for (int i = 0; i < dr.FieldCount; i++) { var propertyDesc = clsDesc.GetPropertyInfo(dr.GetName(i)); if (propertyDesc != null) { if (clsDesc.KeyAttribute != null && propertyDesc.attribute.ColumnName.Equals(clsDesc.KeyAttribute.ColumnName)) { localKey = dr[i]; } if (dr[i] is DBNull) { Debug.LogWarning(string.Format("DBNull found in column : {0}.If it's test record, ignore", dr.GetName(i))); } else if (propertyDesc.attribute.CustomType == null) { if (propertyDesc.attribute.IsFloatCol) { int ori = Convert.ToInt32(dr[i]); float dst = (float)ori / 10000; propertyDesc.propertyInfo.SetValue(model, dst, null); } else { // 设置非自定义值 // bool值特殊处理 if (propertyDesc.propertyInfo.PropertyType == typeof(bool)) { propertyDesc.propertyInfo.SetValue(model, Convert.ToInt32(dr[i]) == 1, null); } else if (propertyDesc.propertyInfo.PropertyType == typeof(int)) { propertyDesc.propertyInfo.SetValue(model, Convert.ToInt32(dr[i]), null); } else { if (propertyDesc.propertyInfo.PropertyType == typeof(string)) { string value = dr[i] as string; propertyDesc.propertyInfo.SetValue(model, value, null); } else { propertyDesc.propertyInfo.SetValue(model, dr[i], null); } } } } else { // 对于自定义类型值, 使用CustomDBClass转换 if (dr[i].GetType() != typeof(string)) { Debug.LogError(string.Format("Column value of custom type must be string : {0}", dr.GetName(i))); } else { var value = ConfigDataBase.Instance.CustomDbClass.ParseType(propertyDesc.attribute.CustomType, dr[i] as string); propertyDesc.propertyInfo.SetValue(model, value, null); } } } else { var splitPropertyInfo = clsDesc.GetSplitPropertyInfo(dr.GetName(i)); var splitRangeInfo = clsDesc.GetSplitRangePropertyInfo(dr.GetName(i)); if (splitPropertyInfo != null) { if (dr[i] is DBNull || dr.GetValue(i) == null) { Debug.LogWarning(string.Format("DBNull found in column : {0}. If it's test record, ignore", dr.GetName(i))); } else { AddSplitColumn(splitPropertyInfo.splitAttribute.Type, splitPropertyInfo.propertyInfo, dr.GetValue(i).ToString(), splitPropertyInfo.splitAttribute.IsCustomType, model); } } else if (splitRangeInfo != null) { if (dr[i] is DBNull || dr.GetValue(i) == null) { Debug.LogWarning(string.Format("DBNull found in column : {0}.If it's test record, ignore", dr.GetName(i))); } else { AddSplitRangeColumn(splitRangeInfo.propertyInfo, dr.GetValue(i).ToString(), model); } } else { int dstPrefixLen = 0; string className = string.Empty; if (string.IsNullOrEmpty(clsDesc.ToClassName)) { className = clsDesc.ClassName + "List"; dstPrefixLen = clsDesc.TableName.Length;// 如此,则合并表不能有editor等类似的前缀 } else // 有to_name注释 { // 需要根据className 去 获得mergeColumnDesc className = clsDesc.ToClassName + "List"; dstPrefixLen = clsDesc.ToNameStr.Length; } var mergeColumnDesc = clsDesc.GetMergePropertyInfo(className); if (mergeColumnDesc != null) { PropertyInfo[] piArr = mergeColumnDesc.mergeAttribute.Type.GetProperties(); DbColumnAttribute secondAtb = piArr[1].GetCustomAttributes(typeof(DbColumnAttribute), false)[0] as DbColumnAttribute; string dstName = secondAtb.ColumnName; int mergeColCount = piArr.Length; string colName = dr.GetName(i); int index = colName.IndexOf('_'); if (index != -1 && dr.FieldCount >= i + mergeColCount - 1) { string prefix = colName.Substring(0, index); dstName = prefix + dstName.Substring(dstPrefixLen); if (colName.Equals(dstName, StringComparison.CurrentCultureIgnoreCase)) { List <string> valueList = new List <string>(); valueList.Add(prefix.ToUpper()); valueList.Add(i.ToString()); for (int j = 1; j < mergeColCount - 1; j++) { colName = dr.GetName(i + j); DbColumnAttribute atb = piArr[j + 1].GetCustomAttributes(typeof(DbColumnAttribute), false)[0] as DbColumnAttribute; dstName = atb.ColumnName; dstName = prefix + dstName.Substring(dstPrefixLen); if (!colName.Equals(dstName, StringComparison.CurrentCultureIgnoreCase)) { throw new InvalidOperationException("DBClassLoader found MergeTable column format error"); } else { valueList.Add((j + i).ToString()); } } AddMergeColumn(mergeColumnDesc.mergeAttribute.Type, mergeColumnDesc.propertyInfo, model, valueList, dr); i += mergeColCount - 2; } else { addNonMergeColumn(mergeColumnDesc.mergeAttribute.Type, dr, clsDesc, ref model, i); } } else { addNonMergeColumn(mergeColumnDesc.mergeAttribute.Type, dr, clsDesc, ref model, i); } } } } } // 填充子表数据 for (int i = 0; i < clsDesc.SubTablePropertyDescList.Count; i++) { var subTablePropertyDesc = clsDesc.SubTablePropertyDescList[i]; // 外键属性都应该是List<>类型 var list = subTablePropertyDesc.propertyInfo.GetValue(model, null) as IList; // 根据具List<>对应的泛型类型获取数据 var subItemList = GetSubItemList(subTablePropertyDesc.attribute.ClassType, dbAcsFty, localKey); // 将于model匹配的数据添加到对应的外键属性 AddSubItems2ReferenceList(subItemList, GetClassDesc(subTablePropertyDesc.attribute.ClassType), list, model); } return(model); }
public SsisApplicationServices() { _dbAccessorFactory = new DbAccessorFactory(); }