Skip to content

Commit f05db03

Browse files
committed
Create Select clause and disallow multiple where calls. Add docs
1 parent f9d6c30 commit f05db03

9 files changed

Lines changed: 302 additions & 112 deletions

File tree

SQLitePCL.pretty.Orm/Attributes.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,14 @@ public ForeignKeyAttribute(string tableName, string columnName)
281281
this.columnName = columnName;
282282
}
283283

284+
/// <summary>
285+
/// The table that constrains the annotated column.
286+
/// </summary>
284287
public string TableName { get { return tableName; } }
288+
289+
/// <summary>
290+
/// The column in the table that constrains the annotated column.
291+
/// </summary>
285292
public string ColumnName { get { return columnName; } }
286293
}
287294

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,25 +136,48 @@
136136
<Compile Include="TableMapping.cs" />
137137
<Compile Include="Attributes.cs" />
138138
<Compile Include="DatabaseConnection.cs" />
139-
<Compile Include="DatabaseConnection.Index.cs" />
140-
<Compile Include="DatabaseConnection.Tables.cs" />
139+
<Compile Include="DatabaseConnection.Find.cs">
140+
<DependentUpon>DatabaseConnection.cs</DependentUpon>
141+
</Compile>
142+
<Compile Include="DatabaseConnection.Index.cs">
143+
<DependentUpon>DatabaseConnection.cs</DependentUpon>
144+
</Compile>
145+
<Compile Include="DatabaseConnection.Tables.cs">
146+
<DependentUpon>DatabaseConnection.cs</DependentUpon>
147+
</Compile>
141148
<Compile Include="SQLBuilder.cs" />
142149
<Compile Include="Interfaces.cs" />
143150
<Compile Include="ColumnMapping.cs" />
144151
<Compile Include="IndexInfo.cs" />
145152
<Compile Include="Reflection.cs" />
146153
<Compile Include="ForeignKeyConstraint.cs" />
147154
<Compile Include="SqlQuery.cs" />
148-
<Compile Include="SqlQuery.Limit.cs" />
149-
<Compile Include="SqlQuery.OrderBy.cs" />
150-
<Compile Include="SqlQuery.Where.cs" />
155+
<Compile Include="SqlQuery.Limit.cs">
156+
<DependentUpon>SqlQuery.cs</DependentUpon>
157+
</Compile>
158+
<Compile Include="SqlQuery.OrderBy.cs">
159+
<DependentUpon>SqlQuery.cs</DependentUpon>
160+
</Compile>
161+
<Compile Include="SqlQuery.Where.cs">
162+
<DependentUpon>SqlQuery.cs</DependentUpon>
163+
</Compile>
151164
<Compile Include="Statement.cs" />
152-
<Compile Include="DatabaseConnection.Delete.cs" />
153-
<Compile Include="DatabaseConnection.Find.cs" />
154-
<Compile Include="DatabaseConnection.InsertOrReplace.cs" />
155-
<Compile Include="SqlQuery.From.cs" />
165+
<Compile Include="DatabaseConnection.Delete.cs">
166+
<DependentUpon>DatabaseConnection.cs</DependentUpon>
167+
</Compile>
168+
<Compile Include="DatabaseConnection.InsertOrReplace.cs">
169+
<DependentUpon>DatabaseConnection.cs</DependentUpon>
170+
</Compile>
171+
<Compile Include="SqlQuery.From.cs">
172+
<DependentUpon>SqlQuery.cs</DependentUpon>
173+
</Compile>
156174
<Compile Include="ResultSet.cs" />
157-
<Compile Include="DatabaseConnection.Query.cs" />
175+
<Compile Include="DatabaseConnection.Query.cs">
176+
<DependentUpon>DatabaseConnection.cs</DependentUpon>
177+
</Compile>
178+
<Compile Include="SqlQuery.Select.cs">
179+
<DependentUpon>SqlQuery.cs</DependentUpon>
180+
</Compile>
158181
</ItemGroup>
159182
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
160183
<ItemGroup>

