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

Commit

Permalink
Refactor out and add SqlLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Aug 12, 2017
1 parent 87563f7 commit e4bca5d
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public override string ToSelectStatement(ModelDefinition modelDef,
sb.Append(" ORDER BY " + orderBy);
}

sb.Append(" OFFSET ").Append(offset.GetValueOrDefault()).Append(" ROWS");

if (rows != null)
sb.Append(" FETCH NEXT ").Append(rows.Value).Append(" ROWS ONLY");
sb.Append(" ").Append(SqlLimit(offset, rows));
}

return StringBuilderCache.ReturnAndFree(sb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ public override string SqlCurrency(string fieldOrValue, string currencySymbol) =

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

public override string SqlLimit(int? offset = null, int? rows = null) => rows == null && offset == null
? ""
: rows != null
? "OFFSET " + offset.GetValueOrDefault() + " ROWS FETCH NEXT " + rows
: "OFFSET " + offset.GetValueOrDefault(int.MaxValue) + " ROWS";

protected SqlConnection Unwrap(IDbConnection db) => (SqlConnection)db.ToDbConnection();

protected SqlCommand Unwrap(IDbCommand cmd) => (SqlCommand)cmd.ToDbCommand();
Expand Down
1 change: 1 addition & 0 deletions src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,6 @@ string ToCreateIndexStatement<T>(Expression<Func<T, object>> field,
string SqlCurrency(string fieldOrValue);
string SqlCurrency(string fieldOrValue, string currencySymbol);
string SqlBool(bool value);
string SqlLimit(int? offset = null, int? rows = null);
}
}
17 changes: 8 additions & 9 deletions src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,15 +542,8 @@ public virtual string ToSelectStatement(ModelDefinition modelDef,

if (offset != null || rows != null)
{
sb.Append("\nLIMIT ");
if (offset == null)
{
sb.Append(rows);
}
else
{
sb.Append(rows.GetValueOrDefault(int.MaxValue)).Append(" OFFSET ").Append(offset);
}
sb.Append("\n");
sb.Append(SqlLimit(offset, rows));
}

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

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

public virtual string SqlLimit(int? offset = null, int? rows = null) => rows == null && offset == null
? ""
: offset == null
? "LIMIT " + rows
: "LIMIT " + rows.GetValueOrDefault(int.MaxValue) + " OFFSET " + offset;

//Async API's, should be overrided by Dialect Providers to use .ConfigureAwait(false)
//Default impl below uses TaskAwaiter shim in async.cs

Expand Down
2 changes: 1 addition & 1 deletion tests/ServiceStack.OrmLite.Tests/AutoQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public void Can_group_by_multiple_columns()

var q = db.From<Rockstar>()
.Join<RockstarAlbum>()
.GroupBy<Rockstar, RockstarAlbum>((r, a) => new { r.Id, AlbumId = a.Id })
.GroupBy<Rockstar, RockstarAlbum>((r, a) => new { r.Id, AlbumId = a.Id, r.FirstName, r.LastName, a.Name })
.Select<Rockstar, RockstarAlbum>((r,a) => new
{
r.Id,
Expand Down
2 changes: 1 addition & 1 deletion tests/ServiceStack.OrmLite.Tests/DbExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Can_get_TableName()
var quotedTable1 = db.GetQuotedTableName<Table1>();

Assert.That(table1.ToLower(), Is.EqualTo("table1"));
Assert.That(quotedTable1.ToLower(), Is.EqualTo("\"table1\""));
Assert.That(quotedTable1.ToLower(), Is.EqualTo("\"table1\"").Or.EqualTo("`table1`"));

if (Dialect == Dialect.Sqlite)
{
Expand Down
6 changes: 4 additions & 2 deletions tests/ServiceStack.OrmLite.Tests/DefaultValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ private static void ResetUpdateDate(IDbConnection db)
db.Update<DefaultValues>(new { UpdatedDateUtc = updateTime }, p => p.Id == 1);
}

private static void VerifyUpdateDate(IDbConnection db, int id = 1)
private void VerifyUpdateDate(IDbConnection db, int id = 1)
{
var row = db.SingleById<DefaultValues>(id);
row.PrintDump();
Assert.That(row.UpdatedDateUtc, Is.GreaterThan(DateTime.UtcNow - TimeSpan.FromMinutes(5)));

if (Dialect != Dialect.MySql) //not returning UTC
Assert.That(row.UpdatedDateUtc, Is.GreaterThan(DateTime.UtcNow - TimeSpan.FromMinutes(5)));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SqlCustomTests : OrmLiteTestBase
[Test]
public void Can_use_CustomSelect_field_in_Typed_Query()
{
if (Dialect == Dialect.PostgreSql) return;
if (Dialect == Dialect.PostgreSql || Dialect == Dialect.SqlServer || Dialect == Dialect.MySql) return;

using (var db = OpenDbConnection())
{
Expand Down

0 comments on commit e4bca5d

Please sign in to comment.