Skip to content

Commit 7fa72ae

Browse files
committed
Break up Offset and Limit into seperate classes.
1 parent abf99d2 commit 7fa72ae

6 files changed

Lines changed: 63 additions & 42 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@
152152
<Compile Include="Reflection.cs" />
153153
<Compile Include="ForeignKeyConstraint.cs" />
154154
<Compile Include="SqlQuery.cs" />
155+
<Compile Include="SqlQuery.Offset.cs">
156+
<DependentUpon>SqlQuery.cs</DependentUpon>
157+
</Compile>
155158
<Compile Include="SqlQuery.Limit.cs">
156159
<DependentUpon>SqlQuery.cs</DependentUpon>
157160
</Compile>

SQLitePCL.pretty.Orm/SqlQuery.Limit.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,22 @@ public sealed class LimitClause<T> : ISqlQuery
1515
{
1616
private readonly OrderByClause<T> orderBy;
1717
private readonly int limit;
18-
private readonly Nullable<int> offset;
1918

20-
internal LimitClause(OrderByClause<T> orderBy, int limit, Nullable<int> offset)
19+
internal LimitClause(OrderByClause<T> orderBy, int limit)
2120
{
2221
this.orderBy = orderBy;
2322
this.limit = limit;
24-
this.offset = offset;
2523
}
2624

2725
/// <summary>
28-
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that limits the result set to a specified number of contiguous elements.
29-
/// </summary>
30-
/// <param name="n">The number of elements to return.</param>
31-
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
32-
public LimitClause<T> Take(int n)
33-
{
34-
Contract.Requires(n >= 0);
35-
return new LimitClause<T>(orderBy, n, offset);
36-
}
37-
38-
/// <summary>
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.
26+
/// Returns a <see cref="OffsetClause&lt;T&gt;"/> that skips a specified number of elements in the result set and then returns the remaining elements.
4027
/// </summary>
4128
/// <param name="n">The number of elements to skip before returning the remaining elements.</param>
42-
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
43-
public LimitClause<T> Skip(int n)
29+
/// <returns>A new <see cref="OffsetClause&lt;T&gt;"/>.</returns>
30+
public OffsetClause<T> Skip(int n)
4431
{
4532
Contract.Requires(n >= 0);
46-
return new LimitClause<T>(orderBy, limit, n);
33+
return new OffsetClause<T>(this, n);
4734
}
4835

4936
/// <summary>
@@ -54,8 +41,7 @@ public override string ToString()
5441
{
5542
return
5643
orderBy.ToString() +
57-
"\r\nLIMIT " + limit +
58-
(offset.HasValue ? "\r\nOFFSET " + offset.Value : "");
44+
"\r\nLIMIT " + limit;
5945
}
6046
}
6147
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
namespace SQLitePCL.pretty.Orm
3+
{
4+
public static partial class SqlQuery
5+
{
6+
/// <summary>
7+
/// The OFFSET clause of a SQL query.
8+
/// </summary>
9+
public sealed class OffsetClause<T> : ISqlQuery
10+
{
11+
private readonly LimitClause<T> limit;
12+
private readonly int offset;
13+
14+
internal OffsetClause(LimitClause<T> limit, int offset)
15+
{
16+
this.limit = limit;
17+
this.offset = offset;
18+
}
19+
20+
/// <summary>
21+
/// Returns a <see cref="System.String"/> that represents the current <see cref="OffsetClause&lt;T&gt;"/>.
22+
/// </summary>
23+
/// <returns>A <see cref="System.String"/> that represents the current <see cref="OffsetClause&lt;T&gt;"/>.</returns>
24+
25+
public override string ToString()
26+
{
27+
return
28+
limit.ToString() +
29+
"\r\nOFFSET " + offset;
30+
}
31+
}
32+
}
33+
}
34+

