/// <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> /// 根据ID删除记录 /// </summary> /// <param name="id">要删除的记录ID</param> /// <returns></returns> public int DeleteById(object id) { int ret = -1; DeleteCondition con = new DeleteCondition(EntityInfo.DBInfo); con.Tables.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(EntityInfo.TableName)); ParamList list = new ParamList(); ScopeList lstScope = new ScopeList(); PrimaryKeyInfo pkInfo = id as PrimaryKeyInfo; if (pkInfo == null) { lstScope.AddEqual(EntityInfo.PrimaryProperty[0].PropertyName, id); } else { pkInfo.FillScope(EntityInfo.PrimaryProperty, lstScope, true); } con.Condition.Append("1=1"); con.Condition.Append(DataAccessCommon.FillCondition(EntityInfo, list, lstScope)); Dictionary <string, bool> cacheTables = null; cacheTables = _oper.DBInfo.QueryCache.CreateMap(EntityInfo.TableName); ret = ExecuteCommand(con.GetSql(true), list, CommandType.Text, cacheTables); return(ret); }
public void FillScope(List <EntityPropertyInfo> lstPkInfo, ScopeList lstScope, bool throwException) { foreach (EntityPropertyInfo info in lstPkInfo) { object value = this[info.PropertyName]; if (value == null && throwException) { throw new KeyNotFoundException("主键属性:" + info.PropertyName + "未赋值"); } lstScope.AddEqual(info.PropertyName, value); } }
/// <summary> /// 删除指定数据 /// </summary> /// <param name="obj">要删除的实体</param> /// <param name="scopeList">要删除的条件</param> /// <param name="isConcurrency">是否版本并发删除</param> /// <returns></returns> public int Delete(EntityBase obj, ScopeList scopeList, bool isConcurrency) { DeleteCondition con = new DeleteCondition(EntityInfo.DBInfo); con.Tables.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(EntityInfo.TableName)); ParamList list = new ParamList(); Type type = EntityInfo.EntityType; con.Condition.Append("1=1"); if (obj != null) { if (scopeList == null)//通过ID删除 { scopeList = new ScopeList(); foreach (EntityPropertyInfo info in EntityInfo.PrimaryProperty) { scopeList.AddEqual(info.PropertyName, info.GetValue(obj)); } } } con.Condition.Append(DataAccessCommon.FillCondition(EntityInfo, list, scopeList)); if (isConcurrency) { int index = 0; foreach (EntityPropertyInfo pInfo in EntityInfo.PropertyInfo) { if (pInfo.IsVersion) { FillWhereConcurrency(con.Condition, pInfo, list, pInfo.GetValue(obj), ref index); } } } Dictionary <string, bool> cacheTables = null; cacheTables = _oper.DBInfo.QueryCache.CreateMap(EntityInfo.TableName); int ret = -1; ret = ExecuteCommand(con.GetSql(true), list, CommandType.Text, cacheTables); return(ret); }
/// <summary> /// 根据主键查找实体 /// </summary> /// <param name="id">主键</param> /// <returns></returns> public T GetEntityById(object id) { ScopeList lstScope = new ScopeList(); PrimaryKeyInfo info = id as PrimaryKeyInfo; if (info == null) { lstScope.AddEqual(CurEntityInfo.PrimaryProperty[0].PropertyName, id); } else { info.FillScope(CurEntityInfo.PrimaryProperty, lstScope, true); } return(GetUnique(lstScope)); }
/// <summary> /// 并发删除 /// </summary> /// <param name="optimisticConcurrency">是否并发控制</param> /// <returns></returns> public virtual int Delete(bool optimisticConcurrency) { DataAccessSetBase dal = GetBaseDataAccess(); ScopeList lstScope = new ScopeList(); foreach (EntityPropertyInfo pInfo in dal.EntityInfo.PrimaryProperty) { object id = pInfo.GetValue(this); if (DefaultType.IsDefaultValue(id)) { throw new Exception("主键:" + pInfo.PropertyName + "的值不能为空"); } lstScope.AddEqual(pInfo.PropertyName, id); } return(dal.Delete(this, lstScope, optimisticConcurrency)); }
/// <summary> /// 根据ID获取记录 /// </summary> /// <param name="id">ID</param> /// <param name="isSearchByCache">是否缓存搜索</param> /// <returns></returns> public T GetObjectById(object id, bool isSearchByCache) { ParamList list = null; T ret = default(T); list = new ParamList(); string tabName = CurEntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(CurEntityInfo.TableName); ScopeList lstScope = new ScopeList(); lstScope.UseCache = isSearchByCache; PrimaryKeyInfo pkInfo = id as PrimaryKeyInfo; if (pkInfo == null) { lstScope.AddEqual(CurEntityInfo.PrimaryProperty[0].PropertyName, id); } else { pkInfo.FillScope(CurEntityInfo.PrimaryProperty, lstScope, true); } SelectCondition sc = GetSelectContant(list, lstScope, GetSelectParams(lstScope)); //sql.Append( DataAccessCommon.FillCondition(CurEntityInfo,list, lstScope)); Dictionary <string, bool> cacheTables = null; if (lstScope.UseCache) { cacheTables = _oper.DBInfo.QueryCache.CreateMap(CurEntityInfo.TableName); } using (IDataReader reader = _oper.Query(sc.GetSql(lstScope.UseCache), list, cacheTables)) { if (reader.Read()) { ret = LoadFromReader(reader); } } return(ret); }
/// <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(); } }