Skip to content

Commit 2ee58da

Browse files
committed
Refactor the query syntax using an interface and classes defined in terms of clauses.
1 parent a79e8ba commit 2ee58da

9 files changed

Lines changed: 164 additions & 83 deletions

File tree

SQLitePCL.pretty.Orm/Interfaces.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,10 @@ public IReadOnlyDictionary<string, IndexInfo> Indexes
8484
}
8585
}
8686
}
87+
88+
public interface ISqlQuery
89+
{
90+
string ToSql();
91+
}
8792
}
8893

SQLitePCL.pretty.Orm/SQLitePCL.pretty.Orm.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,11 @@
148148
<Compile Include="TableMapping.Delete.cs" />
149149
<Compile Include="TableMapping.InsertOrReplace.cs" />
150150
<Compile Include="ForeignKeyConstraint.cs" />
151-
<Compile Include="QueryBuilder.cs" />
152-
<Compile Include="QueryBuilder.Limit.cs" />
153-
<Compile Include="QueryBuilder.OrderBy.cs" />
154-
<Compile Include="QueryBuilder.Where.cs" />
151+
<Compile Include="SqlQuery.cs" />
152+
<Compile Include="SqlQuery.Limit.cs" />
153+
<Compile Include="SqlQuery.OrderBy.cs" />
154+
<Compile Include="SqlQuery.Where.cs" />
155+
<Compile Include="SqlQuery.Select.cs" />
155156
</ItemGroup>
156157
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
157158
<ItemGroup>

