/// <summary> /// 通过引用关系,来删除引用表中引用本对象的所有对象。 /// /// 一般情况下,子类可以在自己重写的 Submit 方法中调用此方法来先删除其它非级联有关系。 /// /// 注意,此方法暂时只会生成 SQL 删除引用表的对象,不主动处理更多的级联关系。(以后再实现。) /// </summary> /// <param name="entity">正在删除这个实体。</param> /// <param name="refProperty">这个引用属性表示了需要删除的另一个实体到 <c>entity</c> 的引用关系。</param> /// <param name="propertyOwner"><c>refProperty</c> 属性对应的拥有者实体类型。</param> protected void DeleteRefInDb(Entity entity, IRefProperty refProperty, Type propertyOwner = null) { if (propertyOwner == null) { propertyOwner = refProperty.OwnerType; } var refRepo = RepositoryFactoryHost.Factory.FindByEntity(propertyOwner) as EntityRepository; refRepo.RdbDataProvider.DeleteRefInDbCore(entity, refProperty); }
/// <summary> /// 实现删除关联数据的逻辑。 /// </summary> /// <param name="entity"></param> /// <param name="refProperty"></param> protected override void DeleteRefCore(Entity entity, IRefProperty refProperty) { var f = QueryFactory.Instance; var table = f.Table(_dataProvider.Repository); var where = f.Constraint(table.Column(refProperty.RefIdProperty), entity.Id); using (var dba = _dataProvider.CreateDbAccesser()) { _dataProvider.DbTable.Delete(dba, where); } }
/// <summary> /// 通过引用关系,来删除引用表中引用本对象的所有对象。 /// /// 一般情况下,子类可以在自己重写的 Submit 方法中调用此方法来先删除其它非级联有关系。 /// /// 注意,此方法暂时只会生成 SQL 删除引用表的对象,不主动处理更多的级联关系。(以后再实现。) /// </summary> /// <param name="entity">正在删除这个实体。</param> /// <param name="refProperty">这个引用属性表示了需要删除的另一个实体到 <c>entity</c> 的引用关系。</param> /// <param name="propertyOwner"><c>refProperty</c> 属性对应的拥有者实体类型。</param> protected void DeleteRef(Entity entity, IRefProperty refProperty, Type propertyOwner = null) { if (propertyOwner == null) { propertyOwner = refProperty.OwnerType; } var refRepo = RepositoryFactoryHost.Factory.FindByEntity(propertyOwner); var saver = (refRepo.DataProvider as IRepositoryDataProviderInternal).DataSaver; saver.DeleteRefCore(entity, refProperty); }
private void DeleteRefInDbCore(Entity entity, IRefProperty refProperty) { var f = QueryFactory.Instance; var table = f.Table(_repository); var where = f.Constraint(table.Column(refProperty.RefIdProperty), entity.Id); using (var dba = this.CreateDbAccesser()) { this.DbTable.Delete(dba, where); } }
/// <summary> /// 通过左数据源和右实体数据源,以及从左到右的引用属性,来构造一个连接。 /// </summary> /// <param name="left">左实体数据源。</param> /// <param name="right">右实体数据源。</param> /// <param name="leftToRight">从左到右的引用属性。</param> /// <returns></returns> public IJoin Join( ITableSource left, ITableSource right, IRefProperty leftToRight ) { var condition = this.Constraint( left.Column(leftToRight), right.Column(Entity.IdProperty) ); var joinType = leftToRight.Nullable ? JoinType.LeftOuter : JoinType.Inner; return(Join(left, right, condition, joinType)); }
private SqlTableSource AddJoinTable(ITableSource fkTable, IRefProperty parentRef, ITableSource pkTable, ITableSource joinTo) { var f = QueryFactory.Instance; var joinType = parentRef.Nullable ? JoinType.LeftOuter : JoinType.Inner; var query = this as IQuery; query.From = f.Join(query.From, joinTo, f.Constraint( fkTable.Column(parentRef.RefIdProperty), pkTable.IdColumn ), joinType); var refTableSource = new SqlTableSource { ForeignKeyTable = fkTable, RefProperty = parentRef, PrimaryKeyTable = pkTable, }; _allJoinTables.Add(refTableSource); return(refTableSource); }
public IPropertyQuery JoinRef(IRefProperty property, Type propertyOwner = null) { if (propertyOwner == null) { propertyOwner = property.OwnerType; } if (_refItems == null) { _refItems = new List <RefTableProperty>(2); } if (_refItems.All(i => i.RefProperty != property.RefIdProperty || i.PropertyOwner != propertyOwner)) { var item = new RefTableProperty(property.RefIdProperty, propertyOwner) { JoinRefType = JoinRefType.JoinOnly }; _refItems.Add(item); } return(this); }
/// <summary> /// 对实体列表中每一个实体都贪婪加载出它的所有引用实体。 /// </summary> /// <param name="list"></param> /// <param name="refProperty">贪婪加载的引用属性。</param> /// <param name="eagerLoadProperties">所有还需要贪婪加载的属性。</param> private void EagerLoadRef(EntityList list, IRefProperty refProperty, List <ConcreteProperty> eagerLoadProperties) { var refIdProperty = refProperty.RefIdProperty; //查询一个大的实体集合,包含列表中所有实体所需要的所有引用实体。 var idList = new List <object>(10); list.EachNode(e => { var refId = e.GetRefNullableId(refIdProperty); if (refId != null && idList.All(id => !id.Equals(refId))) { idList.Add(refId); } return(false); }); if (idList.Count > 0) { var targetRepo = RepositoryFactoryHost.Factory.FindByEntity(refProperty.RefEntityType); var allRefList = targetRepo.GetByIdList(idList.ToArray()); //继续递归加载它的贪婪属性。 this.EagerLoad(allRefList, eagerLoadProperties); //把大的实体集合,根据 Id,设置到每一个实体上。 var refEntityProperty = refProperty.RefEntityProperty; list.EachNode(entity => { var refId = entity.GetRefNullableId(refIdProperty); if (refId != null) { var refEntity = allRefList.Find(refId); entity.LoadProperty(refEntityProperty, refEntity); } return(false); }); } }
/// <summary> /// 子类重写此方法,实现 <see cref="DeleteRef(Entity, IRefProperty, Type)" /> 的具体逻辑。 /// </summary> /// <param name="entity"></param> /// <param name="refProperty"></param> protected abstract void DeleteRefCore(Entity entity, IRefProperty refProperty);
protected override void DeleteRefCore(Entity entity, IRefProperty refProperty) { var f = QueryFactory.Instance; var table = f.Table(_dataProvider.Repository); var where = f.Constraint(table.Column(refProperty.RefIdProperty), entity.Id); using (var dba = _dataProvider.CreateDbAccesser()) { _dataProvider.DbTable.Delete(dba, where); } }
/// <summary> /// 通过引用关系,来删除引用表中引用本对象的所有对象。 /// /// 一般情况下,子类可以在自己重写的 Submit 方法中调用此方法来先删除其它非级联有关系。 /// /// 注意,此方法暂时只会生成 SQL 删除引用表的对象,不主动处理更多的级联关系。(以后再实现。) /// </summary> /// <param name="entity">正在删除这个实体。</param> /// <param name="refProperty">这个引用属性表示了需要删除的另一个实体到 <c>entity</c> 的引用关系。</param> /// <param name="propertyOwner"><c>refProperty</c> 属性对应的拥有者实体类型。</param> protected void DeleteRef(Entity entity, IRefProperty refProperty, Type propertyOwner = null) { if (propertyOwner == null) propertyOwner = refProperty.OwnerType; var refRepo = RepositoryFactoryHost.Factory.FindByEntity(propertyOwner); var saver = (refRepo.DataProvider as IRepositoryDataProviderInternal).DataSaver; saver.DeleteRefCore(entity, refProperty); }
/// <summary> /// 子类重写此方法,实现 <see cref="DeleteRef"/> 的具体逻辑。 /// </summary> /// <param name="entity"></param> /// <param name="refProperty"></param> protected abstract void DeleteRefCore(Entity entity, IRefProperty refProperty);
protected override void DeleteRefCore(Entity entity, IRefProperty refProperty) { throw new NotImplementedException(); }
internal bool IsRefLoadedOrAssigned(IRefProperty refProperty) { var value = this.GetProperty(refProperty.RefEntityProperty); return(value != null); }
public static IJoin Join(this ISource left, ITableSource right, IRefProperty leftToRight) { return(QueryFactory.Instance.Join(left, right, leftToRight)); }
public IPropertyQuery JoinRef(IRefProperty property, Type propertyOwner = null) { if (propertyOwner == null) propertyOwner = property.OwnerType; if (_refItems == null) { _refItems = new List<RefTableProperty>(2); } if (_refItems.All(i => i.RefProperty != property.RefIdProperty || i.PropertyOwner != propertyOwner)) { var item = new RefTableProperty(property.RefIdProperty, propertyOwner) { JoinRefType = JoinRefType.JoinOnly }; _refItems.Add(item); } return this; }
protected override void DeleteRefCore(Entity entity, IRefProperty refProperty) { throw new NotImplementedException(); }
private SqlTableSource AddJoinTable(ITableSource fkTable, IRefProperty parentRef, ITableSource pkTable, ITableSource joinTo) { var f = QueryFactory.Instance; var joinType = parentRef.Nullable ? JoinType.LeftOuter : JoinType.Inner; var query = this as IQuery; query.From = f.Join(query.From, joinTo, f.Constraint( fkTable.Column(parentRef.RefIdProperty), pkTable.IdColumn ), joinType); var refTableSource = new SqlTableSource { ForeignKeyTable = fkTable, RefProperty = parentRef, PrimaryKeyTable = pkTable, }; _allJoinTables.Add(refTableSource); return refTableSource; }
/// <summary> /// 对实体列表中每一个实体都贪婪加载出它的所有引用实体。 /// </summary> /// <param name="list"></param> /// <param name="refProperty">贪婪加载的引用属性。</param> /// <param name="eagerLoadProperties">所有还需要贪婪加载的属性。</param> private void EagerLoadRef(EntityList list, IRefProperty refProperty, List <ConcreteProperty> eagerLoadProperties) { var refIdProperty = refProperty.RefIdProperty; //查询一个大的实体集合,包含列表中所有实体所需要的所有引用实体。 var idList = new List <object>(10); list.EachNode(e => { var refId = e.GetRefNullableId(refIdProperty); if (refId != null && idList.All(id => !id.Equals(refId))) { idList.Add(refId); } return(false); }); if (idList.Count > 0) { var targetRepo = RepositoryFactoryHost.Factory.FindByEntity(refProperty.RefEntityType); var refList = targetRepo.GetByIdList(idList.ToArray()); //继续递归加载它的贪婪属性。 this.EagerLoad(refList, eagerLoadProperties); #region 把实体全部放到排序列表中 //由于数据量可能较大,所以需要进行排序后再顺序加载。 IList <Entity> sortedList = null; if (_repository.SupportTree) { var tmp = new List <Entity>(list.Count); list.EachNode(p => { tmp.Add(p); return(false); }); sortedList = tmp.OrderBy(e => e.GetRefNullableId(refIdProperty)).ToList(); } else { sortedList = list.OrderBy(e => e.GetRefNullableId(refIdProperty)).ToList(); } #endregion #region 使用一次主循环就把所有的子实体都加载到父实体中。 //一次循环就能完全加载的前提是因为父集合按照 Id 排序,子集合按照父 Id 排序。 //把大的实体集合,根据 Id,设置到每一个实体上。 var refEntityProperty = refProperty.RefEntityProperty; int refListIndex = 0, refListCount = refList.Count; var refEntity = refList[refListIndex]; for (int i = 0, c = sortedList.Count; i < c; i++) { var entity = sortedList[i]; var refId = entity.GetRefNullableId(refIdProperty); if (refId != null) { //必须把该对象处理完成后,才能跳出下面的循环。 while (true) { if (object.Equals(refId, refEntity.Id)) { entity.LoadProperty(refEntityProperty, refEntity); break; } else { //检测下一个引用实体。 refListIndex++; //所有父集合已经加载完毕,退出整个循环。 if (refListIndex >= refListCount) { i = c; break; } refEntity = refList[refListIndex]; } } } } #endregion } }
public static IJoin Join(this ISource left, ITableSource right, IRefProperty leftToRight) { return QueryFactory.Instance.Join(left, right, leftToRight); }