Add arithmetic and joined-alias helpers to typed SQL DSL
This commit is contained in:
@@ -202,6 +202,22 @@ namespace DynamORM.Tests.TypedSql
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSelectSqlSupportsArithmeticExpressions()
|
||||
{
|
||||
var cmd = Database.FromTyped<TypedFluentUser>("u")
|
||||
.SelectSql(
|
||||
u => (u.Col(x => x.Id) + Sql.Val(1)).As("plus_one"),
|
||||
u => u.Col(x => x.Id).Sub(2).As("minus_two"),
|
||||
u => u.Col(x => x.Id).Mul(3).As("times_three"),
|
||||
u => u.Col(x => x.Id).Div(4).As("div_four"),
|
||||
u => u.Col(x => x.Id).Mod(5).As("mod_five"));
|
||||
|
||||
Assert.AreEqual(
|
||||
"SELECT (u.\"id_user\" + [$0]) AS \"plus_one\", (u.\"id_user\" - [$1]) AS \"minus_two\", (u.\"id_user\" * [$2]) AS \"times_three\", (u.\"id_user\" / [$3]) AS \"div_four\", (u.\"id_user\" % [$4]) AS \"mod_five\" FROM \"sample_users\" AS u",
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSelectSqlSupportsCustomFunction()
|
||||
{
|
||||
@@ -224,6 +240,36 @@ namespace DynamORM.Tests.TypedSql
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSelectSqlSupportsJoinedAliasHelpers()
|
||||
{
|
||||
var other = Sql.Table<TypedFluentUser>("x");
|
||||
var cmd = Database.FromTyped<TypedFluentUser>("u")
|
||||
.Join<TypedFluentUser>(j => j.Left().As("x").OnSql((l, r) => l.Col(a => a.Id).Eq(r.Col(a => a.Id))))
|
||||
.SelectSql(
|
||||
u => u.All(),
|
||||
u => other.All(),
|
||||
u => other.Col(x => x.Code).As("joined_code"));
|
||||
|
||||
Assert.AreEqual(
|
||||
"SELECT u.*, x.*, x.\"user_code\" AS \"joined_code\" FROM \"sample_users\" AS u LEFT JOIN \"sample_users\" AS x ON (u.\"id_user\" = x.\"id_user\")",
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestWhereSqlSupportsJoinedAliasHelpers()
|
||||
{
|
||||
var other = Sql.Table<TypedFluentUser>("x");
|
||||
var cmd = Database.FromTyped<TypedFluentUser>("u")
|
||||
.Join<TypedFluentUser>(j => j.Left().As("x").OnSql((l, r) => l.Col(a => a.Id).Eq(r.Col(a => a.Id))))
|
||||
.WhereSql(u => other.Col(x => x.Code).IsNotNull())
|
||||
.SelectSql(u => u.Col(x => x.Id));
|
||||
|
||||
Assert.AreEqual(
|
||||
"SELECT u.\"id_user\" FROM \"sample_users\" AS u LEFT JOIN \"sample_users\" AS x ON (u.\"id_user\" = x.\"id_user\") WHERE (x.\"user_code\" IS NOT NULL)",
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSelectSqlSupportsScalarSubQuery()
|
||||
{
|
||||
@@ -271,6 +317,20 @@ namespace DynamORM.Tests.TypedSql
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestOrderBySqlSupportsNullOrderingAndRawFragments()
|
||||
{
|
||||
var cmd = Database.FromTyped<TypedFluentUser>("u")
|
||||
.SelectSql(u => u.Col(x => x.Id))
|
||||
.OrderBySql(
|
||||
u => u.Col(x => x.Code).Asc().NullsLast(),
|
||||
u => Sql.RawOrder("2 DESC"));
|
||||
|
||||
Assert.AreEqual(
|
||||
"SELECT u.\"id_user\" FROM \"sample_users\" AS u ORDER BY u.\"user_code\" ASC NULLS LAST, 2 DESC",
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestWhereSqlSupportsTypedExistsHelper()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user