Exemplo n.º 1
0
        /// <summary>
        /// 子类重写此方法实现批量删除逻辑。
        /// </summary>
        /// <param name="batch"></param>
        protected virtual void ImportDelete(EntityBatch batch)
        {
            string sql           = "DELETE FROM " + batch.Table.Name + " WHERE ID IN (";
            bool   needDelimiter = false;

            foreach (var section in this.EnumerateAllBatches(batch.DeleteBatch, 1000))
            {
                var sqlDelete = new StringBuilder(sql);
                for (int i = 0, c = section.Count; i < c; i++)
                {
                    var item = section[i];
                    if (i > 0)
                    {
                        sqlDelete.Append(',');
                    }
                    else
                    {
                        var keyType = item.KeyProvider.KeyType;
                        needDelimiter = keyType.IsClass;
                    }

                    if (needDelimiter)
                    {
                        sqlDelete.Append('\'').Append(item.Id).Append('\'');
                    }
                    else
                    {
                        sqlDelete.Append(item.Id);
                    }
                }
                sqlDelete.Append(')');

                batch.DBA.ExecuteText(sqlDelete.ToString());
            }
        }
Exemplo n.º 2
0
        private void ReadToBatchRecur(Entity entity, EntityBatch batch)
        {
            switch (entity.PersistenceStatus)
            {
            case PersistenceStatus.Unchanged:
                break;

            case PersistenceStatus.Modified:
                batch.UpdateBatch.Add(entity);
                break;

            case PersistenceStatus.New:
                batch.InsertBatch.Add(entity);
                break;

            case PersistenceStatus.Deleted:
                batch.DeleteBatch.Add(entity);
                break;

            default:
                break;
            }

            this.ReadChildrenRecur(entity, batch);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 递归读取指定父对象中的所有子对象
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <param name="batch">The batch.</param>
 private void ReadChildrenRecur(Entity entity, EntityBatch batch)
 {
     //遍历所有子属性,读取孩子列表
     foreach (var child in entity.GetLoadedChildren())
     {
         var children = child.Value as EntityList;
         //所有孩子列表中的实体,都加入到对应的实体列表中。
         //并递归读取孩子的孩子实体。
         var childBatch = this.FindBatch(children.EntityType);
         ReadToBatchRecur(children, childBatch);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// 子类重写此方法实现批量删除逻辑。
        /// </summary>
        /// <param name="batch"></param>
        protected virtual void ImportDelete(EntityBatch batch)
        {
            foreach (var section in this.EnumerateAllBatches(batch.DeleteBatch, 1000))
            {
                var sqlDelete = new FormattedSql();
                if (batch.Repository.EntityMeta.IsPhantomEnabled)
                {
                    var isPhantomColumn = batch.Table.FindByPropertyName(EntityConvention.Property_IsPhantom.Name).Name;
                    var value           = this.SqlGenerator.DbTypeCoverter.ToDbParameterValue(BooleanBoxes.True);

                    sqlDelete.Append("UPDATE ").Append(batch.Table.Name).Append(" SET ")
                    .Append(isPhantomColumn).Append(" = ").AppendParameter(value)
                    .Append(" WHERE ID IN (");
                }
                else
                {
                    sqlDelete.Append("DELETE FROM ").Append(batch.Table.Name).Append(" WHERE ID IN (");
                }

                bool needDelimiter = false;
                for (int i = 0, c = section.Count; i < c; i++)
                {
                    var item = section[i];
                    if (i > 0)
                    {
                        sqlDelete.Append(',');
                    }
                    else
                    {
                        var keyType = (item as IEntity).IdProvider.KeyType;
                        needDelimiter = keyType.IsClass;
                    }

                    if (needDelimiter)
                    {
                        sqlDelete.Append('\'').Append(item.Id).Append('\'');
                    }
                    else
                    {
                        sqlDelete.Append(item.Id);
                    }
                }
                sqlDelete.Append(')');

                batch.DBA.ExecuteText(sqlDelete, sqlDelete.Parameters);
            }
        }
Exemplo n.º 5
0
        private void ReadToBatchRecur(EntityList entityList, EntityBatch batch)
        {
            var deletedList = entityList.DeletedListField;

            if (deletedList != null)
            {
                for (int i = 0, c = deletedList.Count; i < c; i++)
                {
                    var entity = deletedList[i];
                    this.ReadToBatchRecur(entity, batch);
                }
            }

            for (int i = 0, c = entityList.Count; i < c; i++)
            {
                var entity = entityList[i];
                this.ReadToBatchRecur(entity, batch);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 子类重写此方法实现批量删除逻辑。
        /// </summary>
        /// <param name="batch"></param>
        protected virtual void ImportDelete(EntityBatch batch)
        {
            foreach (var section in this.EnumerateAllBatches(batch.DeleteBatch, 1000))
            {
                FormattedSql sqlDelete = null;
                if (batch.Repository.EntityMeta.IsPhantomEnabled)
                {
                    sqlDelete = "UPDATE " + batch.Table.Name + " SET ISPHANTOM = {0} WHERE ID IN (";
                    sqlDelete.Parameters.Add(BooleanBoxes.True);
                }
                else
                {
                    sqlDelete = "DELETE FROM " + batch.Table.Name + " WHERE ID IN (";
                }

                bool needDelimiter = false;
                for (int i = 0, c = section.Count; i < c; i++)
                {
                    var item = section[i];
                    if (i > 0)
                    {
                        sqlDelete.Append(',');
                    }
                    else
                    {
                        var keyType = item.KeyProvider.KeyType;
                        needDelimiter = keyType.IsClass;
                    }

                    if (needDelimiter)
                    {
                        sqlDelete.Append('\'').Append(item.Id).Append('\'');
                    }
                    else
                    {
                        sqlDelete.Append(item.Id);
                    }
                }
                sqlDelete.Append(')');

                batch.DBA.ExecuteText(sqlDelete, sqlDelete.Parameters);
            }
        }
Exemplo n.º 7
0
        private void ReadToBatchRecur(Entity entity, EntityBatch batch)
        {
            switch (entity.PersistenceStatus)
            {
            case PersistenceStatus.Unchanged:
                break;

            case PersistenceStatus.Modified:
                batch.UpdateBatch.Add(entity);
                break;

            case PersistenceStatus.New:
                batch.InsertBatch.Add(entity);
                break;

            case PersistenceStatus.Deleted:

                batch.DeleteBatch.Add(entity);

                //如果本类启用了假删除,那么它下面的所有实体都需要加载到内存中,这样在读取聚合时,它的聚合子也会读取到待删除列表中。
                if (batch.Repository.EntityMeta.IsPhantomEnabled)
                {
                    //不论这个列表属性是否已经加载,都必须获取其所有的数据行,并标记为删除。
                    batch.Repository.LoadAllChildren(entity);

                    foreach (var child in entity.GetLoadedChildren())
                    {
                        var list = child.Value as EntityList;
                        if (list != null && list.Count > 0)
                        {
                            list.Clear();
                        }
                    }
                }
                break;

            default:
                break;
            }

            this.ReadChildrenRecur(entity, batch);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 递归读取指定父对象中的所有子对象
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="batch">The batch.</param>
        private void ReadChildrenRecur(Entity entity, EntityBatch batch)
        {
            var childrenProperties = batch.Repository.EntityMeta.ChildrenProperties;

            //遍历所有子属性,读取孩子列表
            for (int i = 0, c = childrenProperties.Count; i < c; i++)
            {
                var cp           = childrenProperties[i];
                var listProperty = cp.ManagedProperty as IListProperty;

                var children = entity.GetLazyList(listProperty);
                if (children.Count > 0)
                {
                    //所有孩子列表中的实体,都加入到对应的实体列表中。
                    //并递归读取孩子的孩子实体。
                    var entityType = children[0].GetType();
                    var childBatch = this.FindBatch(entityType);
                    ReadToBatchRecur(children, childBatch);
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 找到对应实体的全部对象列表
        /// </summary>
        /// <param name="entityType"></param>
        /// <returns></returns>
        private EntityBatch FindBatch(Type entityType)
        {
            for (int i = 0, c = _batches.Count; i < c; i++)
            {
                var batch = _batches[i];
                if (batch.EntityType == entityType)
                {
                    return(batch);
                }
            }

            var newBatch = new EntityBatch
            {
                EntityType = entityType,
                Repository = RF.Find(entityType)
            };

            _batches.Add(newBatch);

            return(newBatch);
        }
Exemplo n.º 10
0
 /// <summary>
 /// 子类重写此方法实现批量插入逻辑。
 /// </summary>
 /// <param name="batch"></param>
 protected virtual void ImportInsert(EntityBatch batch)
 {
     throw new NotSupportedException("目前不支持批量插入数据。");
 }
Exemplo n.º 11
0
 /// <summary>
 /// 子类重写此方法实现批量更新逻辑。
 /// </summary>
 /// <param name="batch"></param>
 protected virtual void ImportUpdate(EntityBatch batch)
 {
     throw new NotSupportedException("目前不支持批量更新数据。");
 }
Exemplo n.º 12
0
 /// <summary>
 /// 子类重写此方法实现批量插入逻辑。
 /// </summary>
 /// <param name="batch"></param>
 protected virtual void ImportInsert(EntityBatch batch)
 {
     throw new NotSupportedException("目前不支持批量插入数据。");
 }
Exemplo n.º 13
0
 /// <summary>
 /// 子类重写此方法实现批量更新逻辑。
 /// </summary>
 /// <param name="batch"></param>
 protected virtual void ImportUpdate(EntityBatch batch)
 {
     throw new NotSupportedException("目前不支持批量更新数据。");
 }
Exemplo n.º 14
0
        /// <summary>
        /// 子类重写此方法实现批量删除逻辑。
        /// </summary>
        /// <param name="batch"></param>
        protected virtual void ImportDelete(EntityBatch batch)
        {
            string sql = "DELETE FROM " + batch.Table.Name + " WHERE ID IN (";
            bool needDelimiter = false;

            foreach (var section in this.EnumerateAllBatches(batch.DeleteBatch, 1000))
            {
                var sqlDelete = new StringBuilder(sql);
                for (int i = 0, c = section.Count; i < c; i++)
                {
                    var item = section[i];
                    if (i > 0) { sqlDelete.Append(','); }
                    else
                    {
                        var keyType = item.KeyProvider.KeyType;
                        needDelimiter = keyType.IsClass;
                    }

                    if (needDelimiter)
                    {
                        sqlDelete.Append('\'').Append(item.Id).Append('\'');
                    }
                    else
                    {
                        sqlDelete.Append(item.Id);
                    }
                }
                sqlDelete.Append(')');

                batch.DBA.ExecuteText(sqlDelete.ToString());
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// 递归读取指定父对象中的所有子对象
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="batch">The batch.</param>
        private void ReadChildrenRecur(Entity entity, EntityBatch batch)
        {
            var childrenProperties = batch.Repository.EntityMeta.ChildrenProperties;

            //遍历所有子属性,读取孩子列表
            for (int i = 0, c = childrenProperties.Count; i < c; i++)
            {
                var cp = childrenProperties[i];
                var listProperty = cp.ManagedProperty as IListProperty;

                var children = entity.GetLazyList(listProperty);
                if (children.Count > 0)
                {
                    //所有孩子列表中的实体,都加入到对应的实体列表中。
                    //并递归读取孩子的孩子实体。
                    var entityType = children[0].GetType();
                    var childBatch = this.FindBatch(entityType);
                    ReadToBatchRecur(children, childBatch);
                }
            }
        }
Exemplo n.º 16
0
        private void ReadToBatchRecur(EntityList entityList, EntityBatch batch)
        {
            var deletedList = entityList.DeletedListField;
            if (deletedList != null)
            {
                for (int i = 0, c = deletedList.Count; i < c; i++)
                {
                    var entity = deletedList[i];
                    this.ReadToBatchRecur(entity, batch);
                }
            }

            for (int i = 0, c = entityList.Count; i < c; i++)
            {
                var entity = entityList[i];
                this.ReadToBatchRecur(entity, batch);
            }
        }
Exemplo n.º 17
0
        private void ReadToBatchRecur(Entity entity, EntityBatch batch)
        {
            switch (entity.PersistenceStatus)
            {
                case PersistenceStatus.Unchanged:
                    break;
                case PersistenceStatus.Modified:
                    batch.UpdateBatch.Add(entity);
                    break;
                case PersistenceStatus.New:
                    batch.InsertBatch.Add(entity);
                    break;
                case PersistenceStatus.Deleted:
                    batch.DeleteBatch.Add(entity);
                    break;
                default:
                    break;
            }

            this.ReadChildrenRecur(entity, batch);
        }
Exemplo n.º 18
0
        /// <summary>
        /// 找到对应实体的全部对象列表
        /// </summary>
        /// <param name="entityType"></param>
        /// <returns></returns>
        private EntityBatch FindBatch(Type entityType)
        {
            for (int i = 0, c = _batches.Count; i < c; i++)
            {
                var batch = _batches[i];
                if (batch.EntityType == entityType)
                {
                    return batch;
                }
            }

            var newBatch = new EntityBatch
            {
                EntityType = entityType,
                Repository = RF.Find(entityType)
            };
            _batches.Add(newBatch);

            return newBatch;
        }