Add positional typed multi-table SQL contexts

This commit is contained in:
root
2026-02-27 09:12:17 +01:00
parent c0fb2e5232
commit 8210fbfdb5
5 changed files with 865 additions and 28 deletions

View File

@@ -7303,6 +7303,66 @@ namespace DynamORM
/// <summary>Add typed SQL DSL order by expressions.</summary>
IDynamicTypedSelectQueryBuilder<T> OrderBySql(Func<TypedTableContext<T>, TypedSqlOrderExpression> selector, params Func<TypedTableContext<T>, TypedSqlOrderExpression>[] selectors);
/// <summary>Add typed SQL DSL select items using root and first joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> SelectSql<TJoin1>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlSelectable> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlSelectable>[] selectors);
/// <summary>Add typed SQL DSL select items using root and first two joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> SelectSql<TJoin1, TJoin2>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlSelectable> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlSelectable>[] selectors);
/// <summary>Add typed SQL DSL select items using root and first three joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> SelectSql<TJoin1, TJoin2, TJoin3>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlSelectable> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlSelectable>[] selectors);
/// <summary>Add typed SQL DSL select items using root and first four joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> SelectSql<TJoin1, TJoin2, TJoin3, TJoin4>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlSelectable> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlSelectable>[] selectors);
/// <summary>Add typed SQL DSL where predicate using root and first joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> WhereSql<TJoin1>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlPredicate> predicate);
/// <summary>Add typed SQL DSL where predicate using root and first two joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> WhereSql<TJoin1, TJoin2>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlPredicate> predicate);
/// <summary>Add typed SQL DSL where predicate using root and first three joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> WhereSql<TJoin1, TJoin2, TJoin3>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlPredicate> predicate);
/// <summary>Add typed SQL DSL where predicate using root and first four joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> WhereSql<TJoin1, TJoin2, TJoin3, TJoin4>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlPredicate> predicate);
/// <summary>Add typed SQL DSL having predicate using root and first joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> HavingSql<TJoin1>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlPredicate> predicate);
/// <summary>Add typed SQL DSL having predicate using root and first two joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> HavingSql<TJoin1, TJoin2>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlPredicate> predicate);
/// <summary>Add typed SQL DSL having predicate using root and first three joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> HavingSql<TJoin1, TJoin2, TJoin3>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlPredicate> predicate);
/// <summary>Add typed SQL DSL having predicate using root and first four joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> HavingSql<TJoin1, TJoin2, TJoin3, TJoin4>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlPredicate> predicate);
/// <summary>Add typed SQL DSL group-by expression using root and first joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> GroupBySql<TJoin1>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlExpression>[] selectors);
/// <summary>Add typed SQL DSL group-by expression using root and first two joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> GroupBySql<TJoin1, TJoin2>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlExpression>[] selectors);
/// <summary>Add typed SQL DSL group-by expression using root and first three joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> GroupBySql<TJoin1, TJoin2, TJoin3>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlExpression>[] selectors);
/// <summary>Add typed SQL DSL group-by expression using root and first four joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> GroupBySql<TJoin1, TJoin2, TJoin3, TJoin4>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlExpression>[] selectors);
/// <summary>Add typed SQL DSL order-by expression using root and first joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> OrderBySql<TJoin1>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlOrderExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlOrderExpression>[] selectors);
/// <summary>Add typed SQL DSL order-by expression using root and first two joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> OrderBySql<TJoin1, TJoin2>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlOrderExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlOrderExpression>[] selectors);
/// <summary>Add typed SQL DSL order-by expression using root and first three joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> OrderBySql<TJoin1, TJoin2, TJoin3>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlOrderExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlOrderExpression>[] selectors);
/// <summary>Add typed SQL DSL order-by expression using root and first four joined table contexts.</summary>
IDynamicTypedSelectQueryBuilder<T> OrderBySql<TJoin1, TJoin2, TJoin3, TJoin4>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlOrderExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlOrderExpression>[] selectors);
}
/// <summary>Typed update query builder for mapped entities.</summary>
/// <typeparam name="T">Mapped entity type.</typeparam>
@@ -10869,6 +10929,11 @@ namespace DynamORM
/// <typeparam name="T">Mapped entity type.</typeparam>
internal class DynamicTypedSelectQueryBuilder<T> : DynamicSelectQueryBuilder, IDynamicTypedSelectQueryBuilder<T>
{
private sealed class TypedJoinInfo
{
public Type ModelType { get; set; }
public string Alias { get; set; }
}
private sealed class TypedSqlRenderContext : ITypedSqlRenderContext
{
private readonly DynamicTypedSelectQueryBuilder<T> _builder;
@@ -10916,6 +10981,7 @@ namespace DynamORM
}
}
private readonly DynamicTypeMap _mapper;
private readonly List<TypedJoinInfo> _typedJoins = new List<TypedJoinInfo>();
public DynamicTypedSelectQueryBuilder(DynamicDatabase db)
: base(db)
@@ -10977,6 +11043,7 @@ namespace DynamORM
if (!string.IsNullOrEmpty(condition))
joinExpr += string.Format(" ON {0}", condition);
RegisterTypedJoin(typeof(TRight), rightAlias);
AppendJoinClause(joinExpr);
return this;
}
@@ -11019,6 +11086,7 @@ namespace DynamORM
else if (!string.IsNullOrEmpty(spec.OnRawCondition))
joinExpr += string.Format(" ON {0}", spec.OnRawCondition);
RegisterTypedJoin(typeof(TRight), rightAlias);
AppendJoinClause(joinExpr);
return this;
}
@@ -11076,6 +11144,54 @@ namespace DynamORM
return this;
}
public IDynamicTypedSelectQueryBuilder<T> SelectSql<TJoin1>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlSelectable> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlSelectable>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddSelectSqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlSelectable> item in selectors)
AddSelectSqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> SelectSql<TJoin1, TJoin2>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlSelectable> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlSelectable>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddSelectSqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlSelectable> item in selectors)
AddSelectSqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> SelectSql<TJoin1, TJoin2, TJoin3>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlSelectable> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlSelectable>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddSelectSqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlSelectable> item in selectors)
AddSelectSqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> SelectSql<TJoin1, TJoin2, TJoin3, TJoin4>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlSelectable> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlSelectable>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddSelectSqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2), GetJoinedContext<TJoin4>(3)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlSelectable> item in selectors)
AddSelectSqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2), GetJoinedContext<TJoin4>(3)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> GroupBy<TResult>(Expression<Func<T, TResult>> selector, params Expression<Func<T, TResult>>[] selectors)
{
if (selector == null)
@@ -11105,6 +11221,54 @@ namespace DynamORM
return this;
}
public IDynamicTypedSelectQueryBuilder<T> GroupBySql<TJoin1>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlExpression>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddGroupBySqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlExpression> item in selectors)
AddGroupBySqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> GroupBySql<TJoin1, TJoin2>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlExpression>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddGroupBySqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlExpression> item in selectors)
AddGroupBySqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> GroupBySql<TJoin1, TJoin2, TJoin3>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlExpression>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddGroupBySqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlExpression> item in selectors)
AddGroupBySqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> GroupBySql<TJoin1, TJoin2, TJoin3, TJoin4>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlExpression>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddGroupBySqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2), GetJoinedContext<TJoin4>(3)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlExpression> item in selectors)
AddGroupBySqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2), GetJoinedContext<TJoin4>(3)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> OrderBy<TResult>(Expression<Func<T, TResult>> selector, params Expression<Func<T, TResult>>[] selectors)
{
if (selector == null)
@@ -11134,6 +11298,54 @@ namespace DynamORM
return this;
}
public IDynamicTypedSelectQueryBuilder<T> OrderBySql<TJoin1>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlOrderExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlOrderExpression>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddOrderBySqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlOrderExpression> item in selectors)
AddOrderBySqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> OrderBySql<TJoin1, TJoin2>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlOrderExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlOrderExpression>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddOrderBySqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlOrderExpression> item in selectors)
AddOrderBySqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> OrderBySql<TJoin1, TJoin2, TJoin3>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlOrderExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlOrderExpression>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddOrderBySqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlOrderExpression> item in selectors)
AddOrderBySqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2)));
return this;
}
public IDynamicTypedSelectQueryBuilder<T> OrderBySql<TJoin1, TJoin2, TJoin3, TJoin4>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlOrderExpression> selector, params Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlOrderExpression>[] selectors)
{
if (selector == null)
throw new ArgumentNullException("selector");
AddOrderBySqlSelector(selector(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2), GetJoinedContext<TJoin4>(3)));
if (selectors != null)
foreach (Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlOrderExpression> item in selectors)
AddOrderBySqlSelector(item(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2), GetJoinedContext<TJoin4>(3)));
return this;
}
public new IDynamicTypedSelectQueryBuilder<T> Top(int? top)
{
base.Top(top);
@@ -11173,27 +11385,83 @@ namespace DynamORM
if (predicate == null)
throw new ArgumentNullException("predicate");
string condition = RenderSqlPredicate(predicate);
if (string.IsNullOrEmpty(WhereCondition))
WhereCondition = condition;
else
WhereCondition = string.Format("{0} AND {1}", WhereCondition, condition);
AppendSqlCondition(RenderSqlPredicate(predicate), false);
return this;
}
public IDynamicTypedSelectQueryBuilder<T> WhereSql<TJoin1>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlPredicate> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
AppendSqlCondition(RenderSqlPredicate(predicate(GetRootContext(), GetJoinedContext<TJoin1>(0))), false);
return this;
}
public IDynamicTypedSelectQueryBuilder<T> WhereSql<TJoin1, TJoin2>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlPredicate> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
AppendSqlCondition(RenderSqlPredicate(predicate(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1))), false);
return this;
}
public IDynamicTypedSelectQueryBuilder<T> WhereSql<TJoin1, TJoin2, TJoin3>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlPredicate> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
AppendSqlCondition(RenderSqlPredicate(predicate(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2))), false);
return this;
}
public IDynamicTypedSelectQueryBuilder<T> WhereSql<TJoin1, TJoin2, TJoin3, TJoin4>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlPredicate> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
AppendSqlCondition(RenderSqlPredicate(predicate(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2), GetJoinedContext<TJoin4>(3))), false);
return this;
}
public IDynamicTypedSelectQueryBuilder<T> HavingSql(Func<TypedTableContext<T>, TypedSqlPredicate> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
string condition = RenderSqlPredicate(predicate);
if (string.IsNullOrEmpty(HavingCondition))
HavingCondition = condition;
else
HavingCondition = string.Format("{0} AND {1}", HavingCondition, condition);
AppendSqlCondition(RenderSqlPredicate(predicate), true);
return this;
}
public IDynamicTypedSelectQueryBuilder<T> HavingSql<TJoin1>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedSqlPredicate> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
AppendSqlCondition(RenderSqlPredicate(predicate(GetRootContext(), GetJoinedContext<TJoin1>(0))), true);
return this;
}
public IDynamicTypedSelectQueryBuilder<T> HavingSql<TJoin1, TJoin2>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedSqlPredicate> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
AppendSqlCondition(RenderSqlPredicate(predicate(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1))), true);
return this;
}
public IDynamicTypedSelectQueryBuilder<T> HavingSql<TJoin1, TJoin2, TJoin3>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedSqlPredicate> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
AppendSqlCondition(RenderSqlPredicate(predicate(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2))), true);
return this;
}
public IDynamicTypedSelectQueryBuilder<T> HavingSql<TJoin1, TJoin2, TJoin3, TJoin4>(Func<TypedTableContext<T>, TypedTableContext<TJoin1>, TypedTableContext<TJoin2>, TypedTableContext<TJoin3>, TypedTableContext<TJoin4>, TypedSqlPredicate> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
AppendSqlCondition(RenderSqlPredicate(predicate(GetRootContext(), GetJoinedContext<TJoin1>(0), GetJoinedContext<TJoin2>(1), GetJoinedContext<TJoin3>(2), GetJoinedContext<TJoin4>(3))), true);
return this;
}
public new IDynamicTypedSelectQueryBuilder<T> Having(DynamicColumn column)
{
base.Having(column);
@@ -11237,8 +11505,14 @@ namespace DynamORM
if (selector == null)
throw new ArgumentNullException("selector");
AddSelectSqlSelector(selector(GetRootContext()));
}
private void AddSelectSqlSelector(TypedSqlSelectable item)
{
if (item == null)
throw new ArgumentNullException("item");
TypedSqlRenderContext context = new TypedSqlRenderContext(this);
TypedSqlSelectable item = selector(new TypedTableContext<T>(GetRootAliasOrTableName()));
string rendered = item.Render(context);
_select = string.IsNullOrEmpty(_select) ? rendered : string.Format("{0}, {1}", _select, rendered);
}
@@ -11265,8 +11539,14 @@ namespace DynamORM
if (selector == null)
throw new ArgumentNullException("selector");
AddGroupBySqlSelector(selector(GetRootContext()));
}
private void AddGroupBySqlSelector(TypedSqlExpression item)
{
if (item == null)
throw new ArgumentNullException("item");
TypedSqlRenderContext context = new TypedSqlRenderContext(this);
TypedSqlExpression item = selector(new TypedTableContext<T>(GetRootAliasOrTableName()));
string rendered = item.Render(context);
_groupby = string.IsNullOrEmpty(_groupby) ? rendered : string.Format("{0}, {1}", _groupby, rendered);
}
@@ -11289,15 +11569,25 @@ namespace DynamORM
if (selector == null)
throw new ArgumentNullException("selector");
AddOrderBySqlSelector(selector(GetRootContext()));
}
private void AddOrderBySqlSelector(TypedSqlOrderExpression item)
{
TypedSqlRenderContext context = new TypedSqlRenderContext(this);
TypedSqlOrderExpression item = selector(new TypedTableContext<T>(GetRootAliasOrTableName()));
string rendered = item.Render(context);
_orderby = string.IsNullOrEmpty(_orderby) ? rendered : string.Format("{0}, {1}", _orderby, rendered);
}
private string RenderSqlPredicate(Func<TypedTableContext<T>, TypedSqlPredicate> predicate)
{
return RenderSqlPredicate(predicate(GetRootContext()));
}
private string RenderSqlPredicate(TypedSqlPredicate predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
TypedSqlRenderContext context = new TypedSqlRenderContext(this);
return predicate(new TypedTableContext<T>(GetRootAliasOrTableName())).Render(context);
return predicate.Render(context);
}
private string ParseTypedCondition(Expression expression)
{
@@ -11461,6 +11751,46 @@ namespace DynamORM
return string.IsNullOrEmpty(table.Alias) ? table.Name : table.Alias;
}
private TypedTableContext<T> GetRootContext()
{
return new TypedTableContext<T>(GetRootAliasOrTableName());
}
private TypedTableContext<TJoin> GetJoinedContext<TJoin>(int index)
{
if (index < 0 || index >= _typedJoins.Count)
throw new InvalidOperationException(string.Format("Typed join context at position {0} is not available.", index + 1));
TypedJoinInfo join = _typedJoins[index];
if (join.ModelType != typeof(TJoin))
throw new InvalidOperationException(string.Format("Typed join context at position {0} is {1}, not {2}.", index + 1, join.ModelType.FullName, typeof(TJoin).FullName));
return new TypedTableContext<TJoin>(join.Alias);
}
private void RegisterTypedJoin(Type modelType, string alias)
{
_typedJoins.Add(new TypedJoinInfo
{
ModelType = modelType,
Alias = alias
});
}
private void AppendSqlCondition(string condition, bool having)
{
if (having)
{
if (string.IsNullOrEmpty(HavingCondition))
HavingCondition = condition;
else
HavingCondition = string.Format("{0} AND {1}", HavingCondition, condition);
}
else
{
if (string.IsNullOrEmpty(WhereCondition))
WhereCondition = condition;
else
WhereCondition = string.Format("{0} AND {1}", WhereCondition, condition);
}
}
private static bool IsMemberFromTypedParameter(Expression expression)
{
var member = expression as MemberExpression;