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

Commit e6976b7

Browse files
committed
Fix SQL Server tests
1 parent 951f1a2 commit e6976b7

File tree

3 files changed

+361
-343
lines changed

3 files changed

+361
-343
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServer2012OrmLiteDialectProvider.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,33 @@ public override string ToSelectStatement(QueryType queryType, ModelDefinition mo
8686
.Append(selectExpression)
8787
.Append(bodyExpression);
8888

89-
if (orderByExpression != null)
89+
if (!string.IsNullOrEmpty(orderByExpression))
9090
sb.Append(orderByExpression);
9191

92-
if (queryType == QueryType.Select || (rows == 1 && offset is null or 0))
92+
var skip = offset ?? 0;
93+
if (skip > 0 || rows is > 0)
9394
{
94-
if (queryType == QueryType.Select && orderByExpression.IsEmpty())
95+
if (queryType == QueryType.Select || (rows == 1 && offset is null or 0))
9596
{
96-
var orderBy = (offset is null or 0) && rows == 1 //Avoid for Single requests
97-
? "1"
98-
: this.GetQuotedColumnName(modelDef, modelDef.PrimaryKey);
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+
}
99104

100-
sb.Append(" ORDER BY " + orderBy);
101-
}
105+
// ORDER BY mandatory when using OFFSET/FETCH NEXT
106+
if (orderByExpression.IsEmpty())
107+
{
108+
var orderBy = rows == 1 //Avoid for Single requests
109+
? "1"
110+
: this.GetQuotedColumnName(modelDef, modelDef.PrimaryKey);
102111

103-
sb.Append(" ").Append(SqlLimit(offset, rows));
112+
sb.Append(" ORDER BY " + orderBy);
113+
}
114+
sb.Append(" ").Append(SqlLimit(offset, rows));
115+
}
104116
}
105117

106118
return StringBuilderCache.ReturnAndFree(sb);

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -588,13 +588,7 @@ public override string ToSelectStatement(QueryType queryType, ModelDefinition mo
588588
{
589589
var sql = StringBuilderCache.ReturnAndFree(sb) + orderByExpression;
590590

591-
if (take == int.MaxValue)
592-
return sql;
593-
594-
if (sql.Length < "SELECT".Length)
595-
return sql;
596-
597-
return $"{selectType} TOP {take + sql.Substring(selectType.Length)}";
591+
return AddSqlServerTop(take, sql, selectType);
598592
}
599593

600594
// Required because ordering is done by Windowing function
@@ -613,6 +607,17 @@ public override string ToSelectStatement(QueryType queryType, ModelDefinition mo
613607
return ret;
614608
}
615609

610+
protected static string AddSqlServerTop(int take, string sql, string selectType)
611+
{
612+
if (take == int.MaxValue)
613+
return sql;
614+
615+
if (sql.Length < "SELECT".Length)
616+
return sql;
617+
618+
return $"{selectType} TOP {take + sql.Substring(selectType.Length)}";
619+
}
620+
616621
//SELECT without RowNum and prefer aliases to be able to use in SELECT IN () Reference Queries
617622
public static string UseAliasesOrStripTablePrefixes(string selectExpression)
618623
{

0 commit comments

Comments
 (0)