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

Commit e4bca5d

Browse files
committed
Refactor out and add SqlLimit
1 parent 87563f7 commit e4bca5d

File tree

8 files changed

+23
-18
lines changed

8 files changed

+23
-18
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServer2012OrmLiteDialectProvider.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public override string ToSelectStatement(ModelDefinition modelDef,
3535
sb.Append(" ORDER BY " + orderBy);
3636
}
3737

38-
sb.Append(" OFFSET ").Append(offset.GetValueOrDefault()).Append(" ROWS");
39-
40-
if (rows != null)
41-
sb.Append(" FETCH NEXT ").Append(rows.Value).Append(" ROWS ONLY");
38+
sb.Append(" ").Append(SqlLimit(offset, rows));
4239
}
4340

4441
return StringBuilderCache.ReturnAndFree(sb);

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ public override string SqlCurrency(string fieldOrValue, string currencySymbol) =
360360

361361
public override string SqlBool(bool value) => value ? "1" : "0";
362362

363+
public override string SqlLimit(int? offset = null, int? rows = null) => rows == null && offset == null
364+
? ""
365+
: rows != null
366+
? "OFFSET " + offset.GetValueOrDefault() + " ROWS FETCH NEXT " + rows
367+
: "OFFSET " + offset.GetValueOrDefault(int.MaxValue) + " ROWS";
368+
363369
protected SqlConnection Unwrap(IDbConnection db) => (SqlConnection)db.ToDbConnection();
364370

365371
protected SqlCommand Unwrap(IDbCommand cmd) => (SqlCommand)cmd.ToDbCommand();

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,6 @@ string ToCreateIndexStatement<T>(Expression<Func<T, object>> field,
216216
string SqlCurrency(string fieldOrValue);
217217
string SqlCurrency(string fieldOrValue, string currencySymbol);
218218
string SqlBool(bool value);
219+
string SqlLimit(int? offset = null, int? rows = null);
219220
}
220221
}

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,15 +542,8 @@ public virtual string ToSelectStatement(ModelDefinition modelDef,
542542

543543
if (offset != null || rows != null)
544544
{
545-
sb.Append("\nLIMIT ");
546-
if (offset == null)
547-
{
548-
sb.Append(rows);
549-
}
550-
else
551-
{
552-
sb.Append(rows.GetValueOrDefault(int.MaxValue)).Append(" OFFSET ").Append(offset);
553-
}
545+
sb.Append("\n");
546+
sb.Append(SqlLimit(offset, rows));
554547
}
555548

556549
return StringBuilderCache.ReturnAndFree(sb);
@@ -1590,6 +1583,12 @@ protected virtual string ToDropColumnStatement(Type modelType, string columnName
15901583

15911584
public virtual string SqlBool(bool value) => value ? "true" : "false";
15921585

1586+
public virtual string SqlLimit(int? offset = null, int? rows = null) => rows == null && offset == null
1587+
? ""
1588+
: offset == null
1589+
? "LIMIT " + rows
1590+
: "LIMIT " + rows.GetValueOrDefault(int.MaxValue) + " OFFSET " + offset;
1591+
15931592
//Async API's, should be overrided by Dialect Providers to use .ConfigureAwait(false)
15941593
//Default impl below uses TaskAwaiter shim in async.cs
15951594

tests/ServiceStack.OrmLite.Tests/AutoQueryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ public void Can_group_by_multiple_columns()
447447

448448
var q = db.From<Rockstar>()
449449
.Join<RockstarAlbum>()
450-
.GroupBy<Rockstar, RockstarAlbum>((r, a) => new { r.Id, AlbumId = a.Id })
450+
.GroupBy<Rockstar, RockstarAlbum>((r, a) => new { r.Id, AlbumId = a.Id, r.FirstName, r.LastName, a.Name })
451451
.Select<Rockstar, RockstarAlbum>((r,a) => new
452452
{
453453
r.Id,

tests/ServiceStack.OrmLite.Tests/DbExtensionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void Can_get_TableName()
1616
var quotedTable1 = db.GetQuotedTableName<Table1>();
1717

1818
Assert.That(table1.ToLower(), Is.EqualTo("table1"));
19-
Assert.That(quotedTable1.ToLower(), Is.EqualTo("\"table1\""));
19+
Assert.That(quotedTable1.ToLower(), Is.EqualTo("\"table1\"").Or.EqualTo("`table1`"));
2020

2121
if (Dialect == Dialect.Sqlite)
2222
{

tests/ServiceStack.OrmLite.Tests/DefaultValueTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ private static void ResetUpdateDate(IDbConnection db)
122122
db.Update<DefaultValues>(new { UpdatedDateUtc = updateTime }, p => p.Id == 1);
123123
}
124124

125-
private static void VerifyUpdateDate(IDbConnection db, int id = 1)
125+
private void VerifyUpdateDate(IDbConnection db, int id = 1)
126126
{
127127
var row = db.SingleById<DefaultValues>(id);
128128
row.PrintDump();
129-
Assert.That(row.UpdatedDateUtc, Is.GreaterThan(DateTime.UtcNow - TimeSpan.FromMinutes(5)));
129+
130+
if (Dialect != Dialect.MySql) //not returning UTC
131+
Assert.That(row.UpdatedDateUtc, Is.GreaterThan(DateTime.UtcNow - TimeSpan.FromMinutes(5)));
130132
}
131133

132134
[Test]

tests/ServiceStack.OrmLite.Tests/Expression/SqlCustomTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SqlCustomTests : OrmLiteTestBase
3535
[Test]
3636
public void Can_use_CustomSelect_field_in_Typed_Query()
3737
{
38-
if (Dialect == Dialect.PostgreSql) return;
38+
if (Dialect == Dialect.PostgreSql || Dialect == Dialect.SqlServer || Dialect == Dialect.MySql) return;
3939

4040
using (var db = OpenDbConnection())
4141
{

0 commit comments

Comments
 (0)