SQLitePCL.pretty.Orm/SqlQuery.From.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace SQLitePCL.pretty.Orm
88
{
99
public static partial class SqlQuery
1010
{
11+
/// <summary>
12+
/// The FROM clause of a SQL query.
13+
/// </summary>
1114
public sealed class FromClause<T>
1215
{
1316
private readonly string table;
@@ -17,13 +20,20 @@ internal FromClause(string table)
1720
this.table = table;
1821
}
1922

20-
public WhereClause<T> Select()
23+
/// <summary>
24+
/// Select all columns from the table.
25+
/// </summary>
26+
public SelectClause<T> Select()
2127
{
2228
var table = TableMapping.Get<T>();
2329
var columns = table.Columns.Keys.Select(col => table.TableName + "." + col).ToList();
24-
return new WhereClause<T>(this, columns, null);
30+
return new SelectClause<T>(this, columns);
2531
}
2632

33+
/// <summary>
34+
/// Returns a <see cref="System.String"/> that represents the current <see cref="FromClause&lt;T&gt;"/>.
35+
/// </summary>
36+
/// <returns>A <see cref="System.String"/> that represents the current <see cref="FromClause&lt;T&gt;"/>.</returns>
2737
public override string ToString()
2838
{
2939
return "FROM \"" + table + "\"";

SQLitePCL.pretty.Orm/SqlQuery.Limit.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace SQLitePCL.pretty.Orm
88
{
99
public static partial class SqlQuery
1010
{
11+
/// <summary>
12+
/// The LIMIT clause of a SQL query.
13+
/// </summary>
1114
public sealed class LimitClause<T> : ISqlQuery
1215
{
1316
private readonly OrderByClause<T> orderBy;
@@ -22,27 +25,31 @@ internal LimitClause(OrderByClause<T> orderBy, Nullable<int> limit, Nullable<int
2225
}
2326

2427
/// <summary>
25-
/// Returns a <see cref="TableQuery&lt;T&gt;"/> that limits the result set to a specified number of contiguous elements.
28+
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that limits the result set to a specified number of contiguous elements.
2629
/// </summary>
2730
/// <param name="n">The number of elements to return.</param>
28-
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
31+
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
2932
public LimitClause<T> Take(int n)
3033
{
3134
Contract.Requires(n >= 0);
3235
return new LimitClause<T>(orderBy, n, offset);
3336
}
3437

3538
/// <summary>
36-
/// 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.
39+
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that skips a specified number of elements in the result set and then returns the remaining elements.
3740
/// </summary>
3841
/// <param name="n">The number of elements to skip before returning the remaining elements.</param>
39-
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
42+
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
4043
public LimitClause<T> Skip(int n)
4144
{
4245
Contract.Requires(n >= 0);
4346
return new LimitClause<T>(orderBy, limit, n);
4447
}
4548

49+
/// <summary>
50+
/// Returns a <see cref="System.String"/> that represents the current <see cref="LimitClause&lt;T&gt;"/>.
51+
/// </summary>
52+
/// <returns>A <see cref="System.String"/> that represents the current <see cref="LimitClause&lt;T&gt;"/>.</returns>
4653
public override string ToString()
4754
{
4855
return

SQLitePCL.pretty.Orm/SqlQuery.OrderBy.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ namespace SQLitePCL.pretty.Orm
1111
{
1212
public static partial class SqlQuery
1313
{
14+
/// <summary>
15+
/// The ORDER BY clause of a SQL query.
16+
/// </summary>
1417
public sealed class OrderByClause<T> : ISqlQuery
1518
{
1619
private readonly WhereClause<T> whereClause;
@@ -22,46 +25,57 @@ internal OrderByClause(WhereClause<T> whereClause, IReadOnlyList<Tuple<string, b
2225
this.ordering = ordering;
2326
}
2427

28+
/// <summary>
29+
/// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
30+
/// </summary>
31+
/// <returns>An <seealso cref="OrderByClause&lt;T&gt;"/>.</returns>
32+
/// <param name="orderExpr">A function to extract a key from each element.</param>
33+
/// <typeparam name="TValue">The type of the key</typeparam>
2534
public OrderByClause<T> ThenBy<TValue>(Expression<Func<T, TValue>> orderExpr)
2635
{
2736
Contract.Requires(orderExpr != null);
2837
return AddOrderBy(orderExpr, true);
2938
}
3039

40+
/// <summary>
41+
/// Performs a subsequent ordering of the elements in a sequence in descending order according to a key.
42+
/// </summary>
43+
/// <returns>An <seealso cref="OrderByClause&lt;T&gt;"/>.</returns>
44+
/// <param name="orderExpr">A function to extract a key from each element.</param>
45+
/// <typeparam name="TValue">The type of the key</typeparam>
3146
public OrderByClause<T> ThenByDescending<TValue>(Expression<Func<T, TValue>> orderExpr)
3247
{
3348
Contract.Requires(orderExpr != null);
3449
return AddOrderBy(orderExpr, false);
3550
}
3651

3752
/// <summary>
38-
/// Returns a <see cref="TableQuery&lt;T&gt;"/> that limits the result set to a specified number of contiguous elements.
53+
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that limits the result set to a specified number of contiguous elements.
3954
/// </summary>
4055
/// <param name="n">The number of elements to return.</param>
41-
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
56+
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
4257
public LimitClause<T> Take(int n)
4358
{
4459
Contract.Requires(n >= 0);
4560
return new LimitClause<T>(this, n, null);
4661
}
4762

4863
/// <summary>
49-
/// 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.
64+
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that skips a specified number of elements in the result set and then returns the remaining elements.
5065
/// </summary>
5166
/// <param name="n">The number of elements to skip before returning the remaining elements.</param>
52-
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
67+
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
5368
public LimitClause<T> Skip(int n)
5469
{
5570
Contract.Requires(n >= 0);
5671
return new LimitClause<T>(this, null, n);
5772
}
5873

5974
/// <summary>
60-
/// Returns a <see cref="TableQuery&lt;T&gt;"/> that returns the element at a specified index in the result set.
75+
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that returns the element at a specified index in the result set.
6176
/// </summary>
62-
/// <returns>The <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
6377
/// <param name="index">Index.</param>
64-
/// <returns>A new <see cref="SQLitePCL.pretty.Orm.TableQuery&lt;T&gt;"/>.</returns>
78+
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
6579
public LimitClause<T> ElementAt(int index)
6680
{
6781
Contract.Requires(index >= 0);
@@ -75,6 +89,10 @@ private OrderByClause<T> AddOrderBy<TValue>(Expression<Func<T, TValue>> orderExp
7589
return new OrderByClause<T>(whereClause, orderBy);
7690
}
7791

92+
/// <summary>
93+
/// Returns a <see cref="System.String"/> that represents the current <see cref="LimitClause&lt;T&gt;"/>.
94+
/// </summary>
95+
/// <returns>A <see cref="System.String"/> that represents the current <see cref="LimitClause&lt;T&gt;"/>.</returns>
7896
public override string ToString()
7997
{
8098
return
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using System.Diagnostics.Contracts;
5+
6+
namespace SQLitePCL.pretty.Orm
7+
{
8+
public static partial class SqlQuery
9+
{
10+
public sealed class SelectClause<T> : ISqlQuery
11+
{
12+
// FIXME: Long term, prefer some sort of expression syntax
13+
private readonly IReadOnlyList<String> select;
14+
private readonly FromClause<T> from;
15+
16+
internal SelectClause(FromClause<T> from, IReadOnlyList<String> select)
17+
{
18+
this.from = from;
19+
this.select = select;
20+
}
21+
22+
public WhereClause<T> Where<U,V,W,X,Y,Z>(Expression<Func<T,U,V,W,X,Y,Z,bool>> predExpr)
23+
{
24+
Contract.Requires(predExpr != null);
25+
return this.Where((LambdaExpression) predExpr);
26+
}
27+
28+
public WhereClause<T> Where<U,V,W,X,Y>(Expression<Func<T,U,V,W,X,Y,bool>> predExpr)
29+
{
30+
Contract.Requires(predExpr != null);
31+
return this.Where((LambdaExpression) predExpr);
32+
}
33+
34+
public WhereClause<T> Where<U,V,W,X>(Expression<Func<T,U,V,W,X,bool>> predExpr)
35+
{
36+
Contract.Requires(predExpr != null);
37+
return this.Where((LambdaExpression) predExpr);
38+
}
39+
40+
public WhereClause<T> Where<U,V,W>(Expression<Func<T,U,V,W,bool>> predExpr)
41+
{
42+
Contract.Requires(predExpr != null);
43+
return this.Where((LambdaExpression) predExpr);
44+
}
45+
46+
public WhereClause<T> Where<U,V>(Expression<Func<T,U,V,bool>> predExpr)
47+
{
48+
Contract.Requires(predExpr != null);
49+
return this.Where((LambdaExpression) predExpr);
50+
}
51+
52+
public WhereClause<T> Where<U>(Expression<Func<T,U,bool>> predExpr)
53+
{
54+
Contract.Requires(predExpr != null);
55+
return this.Where((LambdaExpression) predExpr);
56+
}
57+
58+
public WhereClause<T> Where(Expression<Func<T, bool>> predExpr)
59+
{
60+
Contract.Requires(predExpr != null);
61+
return this.Where((LambdaExpression) predExpr);
62+
}
63+
64+
private WhereClause<T> Where(LambdaExpression lambda)
65+
{
66+
var pred = lambda.Body;
67+
return new WhereClause<T>(this, pred);
68+
}
69+
70+
private WhereClause<T> Where()
71+
{
72+
return new WhereClause<T>(this, null);
73+
}
74+
75+
/// <summary>
76+
/// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
77+
/// </summary>
78+
/// <returns>An <seealso cref="OrderByClause&lt;T&gt;"/>.</returns>
79+
/// <param name="orderExpr">A function to extract a key from each element.</param>
80+
/// <typeparam name="TValue">The type of the key</typeparam>
81+
public OrderByClause<T> OrderBy<TValue>(Expression<Func<T, TValue>> orderExpr)
82+
{
83+
return this.Where().OrderBy(orderExpr);
84+
}
85+
86+
/// <summary>
87+
/// Performs a subsequent ordering of the elements in a sequence in descending order according to a key.
88+
/// </summary>
89+
/// <returns>An <seealso cref="OrderByClause&lt;T&gt;"/>.</returns>
90+
/// <param name="orderExpr">A function to extract a key from each element.</param>
91+
/// <typeparam name="TValue">The type of the key</typeparam>
92+
public OrderByClause<T> OrderByDescending<TValue>(Expression<Func<T, TValue>> orderExpr)
93+
{
94+
return this.Where().OrderByDescending(orderExpr);
95+
}
96+
97+
/// <summary>
98+
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that limits the result set to a specified number of contiguous elements.
99+
/// </summary>
100+
/// <param name="n">The number of elements to return.</param>
101+
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
102+
public LimitClause<T> Take(int n)
103+
{
104+
return this.Where().Take(n);
105+
}
106+
107+
/// <summary>
108+
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that skips a specified number of elements in the result set and then returns the remaining elements.
109+
/// </summary>
110+
/// <param name="n">The number of elements to skip before returning the remaining elements.</param>
111+
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
112+
public LimitClause<T> Skip(int n)
113+
{
114+
return this.Where().Skip(n);
115+
}
116+
117+
/// <summary>
118+
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that returns the element at a specified index in the result set.
119+
/// </summary>
120+
/// <returns>The <see cref="LimitClause&lt;T&gt;"/>.</returns>
121+
/// <param name="index">Index.</param>
122+
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
123+
public LimitClause<T> ElementAt(int index)
124+
{
125+
return this.Where().ElementAt(index);
126+
}
127+
128+
/// <summary>
129+
/// Returns a <see cref="System.String"/> that represents the current <see cref="SelectClause&lt;T&gt;"/>.
130+
/// </summary>
131+
/// <returns>A <see cref="System.String"/> that represents the current <see cref="SelectClause&lt;T&gt;"/>.</returns>
132+
public override string ToString()
133+
{
134+
return
135+
"SELECT " + string.Join(", ", select) +
136+
"\r\n" + from.ToString();
137+
}
138+
}
139+
}
140+
}
141+

0 commit comments

Comments
 (0)