SQLitePCL.pretty.Orm/QueryBuilder.Limit.cs renamed to SQLitePCL.pretty.Orm/SqlQuery.Limit.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
namespace SQLitePCL.pretty.Orm
77
{
8-
public static partial class QueryBuilder
8+
public static partial class SqlQuery
99
{
10-
public sealed class LimitQuery
10+
public sealed class LimitClause : ISqlQuery
1111
{
1212
private readonly string table;
1313
private readonly string selection;
@@ -16,7 +16,7 @@ public sealed class LimitQuery
1616
private readonly Nullable<int> limit;
1717
private readonly Nullable<int> offset;
1818

19-
internal LimitQuery(string table, string selection, Expression where, IReadOnlyList<Tuple<string, bool>> ordering, Nullable<int> limit, Nullable<int> offset)
19+
internal LimitClause(string table, string selection, Expression where, IReadOnlyList<Tuple<string, bool>> ordering, Nullable<int> limit, Nullable<int> offset)
2020
{
2121
this.table = table;
2222
this.selection = selection;
@@ -31,24 +31,29 @@ internal LimitQuery(string table, string selection, Expression where, IReadOnlyL
3131
/// </summary>
3232
/// <param name="n">The number of elements to return.</param>
3333
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
34-
public LimitQuery Take(int n)
34+
public LimitClause Take(int n)
3535
{
36-
return new LimitQuery(table, selection, where, ordering, n, offset);
36+
return new LimitClause(table, selection, where, ordering, n, offset);
3737
}
3838

3939
/// <summary>
4040
/// Returns a <see cref="TableQuery&lt;T&gt;"/> that skips a specified number of elements in the result set and then returns the remaining elements.
4141
/// </summary>
4242
/// <param name="n">The number of elements to skip before returning the remaining elements.</param>
4343
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
44-
public LimitQuery Skip(int n)
44+
public LimitClause Skip(int n)
4545
{
46-
return new LimitQuery(table, selection, where, ordering, limit, n);
46+
return new LimitClause(table, selection, where, ordering, limit, n);
4747
}
4848

4949
public override string ToString()
5050
{
51-
return QueryBuilder.ToString(selection, table, where, ordering, limit, offset);
51+
return SqlQuery.ToString(selection, table, where, ordering, limit, offset);
52+
}
53+
54+
public string ToSql()
55+
{
56+
return this.ToString();
5257
}
5358
}
5459
}

SQLitePCL.pretty.Orm/QueryBuilder.OrderBy.cs renamed to SQLitePCL.pretty.Orm/SqlQuery.OrderBy.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77

88
namespace SQLitePCL.pretty.Orm
99
{
10-
public static partial class QueryBuilder
10+
public static partial class SqlQuery
1111
{
12-
public sealed class OrderByQuery<T>
12+
public sealed class OrderByClause<T> : ISqlQuery
1313
{
1414
private readonly string table;
1515
private readonly string selection;
1616
private readonly Expression where;
1717
private readonly IReadOnlyList<Tuple<string, bool>> ordering;
1818

19-
internal OrderByQuery(string table, string selection, Expression where, IReadOnlyList<Tuple<string, bool>> ordering)
19+
internal OrderByClause(string table, string selection, Expression where, IReadOnlyList<Tuple<string, bool>> ordering)
2020
{
2121
this.table = table;
2222
this.selection = selection;
2323
this.where = where;
2424
this.ordering = ordering;
2525
}
2626

27-
public OrderByQuery<T> ThenBy<TValue>(Expression<Func<T, TValue>> orderExpr)
27+
public OrderByClause<T> ThenBy<TValue>(Expression<Func<T, TValue>> orderExpr)
2828
{
2929
return AddOrderBy(orderExpr, true);
3030
}
3131

32-
public OrderByQuery<T> ThenByDescending<TValue>(Expression<Func<T, TValue>> orderExpr)
32+
public OrderByClause<T> ThenByDescending<TValue>(Expression<Func<T, TValue>> orderExpr)
3333
{
3434
return AddOrderBy(orderExpr, false);
3535
}
@@ -39,19 +39,19 @@ public OrderByQuery<T> ThenByDescending<TValue>(Expression<Func<T, TValue>> orde
3939
/// </summary>
4040
/// <param name="n">The number of elements to return.</param>
4141
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
42-
public LimitQuery Take(int n)
42+
public LimitClause Take(int n)
4343
{
44-
return new LimitQuery(table, selection, where, this.ordering, n, null);
44+
return new LimitClause(table, selection, where, this.ordering, n, null);
4545
}
4646

4747
/// <summary>
4848
/// Returns a <see cref="TableQuery&lt;T&gt;"/> that skips a specified number of elements in the result set and then returns the remaining elements.
4949
/// </summary>
5050
/// <param name="n">The number of elements to skip before returning the remaining elements.</param>
5151
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
52-
public LimitQuery Skip(int n)
52+
public LimitClause Skip(int n)
5353
{
54-
return new LimitQuery(table, selection, where, this.ordering, null, n);
54+
return new LimitClause(table, selection, where, this.ordering, null, n);
5555
}
5656

5757
/// <summary>
@@ -60,21 +60,26 @@ public LimitQuery Skip(int n)
6060
/// <returns>The <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
6161
/// <param name="index">Index.</param>
6262
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
63-
public string ElementAt(int index)
63+
public LimitClause ElementAt(int index)
6464
{
65-
return Skip(index).Take(1).ToString();
65+
return Skip(index).Take(1);
6666
}
6767

68-
private OrderByQuery<T> AddOrderBy<TValue>(Expression<Func<T, TValue>> orderExpr, bool asc)
68+
private OrderByClause<T> AddOrderBy<TValue>(Expression<Func<T, TValue>> orderExpr, bool asc)
6969
{
7070
var orderBy = new List<Tuple<string, bool>>(ordering);
7171
orderBy.Add(orderExpr.CompileOrderByExpression(asc));
72-
return new OrderByQuery<T>(table, selection, where, orderBy);
72+
return new OrderByClause<T>(table, selection, where, orderBy);
7373
}
7474

7575
public override string ToString()
7676
{
77-
return QueryBuilder.ToString(selection, table, where, ordering, null, null);
77+
return SqlQuery.ToString(selection, table, where, ordering, null, null);
78+
}
79+
80+
public string ToSql()
81+
{
82+
return this.ToString();
7883
}
7984
}
8085
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Linq;
3+
namespace SQLitePCL.pretty.Orm
4+
{
5+
public static partial class SqlQuery
6+
{
7+
public sealed class SelectClause<T>
8+
{
9+
private readonly string from;
10+
11+
internal SelectClause(string from)
12+
{
13+
this.from = from;
14+
}
15+
16+
public WhereClause<T> Select()
17+
{
18+
return new WhereClause<T>(this.from, "*", null);
19+
}
20+
21+
public WhereClause<T> Count()
22+
{
23+
return new WhereClause<T>(this.from, "COUNT(*)", null);
24+
}
25+
}
26+
}
27+
}
28+
29+

SQLitePCL.pretty.Orm/QueryBuilder.Where.cs renamed to SQLitePCL.pretty.Orm/SqlQuery.Where.cs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,97 +5,98 @@
55

66
namespace SQLitePCL.pretty.Orm
77
{
8-
public static partial class QueryBuilder
8+
public static partial class SqlQuery
99
{
10-
public sealed class WhereQuery<T>
10+
public sealed class WhereClause<T> : ISqlQuery
1111
{
12-
private const string selection = "*";
1312
private readonly string table;
13+
private readonly string selection;
1414
private readonly Expression where;
1515

16-
internal WhereQuery(string table, Expression where)
16+
internal WhereClause(string table, string selection, Expression where)
1717
{
1818
this.table = table;
19+
this.selection = selection;
1920
this.where = where;
2021
}
2122

22-
public OrderByQuery<T> OrderBy<TValue>(Expression<Func<T, TValue>> orderExpr)
23+
public OrderByClause<T> OrderBy<TValue>(Expression<Func<T, TValue>> orderExpr)
2324
{
2425
return CreateOrderBy(orderExpr, true);
2526
}
2627

27-
public OrderByQuery<T> OrderByDescending<TValue>(Expression<Func<T, TValue>> orderExpr)
28+
public OrderByClause<T> OrderByDescending<TValue>(Expression<Func<T, TValue>> orderExpr)
2829
{
2930
return CreateOrderBy(orderExpr, false);
3031
}
3132

32-
private OrderByQuery<T> CreateOrderBy<TValue>(Expression<Func<T, TValue>> orderExpr, bool asc)
33+
private OrderByClause<T> CreateOrderBy<TValue>(Expression<Func<T, TValue>> orderExpr, bool asc)
3334
{
3435
var orderBy = new List<Tuple<string, bool>>();
3536
orderBy.Add(orderExpr.CompileOrderByExpression(asc));
36-
return new OrderByQuery<T>(table, selection, where, orderBy);
37+
return new OrderByClause<T>(table, selection, where, orderBy);
3738
}
3839

39-
public WhereQuery<T> Where<U,V,W,X,Y,Z>(Expression<Func<T,U,V,W,X,Y,Z,bool>> predExpr)
40+
public WhereClause<T> Where<U,V,W,X,Y,Z>(Expression<Func<T,U,V,W,X,Y,Z,bool>> predExpr)
4041
{
4142
return this.Where((LambdaExpression) predExpr);
4243
}
4344

44-
public WhereQuery<T> Where<U,V,W,X,Y>(Expression<Func<T,U,V,W,X,Y,bool>> predExpr)
45+
public WhereClause<T> Where<U,V,W,X,Y>(Expression<Func<T,U,V,W,X,Y,bool>> predExpr)
4546
{
4647
return this.Where((LambdaExpression) predExpr);
4748
}
4849

49-
public WhereQuery<T> Where<U,V,W,X>(Expression<Func<T,U,V,W,X,bool>> predExpr)
50+
public WhereClause<T> Where<U,V,W,X>(Expression<Func<T,U,V,W,X,bool>> predExpr)
5051
{
5152
return this.Where((LambdaExpression) predExpr);
5253
}
5354

54-
public WhereQuery<T> Where<U,V,W>(Expression<Func<T,U,V,W,bool>> predExpr)
55+
public WhereClause<T> Where<U,V,W>(Expression<Func<T,U,V,W,bool>> predExpr)
5556
{
5657
return this.Where((LambdaExpression) predExpr);
5758
}
5859

59-
public WhereQuery<T> Where<U,V>(Expression<Func<T,U,V,bool>> predExpr)
60+
public WhereClause<T> Where<U,V>(Expression<Func<T,U,V,bool>> predExpr)
6061
{
6162
return this.Where((LambdaExpression) predExpr);
6263
}
6364

64-
public WhereQuery<T> Where<U>(Expression<Func<T,U,bool>> predExpr)
65+
public WhereClause<T> Where<U>(Expression<Func<T,U,bool>> predExpr)
6566
{
6667
return this.Where((LambdaExpression) predExpr);
6768
}
6869

69-
public WhereQuery<T> Where(Expression<Func<T, bool>> predExpr)
70+
public WhereClause<T> Where(Expression<Func<T, bool>> predExpr)
7071
{
7172
return this.Where((LambdaExpression) predExpr);
7273
}
7374

74-
private WhereQuery<T> Where(LambdaExpression lambda)
75+
private WhereClause<T> Where(LambdaExpression lambda)
7576
{
7677
var pred = lambda.Body;
7778
var where = this.where == null ? pred : Expression.AndAlso(this.where, pred);
78-
return new WhereQuery<T>(table, where);
79+
return new WhereClause<T>(table, selection, where);
7980
}
8081

8182
/// <summary>
8283
/// Returns a <see cref="TableQuery&lt;T&gt;"/> that limits the result set to a specified number of contiguous elements.
8384
/// </summary>
8485
/// <param name="n">The number of elements to return.</param>
8586
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
86-
public LimitQuery Take(int n)
87+
public LimitClause Take(int n)
8788
{
88-
return new LimitQuery(table, selection, where, new List<Tuple<string, bool>>(), n, null);
89+
return new LimitClause(table, selection, where, new List<Tuple<string, bool>>(), n, null);
8990
}
9091

9192
/// <summary>
9293
/// Returns a <see cref="TableQuery&lt;T&gt;"/> that skips a specified number of elements in the result set and then returns the remaining elements.
9394
/// </summary>
9495
/// <param name="n">The number of elements to skip before returning the remaining elements.</param>
9596
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
96-
public LimitQuery Skip(int n)
97+
public LimitClause Skip(int n)
9798
{
98-
return new LimitQuery(table, selection, where, new List<Tuple<string, bool>>(), null, n);
99+
return new LimitClause(table, selection, where, new List<Tuple<string, bool>>(), null, n);
99100
}
100101

101102
/// <summary>
@@ -104,14 +105,19 @@ public LimitQuery Skip(int n)
104105
/// <returns>The <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
105106
/// <param name="index">Index.</param>
106107
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
107-
public string ElementAt(int index)
108+
public LimitClause ElementAt(int index)
108109
{
109-
return Skip(index).Take(1).ToString();
110+
return Skip(index).Take(1);
110111
}
111112

112113
public override string ToString()
113114
{
114-
return QueryBuilder.ToString(selection, table, where, Enumerable.Empty<Tuple<string, bool>>(), null, null);
115+
return SqlQuery.ToString(selection, table, where, Enumerable.Empty<Tuple<string, bool>>(), null, null);
116+
}
117+
118+
public string ToSql()
119+
{
120+
return this.ToString();
115121
}
116122
}
117123
}

0 commit comments

Comments
 (0)