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

Commit 6430aad

Browse files
committed
Add x-plat SqlBool
1 parent 4644b28 commit 6430aad

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ public override string GetLoadChildrenSubSelect<From>(SqlExpression<From> expr)
358358
public override string SqlCurrency(string fieldOrValue, string currencySymbol) =>
359359
SqlConcat(new[] { "'" + currencySymbol + "'", $"CONVERT(VARCHAR, CONVERT(MONEY, {fieldOrValue}), 1)" });
360360

361+
public override string SqlBool(bool value) => value ? "1" : "0";
362+
361363
protected SqlConnection Unwrap(IDbConnection db) => (SqlConnection)db.ToDbConnection();
362364

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

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProviderBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ public override string GetColumnDefinition(FieldDefinition fieldDef)
207207
public override string SqlConcat(IEnumerable<object> args) => string.Join(" || ", args);
208208

209209
public override string SqlCurrency(string fieldOrValue, string currencySymbol) => SqlConcat(new []{ "'" + currencySymbol + "'", "printf(\"%.2f\", " + fieldOrValue + ")" });
210+
211+
public override string SqlBool(bool value) => value ? "1" : "0";
210212
}
211213

212214
public static class SqliteExtensions

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,5 +215,6 @@ string ToCreateIndexStatement<T>(Expression<Func<T, object>> field,
215215
string SqlConcat(IEnumerable<object> args);
216216
string SqlCurrency(string fieldOrValue);
217217
string SqlCurrency(string fieldOrValue, string currencySymbol);
218+
string SqlBool(bool value);
218219
}
219220
}

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,8 @@ protected virtual string ToDropColumnStatement(Type modelType, string columnName
15881588

15891589
public virtual string SqlCurrency(string fieldOrValue, string currencySymbol) => SqlConcat(new List<string> { currencySymbol, fieldOrValue });
15901590

1591+
public virtual string SqlBool(bool value) => value ? "true" : "false";
1592+
15911593
//Async API's, should be overrided by Dialect Providers to use .ConfigureAwait(false)
15921594
//Default impl below uses TaskAwaiter shim in async.cs
15931595

tests/ServiceStack.OrmLite.Tests/SqlDialectTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Sqltest
99
[AutoIncrement]
1010
public int Id { get; set; }
1111
public double Value { get; set; }
12+
public bool Bool { get; set; }
1213
}
1314

1415
public class SqlDialectTests : OrmLiteTestBase
@@ -79,5 +80,25 @@ public void Does_format_currency()
7980
}
8081
}
8182

83+
[Test]
84+
public void Does_handle_booleans()
85+
{
86+
using (var db = OpenDbConnection())
87+
{
88+
db.DropAndCreateTable<Sqltest>();
89+
90+
db.Insert(new Sqltest { Value = 0, Bool = false });
91+
db.Insert(new Sqltest { Value = 1, Bool = true });
92+
93+
var sqlBool = db.GetDialectProvider().SqlBool(false);
94+
var result = db.Scalar<double>($"SELECT Value from sqltest where Bool = {sqlBool}");
95+
Assert.That(result, Is.EqualTo(0));
96+
97+
sqlBool = db.GetDialectProvider().SqlBool(true);
98+
result = db.Scalar<double>($"SELECT Value from sqltest where Bool = {sqlBool}");
99+
Assert.That(result, Is.EqualTo(1));
100+
}
101+
}
102+
82103
}
83104
}

0 commit comments

Comments
 (0)