1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using Ant.DbExpressions;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Ant.SqlServer
- {
- class SqlGenerator_OffsetFetch : SqlGenerator
- {
- /// <summary>
- /// SQL2012实现分页
- /// </summary>
- /// <param name="exp"></param>
- protected override void BuildLimitSql(DbExpressions.DbSqlQueryExpression exp)
- {
- //order by number offset 10 rows fetch next 20 rows only;
- this._sqlBuilder.Append("SELECT ");
- List<DbColumnSegment> columns = exp.ColumnSegments;
- for (int i = 0; i < columns.Count; i++)
- {
- DbColumnSegment column = columns[i];
- if (i > 0)
- this._sqlBuilder.Append(",");
- this.AppendColumnSegment(column);
- }
- this._sqlBuilder.Append(" FROM ");
- exp.Table.Accept(this);
- this.BuildWhereState(exp.Condition);
- this.BuildGroupState(exp);
- List<DbOrdering> orderings = exp.Orderings;
- if (orderings.Count == 0)
- {
- DbExpression orderingExp = DbExpression.Add(UtilConstants.DbParameter_1, DbConstantExpression.Zero, DbConstantExpression.Zero.Type, null);
- DbOrdering ordering = new DbOrdering(orderingExp, OrderType.Asc);
- orderings = new List<DbOrdering>(1);
- orderings.Add(ordering);
- }
- this.BuildOrderState(orderings);
- this._sqlBuilder.Append(" OFFSET ", exp.SkipCount.Value.ToString(), " ROWS");
- if (exp.TakeCount != null)
- {
- this._sqlBuilder.Append(" FETCH NEXT ", exp.TakeCount.Value.ToString(), " ROWS ONLY");
- }
- }
- }
- }
|