Ejemplo n.º 1
0
        public static IDbCommand ToCmd(this string sql, DbType type, object value, ParameterDirection direction = ParameterDirection.Input)
        {
            IDbCommand cmd  = ToCmd(sql);
            var        name = sql.Split(' ').FirstOrDefault(param => param.StartsWith("@"));

            cmd.AddParams(name.Trim(), type, value, direction);
            return(cmd);
        }
Ejemplo n.º 2
0
 public static IDbCommand AddParams(this IDbCommand cmd, string name, DbType type, object value, ParameterDirection direction = ParameterDirection.Input)
 {
     cmd.AddParams(name.ToParam(type, value, direction));
     return(cmd);
 }
Ejemplo n.º 3
0
        public void BuildUpdate(IDbCommand command,
                                object data,
                                string tableName,
                                IFieldInfoCollection fields,
                                string whereExpression  = null,
                                OnConflictOption option = OnConflictOption.Default,
                                object keyValue         = null,
                                object extraPrams       = null)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (fields is null)
            {
                throw new ArgumentNullException(nameof(fields));
            }
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("message", nameof(tableName));
            }

            var pkField = fields.PrimaryKeyField ?? throw new InvalidOperationException($"Primary Key field required for Update command");

            if (keyValue is null)
            {
                keyValue = pkField.GetFieldValue(data);
            }
            if (keyValue is null)
            {
                throw new InvalidOperationException($"{pkField.Property.Name} should not be null");
            }



            var columnNames       = new List <string>();
            var columnExpressions = new List <string>();

            foreach (var field in fields)
            {
                object value = field.GetFieldValueOrDefault(data) ?? DBNull.Value;
                var    param = MakeParameter(command, field, value);
                command.Parameters.Add(param);

                if ((field.PersistanceFlags & PersistanceFlags.OnUpdate) == PersistanceFlags.OnUpdate)
                {
                    columnNames.Add(field.Name);
                    columnExpressions.Add(param.ParameterName);
                }
            }

            if (extraPrams != null)
            {
                command.AddParams(extraPrams);
            }

            whereExpression = whereExpression ?? pkField.Name + " = " + GetParameterName(pkField);
            var where       = new WhereClause(whereExpression);
            var expression = new SqlUpdateCommand()
            {
                TableName        = tableName,
                ColumnNames      = columnNames,
                ValueExpressions = columnExpressions,
                ConflictOption   = option,
                Where            = where
            };

            command.CommandText = expression.ToString();
        }