Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OFFSET-FETCH clause problem for MS SQL Server prior to 2012 #110

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dapper.FastCrud/Configuration/OrmConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public virtual SqlDatabaseOptions GetDatabaseOptions(SqlDialect dialect)
switch (dialect)
{
case SqlDialect.MsSql:
case SqlDialect.MsSql2008:
return _defaultMsSqlDatabaseOptions;
case SqlDialect.PostgreSql:
return _defaultPostgreSqlDatabaseOptions;
Expand Down
7 changes: 6 additions & 1 deletion Dapper.FastCrud/Configuration/SqlDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ namespace Dapper.FastCrud
public enum SqlDialect
{
/// <summary>
/// MS SQL Server
/// MS SQL Server 2012 and later
/// </summary>
MsSql,

/// <summary>
/// MS SQL Server 2008
/// </summary>
MsSql2008,

/// <summary>
/// MySql
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions Dapper.FastCrud/EntityDescriptors/TypedEntityDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ private ISqlStatements ConstructSqlStatements(EntityMapping entityMapping)
case SqlDialect.MsSql:
statementSqlBuilder = new MsSqlBuilder(this, entityMapping);
break;
case SqlDialect.MsSql2008:
statementSqlBuilder = new MsSql2008Builder(this, entityMapping);
break;
case SqlDialect.MySql:
statementSqlBuilder = new MySqlBuilder(this, entityMapping);
break;
Expand Down
41 changes: 41 additions & 0 deletions Dapper.FastCrud/SqlBuilders/MsSql2008Builder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Dapper.FastCrud.SqlBuilders
{
using System;
using System.Linq;
using Dapper.FastCrud.EntityDescriptors;
using Dapper.FastCrud.Mappings;

internal class MsSql2008Builder : MsSqlBuilder
{
public MsSql2008Builder(EntityDescriptor entityDescriptor, EntityMapping entityMapping)
: base(entityDescriptor, entityMapping, SqlDialect.MsSql2008)
{
}

protected override string ConstructFullSelectStatementInternal(
string selectClause,
string fromClause,
FormattableString whereClause = null,
FormattableString orderClause = null,
long? skipRowsCount = null,
long? limitRowsCount = null,
bool forceTableColumnResolution = false)
{
string orderBySql = (orderClause == null) ? string.Empty : " ORDER BY " + this.ResolveWithSqlFormatter(orderClause, forceTableColumnResolution);
string whereSql = (whereClause == null) ? string.Empty : " WHERE " + this.ResolveWithSqlFormatter(whereClause, forceTableColumnResolution);

if (String.IsNullOrEmpty(orderBySql) || !(skipRowsCount.HasValue || limitRowsCount.HasValue))
{
return ResolveWithCultureInvariantFormatter($"SELECT {selectClause} FROM {fromClause} {whereSql} {orderBySql}");
}

string sql = "SELECT * FROM (";
sql += this.ResolveWithCultureInvariantFormatter($"SELECT {selectClause}, ROW_NUMBER() OVER({orderBySql}) AS [Tu3gD4i0_INDEX] FROM {fromClause} {whereSql}");
sql += ") AS Tu3gD4i0";
sql += " WHERE " + this.ResolveWithCultureInvariantFormatter($"[Tu3gD4i0_INDEX] BETWEEN {(skipRowsCount ?? 0) + 1} AND {(skipRowsCount ?? 0) + limitRowsCount}");
sql += " ORDER BY Tu3gD4i0_INDEX ASC"

return sql;
}
}
}