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

Commit b6ec56b

Browse files
committed
Add support for x-plat Sql.Cast()
1 parent e5ab084 commit b6ec56b

File tree

9 files changed

+49
-2
lines changed

9 files changed

+49
-2
lines changed

src/ServiceStack.OrmLite.MySql/MySqlDialectProviderBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ public override string SqlConflict(string sql, string conflictResolution)
216216
public override string SqlCurrency(string fieldOrValue, string currencySymbol) =>
217217
SqlConcat(new[] { "'" + currencySymbol + "'", "cast(" + fieldOrValue + " as decimal(15,2))" });
218218

219+
public override string SqlCast(object fieldOrValue, string castAs) =>
220+
castAs == Sql.VARCHAR
221+
? $"CAST({fieldOrValue} AS CHAR(1000))"
222+
: $"CAST({fieldOrValue} AS {castAs})";
223+
219224
protected DbConnection Unwrap(IDbConnection db)
220225
{
221226
return (DbConnection)db.ToDbConnection();

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ public override string SqlCurrency(string fieldOrValue, string currencySymbol) =
559559
? fieldOrValue + "::text::money::text"
560560
: "replace(" + fieldOrValue + "::text::money::text,'$','" + currencySymbol + "')";
561561

562+
public override string SqlCast(object fieldOrValue, string castAs) =>
563+
$"{fieldOrValue}::{castAs}";
564+
562565
protected NpgsqlConnection Unwrap(IDbConnection db)
563566
{
564567
return (NpgsqlConnection)db.ToDbConnection();

src/ServiceStack.OrmLite.SqlServer/SqlServer2016Expression.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ protected override object VisitSqlMethodCall(MethodCallExpression m)
2828
case nameof(Sql.As):
2929
statement = $"{quotedColName} AS {DialectProvider.GetQuotedColumnName(RemoveQuoteFromAlias(args[0].ToString()))}";
3030
break;
31+
case nameof(Sql.Cast):
32+
statement = DialectProvider.SqlCast(quotedColName, args[0].ToString());
33+
break;
3134
case nameof(Sql.Sum):
3235
case nameof(Sql.Count):
3336
case nameof(Sql.Min):

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,11 @@ public override string SqlLimit(int? offset = null, int? rows = null) => rows ==
556556
? "OFFSET " + offset.GetValueOrDefault() + " ROWS FETCH NEXT " + rows + " ROWS ONLY"
557557
: "OFFSET " + offset.GetValueOrDefault(int.MaxValue) + " ROWS";
558558

559+
public override string SqlCast(object fieldOrValue, string castAs) =>
560+
castAs == Sql.VARCHAR
561+
? $"CAST({fieldOrValue} AS VARCHAR(MAX))"
562+
: $"CAST({fieldOrValue} AS {castAs})";
563+
559564
protected SqlConnection Unwrap(IDbConnection db) => (SqlConnection)db.ToDbConnection();
560565

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

src/ServiceStack.OrmLite/Expressions/Sql.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace ServiceStack.OrmLite
77
{
88
public static partial class Sql
99
{
10+
public static string VARCHAR = nameof(VARCHAR);
11+
1012
public static List<object> Flatten(IEnumerable list)
1113
{
1214
var ret = new List<object>();
@@ -68,6 +70,8 @@ public static List<object> Flatten(IEnumerable list)
6870

6971
public static T Custom<T>(string customSql) => default(T);
7072

73+
public static string Cast(object value, string castAs) => $"CAST({value} AS {castAs})";
74+
7175
public const string EOT= "0 EOT";
7276
}
7377

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,6 +2351,9 @@ protected virtual object VisitSqlMethodCall(MethodCallExpression m)
23512351
case nameof(Sql.As):
23522352
statement = $"{quotedColName} AS {DialectProvider.GetQuotedColumnName(RemoveQuoteFromAlias(args[0].ToString()))}";
23532353
break;
2354+
case nameof(Sql.Cast):
2355+
statement = DialectProvider.SqlCast(quotedColName, args[0].ToString());
2356+
break;
23542357
case nameof(Sql.Sum):
23552358
case nameof(Sql.Count):
23562359
case nameof(Sql.Min):

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,6 @@ string ToCreateIndexStatement<T>(Expression<Func<T, object>> field,
220220
string SqlCurrency(string fieldOrValue, string currencySymbol);
221221
string SqlBool(bool value);
222222
string SqlLimit(int? offset = null, int? rows = null);
223+
string SqlCast(object fieldOrValue, string castAs);
223224
}
224225
}

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,8 @@ public virtual string SqlLimit(int? offset = null, int? rows = null) => rows ==
15951595
: offset == null
15961596
? "LIMIT " + rows
15971597
: "LIMIT " + rows.GetValueOrDefault(int.MaxValue) + " OFFSET " + offset;
1598+
1599+
public virtual string SqlCast(object fieldOrValue, string castAs) => $"CAST({fieldOrValue} AS {castAs})";
15981600

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

tests/ServiceStack.OrmLite.Tests/SqlDialectTests.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using NUnit.Framework;
34
using ServiceStack.DataAnnotations;
45
using ServiceStack.Text;
@@ -15,7 +16,7 @@ public class Sqltest
1516

1617
public class SqlDialectTests : OrmLiteTestBase
1718
{
18-
//public SqlDialectTests() : base(Dialect.PostgreSql) {}
19+
// public SqlDialectTests() : base(Dialect.SqlServer2008) {}
1920

2021
[Test]
2122
public void Does_concat_values()
@@ -36,6 +37,26 @@ public void Does_concat_values()
3637
}
3738
}
3839

40+
[Test]
41+
public void Does_concat_values_in_SqlExpression()
42+
{
43+
using (var db = OpenDbConnection())
44+
{
45+
db.DropAndCreateTable<Sqltest>();
46+
47+
db.Insert(new Sqltest { Value = 123.456, Bool = true });
48+
49+
var results = db.Select<Dictionary<string, object>>(db.From<Sqltest>()
50+
.Select(x => new {
51+
x.Id,
52+
text = Sql.As(Sql.Cast(x.Id, Sql.VARCHAR) + " : " + Sql.Cast(x.Value, Sql.VARCHAR) + " : " + Sql.Cast(x.Bool, Sql.VARCHAR) + " string", "text")
53+
}));
54+
55+
Assert.That(results[0]["text"], Is.EqualTo("1 : 123.456 : 1 string")
56+
.Or.EqualTo("1 : 123.456 : true string"));
57+
}
58+
}
59+
3960
[Test]
4061
public void Does_format_currency()
4162
{

0 commit comments

Comments
 (0)