Пример #1
0
        public void SetOrderBy(KeyValuePair <string, bool>[] columns)
        {
            if (columns == null || columns.Length == 0)
            {
                orderBy = null;
                return;
            }

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < columns.Length; ++i)
            {
                SqlQueryUtils.AppendColumnName(sb, columns[i].Key);

                if (columns[i].Value)
                {
                    sb.Append(" DESC");
                }

                if (i < columns.Length - 1)
                {
                    sb.Append(',');
                }
            }
            orderBy = sb.ToString();
        }
Пример #2
0
        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 DbCommand CreateSelectCommand(WhereClip where, string[] columns)
        {
            Check.Require(((object)where) != null && where.From != null, "expr and expr.From could not be null!");
            Check.Require(columns != null && columns.Length > 0, "columns could not be null or empty!");

            DbCommand cmd = fac.CreateCommand();

            cmd.CommandType = CommandType.Text;

            StringBuilder sb = new StringBuilder("SELECT ");

            for (int i = 0; i < columns.Length; ++i)
            {
                SqlQueryUtils.AppendColumnName(sb, columns[i]);

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

            AddExpressionParameters(where, cmd);

            cmd.CommandText = SqlQueryUtils.ReplaceDatabaseTokens(sb.ToString(), leftToken, rightToken, paramPrefixToken, wildcharToken, wildsinglecharToken);
            PrepareCommand(cmd);
            return(cmd);
        }
        public static string ValidColumnName(string columnName)
        {
            Check.Require(columnName != null, "columnName could not be null!");

            StringBuilder sb = new StringBuilder();

            SqlQueryUtils.AppendColumnName(sb, columnName);
            return(sb.ToString());
        }
Пример #5
0
 protected void InitColumnExpression(string columnName, DbType type)
 {
     if (this.sql.Length > 0)
     {
         this.sql = new StringBuilder();
     }
     SqlQueryUtils.AppendColumnName(this.sql, columnName);
     this.DbType = type;
 }
        private static string Func(string funcName, string columnName)
        {
            Check.Require(!string.IsNullOrEmpty(funcName), "funcName could not be null or empty!");
            Check.Require(!string.IsNullOrEmpty(columnName), "columnName could not be null or empty!");

            StringBuilder sb = new StringBuilder(funcName);

            sb.Append("(");
            SqlQueryUtils.AppendColumnName(sb, columnName);
            sb.Append(')');
            return(sb.ToString());
        }
        public static string DatePart(string columnName, DatePartType partType)
        {
            Check.Require(!string.IsNullOrEmpty(columnName), "columnName could not be null or empty!");

            StringBuilder sb = new StringBuilder("DATEPART(");

            sb.Append(partType.ToString());
            sb.Append(',');
            SqlQueryUtils.AppendColumnName(sb, columnName);
            sb.Append(')');
            return(sb.ToString());
        }
        public static string Count(string columnName, bool isDistinct)
        {
            Check.Require(!string.IsNullOrEmpty(columnName), "columnName could not be null or empty!");

            StringBuilder sb = new StringBuilder();

            sb.Append("COUNT(");
            if (isDistinct)
            {
                sb.Append("DISTINCT ");
            }
            SqlQueryUtils.AppendColumnName(sb, columnName);
            sb.Append(')');
            return(sb.ToString());
        }
        //public static string SubString(string columnName, int start)
        //{
        //    Check.Require(!string.IsNullOrEmpty(columnName), "columnName could not be null or empty!");
        //    Check.Require(start >= 0, "start must >= 0!");

        //    StringBuilder sb = new StringBuilder("SUBSTRING(");
        //    SqlQueryUtils.AppendColumnName(sb, columnName);
        //    sb.Append(',');
        //    sb.Append(start + 1);
        //    sb.Append(",LEN(");
        //    SqlQueryUtils.AppendColumnName(sb, columnName);
        //    sb.Append(')');
        //    sb.Append(')');
        //    return sb.ToString();
        //}

        public static string SubString(string columnName, int start, int length)
        {
            Check.Require(!string.IsNullOrEmpty(columnName), "columnName could not be null or empty!");
            Check.Require(start >= 0, "start must >= 0!");
            Check.Require(length >= 0, "length must = 0!");

            StringBuilder sb = new StringBuilder("SUBSTRING(");

            SqlQueryUtils.AppendColumnName(sb, columnName);
            sb.Append(',');
            sb.Append(start + 1);
            sb.Append(',');
            sb.Append(length);
            sb.Append(')');
            return(sb.ToString());
        }
Пример #10
0
        public void SetGroupBy(string[] columns)
        {
            if (columns == null || columns.Length == 0)
            {
                groupBy = null;
                return;
            }

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < columns.Length; ++i)
            {
                SqlQueryUtils.AppendColumnName(sb, columns[i]);

                if (i < columns.Length - 1)
                {
                    sb.Append(',');
                }
            }
            groupBy = sb.ToString();
        }
Пример #11
0
        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);
        }