Example #1
0
        public override DbExpression Visit(DbUpdateExpression exp)
        {
            this._sqlBuilder.Append("UPDATE ");
            this.AppendTable(exp.Table);
            this._sqlBuilder.Append(" SET ");

            bool first = true;

            foreach (var item in exp.UpdateColumns)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    this._sqlBuilder.Append(",");
                }

                this.QuoteName(item.Key.Name);
                this._sqlBuilder.Append("=");

                DbExpression valExp = DbExpressionExtension.StripInvalidConvert(item.Value);
                AmendDbInfo(item.Key, valExp);
                DbValueExpressionTransformer.Transform(valExp).Accept(this);
            }

            this.AppendOutputClause(exp.Returns);
            this.BuildWhereState(exp.Condition);

            return(exp);
        }
Example #2
0
        public override DbExpression Visit(DbInsertExpression exp)
        {
            string separator = "";

            this._sqlBuilder.Append("INSERT INTO ");
            this.AppendTable(exp.Table);
            this._sqlBuilder.Append("(");

            separator = "";
            foreach (var item in exp.InsertColumns)
            {
                this._sqlBuilder.Append(separator);
                this.QuoteName(item.Key.Name);
                separator = ",";
            }

            this._sqlBuilder.Append(")");

            this.AppendOutputClause(exp.Returns);

            this._sqlBuilder.Append(" VALUES(");
            separator = "";
            foreach (var item in exp.InsertColumns)
            {
                this._sqlBuilder.Append(separator);

                DbExpression valExp = DbExpressionExtension.StripInvalidConvert(item.Value);
                AmendDbInfo(item.Key, valExp);
                DbValueExpressionTransformer.Transform(valExp).Accept(this);
                separator = ",";
            }

            this._sqlBuilder.Append(")");

            return(exp);
        }
Example #3
0
 static DbExpression EnsureDbExpressionReturnCSharpBoolean(DbExpression exp)
 {
     return(DbValueExpressionTransformer.Transform(exp));
 }
Example #4
0
        protected virtual void BuildLimitSql(DbSqlQueryExpression exp)
        {
            bool shouldSortResults = false;

            if (exp.TakeCount != null)
            {
                shouldSortResults = true;
            }
            else if (this._sqlBuilder.Length == 0)
            {
                shouldSortResults = true;
            }

            this._sqlBuilder.Append("SELECT ");

            this.AppendDistinct(exp.IsDistinct);

            if (exp.TakeCount != null)
            {
                this._sqlBuilder.Append("TOP ", exp.TakeCount.ToString(), " ");
            }

            string tableAlias = "T";

            List <DbColumnSegment> columns = exp.ColumnSegments;

            for (int i = 0; i < columns.Count; i++)
            {
                DbColumnSegment column = columns[i];
                if (i > 0)
                {
                    this._sqlBuilder.Append(",");
                }

                this.QuoteName(tableAlias);
                this._sqlBuilder.Append(".");
                this.QuoteName(column.Alias);
                this._sqlBuilder.Append(" AS ");
                this.QuoteName(column.Alias);
            }

            this._sqlBuilder.Append(" FROM ");
            this._sqlBuilder.Append("(");

            //------------------------//
            this._sqlBuilder.Append("SELECT ");
            for (int i = 0; i < columns.Count; i++)
            {
                DbColumnSegment column = columns[i];
                if (i > 0)
                {
                    this._sqlBuilder.Append(",");
                }

                DbValueExpressionTransformer.Transform(column.Body).Accept(this);
                this._sqlBuilder.Append(" AS ");
                this.QuoteName(column.Alias);
            }

            List <DbOrdering> orderings = exp.Orderings;

            if (orderings.Count == 0)
            {
                DbOrdering ordering = new DbOrdering(UtilConstants.DbParameter_1, DbOrderType.Asc);
                orderings = new List <DbOrdering>(1);
                orderings.Add(ordering);
            }

            string row_numberName = GenRowNumberName(columns);

            this._sqlBuilder.Append(",ROW_NUMBER() OVER(ORDER BY ");
            this.ConcatOrderings(orderings);
            this._sqlBuilder.Append(") AS ");
            this.QuoteName(row_numberName);
            this._sqlBuilder.Append(" FROM ");
            exp.Table.Accept(this);
            this.BuildWhereState(exp.Condition);
            this.BuildGroupState(exp);
            //------------------------//

            this._sqlBuilder.Append(")");
            this._sqlBuilder.Append(" AS ");
            this.QuoteName(tableAlias);
            this._sqlBuilder.Append(" WHERE ");
            this.QuoteName(tableAlias);
            this._sqlBuilder.Append(".");
            this.QuoteName(row_numberName);
            this._sqlBuilder.Append(" > ");
            this._sqlBuilder.Append(exp.SkipCount.ToString());

            if (shouldSortResults)
            {
                this._sqlBuilder.Append(" ORDER BY ");
                this.QuoteName(tableAlias);
                this._sqlBuilder.Append(".");
                this.QuoteName(row_numberName);
                this._sqlBuilder.Append(" ASC");
            }
        }
Example #5
0
 internal void AppendColumnSegment(DbColumnSegment seg)
 {
     DbValueExpressionTransformer.Transform(seg.Body).Accept(this);
     this._sqlBuilder.Append(" AS ");
     this.QuoteName(seg.Alias);
 }