/// <summary> /// 填充子属性列表 /// </summary> /// <param name="lst">集合</param> /// <param name="childPropertyName">子属性名</param> /// <param name="objs">实体</param> /// <param name="objType">类型</param> /// <param name="filter">筛选条件</param> private static void FillEntityChidList(IEnumerable lst, string childPropertyName, Queue <object> objs, Type objType, ScopeList filter) { if (lst == null) { return; } EntityInfoHandle entityInfo = EntityInfoManager.GetEntityHandle(objType); if (entityInfo == null) { throw new Exception("找不到类:" + objType.FullName + "的映射"); } EntityMappingInfo mappingInfo = entityInfo.MappingInfo[childPropertyName]; if (mappingInfo == null) { throw new Exception("找不到子属性:" + childPropertyName); } Queue <object> pks = CollectFks(lst, mappingInfo.SourceProperty); EntityInfoHandle childHandle = mappingInfo.TargetProperty.BelongInfo;//获取子元素的信息 Dictionary <string, List <object> > dic = GetEntityDictionary(lst, mappingInfo); FillChilds(pks, childHandle, mappingInfo, dic, childPropertyName, objs, filter); }
/// <summary> /// 创建Reader缓存 /// </summary> internal static List <EntityPropertyInfo> GenerateCache(IDataReader reader, EntityInfoHandle entityInfo) { IDBAdapter idb = entityInfo.DBInfo.CurrentDbAdapter; Dictionary <string, EntityPropertyInfo> dicParamHandle = new Dictionary <string, EntityPropertyInfo>();//获取字段名跟属性对照的集合 //Dictionary<string, EntityPropertyInfo>.Enumerator enums = entityInfo.PropertyInfo.GetPropertyEnumerator(); foreach (EntityPropertyInfo info in entityInfo.PropertyInfo) { //EntityPropertyInfo info=enums.Current.Value; dicParamHandle.Add(info.ParamName, info); dicParamHandle[idb.FormatParam(info.ParamName)] = info; } List <EntityPropertyInfo> cacheReader = new List <EntityPropertyInfo>(); for (int i = 0; i < reader.FieldCount; i++) //把属性按照指定字段名顺序排列到List里边 { EntityPropertyInfo info = null; if (dicParamHandle.TryGetValue(reader.GetName(i), out info))//获取数据库字段名对应的实体字段反射 { cacheReader.Add(info); } else { cacheReader.Add(null); } } return(cacheReader); //dicReaderCache.Add(type.FullName,cacheReader); }
/// <summary> /// 建方法 /// </summary> /// <param name="classType"></param> /// <param name="inspectorFieldBuilder"></param> /// <param name="typeBuilder"></param> private void BuildMethod(Type classType, TypeBuilder typeBuilder) { EntityInfoHandle entityInfo = EntityInfoManager.GetEntityHandle(classType); MethodInfo method = null; foreach (EntityPropertyInfo pInfo in entityInfo.PropertyInfo) { UpdatePropertyInfo updateInfo = entityInfo.GetUpdatePropertyInfo(pInfo.PropertyName); if (updateInfo != null) { method = _mapupdateMethod;//如果是关联属性则调用OnMapPropertyUpdated } else { method = _updateMethod;//如果是一般属性则调用OnPropertyUpdated通知 } BuildEmit(classType, pInfo.BelongPropertyInfo, typeBuilder, method); } foreach (EntityMappingInfo mInfo in entityInfo.MappingInfo) { FieldInfo finfo = mInfo.BelongFieldInfo; if (mInfo.IsParent) { BuildEmit(classType, mInfo.BelongPropertyInfo, typeBuilder, _mapupdateMethod); //创建set方法 BuildMapEmit(classType, mInfo.BelongPropertyInfo, finfo, typeBuilder, _fillParent); //创建get方法 } else { BuildMapEmit(classType, mInfo.BelongPropertyInfo, finfo, typeBuilder, _fillChildMethod);//创建get方法 } } }
/// <summary> /// 查询并填充子类信息 /// </summary> /// <param name="pks"></param> /// <param name="mappingInfo"></param> /// <param name="dicEntity"></param> /// <param name="dao"></param> /// <param name="lstParamNames"></param> /// <param name="db"></param> /// <param name="curObjs"></param> /// <param name="filter"></param> private static void FillChildReader(Queue <object> pks, EntityMappingInfo mappingInfo, Dictionary <string, List <object> > dicEntity, BQLDbBase dao, ref List <EntityPropertyInfo> lstParamNames, DBInfo db, Queue <object> curObjs, ScopeList filter) { EntityInfoHandle childInfo = mappingInfo.TargetProperty.BelongInfo; string fullName = mappingInfo.TargetProperty.BelongInfo.EntityType.FullName; Type childType = mappingInfo.TargetProperty.BelongInfo.EntityType; List <object> senders = null; while (pks.Count > 0) { Queue <object> searchPks = GetSearchPKs(pks); if (searchPks.Count <= 0) { break; } ScopeList lstScope = new ScopeList(); lstScope.AddScopeList(filter); lstScope.AddIn(mappingInfo.TargetProperty.PropertyName, searchPks); using (IDataReader reader = dao.QueryReader(lstScope, childInfo.EntityType)) { //获取子表的get列表 if (lstParamNames == null) { lstParamNames = CacheReader.GenerateCache(reader, childInfo);//创建一个缓存数值列表 } while (reader.Read()) { string fk = reader[mappingInfo.TargetProperty.ParamName].ToString(); if (!dicEntity.TryGetValue(fk, out senders)) { continue; } object obj = childInfo.CreateSelectProxyInstance(); if (curObjs != null) { curObjs.Enqueue(obj); } CacheReader.FillObjectFromReader(reader, lstParamNames, obj, db); foreach (object sender in senders) { if (mappingInfo.IsParent) { mappingInfo.SetValue(sender, obj); } else { IList lst = (IList)mappingInfo.GetValue(sender); lst.Add(obj); } } } } } }
/// <summary> /// 获取当前实体的信息 /// </summary> /// <returns></returns> public EntityInfoHandle GetEntityInfo() { if (_thisInfo == null) { _thisInfo = EntityInfoManager.GetEntityHandle(CH.GetRealType(this)); } return(_thisInfo); }
//private Dictionary<string, EntityBase> _dicInstance = new Dictionary<string, EntityBase>();//已经实例化的实体 //private IList _baseList; /// <summary> /// 别名映射 /// </summary> /// <param name="table"></param> /// <param name="aliasName"></param> public AliasTableMapping(BQLEntityTableHandle table, TableAliasNameManager belongManager, EntityMappingInfo mappingInfo) { _belongManager = belongManager; _entityInfo = table.GetEntityInfo(); _table = new BQLAliasHandle(table, _belongManager.NextTableAliasName()); _mappingInfo = mappingInfo; InitParam(table); }
public static void FillInfoFromReader(IDataReader reader, EntityInfoHandle entityInfo, object obj) { if (reader != null && !reader.IsClosed && obj != null) { List <EntityPropertyInfo> lstParamNames = GenerateCache(reader, entityInfo); FillObjectFromReader(reader, lstParamNames, obj, entityInfo.DBInfo); } }
/// <summary> /// 获取数据层(用作运行SQL) /// </summary> /// <returns></returns> public DataAccessSetBase GetDAL() { if (_dal == null) { EntityInfoHandle handle = EntityInfoManager.GetEntityHandle(typeof(T)); _dal = new DataAccessSetBase(handle); _dal.Oper = StaticConnection.GetStaticOperate(handle.DBInfo); } return(_dal); }
/// <summary> /// 获取基础数据层 /// </summary> /// <returns></returns> private DataAccessSetBase GetBaseDataAccess() { if (_dal == null) { EntityInfoHandle handle = EntityInfoManager.GetEntityHandle(CH.GetRealType(this)); _dal = new DataAccessSetBase(handle); _dal.Oper = StaticConnection.GetStaticOperate(handle.DBInfo); } return(_dal); }
/// <summary> /// 获取类型对应的表名 /// </summary> /// <param name="type"></param> /// <returns></returns> public string GetTableName(Type type) { EntityInfoHandle eInfo = EntityInfoManager.GetEntityHandle(type); if (eInfo != null) { return(eInfo.TableName); } return(""); }
/// <summary> /// 获取排序 /// </summary> /// <param name="lstScort"></param> /// <param name="table"></param> /// <param name="entityType"></param> /// <returns></returns> protected BQLParamHandle[] GetSort(SortList lstScort, BQLTableHandle table, Type entityType) { EntityInfoHandle entityInfo = null; if (entityType != null) { entityInfo = EntityInfoManager.GetEntityHandle(entityType); } return(BQLConditionScope.GetSort(lstScort, table, entityInfo)); }
/// <summary> /// 填充信息 /// </summary> /// <param name="condition"></param> /// <param name="table"></param> /// <param name="lstScope"></param> /// <param name="entityType"></param> public BQLCondition FillCondition(BQLCondition condition, BQLTableHandle table, ScopeList lstScope, Type entityType) { BQLCondition ret; EntityInfoHandle entityInfo = null; if (entityType != null) { entityInfo = EntityInfoManager.GetEntityHandle(entityType); } ret = BQLConditionScope.FillCondition(condition, table, lstScope, entityInfo); return(ret); }
/// <summary> /// 从Reader里边读取一个对象数据 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="reader">reader</param> /// <returns></returns> public static T LoadFormReader <T>(IDataReader reader, EntityInfoHandle entityInfo) where T : EntityBase, new() { //string fullName = typeof(T).FullName; if (reader != null && !reader.IsClosed) { List <EntityPropertyInfo> lstParamNames = GenerateCache(reader, entityInfo); T ret = (T)entityInfo.CreateSelectProxyInstance();//实例化对象 FillObjectFromReader(reader, lstParamNames, ret, entityInfo.DBInfo); return(ret); } return(default(T)); }
/// <summary> /// 获取指定对应字段名的属性句柄 /// </summary> /// <param name="type">类型</param> /// <param name="pkName">字段名</param> /// <returns></returns> private static EntityPropertyInfo GetPkHandle(Type type, string pkName) { EntityInfoHandle classInfo = EntityInfoManager.GetEntityHandle(type); foreach (EntityPropertyInfo info in classInfo.PropertyInfo) { if (pkName == info.ParamName) { return(info); } } return(null); }
private BQLEntityTableHandle _belongTable;//父表 /// <summary> /// 实体属性信息 /// </summary> /// <param name="entityInfo">实体信息</param> /// <param name="propertyName">属性名</param> internal BQLEntityParamHandle(EntityInfoHandle entityInfo, string propertyName, BQLEntityTableHandle belongTable) { this.entityInfo = entityInfo; if (propertyName != "*") { pinfo = entityInfo.PropertyInfo[propertyName]; if (pinfo == null) { throw new MissingMemberException(entityInfo.EntityType.FullName + "类中不包含属性:" + propertyName); } this._valueDbType = pinfo.SqlType; } _belongTable = belongTable; }
/// <summary> /// 添加子表 /// </summary> /// <param name="table">子表</param> public AliasTableMapping AddChildTable(BQLEntityTableHandle table) { AliasTableMapping retTable = null; Stack <BQLEntityTableHandle> stkTables = new Stack <BQLEntityTableHandle>(); BQLEntityTableHandle curTable = table; do { stkTables.Push(curTable); curTable = curTable.GetParentTable(); } while (!CommonMethods.IsNull(curTable)); AliasTableMapping lastTable = null;//上一个表 while (stkTables.Count > 0) { BQLEntityTableHandle cTable = stkTables.Pop(); string pName = cTable.GetPropertyName(); if (string.IsNullOrEmpty(pName)) { lastTable = this; retTable = this; } else { if (!lastTable._dicChildTables.ContainsKey(pName)) { EntityInfoHandle entityInfo = retTable.EntityInfo; EntityMappingInfo mapInfo = entityInfo.MappingInfo[pName]; if (mapInfo != null) { retTable = new AliasTableMapping(cTable, _belongManager, mapInfo); lastTable._dicChildTables[pName] = retTable; lastTable = retTable; } else { throw new MissingMemberException("实体:" + entityInfo.EntityType.FullName + "中找不到属性:" + pName + ""); } } else { retTable = lastTable._dicChildTables[pName]; lastTable = retTable; } } } return(retTable); }
/// <summary> /// 填充关系 /// </summary> /// <param name="tableInfo">表信息</param> /// <param name="entityInfo">实体信息</param> private static void FillRelation(KeyWordTableParamItem tableInfo, EntityInfoHandle entityInfo) { List <TableRelationAttribute> trs = new List <TableRelationAttribute>(); foreach (EntityMappingInfo info in entityInfo.MappingInfo) { TableRelationAttribute tableAttr = info.MappingInfo; if (!tableAttr.IsParent || tableAttr.IsToDB) { continue; } trs.Add(tableAttr); } tableInfo.RelationItems = trs; }
/// <summary> /// 填充该列表的子类 /// </summary> /// <param name="sender">发送者</param> public static void FillChildList(string propertyName, EntityBase sender) { Type senderType = CH.GetRealType(sender); //发送类的类型 EntityInfoHandle senderHandle = EntityInfoManager.GetEntityHandle(senderType); //获取发送类的信息 EntityMappingInfo mappingInfo = senderHandle.MappingInfo[propertyName]; if (mappingInfo != null) { EntityPropertyInfo pkHandle = mappingInfo.SourceProperty;//获取实体主键属性句柄 object lst = Activator.CreateInstance(mappingInfo.FieldType); mappingInfo.SetValue(sender, lst); object pk = pkHandle.GetValue(sender); EntityInfoHandle childHandle = mappingInfo.TargetProperty.BelongInfo;//获取子元素的信息 FillChilds(pk, childHandle, mappingInfo, sender, propertyName); } }
/// <summary> /// 通知映射属性已经被修改 /// </summary> /// <param name="propertyName"></param> protected internal virtual void OnMapPropertyUpdated(string propertyName) { EntityInfoHandle entityInfo = GetEntityInfo(); UpdatePropertyInfo updateInfo = entityInfo.GetUpdatePropertyInfo(propertyName); if (updateInfo != null) { string updatePropertyName = updateInfo.UpdateProperty(this); if (!string.IsNullOrEmpty(updatePropertyName)) { OnPropertyUpdated(updatePropertyName); } else { OnPropertyUpdated(propertyName); } } }
/// <summary> /// 从Reader里边读取数据集合(快速) /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="reader">reader</param> /// <returns></returns> public static List <T> LoadFormReaderList <T>(IDataReader reader) where T : EntityBase, new() { EntityInfoHandle entityInfo = EntityInfoManager.GetEntityHandle(typeof(T)); List <T> retLst = new List <T>(); if (reader != null && !reader.IsClosed) { List <EntityPropertyInfo> lstParamNames = GenerateCache(reader, entityInfo); while (reader.Read()) { T obj = entityInfo.CreateSelectProxyInstance() as T; FillObjectFromReader(reader, lstParamNames, obj, entityInfo.DBInfo); retLst.Add(obj); } } return(retLst); }
/// <summary> /// 填充字段信息 /// </summary> /// <param name="tableInfo">表信息</param> /// <param name="entityInfo">实体信息</param> private static void FillParamInfos(KeyWordTableParamItem tableInfo, EntityInfoHandle entityInfo) { List <EntityParam> prms = new List <EntityParam>(); foreach (EntityPropertyInfo pkInfo in entityInfo.PrimaryProperty) { prms.Add(pkInfo.ParamInfo); } foreach (EntityPropertyInfo pInfo in entityInfo.PropertyInfo) { if (pInfo.IsPrimaryKey) { continue; } prms.Add(pInfo.ParamInfo); } tableInfo.Params = prms; }
/// <summary> /// 提交属性更新通知 /// </summary> /// <param name="propertys">属性集合(null则所有属性都通知更新)</param> public void SubmitUpdateProperty(IEnumerable propertys) { if (propertys == null) { EntityInfoHandle eHandle = GetEntityInfo(); foreach (EntityPropertyInfo pinfo in eHandle.PropertyInfo) { _dicUpdateProperty___[pinfo.PropertyName] = true; } return; } foreach (object oproName in propertys) { string proName = oproName as string; if (!string.IsNullOrEmpty(proName)) { _dicUpdateProperty___[proName] = true; } } }
/// <summary> /// 从Reader里边读取数据集合(快速,返回集合的大小) /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="reader">reader</param> /// <param name="entityInfo">实体类信息</param> /// <param name="totalSize">集合总大小</param> /// <returns></returns> public static List <T> LoadFormReaderList <T>(IDataReader reader, EntityInfoHandle entityInfo, out int totalSize) where T : EntityBase, new() { List <T> retLst = new List <T>(); totalSize = 0;//初始化当前记录集总大小 if (reader != null && !reader.IsClosed) { List <EntityPropertyInfo> lstParamNames = GenerateCache(reader, entityInfo); while (reader.Read()) { T obj = (T)entityInfo.CreateSelectProxyInstance(); int curSize = 0; //获取当前值大小 FillObjectFromReader(reader, lstParamNames, obj, out curSize); totalSize += curSize; //加到当前记录总大小里边 retLst.Add(obj); } } return(retLst); }
/// <summary> /// 填充查询条件并返回条件的SQL语句( and 开头) /// </summary> /// <param name="lstParam">参数列表</param> /// <param name="lstScope">范围查询集合</param> /// <param name="CurEntityInfo">当前实体信息</param> /// <param name="index">索引</param> /// <returns></returns> internal static string FillCondition(EntityInfoHandle curEntityInfo, ParamList lstParam, ScopeList lstScope, ref int index) { if (lstScope == null) { return(""); } StringBuilder ret = new StringBuilder(); for (int i = 0; i < lstScope.Count; i++) { Scope objScope = lstScope[i]; EntityPropertyInfo info = null; if (!string.IsNullOrEmpty(objScope.PropertyName)) { info = curEntityInfo.PropertyInfo[objScope.PropertyName]; } if (objScope.ScopeType == ScopeType.Scope) { ScopeList lstInnerScope = objScope.Value1 as ScopeList; if (lstInnerScope != null) { string strSql = FillCondition(curEntityInfo, lstParam, lstInnerScope, ref index); string connectString = DataAccessCommon.GetConnectString(objScope); StringBuilder sbstrSQL = new StringBuilder(strSql); DataAccessCommon.TrimAnd(sbstrSQL); ret.Append(" "); ret.Append(connectString); ret.Append(" (" + sbstrSQL.ToString() + ")"); } } else { string pName = (info != null ? info.ParamName : ""); DbType dbType = (info != null ? info.SqlType : DbType.Object); ret.Append(FormatScorp(objScope, lstParam, pName, dbType, index, curEntityInfo.EntityType)); } index++; } return(ret.ToString()); }
/// <summary> /// 设置所属的实体的信息 /// </summary> /// <param name="entityInfo">实体信息</param> /// <returns></returns> protected void SetEntityInfo(EntityInfoHandle entityInfo, BQLEntityTableHandle parentTable, string propertyName) { _entityInfo = entityInfo; _parentTable = parentTable; _propertyName = propertyName; if (string.IsNullOrEmpty(propertyName)) { _entityKey = entityInfo.EntityType.Name; } else { if (!CommonMethods.IsNull(parentTable)) { StringBuilder sb = new StringBuilder(50); sb.Append(parentTable.GetEntityKey()); sb.Append("."); sb.Append(propertyName); _entityKey = sb.ToString(); } } }
/// <summary> /// 填充字类列表 /// </summary> /// <param name="pks">ID集合</param> /// <param name="childHandle">子元素的信息句柄</param> /// <param name="mappingInfo">映射信息</param> /// <param name="dicElement">元素</param> /// <param name="propertyName">属性名</param> /// <param name="curObjs"></param> /// <param name="filter"></param> private static void FillChilds(Queue <object> pks, EntityInfoHandle childHandle, EntityMappingInfo mappingInfo, Dictionary <string, List <object> > dicEntity, string propertyName, Queue <object> curObjs, ScopeList filter) { DBInfo db = childHandle.DBInfo; DataBaseOperate oper = childHandle.DBInfo.DefaultOperate; BQLDbBase dao = new BQLDbBase(oper); Queue <object> needCollect = null; List <EntityPropertyInfo> lstParamNames = null; try { while (pks.Count > 0) { needCollect = GetCurPks(pks); FillChildReader(needCollect, mappingInfo, dicEntity, dao, ref lstParamNames, db, curObjs, filter); } } finally { oper.AutoClose(); } }
/// <summary> /// 根据Reader结构和实体属性的映射生成空的DataTable /// </summary> /// <param name="reader">Reader</param> /// <param name="datatableName">数据表名</param> /// <param name="entityType">实体类</param> /// <param name="isEmpty">是否生成空的DataTable</param> /// <returns></returns> public static DataTable GenerateDataTable(IDataReader reader, string datatableName, Type entityType, bool isEmpty) { DataTable dt = new DataTable(); EntityInfoHandle entityInfo = EntityInfoManager.GetEntityHandle(entityType); List <EntityPropertyInfo> lstParamNames = GenerateCache(reader, entityInfo); dt.BeginLoadData(); foreach (EntityPropertyInfo info in lstParamNames) { if (info != null) { Type fieldType = info.FieldType; if (DefaultType.EqualType(fieldType, DefaultType.BooleanType)) { fieldType = typeof(bool); } dt.Columns.Add(info.PropertyName, fieldType); } } if (!isEmpty) { while (reader.Read()) { DataRow dr = dt.NewRow(); for (int i = 0; i < lstParamNames.Count; i++) { if (!reader.IsDBNull(i) && lstParamNames[i] != null) { dr[i] = reader[i]; } } dt.Rows.Add(dr); dt.AcceptChanges(); } } dt.EndLoadData(); return(dt); }
/// <summary> /// 检查数据库 /// </summary> /// <param name="db">数据库</param> /// <returns></returns> public static List <string> CheckDataBase(DBInfo db) { List <BQLEntityTableHandle> tables = db.GetAllTables(); List <KeyWordTableParamItem> lstTable = new List <KeyWordTableParamItem>(); foreach (BQLEntityTableHandle entity in tables) { EntityInfoHandle entityInfo = entity.GetEntityInfo(); string tableName = entityInfo.TableName; if (string.IsNullOrEmpty(tableName)) { continue; } KeyWordTableParamItem tableInfo = new KeyWordTableParamItem(tableName, null); FillParamInfos(tableInfo, entityInfo); FillRelation(tableInfo, entityInfo); lstTable.Add(tableInfo); } List <string> sqls = TableChecker.CheckTable(db, lstTable); return(sqls); }
/// <summary> /// 填充字类列表 /// </summary> /// <param name="pks">ID集合</param> /// <param name="childHandle">子元素的信息句柄</param> /// <param name="mappingInfo">映射信息</param> /// <param name="dicElement">元素</param> private static void FillChilds(object pk, EntityInfoHandle childHandle, EntityMappingInfo mappingInfo, EntityBase sender, string propertyName) { EntityInfoHandle childInfo = mappingInfo.TargetProperty.BelongInfo; DBInfo db = childInfo.DBInfo; DataBaseOperate oper = childHandle.DBInfo.DefaultOperate; BQLDbBase dao = new BQLDbBase(oper); ScopeList lstScope = new ScopeList(); sender.OnFillChild(propertyName, lstScope); lstScope.AddEqual(mappingInfo.TargetProperty.PropertyName, pk); IDataReader reader = dao.QueryReader(lstScope, childInfo.EntityType); try { string fullName = mappingInfo.TargetProperty.BelongInfo.EntityType.FullName; Type childType = mappingInfo.TargetProperty.BelongInfo.EntityType; //获取子表的get列表 List <EntityPropertyInfo> lstParamNames = CacheReader.GenerateCache(reader, childInfo);//创建一个缓存数值列表 IList lst = (IList)mappingInfo.GetValue(sender); while (reader.Read()) { object obj = childInfo.CreateSelectProxyInstance(); //string fk = reader[mappingInfo.TargetProperty.ParamName].ToString(); CacheReader.FillObjectFromReader(reader, lstParamNames, obj, db); lst.Add(obj); } } finally { reader.Close(); oper.AutoClose(); } }
public string GetIdentityParamValue(EntityInfoHandle entityInfo, EntityPropertyInfo info) { return("nextval('\"" + GetSequenceName(info) + "\"')"); }