SQLitePCL.pretty.Orm/SqlQuery.OrderBy.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,29 @@ public OrderByClause<T> ThenByDescending<TValue>(Expression<Func<T, TValue>> ord
5757
public LimitClause<T> Take(int n)
5858
{
5959
Contract.Requires(n >= 0);
60-
return new LimitClause<T>(this, n, null);
60+
return new LimitClause<T>(this, n);
6161
}
6262

6363
/// <summary>
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.
64+
/// Returns a <see cref="OffsetClause&lt;T&gt;"/> that skips a specified number of elements in the result set and then returns the remaining elements.
6565
/// </summary>
6666
/// <param name="n">The number of elements to skip before returning the remaining elements.</param>
67-
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
68-
public LimitClause<T> Skip(int n)
67+
/// <returns>A new <see cref="OffsetClause&lt;T&gt;"/>.</returns>
68+
public OffsetClause<T> Skip(int n)
6969
{
70-
Contract.Requires(n >= 0);
71-
7270
//If the LIMIT expression evaluates to a negative value, then there is no upper bound on the number of rows returned
73-
return new LimitClause<T>(this, -1, n);
71+
return new LimitClause<T>(this, -1).Skip(n);
7472
}
7573

7674
/// <summary>
77-
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that returns the element at a specified index in the result set.
75+
/// Returns a <see cref="OffsetClause&lt;T&gt;"/> that returns the element at a specified index in the result set.
7876
/// </summary>
79-
/// <param name="index">Index.</param>
80-
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
81-
public LimitClause<T> ElementAt(int index)
77+
/// <param name="index">The index of the element to retrieve.</param>
78+
/// <returns>A new <see cref="OffsetClause&lt;T&gt;"/>.</returns>
79+
public OffsetClause<T> ElementAt(int index)
8280
{
8381
Contract.Requires(index >= 0);
84-
return Skip(index).Take(1);
82+
return this.Take(1).Skip(index);
8583
}
8684

8785
private OrderByClause<T> AddOrderBy<TValue>(Expression<Func<T, TValue>> orderExpr, bool asc)

SQLitePCL.pretty.Orm/SqlQuery.Select.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,22 @@ public LimitClause<T> Take(int n)
157157
}
158158

159159
/// <summary>
160-
/// 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.
160+
/// Returns a <see cref="OffsetClause&lt;T&gt;"/> that skips a specified number of elements in the result set and then returns the remaining elements.
161161
/// </summary>
162162
/// <param name="n">The number of elements to skip before returning the remaining elements.</param>
163163
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
164-
public LimitClause<T> Skip(int n)
164+
public OffsetClause<T> Skip(int n)
165165
{
166166
return this.Where().Skip(n);
167167
}
168168

169169
/// <summary>
170-
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that returns the element at a specified index in the result set.
170+
/// Returns a <see cref="OffsetClause&lt;T&gt;"/> that returns the element at a specified index in the result set.
171171
/// </summary>
172-
/// <returns>The <see cref="LimitClause&lt;T&gt;"/>.</returns>
173-
/// <param name="index">Index.</param>
174-
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
175-
public LimitClause<T> ElementAt(int index)
172+
/// <returns>The <see cref="OffsetClause&lt;T&gt;"/>.</returns>
173+
/// <param name="index">The index of the element to retrieve.</param>
174+
/// <returns>A new <see cref="OffsetClause&lt;T&gt;"/>.</returns>
175+
public OffsetClause<T> ElementAt(int index)
176176
{
177177
return this.Where().ElementAt(index);
178178
}

SQLitePCL.pretty.Orm/SqlQuery.Where.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public LimitClause<T> Take(int n)
7878
/// </summary>
7979
/// <param name="n">The number of elements to skip before returning the remaining elements.</param>
8080
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
81-
public LimitClause<T> Skip(int n)
81+
public OffsetClause<T> Skip(int n)
8282
{
8383
return this.OrderByNone().Skip(n);
8484
}
@@ -87,9 +87,9 @@ public LimitClause<T> Skip(int n)
8787
/// Returns a <see cref="LimitClause&lt;T&gt;"/> that returns the element at a specified index in the result set.
8888
/// </summary>
8989
/// <returns>The <see cref="LimitClause&lt;T&gt;"/>.</returns>
90-
/// <param name="index">Index.</param>
90+
/// <param name="index">The index of the element to retrieve.</param>
9191
/// <returns>A new <see cref="LimitClause&lt;T&gt;"/>.</returns>
92-
public LimitClause<T> ElementAt(int index)
92+
public OffsetClause<T> ElementAt(int index)
9393
{
9494
return this.OrderByNone().ElementAt(index);
9595
}

0 commit comments

Comments
 (0)