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

Commit 9e7d637

Browse files
committed
Add distinct Skip/Take methods as alias for Limit
1 parent c223a27 commit 9e7d637

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

src/ServiceStack.OrmLite.Firebird/FirebirdSqlExpression.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ public override string LimitExpression
165165
{
166166
get
167167
{
168-
if (!Skip.HasValue) return "";
169-
int fromRow = Skip.Value + 1;
168+
if (!Offset.HasValue) return "";
169+
int fromRow = Offset.Value + 1;
170170
if (fromRow <= 0)
171171
throw new ArgumentException(
172-
string.Format("Skip value:'{0}' must be>=0", Skip.Value));
172+
string.Format("Skip value:'{0}' must be>=0", Offset.Value));
173173
string toRow;
174174
if (Rows.HasValue)
175175
{

src/ServiceStack.OrmLite.Oracle/OracleSqlExpression.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ protected override string ApplyPaging(string sql)
5858
{
5959
if (!Rows.HasValue)
6060
return sql;
61-
if (!Skip.HasValue)
61+
if (!Offset.HasValue)
6262
{
63-
Skip = 0;
63+
Offset = 0;
6464
}
6565
sql = UpdateWithOrderByIfNecessary(sql);
6666
var sb = new StringBuilder();
6767
sb.AppendLine("SELECT * FROM (");
6868
sb.AppendLine("SELECT \"_ss_ormlite_1_\".*, ROWNUM RNUM FROM (");
6969
sb.Append(sql);
7070
sb.AppendLine(") \"_ss_ormlite_1_\"");
71-
sb.AppendFormat("WHERE ROWNUM <= {0} + {1}) \"_ss_ormlite_2_\" ", Skip.Value, Rows.Value);
72-
sb.AppendFormat("WHERE \"_ss_ormlite_2_\".RNUM > {0}", Skip.Value);
71+
sb.AppendFormat("WHERE ROWNUM <= {0} + {1}) \"_ss_ormlite_2_\" ", Offset.Value, Rows.Value);
72+
sb.AppendFormat("WHERE \"_ss_ormlite_2_\".RNUM > {0}", Offset.Value);
7373

7474
return sb.ToString();
7575
}

src/ServiceStack.OrmLite.PostgreSQL/PostgreSqlExpression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public override string LimitExpression
88
{
99
if (!Rows.HasValue) return "";
1010
string offset;
11-
if (Skip.HasValue)
11+
if (Offset.HasValue)
1212
{
13-
offset = string.Format(" OFFSET {0}", Skip.Value);
13+
offset = string.Format(" OFFSET {0}", Offset.Value);
1414
}
1515
else
1616
{

src/ServiceStack.OrmLite.SqlServer/SqlServerExpression.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public class SqlServerExpression<T> : SqlExpression<T>
77
{
88
public override string ToSelectStatement()
99
{
10-
if (!Skip.HasValue && !Rows.HasValue)
10+
if (!Offset.HasValue && !Rows.HasValue)
1111
return base.ToSelectStatement();
1212

1313
AssertValidSkipRowValues();
1414

15-
var skip = Skip.HasValue ? Skip.Value : 0;
15+
var skip = Offset.HasValue ? Offset.Value : 0;
1616
var take = Rows.HasValue ? Rows.Value : int.MaxValue;
1717

1818
var sql = "";
@@ -86,8 +86,8 @@ public override string ToUpdateStatement(T item, bool excludeDefaults = false)
8686

8787
protected virtual void AssertValidSkipRowValues()
8888
{
89-
if (Skip.HasValue && Skip.Value < 0)
90-
throw new ArgumentException(String.Format("Skip value:'{0}' must be>=0", Skip.Value));
89+
if (Offset.HasValue && Offset.Value < 0)
90+
throw new ArgumentException(String.Format("Skip value:'{0}' must be>=0", Offset.Value));
9191

9292
if (Rows.HasValue && Rows.Value <0)
9393
throw new ArgumentException(string.Format("Rows value:'{0}' must be>=0", Rows.Value));

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,23 @@ private void BuildOrderByClauseInternal()
329329
}
330330
}
331331

332+
/// <summary>
333+
/// Offset of the first row to return. The offset of the initial row is 0
334+
/// </summary>
335+
public virtual SqlExpression<T> Skip(int? skip = null)
336+
{
337+
Offset = skip;
338+
return this;
339+
}
340+
341+
/// <summary>
342+
/// Number of rows returned by a SELECT statement
343+
/// </summary>
344+
public virtual SqlExpression<T> Take(int? take = null)
345+
{
346+
Rows = take;
347+
return this;
348+
}
332349

333350
/// <summary>
334351
/// Set the specified offset and rows for SQL Limit clause.
@@ -341,12 +358,13 @@ private void BuildOrderByClauseInternal()
341358
/// </param>
342359
public virtual SqlExpression<T> Limit(int skip, int rows)
343360
{
344-
Skip = skip;
361+
Offset = skip;
345362
Rows = rows;
346363
return this;
347364
}
365+
348366
/// <summary>
349-
/// Set the specified offset and rows for SQL Limit clause where they're not null.
367+
/// Set the specified offset and rows for SQL Limit clause where they exist.
350368
/// </summary>
351369
/// <param name='skip'>
352370
/// Offset of the first row to return. The offset of the initial row is 0
@@ -356,10 +374,8 @@ public virtual SqlExpression<T> Limit(int skip, int rows)
356374
/// </param>
357375
public virtual SqlExpression<T> Limit(int? skip, int? rows)
358376
{
359-
if (skip != null)
360-
Skip = skip;
361-
if (rows != null)
362-
Rows = rows;
377+
Offset = skip ?? 0;
378+
Rows = rows;
363379
return this;
364380
}
365381

@@ -371,8 +387,8 @@ public virtual SqlExpression<T> Limit(int? skip, int? rows)
371387
/// </param>
372388
public virtual SqlExpression<T> Limit(int rows)
373389
{
390+
Offset = 0;
374391
Rows = rows;
375-
Skip = 0;
376392
return this;
377393
}
378394

@@ -381,7 +397,7 @@ public virtual SqlExpression<T> Limit(int rows)
381397
/// </summary>
382398
public virtual SqlExpression<T> Limit()
383399
{
384-
Skip = null;
400+
Offset = null;
385401
Rows = null;
386402
return this;
387403
}
@@ -602,7 +618,7 @@ public virtual string LimitExpression
602618
{
603619
get
604620
{
605-
if (!Skip.HasValue) return "";
621+
if (!Offset.HasValue) return "";
606622
string rows;
607623
if (Rows.HasValue)
608624
{
@@ -612,12 +628,12 @@ public virtual string LimitExpression
612628
{
613629
rows = string.Empty;
614630
}
615-
return string.Format("LIMIT {0}{1}", Skip.Value, rows);
631+
return string.Format("LIMIT {0}{1}", Offset.Value, rows);
616632
}
617633
}
618634

619635
public int? Rows { get; set; }
620-
public int? Skip { get; set; }
636+
public int? Offset { get; set; }
621637

622638
public IList<string> UpdateFields
623639
{

0 commit comments

Comments
 (0)