Class QueryHelper
public static class QueryHelper
- Inheritance
-
QueryHelper
Methods
CanBeEvaluated(IQueryElement, EvaluationContext)
public static bool CanBeEvaluated(this IQueryElement expr, EvaluationContext context)
Parameters
exprIQueryElementcontextEvaluationContext
Returns
CanBeEvaluated(IQueryElement, bool)
public static bool CanBeEvaluated(this IQueryElement expr, bool withParameters)
Parameters
exprIQueryElementwithParametersbool
Returns
CanRemoveOrderBy(SelectQuery, SqlProviderFlags, QueryInformation)
Detects when we can remove order
public static bool CanRemoveOrderBy(SelectQuery selectQuery, SqlProviderFlags flags, QueryInformation information)
Parameters
selectQuerySelectQueryflagsSqlProviderFlagsinformationQueryInformation
Returns
CollectDependencies(IQueryElement, IEnumerable<ISqlTableSource>, HashSet<ISqlExpression>, IEnumerable<IQueryElement>?)
public static void CollectDependencies(IQueryElement root, IEnumerable<ISqlTableSource> sources, HashSet<ISqlExpression> found, IEnumerable<IQueryElement>? ignore = null)
Parameters
rootIQueryElementsourcesIEnumerable<ISqlTableSource>foundHashSet<ISqlExpression>ignoreIEnumerable<IQueryElement>
CollectUniqueKeys(ISqlTableSource, bool, List<IList<ISqlExpression>>)
Collects unique keys from different sources.
public static void CollectUniqueKeys(ISqlTableSource tableSource, bool includeDistinct, List<IList<ISqlExpression>> knownKeys)
Parameters
tableSourceISqlTableSourceincludeDistinctboolFlag to include Distinct as unique key.
knownKeysList<IList<ISqlExpression>>List with found keys.
CollectUniqueKeys(SqlTableSource, List<IList<ISqlExpression>>)
Collects unique keys from different sources.
public static void CollectUniqueKeys(SqlTableSource tableSource, List<IList<ISqlExpression>> knownKeys)
Parameters
tableSourceSqlTableSourceknownKeysList<IList<ISqlExpression>>List with found keys.
CollectUsedSources(IQueryElement, HashSet<ISqlTableSource>, IEnumerable<IQueryElement>?)
public static void CollectUsedSources(IQueryElement root, HashSet<ISqlTableSource> found, IEnumerable<IQueryElement>? ignore = null)
Parameters
rootIQueryElementfoundHashSet<ISqlTableSource>ignoreIEnumerable<IQueryElement>
ConcatSearchCondition(SqlWhereClause, SqlSearchCondition)
public static void ConcatSearchCondition(this SqlWhereClause where, SqlSearchCondition search)
Parameters
whereSqlWhereClausesearchSqlSearchCondition
ContainsAggregationFunctionOneLevel(IQueryElement)
public static bool ContainsAggregationFunctionOneLevel(IQueryElement expr)
Parameters
exprIQueryElement
Returns
ContainsAggregationOrWindowFunction(IQueryElement)
public static bool ContainsAggregationOrWindowFunction(IQueryElement expr)
Parameters
exprIQueryElement
Returns
ContainsAggregationOrWindowFunctionDeep(IQueryElement)
public static bool ContainsAggregationOrWindowFunctionDeep(IQueryElement expr)
Parameters
exprIQueryElement
Returns
ContainsAggregationOrWindowFunctionOneLevel(IQueryElement)
public static bool ContainsAggregationOrWindowFunctionOneLevel(IQueryElement expr)
Parameters
exprIQueryElement
Returns
ContainsElement(IQueryElement, IQueryElement)
public static bool ContainsElement(IQueryElement testedRoot, IQueryElement element)
Parameters
testedRootIQueryElementelementIQueryElement
Returns
ConvertFormatToConcatenation(string, IList<ISqlExpression>)
public static ISqlExpression ConvertFormatToConcatenation(string format, IList<ISqlExpression> parameters)
Parameters
formatstringparametersIList<ISqlExpression>
Returns
CorrectSearchConditionNesting(SelectQuery, SqlCondition, HashSet<ISqlTableSource>)
public static SqlCondition CorrectSearchConditionNesting(SelectQuery sql, SqlCondition condition, HashSet<ISqlTableSource> forTableSources)
Parameters
sqlSelectQueryconditionSqlConditionforTableSourcesHashSet<ISqlTableSource>
Returns
CountElements(ISqlExpression)
public static IDictionary<QueryElementType, int> CountElements(ISqlExpression expr)
Parameters
exprISqlExpression
Returns
DependencyCount(IQueryElement, IQueryElement, HashSet<IQueryElement>?)
public static int DependencyCount(IQueryElement testedRoot, IQueryElement onElement, HashSet<IQueryElement>? elementsToIgnore = null)
Parameters
testedRootIQueryElementonElementIQueryElementelementsToIgnoreHashSet<IQueryElement>
Returns
EnsureConjunction(SqlSearchCondition)
Ensures that expression is not A OR B but (A OR B) Function makes all needed manipulations for that
public static SqlSearchCondition EnsureConjunction(this SqlSearchCondition searchCondition)
Parameters
searchConditionSqlSearchCondition
Returns
EnsureConjunction(SqlWhereClause)
Ensures that expression is not A OR B but (A OR B) Function makes all needed manipulations for that
public static SqlWhereClause EnsureConjunction(this SqlWhereClause whereClause)
Parameters
whereClauseSqlWhereClause
Returns
EnumerateAccessibleSources(SelectQuery)
Enumerates table sources recursively based on joins
public static IEnumerable<ISqlTableSource> EnumerateAccessibleSources(SelectQuery selectQuery)
Parameters
selectQuerySelectQuery
Returns
EnumerateAccessibleSources(SqlTableSource)
public static IEnumerable<ISqlTableSource> EnumerateAccessibleSources(SqlTableSource tableSource)
Parameters
tableSourceSqlTableSource
Returns
EnumerateAccessibleTables(SelectQuery)
public static IEnumerable<SqlTable> EnumerateAccessibleTables(SelectQuery selectQuery)
Parameters
selectQuerySelectQuery
Returns
EnumerateInnerJoined(SelectQuery)
public static IEnumerable<SqlTableSource> EnumerateInnerJoined(SelectQuery selectQuery)
Parameters
selectQuerySelectQuery
Returns
EnumerateInnerJoined(SqlTableSource)
public static IEnumerable<SqlTableSource> EnumerateInnerJoined(SqlTableSource tableSource)
Parameters
tableSourceSqlTableSource
Returns
EnumerateJoins(SelectQuery)
public static IEnumerable<SqlJoinedTable> EnumerateJoins(SelectQuery selectQuery)
Parameters
selectQuerySelectQuery
Returns
EnumerateJoins(SqlTableSource)
public static IEnumerable<SqlJoinedTable> EnumerateJoins(SqlTableSource tableSource)
Parameters
tableSourceSqlTableSource
Returns
EnumerateLevelSources(SelectQuery)
public static IEnumerable<ISqlTableSource> EnumerateLevelSources(SelectQuery selectQuery)
Parameters
selectQuerySelectQuery
Returns
EnumerateLevelSources(SqlTableSource)
public static IEnumerable<ISqlTableSource> EnumerateLevelSources(SqlTableSource tableSource)
Parameters
tableSourceSqlTableSource
Returns
EnumerateLevelTables(SelectQuery)
public static IEnumerable<SqlTable> EnumerateLevelTables(SelectQuery selectQuery)
Parameters
selectQuerySelectQuery
Returns
EvaluateBoolExpression(IQueryElement, EvaluationContext, bool?)
public static bool? EvaluateBoolExpression(this IQueryElement expr, EvaluationContext context, bool? defaultValue = null)
Parameters
exprIQueryElementcontextEvaluationContextdefaultValuebool?
Returns
- bool?
EvaluateExpression(IQueryElement, EvaluationContext)
public static object? EvaluateExpression(this IQueryElement expr, EvaluationContext context)
Parameters
exprIQueryElementcontextEvaluationContext
Returns
ExtractField(ISqlExpression)
Returns SqlField from specific expression. Usually from SqlColumn. Conversion is ignored.
public static SqlField? ExtractField(ISqlExpression expression)
Parameters
expressionISqlExpression
Returns
- SqlField
Field instance associated with expression
FindJoin(SelectQuery, Func<SqlJoinedTable, bool>)
public static SqlJoinedTable? FindJoin(this SelectQuery query, Func<SqlJoinedTable, bool> match)
Parameters
querySelectQuerymatchFunc<SqlJoinedTable, bool>
Returns
GenerateEquality(ISqlExpression, ISqlExpression, bool)
public static SqlCondition GenerateEquality(ISqlExpression field1, ISqlExpression field2, bool compareNullsAsValues)
Parameters
field1ISqlExpressionfield2ISqlExpressioncompareNullsAsValuesbool
Returns
GetBoolValue(ISqlExpression, EvaluationContext)
public static bool? GetBoolValue(ISqlExpression expression, EvaluationContext context)
Parameters
expressionISqlExpressioncontextEvaluationContext
Returns
- bool?
GetColumnDescriptor(ISqlExpression?)
Returns ColumnDescriptor for expr.
public static ColumnDescriptor? GetColumnDescriptor(ISqlExpression? expr)
Parameters
exprISqlExpressionTested SQL Expression.
Returns
- ColumnDescriptor
Associated column descriptor or
null.
GetDbDataType(ISqlExpression?)
public static DbDataType GetDbDataType(ISqlExpression? expr)
Parameters
exprISqlExpression
Returns
GetDeleteTable(SqlDeleteStatement)
public static SqlTable? GetDeleteTable(this SqlDeleteStatement deleteStatement)
Parameters
deleteStatementSqlDeleteStatement
Returns
GetExpressionType(ISqlExpression)
public static DbDataType GetExpressionType(this ISqlExpression expr)
Parameters
exprISqlExpression
Returns
GetParameterValue(SqlParameter, IReadOnlyParameterValues?)
public static SqlParameterValue GetParameterValue(this SqlParameter parameter, IReadOnlyParameterValues? parameterValues)
Parameters
parameterSqlParameterparameterValuesIReadOnlyParameterValues
Returns
GetUnderlyingExpression(ISqlExpression?)
Unwraps SqlColumn and returns underlying expression.
public static ISqlExpression? GetUnderlyingExpression(ISqlExpression? expression)
Parameters
expressionISqlExpression
Returns
- ISqlExpression
Underlying expression.
GetUnderlyingExpressionValue(SqlExpression, bool)
public static ISqlExpression GetUnderlyingExpressionValue(SqlExpression sqlExpression, bool checkNullability)
Parameters
sqlExpressionSqlExpressioncheckNullabilitybool
Returns
GetUnderlyingField(ISqlExpression)
Returns SqlField from specific expression. Usually from SqlColumn. Complex expressions ignored.
public static SqlField? GetUnderlyingField(ISqlExpression expression)
Parameters
expressionISqlExpression
Returns
- SqlField
Field instance associated with expression
GetUpdateTable(SqlUpdateStatement)
public static SqlTable? GetUpdateTable(this SqlUpdateStatement updateStatement)
Parameters
updateStatementSqlUpdateStatement
Returns
GetUsedSources(ISqlExpression, HashSet<ISqlTableSource>)
Retrieves which sources are used in the rootexpression
public static void GetUsedSources(ISqlExpression root, HashSet<ISqlTableSource> foundSources)
Parameters
rootISqlExpressionExpression to analyze.
foundSourcesHashSet<ISqlTableSource>Output container for detected sources/
GetValueConverter(ISqlExpression?)
Returns IValueConverter for expr.
public static IValueConverter? GetValueConverter(ISqlExpression? expr)
Parameters
exprISqlExpressionTested SQL Expression.
Returns
- IValueConverter
Associated converter or
null.
HasOuterReferences(ISet<ISqlTableSource>, ISqlExpression)
public static bool HasOuterReferences(ISet<ISqlTableSource> sources, ISqlExpression expr)
Parameters
sourcesISet<ISqlTableSource>exprISqlExpression
Returns
HasQueryParameters(ISqlExpression)
public static bool HasQueryParameters(ISqlExpression expression)
Parameters
expressionISqlExpression
Returns
IsAggregationFunction(IQueryElement)
public static bool IsAggregationFunction(IQueryElement expr)
Parameters
exprIQueryElement
Returns
IsAggregationOrWindowFunction(IQueryElement)
public static bool IsAggregationOrWindowFunction(IQueryElement expr)
Parameters
exprIQueryElement
Returns
IsComplexExpression(ISqlExpression)
public static bool IsComplexExpression(this ISqlExpression expr)
Parameters
exprISqlExpression
Returns
IsConstant(ISqlExpression)
Returns true if tested expression is constant during query execution (e.g. value or parameter).
public static bool IsConstant(ISqlExpression expr)
Parameters
exprISqlExpressionTested expression.
Returns
IsConstantFast(ISqlExpression)
public static bool IsConstantFast(ISqlExpression expr)
Parameters
exprISqlExpression
Returns
IsDependsOn(IQueryElement, IQueryElement, HashSet<IQueryElement>?)
public static bool IsDependsOn(IQueryElement testedRoot, IQueryElement onElement, HashSet<IQueryElement>? elementsToIgnore = null)
Parameters
testedRootIQueryElementonElementIQueryElementelementsToIgnoreHashSet<IQueryElement>
Returns
IsDependsOn(IQueryElement, HashSet<ISqlTableSource>, HashSet<IQueryElement>?)
public static bool IsDependsOn(IQueryElement testedRoot, HashSet<ISqlTableSource> onSources, HashSet<IQueryElement>? elementsToIgnore = null)
Parameters
testedRootIQueryElementonSourcesHashSet<ISqlTableSource>elementsToIgnoreHashSet<IQueryElement>
Returns
IsEqualTables(SqlTable?, SqlTable?)
public static bool IsEqualTables(SqlTable? table1, SqlTable? table2)
Parameters
Returns
IsExpression(ISqlExpression)
Returns true if it is anything except Field or Column.
public static bool IsExpression(ISqlExpression expr)
Parameters
exprISqlExpressionTested expression
Returns
- bool
true if tested expression is not a Field or Column
IsMutable(IQueryElement)
public static bool IsMutable(this IQueryElement expr)
Parameters
exprIQueryElement
Returns
IsTransitiveExpression(SqlExpression, bool)
public static bool IsTransitiveExpression(SqlExpression sqlExpression, bool checkNullability)
Parameters
sqlExpressionSqlExpressioncheckNullabilitybool
Returns
IsWindowFunction(IQueryElement)
public static bool IsWindowFunction(IQueryElement expr)
Parameters
exprIQueryElement
Returns
JoinRemoval<TContext>(TContext, SqlStatement, Func<TContext, SqlStatement, SqlJoinedTable, bool>)
Removes Join from query based on joinFunc result.
public static SqlStatement JoinRemoval<TContext>(TContext context, SqlStatement statement, Func<TContext, SqlStatement, SqlJoinedTable, bool> joinFunc)
Parameters
contextTContextjoinFunccontext object.statementSqlStatementSource statement.
joinFuncFunc<TContext, SqlStatement, SqlJoinedTable, bool>
Returns
- SqlStatement
Same or new statement with removed joins.
Type Parameters
TContext
MoveOrderByUp(params SelectQuery[])
Helper function for moving Ordering up in select tree.
public static void MoveOrderByUp(params SelectQuery[] queries)
Parameters
queriesSelectQuery[]Array of queries
MoveSearchConditionsToJoin(SelectQuery, SqlJoinedTable, List<SqlCondition>?)
public static void MoveSearchConditionsToJoin(SelectQuery sql, SqlJoinedTable joinedTable, List<SqlCondition>? movedConditions)
Parameters
sqlSelectQueryjoinedTableSqlJoinedTablemovedConditionsList<SqlCondition>
NeedColumnForExpression(SelectQuery, ISqlExpression, bool)
Returns correct column or field according to nesting.
public static ISqlExpression? NeedColumnForExpression(SelectQuery selectQuery, ISqlExpression forExpression, bool inProjection)
Parameters
selectQuerySelectQueryAnalyzed query.
forExpressionISqlExpressionExpression that has to be enveloped by column.
inProjectionboolIf 'true', function ensures that column is created. If 'false' it may return Field if it fits to nesting level.
Returns
- ISqlExpression
Returns Column of Field according to its nesting level. May return null if expression is not valid for
selectQuery
NeedParameterInlining(ISqlExpression)
public static bool NeedParameterInlining(ISqlExpression expression)
Parameters
expressionISqlExpression
Returns
RemoveNotUnusedColumns(SelectQuery)
public static void RemoveNotUnusedColumns(this SelectQuery selectQuery)
Parameters
selectQuerySelectQuery
RootQuery(SelectQuery)
public static SelectQuery RootQuery(this SelectQuery query)
Parameters
querySelectQuery
Returns
ShouldCheckForNull(ISqlExpression)
public static bool ShouldCheckForNull(this ISqlExpression expr)
Parameters
exprISqlExpression
Returns
SuggestDbDataType(ISqlExpression)
public static DbDataType? SuggestDbDataType(ISqlExpression expr)
Parameters
exprISqlExpression
Returns
ToDebugString(IQueryElement)
public static string ToDebugString(this IQueryElement expr)
Parameters
exprIQueryElement
Returns
TransformExpressionIndexes<TContext>(TContext, string, Func<TContext, int, int>)
public static string TransformExpressionIndexes<TContext>(TContext context, string expression, Func<TContext, int, int> transformFunc)
Parameters
Returns
Type Parameters
TContext
TransformInnerJoinsToWhere(SelectQuery)
Transforms SELECT * FROM A INNER JOIN B ON A.ID = B.ID to SELECT * FROM A, B WHERE A.ID = B.ID
public static SelectQuery TransformInnerJoinsToWhere(this SelectQuery selectQuery)
Parameters
selectQuerySelectQueryInput SelectQuery.
Returns
- SelectQuery
The same query instance.
TryConvertOrderedDistinctToGroupBy(SelectQuery, SqlProviderFlags)
Converts ORDER BY DISTINCT to GROUP BY equivalent
public static bool TryConvertOrderedDistinctToGroupBy(SelectQuery select, SqlProviderFlags flags)
Parameters
selectSelectQueryflagsSqlProviderFlags
Returns
TryEvaluateExpression(IQueryElement, EvaluationContext, out object?)
public static bool TryEvaluateExpression(this IQueryElement expr, EvaluationContext context, out object? result)
Parameters
exprIQueryElementcontextEvaluationContextresultobject
Returns
TryRemoveDistinct(SelectQuery, QueryInformation)
Detects when we can remove order
public static bool TryRemoveDistinct(SelectQuery selectQuery, QueryInformation information)
Parameters
selectQuerySelectQueryinformationQueryInformation
Returns
UnwrapExpression(ISqlExpression, bool)
public static ISqlExpression UnwrapExpression(ISqlExpression expr, bool checkNullability)
Parameters
exprISqlExpressioncheckNullabilitybool
Returns
ValidateTable(SelectQuery, ISqlTableSource)
public static bool ValidateTable(SelectQuery selectQuery, ISqlTableSource table)
Parameters
selectQuerySelectQuerytableISqlTableSource
Returns
WrapQuery<TStatement>(TStatement, SelectQuery, bool)
Wraps queryToWrap by another select.
Keeps columns count the same. After modification statement is equivalent symantically.
--before
SELECT c1, c2
FROM A
-- after
SELECT B.c1, B.c2
FROM (
SELECT c1, c2
FROM A
) B
public static TStatement WrapQuery<TStatement>(TStatement statement, SelectQuery queryToWrap, bool allowMutation) where TStatement : SqlStatement
Parameters
statementTStatementStatement which may contain tested query
queryToWrapSelectQueryTells which select query needs enveloping
allowMutationboolWrapped query can be not recreated for performance considerations.
Returns
- TStatement
The same
statementor modified statement when wrapping has been performed.
Type Parameters
TStatement
WrapQuery<TStatement, TContext>(TContext, TStatement, Func<TContext, SelectQuery, IQueryElement?, bool>, Action<TContext, SelectQuery, SelectQuery>?, bool, bool)
Wraps queries by another select. Keeps columns count the same. After modification statement is equivalent symantically.
public static TStatement WrapQuery<TStatement, TContext>(TContext context, TStatement statement, Func<TContext, SelectQuery, IQueryElement?, bool> wrapTest, Action<TContext, SelectQuery, SelectQuery>? onWrap, bool allowMutation, bool withStack) where TStatement : SqlStatement
Parameters
contextTContextonWrapandwrapTestcontext object.statementTStatementwrapTestFunc<TContext, SelectQuery, IQueryElement, bool>Delegate for testing when query needs to be wrapped.
onWrapAction<TContext, SelectQuery, SelectQuery>After enveloping query this function called for prcess needed optimizations.
allowMutationboolWrapped query can be not recreated for performance considerations.
withStackboolMust be set to
true, ifwrapTestfunction use 3rd parameter (containing parent element) otherwise it will be always null.
Returns
- TStatement
The same
statementor modified statement when wrapping has been performed.
Type Parameters
TStatementTContextType of
onWrapandwrapTestcontext object.
WrapQuery<TStatement, TContext>(TContext, TStatement, Func<TContext, SelectQuery, IQueryElement?, int>, Action<TContext, IReadOnlyList<SelectQuery>>, bool, bool)
Wraps tested query in subquery(s). Keeps columns count the same. After modification statement is equivalent semantically.
--before
SELECT c1, c2 -- QA
FROM A
-- after (with 2 subqueries)
SELECT C.c1, C.c2 -- QC
FROM (
SELECT B.c1, B.c2 -- QB
FROM (
SELECT c1, c2 -- QA
FROM A
) B
FROM
) C
public static TStatement WrapQuery<TStatement, TContext>(TContext context, TStatement statement, Func<TContext, SelectQuery, IQueryElement?, int> wrapTest, Action<TContext, IReadOnlyList<SelectQuery>> onWrap, bool allowMutation, bool withStack) where TStatement : SqlStatement
Parameters
contextTContextonWrapandwrapTestcontext object.statementTStatementStatement which may contain tested query
wrapTestFunc<TContext, SelectQuery, IQueryElement, int>Delegate for testing which query needs to be enveloped. Result of delegate call tells how many subqueries needed. 0 - no changes 1 - one subquery N - N subqueries
onWrapAction<TContext, IReadOnlyList<SelectQuery>>After wrapping query this function called for prcess needed optimizations. Array of queries contains [QC, QB, QA]
allowMutationboolWrapped query can be not recreated for performance considerations.
withStackboolMust be set to
true, ifwrapTestfunction use 3rd parameter (containing parent element) otherwise it will be always null.
Returns
- TStatement
The same
statementor modified statement when wrapping has been performed.
Type Parameters
TStatementTContextType of
onWrapandwrapTestcontext object.