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

Commit 4831496

Browse files
committed
Add OrderBy Random in OrderByFields
1 parent 71db32b commit 4831496

File tree

10 files changed

+19
-21
lines changed

10 files changed

+19
-21
lines changed

src/ServiceStack.OrmLite.Oracle/OracleOrmLiteDialectProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,8 @@ public override string ToRowCountStatement(string innerSql)
11291129
}
11301130

11311131
public override string SqlConcat(IEnumerable<object> args) => string.Join(" || ", args);
1132+
1133+
public override string SqlRandom => "dbms_random.value";
11321134

11331135
protected OracleConnection Unwrap(IDbConnection db)
11341136
{

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,8 @@ public override string SqlCurrency(string fieldOrValue, string currencySymbol) =
706706
public override string SqlCast(object fieldOrValue, string castAs) =>
707707
$"({fieldOrValue})::{castAs}";
708708

709+
public override string SqlRandom => "RANDOM()";
710+
709711
protected NpgsqlConnection Unwrap(IDbConnection db)
710712
{
711713
return (NpgsqlConnection)db.ToDbConnection();

src/ServiceStack.OrmLite.PostgreSQL/PostgreSqlExpression.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ protected override string GetQuotedColumnName(ModelDefinition tableDef, string m
1919
}
2020
return memberName;
2121
}
22-
23-
public override SqlExpression<T> OrderByRandom()
24-
{
25-
return base.OrderBy("RANDOM()");
26-
}
2722
}
2823

2924
}

src/ServiceStack.OrmLite.SqlServer/SqlServerExpression.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ public override string GetSubstringSql(object quotedColumn, int startIndex, int?
2222
: $"substring({quotedColumn}, {startIndex}, LEN({quotedColumn}) - {startIndex} + 1)";
2323
}
2424

25-
public override SqlExpression<T> OrderByRandom()
26-
{
27-
return base.OrderBy("NEWID()");
28-
}
29-
3025
protected override PartialSqlString ToLengthPartialString(object arg)
3126
{
3227
return new PartialSqlString($"LEN({arg})");

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@ public override string SqlCast(object fieldOrValue, string castAs) =>
648648
? $"CAST({fieldOrValue} AS VARCHAR(MAX))"
649649
: $"CAST({fieldOrValue} AS {castAs})";
650650

651+
public override string SqlRandom => "NEWID()";
652+
651653
protected SqlConnection Unwrap(IDbConnection db) => (SqlConnection)db.ToDbConnection();
652654

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

src/ServiceStack.OrmLite.Sqlite/SqliteExpression.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ protected override object VisitSqlMethodCall(MethodCallExpression m)
7070
return new PartialSqlString(statement);
7171
}
7272

73-
public override SqlExpression<T> OrderByRandom()
74-
{
75-
return base.OrderBy("random()");
76-
}
77-
7873
protected override PartialSqlString ToLengthPartialString(object arg)
7974
{
8075
return new PartialSqlString($"LENGTH({arg})");

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProviderBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ public override string SqlConflict(string sql, string conflictResolution)
243243
public override string SqlCurrency(string fieldOrValue, string currencySymbol) => SqlConcat(new []{ "'" + currencySymbol + "'", "printf(\"%.2f\", " + fieldOrValue + ")" });
244244

245245
public override string SqlBool(bool value) => value ? "1" : "0";
246+
247+
public override string SqlRandom => "random()";
246248
}
247249

248250
public static class SqliteExtensions

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ public virtual SqlExpression<T> UnsafeOrderBy(string orderBy)
795795

796796
public virtual SqlExpression<T> OrderByRandom()
797797
{
798-
return OrderBy("RAND()");
798+
return OrderBy(DialectProvider.SqlRandom);
799799
}
800800

801801
public ModelDefinition GetModelDefinition(FieldDefinition fieldDef)
@@ -875,9 +875,11 @@ private SqlExpression<T> OrderByFields(string orderBySuffix, string[] fieldNames
875875
var useName = reverse ? fieldName.Substring(1) : fieldName;
876876

877877
var field = FirstMatchingField(useName);
878-
if (field == null)
879-
throw new ArgumentException("Could not find field " + useName);
880-
var qualifiedName = GetQuotedColumnName(field.Item1, field.Item2.Name);
878+
var qualifiedName = field != null
879+
? GetQuotedColumnName(field.Item1, field.Item2.Name)
880+
: string.Equals("Random", useName)
881+
? DialectProvider.SqlRandom
882+
: throw new ArgumentException("Could not find field " + useName);
881883

882884
if (sbOrderBy.Length > 0)
883885
sbOrderBy.Append(", ");

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ string ToCreateIndexStatement<T>(Expression<Func<T, object>> field,
239239
/// <summary>
240240
/// Return table, row count SQL for listing all tables with their row counts
241241
/// </summary>
242-
/// <param name="live">If true returns live current rowc ounts of each table (slower), otherwise returns cached row counts from RDBMS table stats</param>
242+
/// <param name="live">If true returns live current row counts of each table (slower), otherwise returns cached row counts from RDBMS table stats</param>
243243
/// <param name="schema">The table schema if any</param>
244244
/// <returns></returns>
245245
string ToTableNamesWithRowCountsStatement(bool live, string schema);
@@ -252,5 +252,6 @@ string ToCreateIndexStatement<T>(Expression<Func<T, object>> field,
252252
string SqlBool(bool value);
253253
string SqlLimit(int? offset = null, int? rows = null);
254254
string SqlCast(object fieldOrValue, string castAs);
255+
string SqlRandom { get; }
255256
}
256257
}

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,9 @@ public virtual string SqlLimit(int? offset = null, int? rows = null) => rows ==
16681668

16691669
public virtual string SqlCast(object fieldOrValue, string castAs) => $"CAST({fieldOrValue} AS {castAs})";
16701670

1671-
//Async API's, should be overrided by Dialect Providers to use .ConfigureAwait(false)
1671+
public virtual string SqlRandom => "RAND()";
1672+
1673+
//Async API's, should be overriden by Dialect Providers to use .ConfigureAwait(false)
16721674
//Default impl below uses TaskAwaiter shim in async.cs
16731675

16741676
public virtual Task OpenAsync(IDbConnection db, CancellationToken token = default)

0 commit comments

Comments
 (0)