Skip to content

Commit 9d2cea0

Browse files
authored
OrderByKey and OrderByKeyDescending TableQuery APIs (#17)
1 parent 5c80b9b commit 9d2cea0

File tree

3 files changed

+122
-8
lines changed

3 files changed

+122
-8
lines changed

SQLite.Net.Tests/LinqTest.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ private TestDb CreateDb()
1313
{
1414
var db = new TestDb();
1515
db.CreateTable<Product>();
16+
db.CreateTable<KeyedProduct>();
1617
db.CreateTable<Order>();
1718
db.CreateTable<OrderLine>();
1819
db.CreateTable<OrderHistory>();
@@ -143,6 +144,52 @@ public void Issue96_NullabelIntsInQueries()
143144
Assert.AreEqual(3, db.Table<Issue96_A>().Where(p => p.ClassB == null).Count());
144145
}
145146

147+
[Test]
148+
public void OrderByKey()
149+
{
150+
TestDb db = CreateDb();
151+
152+
var products = new List<KeyedProduct>()
153+
{
154+
new ()
155+
{
156+
Id = ("FOO", 1),
157+
Name = "A",
158+
TotalSales = 1,
159+
},
160+
new()
161+
{
162+
Id = ("FOO", 2),
163+
Name = "C",
164+
TotalSales = 5,
165+
},
166+
new()
167+
{
168+
Id = ("FOO",3),
169+
Name = "D",
170+
TotalSales = 400,
171+
},
172+
new()
173+
{
174+
Id = ("FOO", 10),
175+
Name = "B",
176+
TotalSales = 100,
177+
},
178+
179+
};
180+
foreach (var product in products)
181+
{
182+
db.Insert(product);
183+
}
184+
185+
List<KeyedProduct> asc = (from p in db.Table<KeyedProduct>().OrderByKey() select p).ToList();
186+
Assert.That(products, Is.EquivalentTo(asc));
187+
188+
List<KeyedProduct> desc = (from p in db.Table<KeyedProduct>().OrderByKeyDescending() select p).ToList();
189+
products.Reverse();
190+
Assert.That(products, Is.EquivalentTo(desc));
191+
}
192+
146193
[Test]
147194
public void OrderByCast()
148195
{

SQLite.Net.Tests/TestDb.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,47 @@ public interface IProduct
1616
uint TotalSales { get; set; }
1717
}
1818

19-
public class Product : IProduct
19+
public class Product : BaseProduct, IProduct
2020
{
2121
[AutoIncrement, PrimaryKey]
2222
public int Id { get; set; }
2323

24+
}
25+
26+
public class KeyedProduct : BaseProduct
27+
{
28+
[PrimaryKey]
29+
public (string, int) Id { get; set; }
30+
31+
public override bool Equals(object? obj)
32+
{
33+
var o = obj as KeyedProduct;
34+
if (o == null)
35+
{
36+
return false;
37+
}
38+
return base.Equals(obj) && Id == o.Id;
39+
}
40+
41+
}
42+
43+
44+
public class BaseProduct
45+
{
2446
public string Name { get; set; }
2547
public decimal Price { get; set; }
2648
public uint TotalSales { get; set; }
49+
50+
public override bool Equals(object? obj)
51+
{
52+
var o = obj as BaseProduct;
53+
if (o == null)
54+
{
55+
return false;
56+
}
57+
58+
return o.Name == Name && o.Price == Price && o.TotalSales == TotalSales;
59+
}
2760
}
2861

2962
[Table("Order")]

src/SQLite.Net/TableQuery.cs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,31 @@ public TableQuery<T> OrderByDescending<TValue>(Expression<Func<T, TValue>> order
200200
return AddOrderBy(orderExpr, false);
201201
}
202202

203+
/// <summary>
204+
/// Order the Query based on the Primary Key Column(s)
205+
/// </summary>
206+
public TableQuery<T> OrderByKey()
207+
{
208+
var pks = Table.PKs;
209+
return AddOrdering(pks.Select(pk => new Ordering()
210+
{
211+
ColumnName = pk.Name,
212+
Ascending = true,
213+
}).ToArray());
214+
}
215+
216+
/// <summary>
217+
/// Order the Query based on the Primary Key Column(s)
218+
/// </summary>
219+
public TableQuery<T> OrderByKeyDescending()
220+
{
221+
var pks = Table.PKs;
222+
return AddOrdering(pks.Select(pk => new Ordering()
223+
{
224+
ColumnName = pk.Name,
225+
Ascending = false,
226+
}).ToArray());
227+
}
203228

204229
public TableQuery<T> ThenBy<TValue>(Expression<Func<T, TValue>> orderExpr)
205230
{
@@ -212,6 +237,21 @@ public TableQuery<T> ThenByDescending<TValue>(Expression<Func<T, TValue>> orderE
212237
return AddOrderBy(orderExpr, false);
213238
}
214239

240+
private TableQuery<T> AddOrdering(params Ordering[] orderings)
241+
{
242+
var q = Clone<T>();
243+
if (q._orderBys == null)
244+
{
245+
q._orderBys = new List<Ordering>();
246+
}
247+
foreach (var ordering in orderings)
248+
{
249+
q._orderBys.Add(ordering);
250+
}
251+
return q;
252+
}
253+
254+
215255
private TableQuery<T> AddOrderBy<TValue>( Expression<Func<T, TValue>> orderExpr, bool asc)
216256
{
217257
if (orderExpr == null)
@@ -241,17 +281,11 @@ private TableQuery<T> AddOrderBy<TValue>( Expression<Func<T, TValue>> orderExpr,
241281
throw new NotSupportedException("Order By does not support: " + orderExpr);
242282
}
243283

244-
var q = Clone<T>();
245-
if (q._orderBys == null)
246-
{
247-
q._orderBys = new List<Ordering>();
248-
}
249-
q._orderBys.Add(new Ordering
284+
return AddOrdering(new Ordering
250285
{
251286
ColumnName = GetColumnName(mem),
252287
Ascending = asc
253288
});
254-
return q;
255289
}
256290

257291
private void AddWhere( Expression pred)

0 commit comments

Comments
 (0)