public static ExpressionClip operator /(object left, ExpressionClip right)
        {
            ExpressionClip expr = ExpressionFactory.CreateParameterExpression(right.dbType, left);

            ExpressionFactory.AppendExpression(expr, QueryOperator.Divide, right);
            return(expr);
        }
        public ExpressionClip SubString(int start, int length)
        {
            ExpressionClip expr = ExpressionFactory.CreateCloneExpression(this);

            expr.Sql = ColumnFormatter.SubString(this.Sql, start, length);
            return(expr);
        }
 internal protected ExpressionClip Append(QueryOperator op, ExpressionClip right)
 {
     this.sql.Append(SqlQueryUtils.ToString(op));
     this.sql.Append(SqlQueryUtils.ToString(right));
     SqlQueryUtils.AddParameters(this.parameters, right);
     return(this);
 }
        public ExpressionClip Replace(string subString, string replaceString)
        {
            Check.Require(!string.IsNullOrEmpty(subString), "subString could not be null or empty!");
            Check.Require(!string.IsNullOrEmpty(replaceString), "replaceString could not be null or empty!");

            ExpressionClip expr = ExpressionFactory.CreateCloneExpression(this);
            StringBuilder  sb   = new StringBuilder();

            sb.Append("REPLACE(");
            SqlQueryUtils.AppendColumnName(sb, this.Sql);
            sb.Append(',');
            string paramName = MakeUniqueParamNameWithoutPrefixToken();

            sb.Append(paramName);
            sb.Append(',');
            string paramName2 = MakeUniqueParamNameWithoutPrefixToken();

            sb.Append(paramName2);
            sb.Append(')');

            expr.sql    = sb;
            expr.dbType = DbType.Int32;
            expr.parameters.Add(paramName, new KeyValuePair <DbType, object>(this.dbType, subString));
            expr.Parameters.Add(paramName2, new KeyValuePair <DbType, object>(this.dbType, replaceString));

            return(expr);
        }
        public static ExpressionClip operator!(ExpressionClip left)
        {
            ExpressionClip expr = ExpressionFactory.AppendExpression(new ExpressionClip(), QueryOperator.BitwiseNOT,
                                                                     left);

            return(expr);
        }
        public static ExpressionClip operator /(ExpressionClip left, ExpressionClip right)
        {
            ExpressionClip expr = ExpressionFactory.CreateCloneExpression(left);

            ExpressionFactory.AppendExpression(expr, QueryOperator.Divide, right);
            return(expr);
        }
        public static ExpressionClip operator /(ExpressionClip left, object right)
        {
            ExpressionClip expr = ExpressionFactory.CreateCloneExpression(left);

            ExpressionFactory.AppendExpression(expr, QueryOperator.Divide,
                                               ExpressionFactory.CreateParameterExpression(left.dbType, right));
            return(expr);
        }
        public static ExpressionClip operator %(object left, ExpressionClip right)
        {
            ExpressionClip expr = ExpressionFactory.CreateParameterExpression(right.dbType, left);

            ExpressionFactory.AppendExpression(expr, QueryOperator.Modulo, right);
            expr.dbType = DbType.Int32;
            return(expr);
        }
        public ExpressionClip Avg()
        {
            ExpressionClip expr = ExpressionFactory.CreateCloneExpression(this);

            expr.Sql = ColumnFormatter.Avg(this.Sql);

            return(expr);
        }
        public static ExpressionClip GetCurrentDate()
        {
            ExpressionClip expr = new ExpressionClip();

            expr.Sql    = ColumnFormatter.GetCurrentDate();
            expr.dbType = DbType.DateTime;
            return(expr);
        }
        public static ExpressionClip operator %(ExpressionClip left, ExpressionClip right)
        {
            ExpressionClip expr = ExpressionFactory.CreateCloneExpression(left);

            ExpressionFactory.AppendExpression(expr, QueryOperator.Modulo, right);
            expr.dbType = DbType.Int32;
            return(expr);
        }
        public ExpressionClip Count(bool isDistinct)
        {
            ExpressionClip expr = ExpressionFactory.CreateCloneExpression(this);

            expr.Sql    = ColumnFormatter.Count(this.Sql, isDistinct);
            expr.dbType = DbType.Int32;

            return(expr);
        }
        public ExpressionClip GetDay()
        {
            ExpressionClip expr = ExpressionFactory.CreateCloneExpression(this);

            expr.Sql    = ColumnFormatter.DatePart(this.Sql, ColumnFormatter.DatePartType.Day);
            expr.dbType = DbType.Int32;

            return(expr);
        }
        public ExpressionClip BitwiseAnd(ExpressionClip right)
        {
            ExpressionClip expr = ExpressionFactory.CreateCloneExpression(this);

            ExpressionFactory.AppendExpression(expr, QueryOperator.BitwiseAND,
                                               right);

            return(expr);
        }
        public ExpressionClip BitwiseAnd(object right)
        {
            Check.Require(right != null, "right could not be null!");

            ExpressionClip expr = ExpressionFactory.CreateCloneExpression(this);

            ExpressionFactory.AppendExpression(expr, QueryOperator.BitwiseAND,
                                               ExpressionFactory.CreateParameterExpression(this.dbType, right));

            return(expr);
        }
