Ejemplo n.º 1
0
        public DbCommand CreateUpdateCommand(string tableName, WhereClip where, string[] columns, DbType[] types, object[] values)
        {
            Check.Require(!string.IsNullOrEmpty(tableName), "tableName could not be null or empty!");
            Check.Require(columns != null && types != null && values != null && columns.Length == types.Length && columns.Length == values.Length,
                          "length of columns, types and values must equal!");

            DbCommand cmd = fac.CreateCommand();

            cmd.CommandType = CommandType.Text;

            StringBuilder sb = new StringBuilder("UPDATE ");

            sb.Append(leftToken);
            sb.Append(tableName.TrimStart(leftToken).TrimEnd(rightToken));
            sb.Append(rightToken);
            sb.Append(' ');
            sb.Append("SET ");
            for (int i = 0; i < columns.Length; ++i)
            {
                if (columns[i].Trim()[0] == '[')
                {
                    sb.Append(columns[i].Replace("[", leftToken.ToString()).Replace("]", rightToken.ToString()));
                }
                else
                {
                    sb.Append(leftToken);
                    sb.Append(columns[i].TrimStart(leftToken).TrimEnd(rightToken));
                    sb.Append(rightToken);
                }
                sb.Append('=');
                if (values[i] != null && values[i] is ExpressionClip)
                {
                    ExpressionClip expr = (ExpressionClip)values[i];
                    sb.Append(expr.ToString());
                    AddExpressionParameters(expr, cmd);
                }
                else
                {
                    string paramName = MakeUniqueParamNameWithPrefixToken();
                    sb.Append(paramName);
                    DbParameter p = cmd.CreateParameter();
                    p.ParameterName = paramName;
                    p.DbType        = types[i];
                    p.Value         = values[i] == null ? DBNull.Value : values[i];
                    cmd.Parameters.Add(p);
                }

                if (i < columns.Length - 1)
                {
                    sb.Append(',');
                }
            }

            if ((!WhereClip.IsNullOrEmpty(where)) && where.Sql.Length > 0)
            {
                sb.Append(" WHERE ");
                sb.Append(SqlQueryUtils.RemoveTableAliasNamePrefixes(where.Sql));
                AddExpressionParameters(where, cmd);
            }

            cmd.CommandText = SqlQueryUtils.ReplaceDatabaseTokens(sb.ToString(), leftToken, rightToken, paramPrefixToken, wildcharToken, wildsinglecharToken);
            PrepareCommand(cmd);
            return(cmd);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OrderByClip"/> class.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="descend">if set to <c>true</c> [descend].</param>
        public OrderByClip(ExpressionClip item, bool descend)
        {
            Check.Require(!ExpressionClip.IsNullOrEmpty(item), "item could not be null or empty.");

            orderBys.Add(new KeyValuePair <string, bool>(item.ToString(), descend));
        }