/// <summary> /// 填充字类列表 /// </summary> /// <param name="pks">ID集合</param> /// <param name="fatherInfo">父表对应类的信息</param> /// <param name="dicElement">元素</param> /// <param name="mappingInfo">当前父表对应属性的映射信息</param> private static void FillParent(object pk, EntityInfoHandle fatherInfo, EntityMappingInfo mappingInfo, string propertyName, EntityBase sender) { DBInfo db = fatherInfo.DBInfo; DataBaseOperate oper = fatherInfo.DBInfo.DefaultOperate; BQLDbBase dao = new BQLDbBase(oper); ScopeList lstScope = new ScopeList(); sender.OnFillParent(propertyName, lstScope); lstScope.AddEqual(mappingInfo.TargetProperty.PropertyName, pk); IDataReader reader = dao.QueryReader(lstScope, fatherInfo.EntityType); try { //获取子表的get列表 List <EntityPropertyInfo> lstParamNames = CacheReader.GenerateCache(reader, fatherInfo);//创建一个缓存数值列表 while (reader.Read()) { object newObj = fatherInfo.CreateSelectProxyInstance(); mappingInfo.SetValue(sender, newObj); CacheReader.FillObjectFromReader(reader, lstParamNames, newObj, db); } sender.OnPropertyUpdated(mappingInfo.PropertyName); } finally { reader.Close(); oper.AutoClose(); } }
/// <summary> /// 关联表 /// </summary> /// <param name="from"></param> /// <param name="sourceTable"></param> /// <param name="targetTable"></param> /// <returns></returns> private KeyWordFromItem InnerTable(KeyWordFromItem from, AliasTableMapping sourceTable) { KeyWordFromItem inner = from; string sTableName = sourceTable.TableInfo.GetAliasName(); foreach (KeyValuePair <string, AliasTableMapping> tablePair in sourceTable.ChildTables) { AliasTableMapping tTable = tablePair.Value; string tTableName = tTable.TableInfo.GetAliasName(); EntityMappingInfo minfo = tTable.MappingInfo; BQLCondition fhandle = null; //if (minfo.IsPrimary) //{ fhandle = BQL.Tables[sTableName][minfo.SourceProperty.ParamName] == BQL.Tables[tTableName][minfo.TargetProperty.ParamName]; //} //else //{ // fhandle = BQLDbBase.BQL.Tables[sTableName][minfo.SourceProperty.ParamName] == BQLDbBase.BQL.Tables[tTableName][minfo.PrimaryKey]; //} inner = inner.LeftJoin(tablePair.Value.TableInfo, fhandle); if (tTable.ChildTables.Count > 0) { inner = InnerTable(inner, tTable); } } return(inner); }
/// <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> /// 填充该列表的子类 /// </summary> /// <param name="sender">发送者</param> public static void FillParent(string propertyName, EntityBase sender) { if (sender.HasPropertyChange(propertyName)) { return; } Type senderType = CH.GetRealType(sender); //发送者类型 EntityInfoHandle senderInfo = EntityInfoManager.GetEntityHandle(senderType); //获取发送类的信息 EntityMappingInfo mappingInfo = senderInfo.MappingInfo[propertyName]; //if (mappingInfo.GetValue(sender) != null) //{ // return; //} //IList baseList = sender.GetBaseList();//获取上一次查询的结果集合 //if (baseList == null) //{ // baseList = new ArrayList(); // baseList.Add(sender); //} //Dictionary<string, ArrayList> dicElement = new Dictionary<string, ArrayList>(); if (mappingInfo != null) { EntityPropertyInfo senderHandle = mappingInfo.SourceProperty;//本类的主键属性句柄 //IList pks = CollectFks(baseList, senderHandle, mappingInfo, dicElement, senderInfo.DBInfo); EntityInfoHandle fatherInfo = mappingInfo.TargetProperty.BelongInfo; object pk = senderHandle.GetValue(sender); FillParent(pk, fatherInfo, mappingInfo, propertyName, sender); } }
/// <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); } } } } } }
//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); }
/// <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="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="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(); } }
static void Main(string[] args) { EntityMappingInfo <User> entityMappingInfo = new EntityMappingInfo <User>(); _ = entityMappingInfo.ToTable("TUser").Property(u => u.GetEntityProperty(c => c.Uid).ToCloumn("id")). Property(u => u.GetEntityProperty(c => c.UName).ToCloumn("Name")); Console.WriteLine(entityMappingInfo.ToString()); EntityMappingInfo <Menu> entityMappingInfo1 = new EntityMappingInfo <Menu>(); _ = entityMappingInfo1.ToTable("TMenu").Property(u => u.GetEntityProperty(c => c.Mid).ToCloumn("id")). Property(u => u.GetEntityProperty(c => c.MName).ToCloumn("Name")); Console.WriteLine(entityMappingInfo1.ToString()); Console.WriteLine("**********************************************************"); string connectionSql = JsonConifgUtil.Currnet.AddFile("system", "appsettings.json"). ReadConfig("system"). GetValue("ConnectionSql"); DBManager manager = new DBManager(connectionSql); manager.OnModelBuilder <User>(entityMappingInfo); manager.OnModelBuilder <Menu>(entityMappingInfo1); User use = new User() { Uid = 010, UName = "宁凡栋", UCode = "03815", UAge = 28 }; Menu menu = new Menu() { Mid = 10, MName = "我是菜单" }; //manager.Entity<User>().Insert(use); //manager.Entity<Menu>().Insert(menu); //string sql=manager.Entity<User>().Query(u=>u.Uid==10&&u.UName=="张三"); // Console.WriteLine(sql); manager.Set <UserInfo>(); //manager.Entity<UserInfo>().Insert(new UserInfo //{ // UId = Guid.NewGuid().ToString(), // UName="宁凡栋", // UAccount="003185" //}); //manager.Entity<UserInfo>().Insert(new UserInfo //{ // UId = Guid.NewGuid().ToString(), // UName = "宁俊哲", // UAccount = "003186" //}); List <UserInfo> users = manager.Entity <UserInfo>().Query(u => u.UName == "宁凡栋" || u.UName == "宁俊哲"); users.ForEach(user => { Console.WriteLine(user.ToString() + "==============>" + user.UId); }); Console.Read(); }
/// <summary> /// 属性的关联更新信息 /// </summary> /// <param name="mapInfo"></param> /// <param name="isEntityProperty"></param> public UpdatePropertyInfo(EntityMappingInfo mapInfo, bool isEntityProperty) { _mapInfo = mapInfo; _isEntityProperty = isEntityProperty; }
/// <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> /// 把集合转成字典形式 /// </summary> /// <param name="lst">集合</param> /// <param name="mappingInfo">映射</param> /// <returns></returns> private static Dictionary <string, List <object> > GetEntityDictionary(IEnumerable lst, EntityMappingInfo mappingInfo) { Dictionary <string, List <object> > dic = new Dictionary <string, List <object> >(); string propertyName = mappingInfo.PropertyName; List <object> lstCur = null; foreach (object obj in lst) { EntityBase entity = obj as EntityBase; if (entity == null) { continue; } string key = mappingInfo.SourceProperty.GetValue(entity).ToString(); if (!dic.TryGetValue(key, out lstCur)) { lstCur = new List <object>(); dic[key] = lstCur; } lstCur.Add(entity); //创建子集合 if (!mappingInfo.IsParent) { object childlst = Activator.CreateInstance(mappingInfo.FieldType); mappingInfo.SetValue(entity, childlst); entity.OnFillChild(propertyName, new ScopeList()); } } return(dic); }