Add arithmetic and joined-alias helpers to typed SQL DSL
This commit is contained in:
@@ -15830,11 +15830,21 @@ namespace DynamORM
|
||||
{
|
||||
return new TypedSqlValueExpression<object>(value);
|
||||
}
|
||||
/// <summary>Create typed table context for an explicitly named alias, including joined aliases.</summary>
|
||||
public static TypedTableContext<T> Table<T>(string alias)
|
||||
{
|
||||
return new TypedTableContext<T>(alias);
|
||||
}
|
||||
/// <summary>Create raw SQL expression.</summary>
|
||||
public static TypedSqlExpression<T> Raw<T>(string sql)
|
||||
{
|
||||
return new TypedSqlRawExpression<T>(sql);
|
||||
}
|
||||
/// <summary>Create raw ORDER BY fragment.</summary>
|
||||
public static TypedSqlOrderExpression RawOrder(string sql)
|
||||
{
|
||||
return new TypedSqlRawOrderExpression(Raw<object>(sql));
|
||||
}
|
||||
/// <summary>Create generic function call.</summary>
|
||||
public static TypedSqlExpression<T> Func<T>(string name, params TypedSqlExpression[] arguments)
|
||||
{
|
||||
@@ -15979,6 +15989,31 @@ namespace DynamORM
|
||||
/// <summary>Base SQL expression for the typed DSL.</summary>
|
||||
public abstract class TypedSqlExpression : TypedSqlSelectable
|
||||
{
|
||||
/// <summary>Add arithmetic expression.</summary>
|
||||
public static TypedSqlExpression<object> operator +(TypedSqlExpression left, TypedSqlExpression right)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(left, "+", right);
|
||||
}
|
||||
/// <summary>Subtract arithmetic expression.</summary>
|
||||
public static TypedSqlExpression<object> operator -(TypedSqlExpression left, TypedSqlExpression right)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(left, "-", right);
|
||||
}
|
||||
/// <summary>Multiply arithmetic expression.</summary>
|
||||
public static TypedSqlExpression<object> operator *(TypedSqlExpression left, TypedSqlExpression right)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(left, "*", right);
|
||||
}
|
||||
/// <summary>Divide arithmetic expression.</summary>
|
||||
public static TypedSqlExpression<object> operator /(TypedSqlExpression left, TypedSqlExpression right)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(left, "/", right);
|
||||
}
|
||||
/// <summary>Modulo arithmetic expression.</summary>
|
||||
public static TypedSqlExpression<object> operator %(TypedSqlExpression left, TypedSqlExpression right)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(left, "%", right);
|
||||
}
|
||||
/// <summary>Alias this expression in SELECT clause.</summary>
|
||||
public TypedSqlAliasedExpression As(string alias)
|
||||
{
|
||||
@@ -15994,6 +16029,31 @@ namespace DynamORM
|
||||
{
|
||||
return new TypedSqlOrderExpression(this, false);
|
||||
}
|
||||
/// <summary>Add expression to another value.</summary>
|
||||
public TypedSqlExpression<object> Add(object value)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(this, "+", value as TypedSqlExpression ?? Sql.Val(value));
|
||||
}
|
||||
/// <summary>Subtract another value.</summary>
|
||||
public TypedSqlExpression<object> Sub(object value)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(this, "-", value as TypedSqlExpression ?? Sql.Val(value));
|
||||
}
|
||||
/// <summary>Multiply by another value.</summary>
|
||||
public TypedSqlExpression<object> Mul(object value)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(this, "*", value as TypedSqlExpression ?? Sql.Val(value));
|
||||
}
|
||||
/// <summary>Divide by another value.</summary>
|
||||
public TypedSqlExpression<object> Div(object value)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(this, "/", value as TypedSqlExpression ?? Sql.Val(value));
|
||||
}
|
||||
/// <summary>Modulo by another value.</summary>
|
||||
public TypedSqlExpression<object> Mod(object value)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(this, "%", value as TypedSqlExpression ?? Sql.Val(value));
|
||||
}
|
||||
/// <summary>Equality predicate.</summary>
|
||||
public TypedSqlPredicate Eq(object value)
|
||||
{
|
||||
@@ -16120,19 +16180,63 @@ namespace DynamORM
|
||||
}
|
||||
}
|
||||
/// <summary>Ordered SQL expression.</summary>
|
||||
public sealed class TypedSqlOrderExpression : TypedSqlSelectable
|
||||
public class TypedSqlOrderExpression : TypedSqlSelectable
|
||||
{
|
||||
private readonly TypedSqlExpression _expression;
|
||||
private readonly bool _ascending;
|
||||
private readonly string _nullOrdering;
|
||||
private readonly bool _raw;
|
||||
|
||||
internal TypedSqlOrderExpression(TypedSqlExpression expression, bool ascending)
|
||||
internal TypedSqlOrderExpression(TypedSqlExpression expression, bool ascending, string nullOrdering = null, bool raw = false)
|
||||
{
|
||||
_expression = expression;
|
||||
_ascending = ascending;
|
||||
_nullOrdering = nullOrdering;
|
||||
_raw = raw;
|
||||
}
|
||||
/// <summary>Append NULLS FIRST ordering.</summary>
|
||||
public TypedSqlOrderExpression NullsFirst()
|
||||
{
|
||||
return new TypedSqlOrderExpression(_expression, _ascending, "NULLS FIRST", _raw);
|
||||
}
|
||||
/// <summary>Append NULLS LAST ordering.</summary>
|
||||
public TypedSqlOrderExpression NullsLast()
|
||||
{
|
||||
return new TypedSqlOrderExpression(_expression, _ascending, "NULLS LAST", _raw);
|
||||
}
|
||||
internal override string Render(ITypedSqlRenderContext context)
|
||||
{
|
||||
return string.Format("{0} {1}", _expression.Render(context), _ascending ? "ASC" : "DESC");
|
||||
string rendered = _raw
|
||||
? _expression.Render(context)
|
||||
: string.Format("{0} {1}", _expression.Render(context), _ascending ? "ASC" : "DESC");
|
||||
if (!string.IsNullOrEmpty(_nullOrdering))
|
||||
rendered = string.Format("{0} {1}", rendered, _nullOrdering);
|
||||
|
||||
return rendered;
|
||||
}
|
||||
}
|
||||
internal sealed class TypedSqlRawOrderExpression : TypedSqlOrderExpression
|
||||
{
|
||||
internal TypedSqlRawOrderExpression(TypedSqlExpression expression)
|
||||
: base(expression, true, null, true)
|
||||
{
|
||||
}
|
||||
}
|
||||
internal sealed class TypedSqlBinaryExpression : TypedSqlExpression<object>
|
||||
{
|
||||
private readonly TypedSqlExpression _left;
|
||||
private readonly string _operator;
|
||||
private readonly TypedSqlExpression _right;
|
||||
|
||||
internal TypedSqlBinaryExpression(TypedSqlExpression left, string op, TypedSqlExpression right)
|
||||
{
|
||||
_left = left;
|
||||
_operator = op;
|
||||
_right = right;
|
||||
}
|
||||
internal override string Render(ITypedSqlRenderContext context)
|
||||
{
|
||||
return string.Format("({0} {1} {2})", _left.Render(context), _operator, _right.Render(context));
|
||||
}
|
||||
}
|
||||
internal sealed class TypedSqlColumnExpression<T> : TypedSqlExpression<T>
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -25,12 +25,24 @@ namespace DynamORM.TypedSql
|
||||
return new TypedSqlValueExpression<object>(value);
|
||||
}
|
||||
|
||||
/// <summary>Create typed table context for an explicitly named alias, including joined aliases.</summary>
|
||||
public static TypedTableContext<T> Table<T>(string alias)
|
||||
{
|
||||
return new TypedTableContext<T>(alias);
|
||||
}
|
||||
|
||||
/// <summary>Create raw SQL expression.</summary>
|
||||
public static TypedSqlExpression<T> Raw<T>(string sql)
|
||||
{
|
||||
return new TypedSqlRawExpression<T>(sql);
|
||||
}
|
||||
|
||||
/// <summary>Create raw ORDER BY fragment.</summary>
|
||||
public static TypedSqlOrderExpression RawOrder(string sql)
|
||||
{
|
||||
return new TypedSqlRawOrderExpression(Raw<object>(sql));
|
||||
}
|
||||
|
||||
/// <summary>Create generic function call.</summary>
|
||||
public static TypedSqlExpression<T> Func<T>(string name, params TypedSqlExpression[] arguments)
|
||||
{
|
||||
|
||||
@@ -20,6 +20,36 @@ namespace DynamORM.TypedSql
|
||||
/// <summary>Base SQL expression for the typed DSL.</summary>
|
||||
public abstract class TypedSqlExpression : TypedSqlSelectable
|
||||
{
|
||||
/// <summary>Add arithmetic expression.</summary>
|
||||
public static TypedSqlExpression<object> operator +(TypedSqlExpression left, TypedSqlExpression right)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(left, "+", right);
|
||||
}
|
||||
|
||||
/// <summary>Subtract arithmetic expression.</summary>
|
||||
public static TypedSqlExpression<object> operator -(TypedSqlExpression left, TypedSqlExpression right)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(left, "-", right);
|
||||
}
|
||||
|
||||
/// <summary>Multiply arithmetic expression.</summary>
|
||||
public static TypedSqlExpression<object> operator *(TypedSqlExpression left, TypedSqlExpression right)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(left, "*", right);
|
||||
}
|
||||
|
||||
/// <summary>Divide arithmetic expression.</summary>
|
||||
public static TypedSqlExpression<object> operator /(TypedSqlExpression left, TypedSqlExpression right)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(left, "/", right);
|
||||
}
|
||||
|
||||
/// <summary>Modulo arithmetic expression.</summary>
|
||||
public static TypedSqlExpression<object> operator %(TypedSqlExpression left, TypedSqlExpression right)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(left, "%", right);
|
||||
}
|
||||
|
||||
/// <summary>Alias this expression in SELECT clause.</summary>
|
||||
public TypedSqlAliasedExpression As(string alias)
|
||||
{
|
||||
@@ -38,6 +68,36 @@ namespace DynamORM.TypedSql
|
||||
return new TypedSqlOrderExpression(this, false);
|
||||
}
|
||||
|
||||
/// <summary>Add expression to another value.</summary>
|
||||
public TypedSqlExpression<object> Add(object value)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(this, "+", value as TypedSqlExpression ?? Sql.Val(value));
|
||||
}
|
||||
|
||||
/// <summary>Subtract another value.</summary>
|
||||
public TypedSqlExpression<object> Sub(object value)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(this, "-", value as TypedSqlExpression ?? Sql.Val(value));
|
||||
}
|
||||
|
||||
/// <summary>Multiply by another value.</summary>
|
||||
public TypedSqlExpression<object> Mul(object value)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(this, "*", value as TypedSqlExpression ?? Sql.Val(value));
|
||||
}
|
||||
|
||||
/// <summary>Divide by another value.</summary>
|
||||
public TypedSqlExpression<object> Div(object value)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(this, "/", value as TypedSqlExpression ?? Sql.Val(value));
|
||||
}
|
||||
|
||||
/// <summary>Modulo by another value.</summary>
|
||||
public TypedSqlExpression<object> Mod(object value)
|
||||
{
|
||||
return new TypedSqlBinaryExpression(this, "%", value as TypedSqlExpression ?? Sql.Val(value));
|
||||
}
|
||||
|
||||
/// <summary>Equality predicate.</summary>
|
||||
public TypedSqlPredicate Eq(object value)
|
||||
{
|
||||
@@ -187,20 +247,69 @@ namespace DynamORM.TypedSql
|
||||
}
|
||||
|
||||
/// <summary>Ordered SQL expression.</summary>
|
||||
public sealed class TypedSqlOrderExpression : TypedSqlSelectable
|
||||
public class TypedSqlOrderExpression : TypedSqlSelectable
|
||||
{
|
||||
private readonly TypedSqlExpression _expression;
|
||||
private readonly bool _ascending;
|
||||
private readonly string _nullOrdering;
|
||||
private readonly bool _raw;
|
||||
|
||||
internal TypedSqlOrderExpression(TypedSqlExpression expression, bool ascending)
|
||||
internal TypedSqlOrderExpression(TypedSqlExpression expression, bool ascending, string nullOrdering = null, bool raw = false)
|
||||
{
|
||||
_expression = expression;
|
||||
_ascending = ascending;
|
||||
_nullOrdering = nullOrdering;
|
||||
_raw = raw;
|
||||
}
|
||||
|
||||
/// <summary>Append NULLS FIRST ordering.</summary>
|
||||
public TypedSqlOrderExpression NullsFirst()
|
||||
{
|
||||
return new TypedSqlOrderExpression(_expression, _ascending, "NULLS FIRST", _raw);
|
||||
}
|
||||
|
||||
/// <summary>Append NULLS LAST ordering.</summary>
|
||||
public TypedSqlOrderExpression NullsLast()
|
||||
{
|
||||
return new TypedSqlOrderExpression(_expression, _ascending, "NULLS LAST", _raw);
|
||||
}
|
||||
|
||||
internal override string Render(ITypedSqlRenderContext context)
|
||||
{
|
||||
return string.Format("{0} {1}", _expression.Render(context), _ascending ? "ASC" : "DESC");
|
||||
string rendered = _raw
|
||||
? _expression.Render(context)
|
||||
: string.Format("{0} {1}", _expression.Render(context), _ascending ? "ASC" : "DESC");
|
||||
if (!string.IsNullOrEmpty(_nullOrdering))
|
||||
rendered = string.Format("{0} {1}", rendered, _nullOrdering);
|
||||
|
||||
return rendered;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class TypedSqlRawOrderExpression : TypedSqlOrderExpression
|
||||
{
|
||||
internal TypedSqlRawOrderExpression(TypedSqlExpression expression)
|
||||
: base(expression, true, null, true)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class TypedSqlBinaryExpression : TypedSqlExpression<object>
|
||||
{
|
||||
private readonly TypedSqlExpression _left;
|
||||
private readonly string _operator;
|
||||
private readonly TypedSqlExpression _right;
|
||||
|
||||
internal TypedSqlBinaryExpression(TypedSqlExpression left, string op, TypedSqlExpression right)
|
||||
{
|
||||
_left = left;
|
||||
_operator = op;
|
||||
_right = right;
|
||||
}
|
||||
|
||||
internal override string Render(ITypedSqlRenderContext context)
|
||||
{
|
||||
return string.Format("({0} {1} {2})", _left.Render(context), _operator, _right.Render(context));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user