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

Commit b11000e

Browse files
committed
Fix invalid order by clause in select statement for Limit expression where PK column name has a space in it
Generation of order by expression was not using GetQuotedColumnName function
1 parent 13a1401 commit b11000e

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServerExpressionVisitor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public override string ToSelectStatement()
5656

5757
public override string ToUpdateStatement(T item, bool excludeDefaults = false)
5858
{
59-
6059
var setFields = new StringBuilder();
6160
var dialectProvider = OrmLiteConfig.DialectProvider;
6261

@@ -92,7 +91,7 @@ protected virtual string BuildOrderByIdExpression()
9291
if (ModelDef.PrimaryKey == null)
9392
throw new ApplicationException("Malformed model, no PrimaryKey defined");
9493

95-
return String.Format("ORDER BY {0}", ModelDef.PrimaryKey.FieldName);
94+
return String.Format("ORDER BY {0}", OrmLiteConfig.DialectProvider.GetQuotedColumnName(ModelDef.PrimaryKey.FieldName));
9695
}
9796

9897
public override string LimitExpression

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="TypeWithByteArrayFieldTests.cs" />
8383
<Compile Include="UnicodeTests.cs" />
8484
<Compile Include="UpdateTests.cs" />
85+
<Compile Include="UseCase\TestEntityWithAliases.cs" />
8586
<Compile Include="UseCase\SimpleUseCase.cs" />
8687
<Compile Include="UseCase\TestEntity.cs" />
8788
</ItemGroup>

src/ServiceStack.OrmLite.SqlServerTests/SqlServerExpressionVisitorQueryTest.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,40 @@ public void test_if_and_works_with_nullable_parameter()
124124
Assert.IsTrue(result.Count > 0);
125125
}
126126
}
127+
128+
[Test]
129+
public void test_if_limit_works_with_rows_and_skip_if_pk_columnname_has_space()
130+
{
131+
using (var db = ConnectionString.OpenDbConnection())
132+
{
133+
FillAliasedTestEntityTableWithTestData(db);
134+
135+
var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<TestEntityWithAliases>();
136+
ev.Limit(10, 100);
137+
138+
var result = db.Select(ev);
139+
Assert.NotNull(result);
140+
Assert.AreEqual(100, result.Count);
141+
}
142+
}
143+
144+
[Test]
145+
public void test_if_limit_works_with_rows_and_skip_and_orderby_if_pk_columnname_has_space()
146+
{
147+
using (var db = ConnectionString.OpenDbConnection())
148+
{
149+
FillAliasedTestEntityTableWithTestData(db);
150+
151+
var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<TestEntityWithAliases>();
152+
ev.Limit(10, 100);
153+
ev.OrderBy(e => e.Baz);
154+
155+
var result = db.Select(ev);
156+
Assert.NotNull(result);
157+
Assert.AreEqual(100, result.Count);
158+
Assert.LessOrEqual(result[10].Baz, result[11].Baz);
159+
}
160+
}
127161

128162
protected void FillTestEntityTableWithTestData(IDbConnection db)
129163
{
@@ -140,6 +174,21 @@ protected void FillTestEntityTableWithTestData(IDbConnection db)
140174
}
141175
}
142176

177+
protected void FillAliasedTestEntityTableWithTestData(IDbConnection db)
178+
{
179+
db.CreateTable<TestEntityWithAliases>(true);
180+
181+
for (int i = 1; i < 1000; i++)
182+
{
183+
db.Insert(new TestEntityWithAliases()
184+
{
185+
Foo = RandomString(16),
186+
Bar = RandomString(16),
187+
Baz = RandomDecimal(i)
188+
});
189+
}
190+
}
191+
143192
protected string RandomString(int length)
144193
{
145194
var rnd = new System.Random();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using ServiceStack.DataAnnotations;
6+
7+
namespace ServiceStack.OrmLite.SqlServerTests.UseCase
8+
{
9+
public class TestEntityWithAliases
10+
{
11+
#region Properties
12+
13+
[AutoIncrement]
14+
[Alias("Id Column")]
15+
public int Id { get; set; }
16+
17+
[Alias("Foo Column")]
18+
public String Foo { get; set; }
19+
20+
[Alias("Bar Column")]
21+
public String Bar { get; set; }
22+
23+
//[Index]
24+
[Alias("Baz Column")]
25+
public Decimal Baz { get; set; }
26+
27+
#endregion
28+
}
29+
}

0 commit comments

Comments
 (0)