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

Commit 299023b

Browse files
committed
Order By PK in SQL Server 2012 for limit requests without explicit OrderBy
1 parent 1baf212 commit 299023b

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServer2012OrmLiteDialectProvider.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ public override string ToSelectStatement(ModelDefinition modelDef,
2222
if (offset != null || rows != null)
2323
{
2424
if (orderByExpression.IsEmpty())
25-
sb.Append(" ORDER BY 1");
25+
{
26+
var orderBy = offset == null && rows == 1 //Avoid for Single requests
27+
? "1"
28+
: this.GetQuotedColumnName(modelDef, modelDef.PrimaryKey);
29+
30+
sb.Append(" ORDER BY " + orderBy);
31+
}
2632

2733
sb.Append(" OFFSET ").Append(offset.GetValueOrDefault()).Append(" ROWS");
2834

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.SqlServer.Types;
4+
using NUnit.Framework;
5+
6+
namespace ServiceStack.OrmLite.SqlServerTests.Converters
7+
{
8+
public class InheritanceTest : SqlServerConvertersOrmLiteTestBase
9+
{
10+
public class GeoSuper
11+
{
12+
public long Id { get; set; }
13+
public string Other { get; set; }
14+
}
15+
16+
public class GeoTest : GeoSuper
17+
{
18+
public SqlGeography Location { get; set; }
19+
public SqlGeography NullLocation { get; set; }
20+
public SqlGeometry Shape { get; set; }
21+
}
22+
23+
[Test]
24+
public void Can_limit_on_inherited_Type()
25+
{
26+
InsertData(100);
27+
List<GeoTest> data = null;
28+
using (var db = OpenDbConnection())
29+
{
30+
data = db.Select(db.From<GeoTest>().Limit(0, int.MaxValue));
31+
}
32+
33+
Assert.IsNotNull(data);
34+
Assert.IsTrue(data.Count == 100);
35+
}
36+
37+
private void InsertData(int count)
38+
{
39+
using (var db = OpenDbConnection())
40+
{
41+
db.DropAndCreateTable<GeoTest>();
42+
43+
for (var i = 0; i < count; i++)
44+
{
45+
db.Insert(new GeoTest { Id = i, Location = RandomPosition() });
46+
}
47+
}
48+
}
49+
50+
private SqlGeography RandomPosition()
51+
{
52+
var rand = new Random(DateTime.Now.Millisecond * (int)DateTime.Now.Ticks);
53+
double lat = Math.Round(rand.NextDouble() * 160 - 80, 6);
54+
double lon = Math.Round(rand.NextDouble() * 360 - 180, 6);
55+
var result = SqlGeography.Point(lat, lon, 4326).MakeValid();
56+
return result;
57+
}
58+
}
59+
}

src/ServiceStack.OrmLite.SqlServerTests/ServiceStack.OrmLite.SqlServerTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<Compile Include="DateTimeOffsetTests.cs" />
8686
<Compile Include="InsertParam_GetLastInsertId.cs" />
8787
<Compile Include="Issues\CustomSelectWithPagingIssue.cs" />
88+
<Compile Include="Converters\InheritanceTest.cs" />
8889
<Compile Include="Issues\JamesGeoIssue.cs" />
8990
<Compile Include="Issues\SerializationTests.cs" />
9091
<Compile Include="NestedTransactions.cs" />

tests/ServiceStack.OrmLite.Tests/CaptureSqlCommandFilterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void Can_capture_command_all_Single_Apis()
126126
Assert.That(captured.SqlCommandHistory.Last().Sql.NormalizeSql(),
127127
Is.EqualTo("select id, firstname, lastname, age from person where (age = {0}) limit 1".Fmt(p)). //Sqlite
128128
Or.EqualTo("select top 1 id, firstname, lastname, age from person where (age = {0})".Fmt(p)). //SQLServer < 2012
129-
Or.EqualTo("select id, firstname, lastname, age from person where (age = {0}) order by 1 offset 0 rows fetch next 1 rows only".Fmt(p)). //SQLServer >= 2012
129+
Or.EqualTo("select id, firstname, lastname, age from person where (age = {0}) order by person.id offset 0 rows fetch next 1 rows only".Fmt(p)). //SQLServer >= 2012
130130
Or.EqualTo("select first 1 id, firstname, lastname, age from person where (age = {0})".Fmt(p))); //Firebird
131131

132132
i++; db.ExistsFmt<Person>("Age = {0}", 42);

tests/ServiceStack.OrmLite.Tests/CaptureSqlFilterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void Can_capture_all_Single_Apis()
125125
Is.EqualTo("select id, firstname, lastname, age from person where (age = {0}) limit 1".Fmt(p)). //sqlite
126126
Or.EqualTo("select top 1 id, firstname, lastname, age from person where (age = {0})".Fmt(p)). //SqlServer
127127
Or.EqualTo("select first 1 id, firstname, lastname, age from person where (age = {0})".Fmt(p)). //Firebird
128-
Or.EqualTo("select id, firstname, lastname, age from person where (age = {0}) order by 1 offset 0 rows fetch next 1 rows only".Fmt(p)). //VistaDB
128+
Or.EqualTo("select id, firstname, lastname, age from person where (age = {0}) order by person.id offset 0 rows fetch next 1 rows only".Fmt(p)). //VistaDB
129129
Or.EqualTo("select * from (\r select ssormlite1.*, rownum rnum from (\r select id, firstname, lastname, age from person where (age = {0}) order by person.id) ssormlite1\r where rownum <= 0 + 1) ssormlite2 where ssormlite2.rnum > 0".Fmt(p)) //Oracle
130130
);
131131

0 commit comments

Comments
 (0)