Exemplo n.º 1
0
        /// <summary>
        /// 根据主键值将对象从库中移除。
        /// </summary>
        /// <param name="primaryValues">主键的值。数组的长度必须与实体所定义的主键相匹配。</param>
        /// <param name="fakeProperty">标识假删除的属性。</param>
        /// <param name="fake">如果具有 IsDeletedKey 属性,则提供对对象假删除的支持。</param>
        /// <exception cref="NullReferenceException">对象内部的 Database 或 EntityType 为 null。</exception>
        /// <exception cref="ArgumentNullException"><paramref name="primaryValues"/> 参数为 null。</exception>
        public void Remove(object[] primaryValues, IProperty fakeProperty, bool fake = true)
        {
            Guard.NullReference(database);
            Guard.NullReference(entityType);
            Guard.ArgumentNull(primaryValues, "primaryValues");

            try
            {
                var parameters = new ParameterCollection();
                var context    = CreateContext(parameters);

                var canFake = fakeProperty != null && fake;
                var sql     = canFake ? EntityPersistentQueryBuilder.BuildUpdateFakeDeleteQuery(context, fakeProperty, primaryValues)
                              : EntityPersistentQueryBuilder.BuildDeleteQuery(context, entityType, primaryValues);

                if (!sql.IsNullOrEmpty())
                {
                    database.ExecuteNonQuery(sql, parameters);
                }
            }
            catch (DbException exp)
            {
                throw new EntityPersistentException(SR.GetString(SRKind.FailInEntityRemove), exp);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 使用主键值查询返回一个实体。
        /// </summary>
        /// <param name="primaryValues">主键的值。数组的长度必须与实体所定义的主键相匹配。</param>
        /// <param name="func">对实体对象进行修饰的方法。</param>
        /// <returns>一个实体对象。</returns>
        public IEntity First(object[] primaryValues, Func <object, object> func)
        {
            if (primaryValues == null || primaryValues.Length == 0)
            {
                return(null);
            }

            var parameters = new ParameterCollection();
            var context    = CreateContext(parameters);
            var sql        = EntityPersistentQueryBuilder.BuildGetFirstQuery(context, entityType, primaryValues);

            if (sql.IsNullOrEmpty())
            {
                return(null);
            }

            var mapper = RowMapperFactory.CreateMapper(entityType);

            mapper.RecordWrapper = database.Provider.GetService <IRecordWrapper>();
            using (var reader = database.ExecuteReader(sql, parameters: parameters))
            {
                if (reader.Read())
                {
                    return(func(mapper.Map(reader).InitializeEnvironment(environment)) as IEntity);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        private bool InternalCreate(IEntity entity)
        {
            var parameters = new ParameterCollection();
            var context    = CreateContext(parameters);
            var sql        = EntityPersistentQueryBuilder.BuidCreateQuery(context, entity);

            if (!sql.IsNullOrEmpty())
            {
                var rootType = entity.EntityType.GetRootType();

                //找出自增长序列的属性
                var incProperty = PropertyUnity.GetPersistentProperties(rootType).FirstOrDefault(s => s.Info.GenerateType == IdentityGenerateType.AutoIncrement);

                if (incProperty != null)
                {
                    if (!string.IsNullOrEmpty(context.Syntax.IdentitySelect))
                    {
                        //获得当前插入的自增长序列值
                        var identitySelect = context.Syntax.IdentitySelect;
                        if (!identitySelect.StartsWith(";"))
                        {
                            identitySelect = ";" + identitySelect;
                        }

                        var incValue = PropertyValueHelper.NewValue(context.Database.ExecuteScalar <int>(sql + identitySelect, context.Parameters));
                        entity.InternalSetValue(incProperty, incValue);
                        return(!incValue.IsNullOrEmpty());
                    }
                    else
                    {
                        //使用生成器生成值
                        var generator = context.Database.Provider.GetService <IGeneratorProvider>();
                        if (generator != null)
                        {
                            var metadata = EntityMetadataUnity.GetEntityMetadata(entityType);
                            var inc      = generator.GenerateValue(context.Database, context.Environment == null ? metadata.TableName : context.Environment.GetVariableTableName(metadata), incProperty.Info.FieldName);
                            entity.InternalSetValue(incProperty, inc);

                            parameters.Clear();
                            sql = EntityPersistentQueryBuilder.BuidCreateQuery(context, entity);

                            return(context.Database.ExecuteNonQuery(sql, context.Parameters) > 0);
                        }
                    }
                }

                return(database.ExecuteNonQuery(sql, parameters) > 0);
            }

            return(false);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 更新实体对象的修改。
        /// </summary>
        /// <param name="entity">要更新的实体对象。</param>
        public void Update(IEntity entity)
        {
            ValidationUnity.Validate(entity);
            EntityPersistentSubscribePublisher.OnBeforeUpdate(entity);

            try
            {
                Batch(() =>
                {
                    var parameters = new ParameterCollection();
                    var context    = CreateContext(parameters);
                    var sql        = EntityPersistentQueryBuilder.BuildUpdateQuery(context, entity);

                    var isSucc = false;
                    entity.TryLockModifing(() =>
                    {
                        if (!sql.IsNullOrEmpty())
                        {
                            isSucc = database.ExecuteNonQuery(sql, parameters) > 0;
                        }

                        UpdateRelations(entity);
                    });

                    if (isSucc)
                    {
                        EntityPersistentSubscribePublisher.OnAfterUpdate(entity);
                        entity.SetState(EntityState.Unchanged);
                    }
                });
            }
            catch (Exception exp)
            {
                throw new EntityPersistentException(SR.GetString(SRKind.FailInEntityUpdate), exp);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 将指定的实体对象从库中移除。
        /// </summary>
        /// <param name="entity">要移除的实体对象。</param>
        /// <param name="fakeProperty">标识假删除的属性。</param>
        /// <param name="fake">如果具有 IsDeletedKey 属性,则提供对数据假删除的支持。</param>
        /// <exception cref="NullReferenceException">对象内部的 Database 为 null。</exception>
        /// <exception cref="ArgumentNullException"><paramref name="entity"/> 参数为 null。</exception>
        public void Remove(IEntity entity, IProperty fakeProperty, bool fake = true)
        {
            EntityPersistentSubscribePublisher.OnBeforeRemove(entity);
            try
            {
                Batch(() =>
                {
                    var parameters = new ParameterCollection();
                    var context    = CreateContext(parameters);

                    var canFake = fakeProperty != null && fake;
                    var sql     = canFake ? EntityPersistentQueryBuilder.BuildUpdateFakeDeleteQuery(context, entity, fakeProperty)
                            : EntityPersistentQueryBuilder.BuildDeleteQuery(context, entity);

                    var isSucc = false;
                    entity.TryLockModifing(() =>
                    {
                        if (!sql.IsNullOrEmpty())
                        {
                            isSucc = database.ExecuteNonQuery(sql, parameters) > 0;
                        }

                        UpdateRelations(entity);
                    });
                    if (isSucc)
                    {
                        EntityPersistentSubscribePublisher.OnAfterRemove(entity);
                        entity.SetState(EntityState.Unchanged);
                    }
                });
            }
            catch (DbException exp)
            {
                throw new EntityPersistentException(SR.GetString(SRKind.FailInEntityRemove), exp);
            }
        }