Class QueryHelper
public static class QueryHelper
- Inheritance
-
QueryHelper
Methods
CanBeEvaluated(IQueryElement, EvaluationContext)
public static bool CanBeEvaluated(this IQueryElement expr, EvaluationContext context)
Parameters
expr
IQueryElementcontext
EvaluationContext
Returns
CanBeEvaluated(IQueryElement, bool)
public static bool CanBeEvaluated(this IQueryElement expr, bool withParameters)
Parameters
expr
IQueryElementwithParameters
bool
Returns
CanRemoveOrderBy(SelectQuery, SqlProviderFlags, QueryInformation)
Detects when we can remove order
public static bool CanRemoveOrderBy(SelectQuery selectQuery, SqlProviderFlags flags, QueryInformation information)
Parameters
selectQuery
SelectQueryflags
SqlProviderFlagsinformation
QueryInformation
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
root
IQueryElementsources
IEnumerable<ISqlTableSource>found
HashSet<ISqlExpression>ignore
IEnumerable<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
tableSource
ISqlTableSourceincludeDistinct
boolFlag to include Distinct as unique key.
knownKeys
List<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
tableSource
SqlTableSourceknownKeys
List<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
root
IQueryElementfound
HashSet<ISqlTableSource>ignore
IEnumerable<IQueryElement>
ConcatSearchCondition(SqlWhereClause, SqlSearchCondition)
public static void ConcatSearchCondition(this SqlWhereClause where, SqlSearchCondition search)
Parameters
where
SqlWhereClausesearch
SqlSearchCondition
ContainsAggregationFunctionOneLevel(IQueryElement)
public static bool ContainsAggregationFunctionOneLevel(IQueryElement expr)
Parameters
expr
IQueryElement
Returns
ContainsAggregationOrWindowFunction(IQueryElement)
public static bool ContainsAggregationOrWindowFunction(IQueryElement expr)
Parameters
expr
IQueryElement
Returns
ContainsAggregationOrWindowFunctionDeep(IQueryElement)
public static bool ContainsAggregationOrWindowFunctionDeep(IQueryElement expr)
Parameters
expr
IQueryElement
Returns
ContainsAggregationOrWindowFunctionOneLevel(IQueryElement)
public static bool ContainsAggregationOrWindowFunctionOneLevel(IQueryElement expr)
Parameters
expr
IQueryElement
Returns
ContainsElement(IQueryElement, IQueryElement)
public static bool ContainsElement(IQueryElement testedRoot, IQueryElement element)
Parameters
testedRoot
IQueryElementelement
IQueryElement
Returns
ConvertFormatToConcatenation(string, IList<ISqlExpression>)
public static ISqlExpression ConvertFormatToConcatenation(string format, IList<ISqlExpression> parameters)
Parameters
format
stringparameters
IList<ISqlExpression>
Returns
CorrectSearchConditionNesting(SelectQuery, SqlCondition, HashSet<ISqlTableSource>)
public static SqlCondition CorrectSearchConditionNesting(SelectQuery sql, SqlCondition condition, HashSet<ISqlTableSource> forTableSources)
Parameters
sql
SelectQuerycondition
SqlConditionforTableSources
HashSet<ISqlTableSource>
Returns
CountElements(ISqlExpression)
public static IDictionary<QueryElementType, int> CountElements(ISqlExpression expr)
Parameters
expr
ISqlExpression
Returns
DependencyCount(IQueryElement, IQueryElement, HashSet<IQueryElement>?)
public static int DependencyCount(IQueryElement testedRoot, IQueryElement onElement, HashSet<IQueryElement>? elementsToIgnore = null)
Parameters
testedRoot
IQueryElementonElement
IQueryElementelementsToIgnore
HashSet<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
searchCondition
SqlSearchCondition
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
whereClause
SqlWhereClause
Returns
EnumerateAccessibleSources(SelectQuery)
Enumerates table sources recursively based on joins
public static IEnumerable<ISqlTableSource> EnumerateAccessibleSources(SelectQuery selectQuery)
Parameters
selectQuery
SelectQuery
Returns
EnumerateAccessibleSources(SqlTableSource)
public static IEnumerable<ISqlTableSource> EnumerateAccessibleSources(SqlTableSource tableSource)
Parameters
tableSource
SqlTableSource
Returns
EnumerateAccessibleTables(SelectQuery)
public static IEnumerable<SqlTable> EnumerateAccessibleTables(SelectQuery selectQuery)
Parameters
selectQuery
SelectQuery
Returns
EnumerateInnerJoined(SelectQuery)
public static IEnumerable<SqlTableSource> EnumerateInnerJoined(SelectQuery selectQuery)
Parameters
selectQuery
SelectQuery
Returns
EnumerateInnerJoined(SqlTableSource)
public static IEnumerable<SqlTableSource> EnumerateInnerJoined(SqlTableSource tableSource)
Parameters
tableSource
SqlTableSource
Returns
EnumerateJoins(SelectQuery)
public static IEnumerable<SqlJoinedTable> EnumerateJoins(SelectQuery selectQuery)
Parameters
selectQuery
SelectQuery
Returns
EnumerateJoins(SqlTableSource)
public static IEnumerable<SqlJoinedTable> EnumerateJoins(SqlTableSource tableSource)
Parameters
tableSource
SqlTableSource
Returns
EnumerateLevelSources(SelectQuery)
public static IEnumerable<ISqlTableSource> EnumerateLevelSources(SelectQuery selectQuery)
Parameters
selectQuery
SelectQuery
Returns
EnumerateLevelSources(SqlTableSource)
public static IEnumerable<ISqlTableSource> EnumerateLevelSources(SqlTableSource tableSource)
Parameters
tableSource
SqlTableSource
Returns
EnumerateLevelTables(SelectQuery)
public static IEnumerable<SqlTable> EnumerateLevelTables(SelectQuery selectQuery)
Parameters
selectQuery
SelectQuery
Returns
EvaluateBoolExpression(IQueryElement, EvaluationContext, bool?)
public static bool? EvaluateBoolExpression(this IQueryElement expr, EvaluationContext context, bool? defaultValue = null)
Parameters
expr
IQueryElementcontext
EvaluationContextdefaultValue
bool?
Returns
- bool?
EvaluateExpression(IQueryElement, EvaluationContext)
public static object? EvaluateExpression(this IQueryElement expr, EvaluationContext context)
Parameters
expr
IQueryElementcontext
EvaluationContext
Returns
ExtractField(ISqlExpression)
Returns SqlField from specific expression. Usually from SqlColumn. Conversion is ignored.
public static SqlField? ExtractField(ISqlExpression expression)
Parameters
expression
ISqlExpression
Returns
- SqlField
Field instance associated with expression
FindJoin(SelectQuery, Func<SqlJoinedTable, bool>)
public static SqlJoinedTable? FindJoin(this SelectQuery query, Func<SqlJoinedTable, bool> match)
Parameters
query
SelectQuerymatch
Func<SqlJoinedTable, bool>
Returns
GenerateEquality(ISqlExpression, ISqlExpression, bool)
public static SqlCondition GenerateEquality(ISqlExpression field1, ISqlExpression field2, bool compareNullsAsValues)
Parameters
field1
ISqlExpressionfield2
ISqlExpressioncompareNullsAsValues
bool
Returns
GetBoolValue(ISqlExpression, EvaluationContext)
public static bool? GetBoolValue(ISqlExpression expression, EvaluationContext context)
Parameters
expression
ISqlExpressioncontext
EvaluationContext
Returns
- bool?
GetColumnDescriptor(ISqlExpression?)
Returns ColumnDescriptor for expr
.
public static ColumnDescriptor? GetColumnDescriptor(ISqlExpression? expr)
Parameters
expr
ISqlExpressionTested SQL Expression.
Returns
- ColumnDescriptor
Associated column descriptor or
null
.
GetDbDataType(ISqlExpression?)
public static DbDataType GetDbDataType(ISqlExpression? expr)
Parameters
expr
ISqlExpression
Returns
GetDeleteTable(SqlDeleteStatement)
public static SqlTable? GetDeleteTable(this SqlDeleteStatement deleteStatement)
Parameters
deleteStatement
SqlDeleteStatement
Returns
GetExpressionType(ISqlExpression)
public static DbDataType GetExpressionType(this ISqlExpression expr)
Parameters
expr
ISqlExpression
Returns
GetParameterValue(SqlParameter, IReadOnlyParameterValues?)
public static SqlParameterValue GetParameterValue(this SqlParameter parameter, IReadOnlyParameterValues? parameterValues)
Parameters
parameter
SqlParameterparameterValues
IReadOnlyParameterValues
Returns
GetUnderlyingExpression(ISqlExpression?)
Unwraps SqlColumn and returns underlying expression.
public static ISqlExpression? GetUnderlyingExpression(ISqlExpression? expression)
Parameters
expression
ISqlExpression
Returns
- ISqlExpression
Underlying expression.
GetUnderlyingExpressionValue(SqlExpression, bool)
public static ISqlExpression GetUnderlyingExpressionValue(SqlExpression sqlExpression, bool checkNullability)
Parameters
sqlExpression
SqlExpressioncheckNullability
bool
Returns
GetUnderlyingField(ISqlExpression)
Returns SqlField from specific expression. Usually from SqlColumn. Complex expressions ignored.
public static SqlField? GetUnderlyingField(ISqlExpression expression)
Parameters
expression
ISqlExpression
Returns
- SqlField
Field instance associated with expression
GetUpdateTable(SqlUpdateStatement)
public static SqlTable? GetUpdateTable(this SqlUpdateStatement updateStatement)
Parameters
updateStatement
SqlUpdateStatement
Returns
GetUsedSources(ISqlExpression, HashSet<ISqlTableSource>)
Retrieves which sources are used in the root
expression
public static void GetUsedSources(ISqlExpression root, HashSet<ISqlTableSource> foundSources)
Parameters
root
ISqlExpressionExpression to analyze.
foundSources
HashSet<ISqlTableSource>Output container for detected sources/
GetValueConverter(ISqlExpression?)
Returns IValueConverter for expr
.
public static IValueConverter? GetValueConverter(ISqlExpression? expr)
Parameters
expr
ISqlExpressionTested SQL Expression.
Returns
- IValueConverter
Associated converter or
null
.
HasOuterReferences(ISet<ISqlTableSource>, ISqlExpression)
public static bool HasOuterReferences(ISet<ISqlTableSource> sources, ISqlExpression expr)
Parameters
sources
ISet<ISqlTableSource>expr
ISqlExpression
Returns
HasQueryParameters(ISqlExpression)
public static bool HasQueryParameters(ISqlExpression expression)
Parameters
expression
ISqlExpression
Returns
IsAggregationFunction(IQueryElement)
public static bool IsAggregationFunction(IQueryElement expr)
Parameters
expr
IQueryElement
Returns
IsAggregationOrWindowFunction(IQueryElement)
public static bool IsAggregationOrWindowFunction(IQueryElement expr)
Parameters
expr
IQueryElement
Returns
IsComplexExpression(ISqlExpression)
public static bool IsComplexExpression(this ISqlExpression expr)
Parameters
expr
ISqlExpression
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
expr
ISqlExpressionTested expression.
Returns
IsConstantFast(ISqlExpression)
public static bool IsConstantFast(ISqlExpression expr)
Parameters
expr
ISqlExpression
Returns
IsDependsOn(IQueryElement, IQueryElement, HashSet<IQueryElement>?)
public static bool IsDependsOn(IQueryElement testedRoot, IQueryElement onElement, HashSet<IQueryElement>? elementsToIgnore = null)
Parameters
testedRoot
IQueryElementonElement
IQueryElementelementsToIgnore
HashSet<IQueryElement>
Returns
IsDependsOn(IQueryElement, HashSet<ISqlTableSource>, HashSet<IQueryElement>?)
public static bool IsDependsOn(IQueryElement testedRoot, HashSet<ISqlTableSource> onSources, HashSet<IQueryElement>? elementsToIgnore = null)
Parameters
testedRoot
IQueryElementonSources
HashSet<ISqlTableSource>elementsToIgnore
HashSet<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
expr
ISqlExpressionTested expression
Returns
- bool
true if tested expression is not a Field or Column
IsMutable(IQueryElement)
public static bool IsMutable(this IQueryElement expr)
Parameters
expr
IQueryElement
Returns
IsTransitiveExpression(SqlExpression, bool)
public static bool IsTransitiveExpression(SqlExpression sqlExpression, bool checkNullability)
Parameters
sqlExpression
SqlExpressioncheckNullability
bool
Returns
IsWindowFunction(IQueryElement)
public static bool IsWindowFunction(IQueryElement expr)
Parameters
expr
IQueryElement
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
context
TContextjoinFunc
context object.statement
SqlStatementSource statement.
joinFunc
Func<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
queries
SelectQuery[]Array of queries
MoveSearchConditionsToJoin(SelectQuery, SqlJoinedTable, List<SqlCondition>?)
public static void MoveSearchConditionsToJoin(SelectQuery sql, SqlJoinedTable joinedTable, List<SqlCondition>? movedConditions)
Parameters
sql
SelectQueryjoinedTable
SqlJoinedTablemovedConditions
List<SqlCondition>
NeedColumnForExpression(SelectQuery, ISqlExpression, bool)
Returns correct column or field according to nesting.
public static ISqlExpression? NeedColumnForExpression(SelectQuery selectQuery, ISqlExpression forExpression, bool inProjection)
Parameters
selectQuery
SelectQueryAnalyzed query.
forExpression
ISqlExpressionExpression that has to be enveloped by column.
inProjection
boolIf '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
expression
ISqlExpression
Returns
RemoveNotUnusedColumns(SelectQuery)
public static void RemoveNotUnusedColumns(this SelectQuery selectQuery)
Parameters
selectQuery
SelectQuery
RootQuery(SelectQuery)
public static SelectQuery RootQuery(this SelectQuery query)
Parameters
query
SelectQuery
Returns
ShouldCheckForNull(ISqlExpression)
public static bool ShouldCheckForNull(this ISqlExpression expr)
Parameters
expr
ISqlExpression
Returns
SuggestDbDataType(ISqlExpression)
public static DbDataType? SuggestDbDataType(ISqlExpression expr)
Parameters
expr
ISqlExpression
Returns
ToDebugString(IQueryElement)
public static string ToDebugString(this IQueryElement expr)
Parameters
expr
IQueryElement
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
selectQuery
SelectQueryInput 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
select
SelectQueryflags
SqlProviderFlags
Returns
TryEvaluateExpression(IQueryElement, EvaluationContext, out object?)
public static bool TryEvaluateExpression(this IQueryElement expr, EvaluationContext context, out object? result)
Parameters
expr
IQueryElementcontext
EvaluationContextresult
object
Returns
TryRemoveDistinct(SelectQuery, QueryInformation)
Detects when we can remove order
public static bool TryRemoveDistinct(SelectQuery selectQuery, QueryInformation information)
Parameters
selectQuery
SelectQueryinformation
QueryInformation
Returns
UnwrapExpression(ISqlExpression, bool)
public static ISqlExpression UnwrapExpression(ISqlExpression expr, bool checkNullability)
Parameters
expr
ISqlExpressioncheckNullability
bool
Returns
ValidateTable(SelectQuery, ISqlTableSource)
public static bool ValidateTable(SelectQuery selectQuery, ISqlTableSource table)
Parameters
selectQuery
SelectQuerytable
ISqlTableSource
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
statement
TStatementStatement which may contain tested query
queryToWrap
SelectQueryTells which select query needs enveloping
allowMutation
boolWrapped query can be not recreated for performance considerations.
Returns
- TStatement
The same
statement
or 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
context
TContextonWrap
andwrapTest
context object.statement
TStatementwrapTest
Func<TContext, SelectQuery, IQueryElement, bool>Delegate for testing when query needs to be wrapped.
onWrap
Action<TContext, SelectQuery, SelectQuery>After enveloping query this function called for prcess needed optimizations.
allowMutation
boolWrapped query can be not recreated for performance considerations.
withStack
boolMust be set to
true
, ifwrapTest
function use 3rd parameter (containing parent element) otherwise it will be always null.
Returns
- TStatement
The same
statement
or modified statement when wrapping has been performed.
Type Parameters
TStatement
TContext
Type of
onWrap
andwrapTest
context 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
context
TContextonWrap
andwrapTest
context object.statement
TStatementStatement which may contain tested query
wrapTest
Func<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
onWrap
Action<TContext, IReadOnlyList<SelectQuery>>After wrapping query this function called for prcess needed optimizations. Array of queries contains [QC, QB, QA]
allowMutation
boolWrapped query can be not recreated for performance considerations.
withStack
boolMust be set to
true
, ifwrapTest
function use 3rd parameter (containing parent element) otherwise it will be always null.
Returns
- TStatement
The same
statement
or modified statement when wrapping has been performed.
Type Parameters
TStatement
TContext
Type of
onWrap
andwrapTest
context object.