Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 7b675d0

Browse files
committed
clean up SqlTop impl
1 parent e6976b7 commit 7b675d0

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServer2012OrmLiteDialectProvider.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,15 @@ public override string ToSelectStatement(QueryType queryType, ModelDefinition mo
9292
var skip = offset ?? 0;
9393
if (skip > 0 || rows is > 0)
9494
{
95-
if (queryType == QueryType.Select || (rows == 1 && offset is null or 0))
95+
// Use TOP if offset is unspecified
96+
if (skip == 0)
9697
{
97-
// Use TOP if offset is unspecified
98-
if (offset is null or 0)
99-
{
100-
var selectType = selectExpression.StartsWithIgnoreCase("SELECT DISTINCT") ? "SELECT DISTINCT" : "SELECT";
101-
var sql = StringBuilderCache.ReturnAndFree(sb);
102-
return AddSqlServerTop(rows.GetValueOrDefault(), sql, selectType);
103-
}
98+
var sql = StringBuilderCache.ReturnAndFree(sb);
99+
return SqlTop(sql, rows.GetValueOrDefault());
100+
}
104101

102+
if (queryType == QueryType.Select || rows == 1)
103+
{
105104
// ORDER BY mandatory when using OFFSET/FETCH NEXT
106105
if (orderByExpression.IsEmpty())
107106
{

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,11 @@ public override string ToSelectStatement(QueryType queryType, ModelDefinition mo
583583

584584
var selectType = selectExpression.StartsWithIgnoreCase("SELECT DISTINCT") ? "SELECT DISTINCT" : "SELECT";
585585

586-
//Temp hack for SqlServer <= 2008
586+
//avoid Windowing function if unnecessary
587587
if (skip == 0)
588588
{
589589
var sql = StringBuilderCache.ReturnAndFree(sb) + orderByExpression;
590-
591-
return AddSqlServerTop(take, sql, selectType);
590+
return SqlTop(sql, take, selectType);
592591
}
593592

594593
// Required because ordering is done by Windowing function
@@ -607,15 +606,17 @@ public override string ToSelectStatement(QueryType queryType, ModelDefinition mo
607606
return ret;
608607
}
609608

610-
protected static string AddSqlServerTop(int take, string sql, string selectType)
609+
protected static string SqlTop(string sql, int take, string selectType = null)
611610
{
611+
selectType ??= sql.StartsWithIgnoreCase("SELECT DISTINCT") ? "SELECT DISTINCT" : "SELECT";
612+
612613
if (take == int.MaxValue)
613614
return sql;
614615

615616
if (sql.Length < "SELECT".Length)
616617
return sql;
617618

618-
return $"{selectType} TOP {take + sql.Substring(selectType.Length)}";
619+
return selectType + " TOP " + take + sql.Substring(selectType.Length);
619620
}
620621

621622
//SELECT without RowNum and prefer aliases to be able to use in SELECT IN () Reference Queries

0 commit comments

Comments
 (0)