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

Commit 93e32b2

Browse files
committed
Add support for ordering by column index
1 parent b1a0062 commit 93e32b2

File tree

6 files changed

+115
-1
lines changed

6 files changed

+115
-1
lines changed

lib/ServiceStack.Client.dll

0 Bytes
Binary file not shown.

lib/ServiceStack.Common.dll

0 Bytes
Binary file not shown.

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,11 @@ public virtual SqlExpression<T> OrderBy(string orderBy)
509509
return UnsafeOrderBy(orderBy.SqlVerifyFragment());
510510
}
511511

512+
public virtual SqlExpression<T> OrderBy(long columnIndex)
513+
{
514+
return UnsafeOrderBy(columnIndex.ToString());
515+
}
516+
512517
public virtual SqlExpression<T> UnsafeOrderBy(string orderBy)
513518
{
514519
orderByProperties.Clear();
@@ -692,9 +697,18 @@ private SqlExpression<T> OrderByDescendingInternal(Expression keySelector)
692697
}
693698

694699
public virtual SqlExpression<T> OrderByDescending(string orderBy)
700+
{
701+
return UnsafeOrderByDescending(orderBy.SqlVerifyFragment());
702+
}
703+
704+
public virtual SqlExpression<T> OrderByDescending(long columnIndex)
705+
{
706+
return UnsafeOrderByDescending(columnIndex.ToString());
707+
}
708+
709+
private SqlExpression<T> UnsafeOrderByDescending(string orderBy)
695710
{
696711
orderByProperties.Clear();
697-
orderBy.SqlVerifyFragment();
698712
orderByProperties.Add(orderBy + " DESC");
699713
BuildOrderByClauseInternal();
700714
return this;

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@
139139
<Compile Include="Legacy\ApiSqlServerLegacyTests.cs" />
140140
<Compile Include="SchemaTests.cs" />
141141
<Compile Include="SelectParamTests.cs" />
142+
<Compile Include="Support\ArtistTrackTestBase.cs" />
142143
<Compile Include="Support\JoinSqlBuilder.cs" />
143144
<Compile Include="TemporaryNamingStrategy.cs" />
145+
<Compile Include="UseCase\ArtistTrackSqlExpressions.cs" />
144146
<Compile Include="UseCase\ExpressionsAuthorTests.cs" />
145147
<Compile Include="Expression\ComplexJoinTests.cs" />
146148
<Compile Include="Expression\ExpressionUsingCustomSerializedEnumTests.cs" />
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Collections.Generic;
2+
using System.Data;
3+
using ServiceStack.DataAnnotations;
4+
5+
namespace ServiceStack.OrmLite.Tests.Support
6+
{
7+
public class Artist
8+
{
9+
public int Id { get; set; }
10+
public string Name { get; set; }
11+
12+
[Reference]
13+
public List<Track> Tracks { get; set; }
14+
public override string ToString() => Name;
15+
}
16+
17+
public class Track
18+
{
19+
[AutoIncrement]
20+
public int Id { get; set; }
21+
public string Name { get; set; }
22+
public int ArtistId { get; set; }
23+
public string Album { get; set; }
24+
public int Year { get; set; }
25+
public override string ToString() => Name;
26+
}
27+
28+
public class ArtistTrackTestBase : OrmLiteTestBase
29+
{
30+
static readonly Artist[] Artists = {
31+
new Artist {
32+
Id = 1, Name = "Faith No More",
33+
Tracks = new List<Track> {
34+
new Track { Name = "Everythings Ruined", Album = "Angel Dust", Year = 1992 },
35+
new Track { Name = "Ashes to Ashes", Album = "Album of the Year", Year = 1997 },
36+
}
37+
},
38+
new Artist {
39+
Id = 2, Name = "Live",
40+
Tracks = new List<Track> {
41+
new Track { Name = "Lightning Crashes", Album = "Throwing Copper", Year = 1994 },
42+
new Track { Name = "Lakini's Juice", Album = "Secret Samadhi", Year = 1997 },
43+
}
44+
},
45+
new Artist {
46+
Id = 3, Name = "Nirvana",
47+
Tracks = new List<Track> {
48+
new Track { Name = "Smells Like Teen Spirit", Album = "Nevermind", Year = 1991 },
49+
new Track { Name = "Heart-Shaped Box", Album = "In Utero", Year = 1993 },
50+
}
51+
},
52+
new Artist {
53+
Id = 4, Name = "Pearl Jam",
54+
Tracks = new List<Track> {
55+
new Track { Name = "Alive", Album = "Ten", Year = 1991 },
56+
new Track { Name = "Daughter", Album = "Vs", Year = 1993 },
57+
}
58+
},
59+
};
60+
61+
public IDbConnection CreateArtistAndTrackTablesWithData(IDbConnection db)
62+
{
63+
db.DropAndCreateTable<Artist>();
64+
db.DropAndCreateTable<Track>();
65+
Artists.Each(x => db.Save(x, references: true));
66+
return db;
67+
}
68+
}
69+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using NUnit.Framework;
2+
using ServiceStack.OrmLite.Tests.Support;
3+
using ServiceStack.Text;
4+
5+
namespace ServiceStack.OrmLite.Tests.UseCase
6+
{
7+
[TestFixture]
8+
public class ArtistTrackSqlExpressions : ArtistTrackTestBase
9+
{
10+
[Test]
11+
public void Can_OrderBy_Column_Index()
12+
{
13+
using (var db = CreateArtistAndTrackTablesWithData(OpenDbConnection()))
14+
{
15+
var q = db.From<Track>()
16+
.Where(x => x.Year > 1991)
17+
.And(x => x.Name.Contains("A"))
18+
.GroupBy(x => x.Year)
19+
.OrderByDescending(2)
20+
.ThenBy(x => x.Year)
21+
.Take(1)
22+
.Select(x => new { x.Year, Count = Sql.Count("*") });
23+
24+
var result = db.Dictionary<int, int>(q);
25+
Assert.That(result[1993], Is.EqualTo(2));
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)