Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public virtual string GenerateInsert(IEntityExplain entity, ParameterCollection output)
        {
            if (entity.AnySetted() == false)
            {
                return(null);
            }

            bool hasIncrement;

            return(GenerateInsert(entity, output, false, out hasIncrement));
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public virtual string GenerateUpdate(IEntityExplain entity, ParameterCollection output)
        {
            if (entity.AnySetted() == false)
            {
                return(null);
            }

            var fields     = entity.GetSettedFields();
            var conditions = entity.GetFieldValues(entity.GetKeys());

            if (conditions == null || conditions.Any() == false)
            {
                throw new ArgumentException("实体对象缺少主键值。");
            }
            var values = entity.GetFieldValues(fields.Where(x => entity.IsKey(x) == false));

            return(_stmBuilder.Update(entity, values, conditions, output, SqlOptions.None));
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="output"></param>
        /// <param name="includeIncrement"></param>
        /// <param name="incrementFieldName"></param>
        /// <returns></returns>
        public virtual string GenerateInsertOrUpdate(IEntityExplain entity, ParameterCollection output, bool includeIncrement, out bool hasIncrement, string incrementFieldName = null)
        {
            if (entity.AnySetted() == false)
            {
                hasIncrement = false;
                return(null);
            }
            // 有增量标识的实体对象的特殊处理
            if (entity.Increment != null)
            {
                // 实体对象如果包含主键信息,则只生成update语句,否则只生成insert语句。
                var val = entity[entity.Increment.ColumnName];
                if (val != null && val != DBNull.Value && DbValueCast.Cast <int>(val) >= entity.Increment.StartVal)
                {
                    hasIncrement = false;
                    return(GenerateUpdate(entity, output));
                }
                else
                {
                    return(GenerateInsert(entity, output, includeIncrement, out hasIncrement, incrementFieldName));
                }
            }

            var keys = entity.GetKeys();

            if (keys == null || keys.Length == 0)
            {
                throw new ArgumentException("实体对象缺少主键值。");
            }
            var conditions = entity.GetFieldValues(keys);

            if (conditions == null || conditions.Any() == false)
            {
                throw new ArgumentException("实体对象缺少主键值。");
            }

            var ifConditionSql = _stmBuilder.QueryFormat(null, _stmBuilder.BuildField(keys[0]), _stmBuilder.BuildTableName(entity.TableName), _stmBuilder.Where(conditions, output, SqlOptions.None), null, null, null, SqlOptions.None);
            var ifTrueSql      = GenerateUpdate(entity, output);
            var ifFalseSql     = GenerateInsert(entity, output, includeIncrement, out hasIncrement, incrementFieldName);

            return(_stmBuilder.IfExistsFormat(ifConditionSql, ifTrueSql, ifFalseSql, SqlOptions.None));
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="output"></param>
        /// <param name="includeIncrement"></param>
        /// <param name="incrementFieldName"> </param>
        /// <returns></returns>
        public virtual string GenerateInsert(IEntityExplain entity, ParameterCollection output, bool includeIncrement, out bool hasIncrement, string incrementFieldName = null)
        {
            if (entity.AnySetted() == false)
            {
                hasIncrement = false;
                return(null);
            }

            var fields = entity.GetSettedFields();
            var values = entity.GetFieldValues(fields);

            if (includeIncrement && entity.Increment != null)
            {
                var sql = new StringBuilder();
                sql.AppendLine(_stmBuilder.Insert(entity, values, output, SqlOptions.None));
                sql.AppendLine(_stmBuilder.IncrementByQuery(entity, incrementFieldName, SqlOptions.None));
                hasIncrement = true;
                return(sql.ToString());
            }
            //
            hasIncrement = false;
            return(_stmBuilder.Insert(entity, values, output, SqlOptions.None));
        }