Ejemplo n.º 1
0
        public async Task <bool> ExecuteCommandIdentityIntoEntityAsync()
        {
            var result       = InsertObjs.First();
            var identityKeys = GetIdentityKeys();

            if (identityKeys.Count == 0)
            {
                return(await this.ExecuteCommandAsync() > 0);
            }
            var idValue = await ExecuteReturnBigIdentityAsync();

            Check.Exception(identityKeys.Count > 1, "ExecuteCommandIdentityIntoEntity does not support multiple identity keys");
            var    identityKey = identityKeys.First();
            object setValue    = 0;

            if (idValue > int.MaxValue)
            {
                setValue = idValue;
            }
            else
            {
                setValue = Convert.ToInt32(idValue);
            }
            this.Context.EntityMaintenance.GetProperty <T>(identityKey).SetValue(result, setValue, null);
            return(idValue > 0);
        }
Ejemplo n.º 2
0
        private string _ExecuteCommand()
        {
            if (InsertBuilder.DbColumnInfoList.HasValue())
            {
                var pks = GetPrimaryKeys();
                foreach (var item in InsertBuilder.DbColumnInfoList)
                {
                    var isPk = pks.Any(y => y.Equals(item.DbColumnName, StringComparison.CurrentCultureIgnoreCase)) || item.IsPrimarykey;
                    if (isPk && item.PropertyType == UtilConstants.GuidType && item.Value.ObjToString() == Guid.Empty.ToString())
                    {
                        item.Value = Guid.NewGuid();
                        if (InsertObjs.First().GetType().GetProperties().Any(it => it.Name == item.PropertyName))
                        {
                            InsertObjs.First().GetType().GetProperties().First(it => it.Name == item.PropertyName).SetValue(InsertObjs.First(), item.Value, null);
                        }
                    }
                }
            }
            InsertBuilder.IsReturnIdentity = false;
            PreToSql();
            AutoRemoveDataCache();
            string sql = InsertBuilder.ToSqlString();

            RestoreMapping();
            Before(sql);
            return(sql);
        }
Ejemplo n.º 3
0
        public override bool ExecuteCommandIdentityIntoEntity()
        {
            var result       = InsertObjs.First();
            var identityKeys = GetIdentityKeys();

            if (identityKeys.Count == 0)
            {
                return(this.ExecuteCommand() > 0);
            }
            var idValue = ExecuteReturnBigIdentity();

            Check.Exception(identityKeys.Count > 1, "ExecuteCommandIdentityIntoEntity does not support multiple identity keys");
            var    identityKey = identityKeys.First();
            object setValue    = 0;

            if (idValue > int.MaxValue)
            {
                setValue = idValue;
            }
            else
            {
                setValue = Convert.ToInt32(idValue);
            }
            var propertyName = this.Context.EntityMaintenance.GetPropertyName <T>(identityKey);

            typeof(T).GetProperties().First(t => t.Name.ToUpper() == propertyName.ToUpper()).SetValue(result, setValue, null);
            return(idValue > 0);
        }
Ejemplo n.º 4
0
        internal void Init()
        {
            InsertBuilder.EntityInfo = this.EntityInfo;
            Check.Exception(InsertObjs == null || InsertObjs.Count() == 0, "InsertObjs is null");
            int i = 0;

            foreach (var item in InsertObjs)
            {
                List <DbColumnInfo> insertItem = new List <DbColumnInfo>();
                foreach (var column in EntityInfo.Columns)
                {
                    var columnInfo = new DbColumnInfo()
                    {
                        Value        = column.PropertyInfo.GetValue(item, null),
                        DbColumnName = GetDbColumnName(column.PropertyName),
                        PropertyName = column.PropertyName,
                        PropertyType = UtilMethods.GetUnderType(column.PropertyInfo),
                        TableId      = i
                    };
                    if (columnInfo.PropertyType.IsEnum())
                    {
                        columnInfo.Value = Convert.ToInt64(columnInfo.Value);
                    }
                    insertItem.Add(columnInfo);
                }
                this.InsertBuilder.DbColumnInfoList.AddRange(insertItem);
                ++i;
            }
        }
Ejemplo n.º 5
0
        protected override void PreToSql()
        {
            var identities  = GetSeqNames();
            var insertCount = InsertObjs.Count();

            InsertBuilder.OracleSeqInfoList = new Dictionary <string, int>();
            if ((identities.HasValue() && insertCount > 1) || InsertBuilder.IsBlukCopy)
            {
                Check.Exception(identities.Count != identities.Distinct().Count(), "The field sequence needs to be unique");
                foreach (var seqName in identities)
                {
                    int seqBeginValue = 0;
                    seqBeginValue = this.Ado.GetInt("select  " + seqName + ".Nextval  from dual");
                    //Console.WriteLine(seqBeginValue);
                    var nextLength = insertCount - 1;
                    if (nextLength > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine(" select " + seqName + ".nextval,t.* from (");
                        for (int i = 0; i < nextLength; i++)
                        {
                            sb.AppendLine(" select 1 from dual");
                            if (i < (nextLength - 1))
                            {
                                sb.AppendLine("union all");
                            }
                        }
                        sb.AppendLine(" )t");
                        this.Ado.SqlQuery <int>(sb.ToString());
                    }
                    InsertBuilder.OracleSeqInfoList.Add(seqName, seqBeginValue);
                }
            }
            base.PreToSql();
        }
