Class QueryHelper
Assembly: linq2db.dll
Syntax
public static class QueryHelper
Methods
|
Improve this Doc
View Source
CanBeEvaluated(IQueryElement, EvaluationContext)
Declaration
public static bool CanBeEvaluated(this IQueryElement expr, EvaluationContext context)
Parameters
Returns
|
Improve this Doc
View Source
CanBeEvaluated(IQueryElement, bool)
Declaration
public static bool CanBeEvaluated(this IQueryElement expr, bool withParameters)
Parameters
Returns
|
Improve this Doc
View Source
Detects when we can remove order
Declaration
public static bool CanRemoveOrderBy(SelectQuery selectQuery, SqlProviderFlags flags, QueryInformation information)
Parameters
Returns
|
Improve this Doc
View Source
CollectDependencies(IQueryElement, IEnumerable<ISqlTableSource>, HashSet<ISqlExpression>, IEnumerable<IQueryElement>?)
Declaration
public static void CollectDependencies(IQueryElement root, IEnumerable<ISqlTableSource> sources, HashSet<ISqlExpression> found, IEnumerable<IQueryElement>? ignore = null)
Parameters
|
Improve this Doc
View Source
CollectUniqueKeys(ISqlTableSource, bool, List<IList<ISqlExpression>>)
Collects unique keys from different sources.
Declaration
public static void CollectUniqueKeys(ISqlTableSource tableSource, bool includeDistinct, List<IList<ISqlExpression>> knownKeys)
Parameters
|
Improve this Doc
View Source
CollectUniqueKeys(SqlTableSource, List<IList<ISqlExpression>>)
Collects unique keys from different sources.
Declaration
public static void CollectUniqueKeys(SqlTableSource tableSource, List<IList<ISqlExpression>> knownKeys)
Parameters
|
Improve this Doc
View Source
CollectUsedSources(IQueryElement, HashSet<ISqlTableSource>, IEnumerable<IQueryElement>?)
Declaration
public static void CollectUsedSources(IQueryElement root, HashSet<ISqlTableSource> found, IEnumerable<IQueryElement>? ignore = null)
Parameters
|
Improve this Doc
View Source
ConcatSearchCondition(SqlWhereClause, SqlSearchCondition)
Declaration
public static void ConcatSearchCondition(this SqlWhereClause where, SqlSearchCondition search)
Parameters
|
Improve this Doc
View Source
ContainsAggregationOrWindowFunction(IQueryElement)
Declaration
public static bool ContainsAggregationOrWindowFunction(IQueryElement expr)
Parameters
Returns
|
Improve this Doc
View Source
ContainsAggregationOrWindowFunctionDeep(IQueryElement)
Declaration
public static bool ContainsAggregationOrWindowFunctionDeep(IQueryElement expr)
Parameters
Returns
|
Improve this Doc
View Source
ContainsAggregationOrWindowFunctionOneLevel(IQueryElement)
Declaration
public static bool ContainsAggregationOrWindowFunctionOneLevel(IQueryElement expr)
Parameters
Returns
|
Improve this Doc
View Source
ContainsElement(IQueryElement, IQueryElement)
Declaration
public static bool ContainsElement(IQueryElement testedRoot, IQueryElement element)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static ISqlExpression ConvertFormatToConcatenation(string format, IList<ISqlExpression> parameters)
Parameters
Returns
|
Improve this Doc
View Source
CorrectSearchConditionNesting(SelectQuery, SqlCondition, HashSet<ISqlTableSource>)
Declaration
public static SqlCondition CorrectSearchConditionNesting(SelectQuery sql, SqlCondition condition, HashSet<ISqlTableSource> forTableSources)
Parameters
Returns
|
Improve this Doc
View Source
CountElements(ISqlExpression)
Declaration
public static IDictionary<QueryElementType, int> CountElements(ISqlExpression expr)
Parameters
Returns
|
Improve this Doc
View Source
DependencyCount(IQueryElement, IQueryElement, HashSet<IQueryElement>?)
Declaration
public static int DependencyCount(IQueryElement testedRoot, IQueryElement onElement, HashSet<IQueryElement>? elementsToIgnore = null)
Parameters
Returns
|
Improve this Doc
View Source
EnsureConjunction(SqlSearchCondition)
Ensures that expression is not A OR B but (A OR B)
Function makes all needed manipulations for that
Declaration
public static SqlSearchCondition EnsureConjunction(this SqlSearchCondition searchCondition)
Parameters
Returns
|
Improve this Doc
View Source
EnsureConjunction(SqlWhereClause)
Ensures that expression is not A OR B but (A OR B)
Function makes all needed manipulations for that
Declaration
public static SqlWhereClause EnsureConjunction(this SqlWhereClause whereClause)
Parameters
Returns
|
Improve this Doc
View Source
EnumerateAccessibleSources(SelectQuery)
Enumerates table sources recursively based on joins
Declaration
public static IEnumerable<ISqlTableSource> EnumerateAccessibleSources(SelectQuery selectQuery)
Parameters
Returns
|
Improve this Doc
View Source
EnumerateAccessibleSources(SqlTableSource)
Declaration
public static IEnumerable<ISqlTableSource> EnumerateAccessibleSources(SqlTableSource tableSource)
Parameters
Returns
|
Improve this Doc
View Source
EnumerateAccessibleTables(SelectQuery)
Declaration
public static IEnumerable<SqlTable> EnumerateAccessibleTables(SelectQuery selectQuery)
Parameters
Returns
|
Improve this Doc
View Source
EnumerateInnerJoined(SelectQuery)
Declaration
public static IEnumerable<SqlTableSource> EnumerateInnerJoined(SelectQuery selectQuery)
Parameters
Returns
|
Improve this Doc
View Source
EnumerateInnerJoined(SqlTableSource)
Declaration
public static IEnumerable<SqlTableSource> EnumerateInnerJoined(SqlTableSource tableSource)
Parameters
Returns
|
Improve this Doc
View Source
EnumerateJoins(SelectQuery)
Declaration
public static IEnumerable<SqlJoinedTable> EnumerateJoins(SelectQuery selectQuery)
Parameters
Returns
|
Improve this Doc
View Source
EnumerateJoins(SqlTableSource)
Declaration
public static IEnumerable<SqlJoinedTable> EnumerateJoins(SqlTableSource tableSource)
Parameters
Returns
|
Improve this Doc
View Source
EnumerateLevelSources(SelectQuery)
Declaration
public static IEnumerable<ISqlTableSource> EnumerateLevelSources(SelectQuery selectQuery)
Parameters
Returns
|
Improve this Doc
View Source
EnumerateLevelSources(SqlTableSource)
Declaration
public static IEnumerable<ISqlTableSource> EnumerateLevelSources(SqlTableSource tableSource)
Parameters
Returns
|
Improve this Doc
View Source
EnumerateLevelTables(SelectQuery)
Declaration
public static IEnumerable<SqlTable> EnumerateLevelTables(SelectQuery selectQuery)
Parameters
Returns
|
Improve this Doc
View Source
EvaluateBoolExpression(IQueryElement, EvaluationContext, bool?)
Declaration
public static bool? EvaluateBoolExpression(this IQueryElement expr, EvaluationContext context, bool? defaultValue = null)
Parameters
Returns
|
Improve this Doc
View Source
EvaluateExpression(IQueryElement, EvaluationContext)
Declaration
public static object? EvaluateExpression(this IQueryElement expr, EvaluationContext context)
Parameters
Returns
|
Improve this Doc
View Source
Returns SqlField from specific expression. Usually from SqlColumn.
Conversion is ignored.
Declaration
public static SqlField? ExtractField(ISqlExpression expression)
Parameters
Returns
Type |
Description |
SqlField |
Field instance associated with expression
|
|
Improve this Doc
View Source
FindJoin(SelectQuery, Func<SqlJoinedTable, bool>)
Declaration
public static SqlJoinedTable? FindJoin(this SelectQuery query, Func<SqlJoinedTable, bool> match)
Parameters
Returns
|
Improve this Doc
View Source
GenerateEquality(ISqlExpression, ISqlExpression, bool)
Declaration
public static SqlCondition GenerateEquality(ISqlExpression field1, ISqlExpression field2, bool compareNullsAsValues)
Parameters
Returns
|
Improve this Doc
View Source
GetBoolValue(ISqlExpression, EvaluationContext)
Declaration
public static bool? GetBoolValue(ISqlExpression expression, EvaluationContext context)
Parameters
Returns
|
Improve this Doc
View Source
GetColumnDescriptor(ISqlExpression?)
Declaration
public static ColumnDescriptor? GetColumnDescriptor(ISqlExpression? expr)
Parameters
Returns
|
Improve this Doc
View Source
GetDbDataType(ISqlExpression?)
Declaration
public static DbDataType GetDbDataType(ISqlExpression? expr)
Parameters
Returns
|
Improve this Doc
View Source
GetDeleteTable(SqlDeleteStatement)
Declaration
public static SqlTable? GetDeleteTable(this SqlDeleteStatement deleteStatement)
Parameters
Returns
|
Improve this Doc
View Source
GetExpressionType(ISqlExpression)
Declaration
public static DbDataType GetExpressionType(this ISqlExpression expr)
Parameters
Returns
|
Improve this Doc
View Source
GetParameterValue(SqlParameter, IReadOnlyParameterValues?)
Declaration
public static SqlParameterValue GetParameterValue(this SqlParameter parameter, IReadOnlyParameterValues? parameterValues)
Parameters
Returns
|
Improve this Doc
View Source
GetUnderlyingExpression(ISqlExpression?)
Unwraps SqlColumn and returns underlying expression.
Declaration
public static ISqlExpression? GetUnderlyingExpression(ISqlExpression? expression)
Parameters
Returns
|
Improve this Doc
View Source
GetUnderlyingExpressionValue(SqlExpression, bool)
Declaration
public static ISqlExpression GetUnderlyingExpressionValue(SqlExpression sqlExpression, bool checkNullability)
Parameters
Returns
|
Improve this Doc
View Source
GetUnderlyingField(ISqlExpression)
Returns SqlField from specific expression. Usually from SqlColumn.
Complex expressions ignored.
Declaration
public static SqlField? GetUnderlyingField(ISqlExpression expression)
Parameters
Returns
Type |
Description |
SqlField |
Field instance associated with expression
|
|
Improve this Doc
View Source
GetUpdateTable(SqlUpdateStatement)
Declaration
public static SqlTable? GetUpdateTable(this SqlUpdateStatement updateStatement)
Parameters
Returns
|
Improve this Doc
View Source
GetUsedSources(ISqlExpression, HashSet<ISqlTableSource>)
Retrieves which sources are used in the root
expression
Declaration
public static void GetUsedSources(ISqlExpression root, HashSet<ISqlTableSource> foundSources)
Parameters
|
Improve this Doc
View Source
GetValueConverter(ISqlExpression?)
Declaration
public static IValueConverter? GetValueConverter(ISqlExpression? expr)
Parameters
Returns
|
Improve this Doc
View Source
HasOuterReferences(ISet<ISqlTableSource>, ISqlExpression)
Declaration
public static bool HasOuterReferences(ISet<ISqlTableSource> sources, ISqlExpression expr)
Parameters
Returns
|
Improve this Doc
View Source
HasQueryParameters(ISqlExpression)
Declaration
public static bool HasQueryParameters(ISqlExpression expression)
Parameters
Returns
|
Improve this Doc
View Source
IsAggregationFunction(IQueryElement)
Declaration
public static bool IsAggregationFunction(IQueryElement expr)
Parameters
Returns
|
Improve this Doc
View Source
IsAggregationOrWindowFunction(IQueryElement)
Declaration
public static bool IsAggregationOrWindowFunction(IQueryElement expr)
Parameters
Returns
|
Improve this Doc
View Source
IsComplexExpression(ISqlExpression)
Declaration
public static bool IsComplexExpression(this ISqlExpression expr)
Parameters
Returns
|
Improve this Doc
View Source
IsConstant(ISqlExpression)
Returns true
if tested expression is constant during query execution (e.g. value or parameter).
Declaration
public static bool IsConstant(ISqlExpression expr)
Parameters
Returns
|
Improve this Doc
View Source
IsConstantFast(ISqlExpression)
Declaration
public static bool IsConstantFast(ISqlExpression expr)
Parameters
Returns
|
Improve this Doc
View Source
IsDependsOn(IQueryElement, IQueryElement, HashSet<IQueryElement>?)
Declaration
public static bool IsDependsOn(IQueryElement testedRoot, IQueryElement onElement, HashSet<IQueryElement>? elementsToIgnore = null)
Parameters
Returns
|
Improve this Doc
View Source
IsDependsOn(IQueryElement, HashSet<ISqlTableSource>, HashSet<IQueryElement>?)
Declaration
public static bool IsDependsOn(IQueryElement testedRoot, HashSet<ISqlTableSource> onSources, HashSet<IQueryElement>? elementsToIgnore = null)
Parameters
Returns
|
Improve this Doc
View Source
IsEqualTables(SqlTable?, SqlTable?)
Declaration
public static bool IsEqualTables(SqlTable? table1, SqlTable? table2)
Parameters
Returns
|
Improve this Doc
View Source
IsExpression(ISqlExpression)
Returns true if it is anything except Field or Column.
Declaration
public static bool IsExpression(ISqlExpression expr)
Parameters
Returns
Type |
Description |
bool |
true if tested expression is not a Field or Column
|
|
Improve this Doc
View Source
IsMutable(IQueryElement)
Declaration
public static bool IsMutable(this IQueryElement expr)
Parameters
Returns
|
Improve this Doc
View Source
IsTransitiveExpression(SqlExpression, bool)
Declaration
public static bool IsTransitiveExpression(SqlExpression sqlExpression, bool checkNullability)
Parameters
Returns
|
Improve this Doc
View Source
IsWindowFunction(IQueryElement)
Declaration
public static bool IsWindowFunction(IQueryElement expr)
Parameters
Returns
|
Improve this Doc
View Source
JoinRemoval<TContext>(TContext, SqlStatement, Func<TContext, SqlStatement, SqlJoinedTable, bool>)
Removes Join from query based on joinFunc
result.
Declaration
public static SqlStatement JoinRemoval<TContext>(TContext context, SqlStatement statement, Func<TContext, SqlStatement, SqlJoinedTable, bool> joinFunc)
Parameters
Returns
Type |
Description |
SqlStatement |
Same or new statement with removed joins.
|
Type Parameters
Name |
Description |
TContext |
|
|
Improve this Doc
View Source
MoveOrderByUp(params SelectQuery[])
Helper function for moving Ordering up in select tree.
Declaration
public static void MoveOrderByUp(params SelectQuery[] queries)
Parameters
Type |
Name |
Description |
SelectQuery[] |
queries |
Array of queries
|
|
Improve this Doc
View Source
MoveSearchConditionsToJoin(SelectQuery, SqlJoinedTable, List<SqlCondition>?)
Declaration
public static void MoveSearchConditionsToJoin(SelectQuery sql, SqlJoinedTable joinedTable, List<SqlCondition>? movedConditions)
Parameters
|
Improve this Doc
View Source
NeedColumnForExpression(SelectQuery, ISqlExpression, bool)
Returns correct column or field according to nesting.
Declaration
public static ISqlExpression? NeedColumnForExpression(SelectQuery selectQuery, ISqlExpression forExpression, bool inProjection)
Parameters
Type |
Name |
Description |
SelectQuery |
selectQuery |
Analyzed query.
|
ISqlExpression |
forExpression |
Expression that has to be enveloped by column.
|
bool |
inProjection |
If 'true', function ensures that column is created. If 'false' it may return Field if it fits to nesting level.
|
Returns
Type |
Description |
ISqlExpression |
Returns Column of Field according to its nesting level. May return null if expression is not valid for selectQuery
|
|
Improve this Doc
View Source
NeedParameterInlining(ISqlExpression)
Declaration
public static bool NeedParameterInlining(ISqlExpression expression)
Parameters
Returns
|
Improve this Doc
View Source
RemoveNotUnusedColumns(SelectQuery)
Declaration
public static void RemoveNotUnusedColumns(this SelectQuery selectQuery)
Parameters
|
Improve this Doc
View Source
RootQuery(SelectQuery)
Declaration
public static SelectQuery RootQuery(this SelectQuery query)
Parameters
Returns
|
Improve this Doc
View Source
ShouldCheckForNull(ISqlExpression)
Declaration
public static bool ShouldCheckForNull(this ISqlExpression expr)
Parameters
Returns
|
Improve this Doc
View Source
SuggestDbDataType(ISqlExpression)
Declaration
public static DbDataType? SuggestDbDataType(ISqlExpression expr)
Parameters
Returns
|
Improve this Doc
View Source
ToDebugString(IQueryElement)
Declaration
public static string ToDebugString(this IQueryElement expr)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static string TransformExpressionIndexes<TContext>(TContext context, string expression, Func<TContext, int, int> transformFunc)
Parameters
Type |
Name |
Description |
TContext |
context |
|
string |
expression |
|
Func<TContext, int, int> |
transformFunc |
|
Returns
Type Parameters
Name |
Description |
TContext |
|
|
Improve this Doc
View Source
Transforms
SELECT * FROM A
INNER JOIN B ON A.ID = B.ID
to
SELECT * FROM A, B
WHERE A.ID = B.ID
Declaration
public static SelectQuery TransformInnerJoinsToWhere(this SelectQuery selectQuery)
Parameters
Type |
Name |
Description |
SelectQuery |
selectQuery |
Input SelectQuery.
|
Returns
|
Improve this Doc
View Source
TryConvertOrderedDistinctToGroupBy(SelectQuery, SqlProviderFlags)
Converts ORDER BY DISTINCT to GROUP BY equivalent
Declaration
public static bool TryConvertOrderedDistinctToGroupBy(SelectQuery select, SqlProviderFlags flags)
Parameters
Returns
|
Improve this Doc
View Source
TryEvaluateExpression(IQueryElement, EvaluationContext, out object?)
Declaration
public static bool TryEvaluateExpression(this IQueryElement expr, EvaluationContext context, out object? result)
Parameters
Returns
|
Improve this Doc
View Source
Detects when we can remove order
Declaration
public static bool TryRemoveDistinct(SelectQuery selectQuery, QueryInformation information)
Parameters
Returns
|
Improve this Doc
View Source
UnwrapExpression(ISqlExpression, bool)
Declaration
public static ISqlExpression UnwrapExpression(ISqlExpression expr, bool checkNullability)
Parameters
Returns
|
Improve this Doc
View Source
ValidateTable(SelectQuery, ISqlTableSource)
Declaration
public static bool ValidateTable(SelectQuery selectQuery, ISqlTableSource table)
Parameters
Returns
|
Improve this Doc
View Source
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
Declaration
public static TStatement WrapQuery<TStatement>(TStatement statement, SelectQuery queryToWrap, bool allowMutation) where TStatement : SqlStatement
Parameters
Type |
Name |
Description |
TStatement |
statement |
Statement which may contain tested query
|
SelectQuery |
queryToWrap |
Tells which select query needs enveloping
|
bool |
allowMutation |
Wrapped query can be not recreated for performance considerations.
|
Returns
Type |
Description |
TStatement |
The same statement or modified statement when wrapping has been performed.
|
Type Parameters
Name |
Description |
TStatement |
|
|
Improve this Doc
View Source
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.
Declaration
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
Type |
Name |
Description |
TContext |
context |
onWrap and wrapTest context object.
|
TStatement |
statement |
|
Func<TContext, SelectQuery, IQueryElement, bool> |
wrapTest |
Delegate for testing when query needs to be wrapped.
|
Action<TContext, SelectQuery, SelectQuery> |
onWrap |
After enveloping query this function called for prcess needed optimizations.
|
bool |
allowMutation |
Wrapped query can be not recreated for performance considerations.
|
bool |
withStack |
Must be set to true , if wrapTest function use 3rd parameter (containing parent element) otherwise it will be always null.
|
Returns
Type |
Description |
TStatement |
The same statement or modified statement when wrapping has been performed.
|
Type Parameters
Name |
Description |
TStatement |
|
TContext |
Type of onWrap and wrapTest context object.
|
|
Improve this Doc
View Source
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
Declaration
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
Type |
Name |
Description |
TContext |
context |
onWrap and wrapTest context object.
|
TStatement |
statement |
Statement which may contain tested query
|
Func<TContext, SelectQuery, IQueryElement, int> |
wrapTest |
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
|
Action<TContext, IReadOnlyList<SelectQuery>> |
onWrap |
After wrapping query this function called for prcess needed optimizations. Array of queries contains [QC, QB, QA]
|
bool |
allowMutation |
Wrapped query can be not recreated for performance considerations.
|
bool |
withStack |
Must be set to true , if wrapTest function use 3rd parameter (containing parent element) otherwise it will be always null.
|
Returns
Type |
Description |
TStatement |
The same statement or modified statement when wrapping has been performed.
|
Type Parameters
Name |
Description |
TStatement |
|
TContext |
Type of onWrap and wrapTest context object.
|