Example #16
0
        internal protected WhereClip Or(ExpressionClip left, QueryOperator op, ExpressionClip right)
        {
            if (this.sql.Length > 0)
            {
                this.sql.Append(" OR ");
            }
            this.sql.Append(SqlQueryUtils.ToString(left));
            this.sql.Append(SqlQueryUtils.ToString(op));
            this.sql.Append(SqlQueryUtils.ToString(right));

            SqlQueryUtils.AddParameters(this.parameters, left);
            SqlQueryUtils.AddParameters(this.parameters, right);

            return(this);
        }
        public static WhereClip operator <=(ExpressionClip left, ExpressionClip right)
        {
            WhereClip where = new WhereClip();
            if (ExpressionClip.IsNullOrEmpty(right))
            {
                where = ExpressionFactory.AppendWhereAnd(where, left, QueryOperator.LessOrEqual, null);
            }
            else if (ExpressionClip.IsNullOrEmpty(left))
            {
                where = ExpressionFactory.AppendWhereAnd(where, right, QueryOperator.GreaterOrEqual, null);
            }
            else
            {
                ExpressionFactory.AppendWhereAnd(where, left, QueryOperator.LessOrEqual, right);
            }

            return(where);
        }
        public ExpressionClip SubString(int start)
        {
            Check.Require(start >= 0, "start must >= 0!");

            ExpressionClip expr      = ExpressionFactory.CreateCloneExpression(this);
            ExpressionClip cloneExpr = ExpressionFactory.CreateCloneExpression(this);
            StringBuilder  sb        = new StringBuilder("SUBSTRING(");

            SqlQueryUtils.AppendColumnName(sb, this.Sql);
            sb.Append(',');
            sb.Append(start + 1);
            sb.Append(",LEN(");
            SqlQueryUtils.AppendColumnName(sb, cloneExpr.Sql);
            sb.Append(')');
            sb.Append(')');

            expr.sql = sb;
            SqlQueryUtils.AddParameters(expr.parameters, cloneExpr);

            return(expr);
        }
        public virtual object Clone()
        {
            ExpressionClip newExpr = new ExpressionClip();

            newExpr.dbType = this.dbType;
            string tempSql = this.sql.ToString();

            Dictionary <string, KeyValuePair <DbType, object> > .Enumerator en = this.parameters.GetEnumerator();
            while (en.MoveNext())
            {
                object value = en.Current.Value.Value;
                if (value != null && value != DBNull.Value && value is ICloneable)
                {
                    value = ((ICloneable)value).Clone();
                }

                string newParamName = MakeUniqueParamNameWithoutPrefixToken();
                tempSql = tempSql.Replace('@' + en.Current.Key, '@' + newParamName);
                newExpr.Parameters.Add(newParamName, new KeyValuePair <DbType, object>(en.Current.Value.Key, value));
            }
            newExpr.sql.Append(tempSql);
            return(newExpr);
        }
        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!");
            //if (where != null)
            //{
            //    Check.Require(where.From == null, "expr.From must be null in Update");
            //}
            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)
            {
                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);
        }
 public static WhereClip AppendWhereOr(WhereClip where, ExpressionClip left, QueryOperator op, ExpressionClip right)
 {
     return(where.Or(left, op, right));
 }
 public static ExpressionClip AppendExpression(ExpressionClip left, QueryOperator op, ExpressionClip right)
 {
     return(left.Append(op, right));
 }
        public static ExpressionClip CreateCloneExpression(ExpressionClip expr)
        {
            Check.Require(!ExpressionClip.IsNullOrEmpty(expr));

            return((ExpressionClip)expr.Clone());
        }
 public static bool IsNullOrEmpty(ExpressionClip expr)
 {
     return(((object)expr) == null || expr.sql.Length == 0);
 }