Ejemplo n.º 6
0
        public virtual int ExecuteCommand()
        {
            if (this.InsertObjs.Count() == 1 && this.InsertObjs.First() == null)
            {
                return(0);
            }
            if (InsertBuilder.DbColumnInfoList.HasValue())
            {
                var pks = GetPrimaryKeys();
                foreach (var item in InsertBuilder.DbColumnInfoList)
                {
                    var isPk = pks.Any(y => y.Equals(item.DbColumnName, StringComparison.CurrentCultureIgnoreCase)) || item.IsPrimarykey;
                    if (isPk && item.PropertyType == UtilConstants.GuidType && item.Value.ObjToString() == Guid.Empty.ToString())
                    {
                        item.Value = Guid.NewGuid();
                        if (InsertObjs.First().GetType().GetProperties().Any(it => it.Name == item.PropertyName))
                        {
                            InsertObjs.First().GetType().GetProperties().First(it => it.Name == item.PropertyName).SetValue(InsertObjs.First(), item.Value, null);
                        }
                    }
                }
            }
            InsertBuilder.IsReturnIdentity = false;
            PreToSql();
            AutoRemoveDataCache();
            string sql = InsertBuilder.ToSqlString();

            RestoreMapping();
            Before(sql);
            var result = Ado.ExecuteCommand(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());

            After(sql, null);
            return(result);
        }
Ejemplo n.º 7
0
        public virtual bool ExecuteCommandIdentityIntoEntity()
        {
            var result       = InsertObjs.First();
            var identityKeys = GetIdentityKeys();

            if (identityKeys.Count == 0)
            {
                return(this.ExecuteCommand() > 0);
            }
            var idValue = ExecuteReturnBigIdentity();

            Check.Exception(identityKeys.Count > 1, "ExecuteCommandIdentityIntoEntity does not support multiple identity keys");
            var    identityKey = identityKeys.First();
            object setValue    = 0;

            if (idValue > int.MaxValue)
            {
                setValue = idValue;
            }
            else
            {
                setValue = Convert.ToInt32(idValue);
            }
            this.Context.EntityProvider.GetProperty <T>(identityKey).SetValue(result, setValue, null);
            return(idValue > 0);
        }
Ejemplo n.º 8
0
        public T ExecuteReturnEntity()
        {
            var result       = InsertObjs.First();
            var identityKeys = GetIdentityKeys();

            if (identityKeys.Count == 0)
            {
                return(result);
            }
            var idValue = ExecuteReturnIdentity();

            Check.Exception(identityKeys.Count > 1, "ExecuteReutrnEntity does not support multiple identity keys");
            var identityKey = identityKeys.First();

            this.Context.EntityProvider.GetProperty <T>(identityKey).SetValue(result, idValue, null);
            return(result);
        }
Ejemplo n.º 9
0
        protected override void PreToSql()
        {
            var identities  = GetSeqNames();
            var insertCount = InsertObjs.Count();

            InsertBuilder.OracleSeqInfoList = new Dictionary <string, int>();
            if (identities.HasValue() && insertCount > 1)
            {
                Check.Exception(identities.Count != identities.Distinct().Count(), "The field sequence needs to be unique");
                foreach (var seqName in identities)
                {
                    int seqBeginValue = 0;
                    this.Ado.ExecuteCommand("alter sequence " + seqName + " increment by " + insertCount);
                    seqBeginValue = this.Ado.GetInt("select  " + seqName + ".Nextval  from dual") - insertCount;
                    this.Ado.ExecuteCommand("alter sequence " + seqName + " increment by " + 1);
                    InsertBuilder.OracleSeqInfoList.Add(seqName, seqBeginValue);
                }
            }
            base.PreToSql();
        }
Ejemplo n.º 10
0
        internal void Init()
        {
            InsertBuilder.EntityInfo = this.EntityInfo;
            Check.Exception(InsertObjs == null || InsertObjs.Count() == 0, "InsertObjs is null");
            int i = 0;

            foreach (var item in InsertObjs)
            {
                List <DbColumnInfo> insertItem = new List <DbColumnInfo>();
                if (item is Dictionary <string, object> )
                {
                    SetInsertItemByDic(i, item, insertItem);
                }
                else
                {
                    SetInsertItemByEntity(i, item, insertItem);
                }
                this.InsertBuilder.DbColumnInfoList.AddRange(insertItem);
                ++i;
            }
        }
Ejemplo n.º 11
0
        public async Task <T> ExecuteReturnEntityAsync()
        {
            await ExecuteCommandIdentityIntoEntityAsync();

            return(InsertObjs.First());
        }
Ejemplo n.º 12
0
 public virtual T ExecuteReturnEntity()
 {
     ExecuteCommandIdentityIntoEntity();
     return(InsertObjs.First());
 }