Table of Contents

Class QueryHelper

Namespace
LinqToDB.SqlQuery
Assembly
linq2db.dll
public static class QueryHelper
Inheritance
QueryHelper

Methods

CanBeEvaluated(IQueryElement, EvaluationContext)

public static bool CanBeEvaluated(this IQueryElement expr, EvaluationContext context)

Parameters

expr IQueryElement
context EvaluationContext

Returns

bool

CanBeEvaluated(IQueryElement, bool)

public static bool CanBeEvaluated(this IQueryElement expr, bool withParameters)

Parameters

expr IQueryElement
withParameters bool

Returns

bool

CanRemoveOrderBy(SelectQuery, SqlProviderFlags, QueryInformation)

Detects when we can remove order

public static bool CanRemoveOrderBy(SelectQuery selectQuery, SqlProviderFlags flags, QueryInformation information)

Parameters

selectQuery SelectQuery
flags SqlProviderFlags
information QueryInformation

Returns

bool

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 IQueryElement
sources 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 ISqlTableSource
includeDistinct bool

Flag 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 SqlTableSource
knownKeys 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 IQueryElement
found HashSet<ISqlTableSource>
ignore IEnumerable<IQueryElement>

ConcatSearchCondition(SqlWhereClause, SqlSearchCondition)

public static void ConcatSearchCondition(this SqlWhereClause where, SqlSearchCondition search)

Parameters

where SqlWhereClause
search SqlSearchCondition

ContainsAggregationFunctionOneLevel(IQueryElement)

public static bool ContainsAggregationFunctionOneLevel(IQueryElement expr)

Parameters

expr IQueryElement

Returns

bool

ContainsAggregationOrWindowFunction(IQueryElement)

public static bool ContainsAggregationOrWindowFunction(IQueryElement expr)

Parameters

expr IQueryElement

Returns

bool

ContainsAggregationOrWindowFunctionDeep(IQueryElement)

public static bool ContainsAggregationOrWindowFunctionDeep(IQueryElement expr)

Parameters

expr IQueryElement

Returns

bool

ContainsAggregationOrWindowFunctionOneLevel(IQueryElement)

public static bool ContainsAggregationOrWindowFunctionOneLevel(IQueryElement expr)

Parameters

expr IQueryElement

Returns

bool

ContainsElement(IQueryElement, IQueryElement)

public static bool ContainsElement(IQueryElement testedRoot, IQueryElement element)

Parameters

testedRoot IQueryElement
element IQueryElement

Returns

bool

ConvertFormatToConcatenation(string, IList<ISqlExpression>)

public static ISqlExpression ConvertFormatToConcatenation(string format, IList<ISqlExpression> parameters)

Parameters

format string
parameters IList<ISqlExpression>

Returns

ISqlExpression

CorrectSearchConditionNesting(SelectQuery, SqlCondition, HashSet<ISqlTableSource>)

public static SqlCondition CorrectSearchConditionNesting(SelectQuery sql, SqlCondition condition, HashSet<ISqlTableSource> forTableSources)

Parameters

sql SelectQuery
condition SqlCondition
forTableSources HashSet<ISqlTableSource>

Returns

SqlCondition

CountElements(ISqlExpression)

public static IDictionary<QueryElementType, int> CountElements(ISqlExpression expr)

Parameters

expr ISqlExpression

Returns

IDictionary<QueryElementType, int>

DependencyCount(IQueryElement, IQueryElement, HashSet<IQueryElement>?)

public static int DependencyCount(IQueryElement testedRoot, IQueryElement onElement, HashSet<IQueryElement>? elementsToIgnore = null)

Parameters

testedRoot IQueryElement
onElement IQueryElement
elementsToIgnore HashSet<IQueryElement>

Returns

int

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

SqlSearchCondition

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

SqlWhereClause

EnumerateAccessibleSources(SelectQuery)

Enumerates table sources recursively based on joins

public static IEnumerable<ISqlTableSource> EnumerateAccessibleSources(SelectQuery selectQuery)

Parameters

selectQuery SelectQuery

Returns

IEnumerable<ISqlTableSource>

EnumerateAccessibleSources(SqlTableSource)

public static IEnumerable<ISqlTableSource> EnumerateAccessibleSources(SqlTableSource tableSource)

Parameters

tableSource SqlTableSource

Returns

IEnumerable<ISqlTableSource>

EnumerateAccessibleTables(SelectQuery)

public static IEnumerable<SqlTable> EnumerateAccessibleTables(SelectQuery selectQuery)

Parameters

selectQuery SelectQuery

Returns

IEnumerable<SqlTable>

EnumerateInnerJoined(SelectQuery)

public static IEnumerable<SqlTableSource> EnumerateInnerJoined(SelectQuery selectQuery)

Parameters

selectQuery SelectQuery

Returns

IEnumerable<SqlTableSource>

EnumerateInnerJoined(SqlTableSource)

public static IEnumerable<SqlTableSource> EnumerateInnerJoined(SqlTableSource tableSource)

Parameters

tableSource SqlTableSource

Returns

IEnumerable<SqlTableSource>

EnumerateJoins(SelectQuery)

public static IEnumerable<SqlJoinedTable> EnumerateJoins(SelectQuery selectQuery)

Parameters

selectQuery SelectQuery

Returns

IEnumerable<SqlJoinedTable>

EnumerateJoins(SqlTableSource)

public static IEnumerable<SqlJoinedTable> EnumerateJoins(SqlTableSource tableSource)

Parameters

tableSource SqlTableSource

Returns

IEnumerable<SqlJoinedTable>

EnumerateLevelSources(SelectQuery)

public static IEnumerable<ISqlTableSource> EnumerateLevelSources(SelectQuery selectQuery)

Parameters

selectQuery SelectQuery

Returns

IEnumerable<ISqlTableSource>

EnumerateLevelSources(SqlTableSource)

public static IEnumerable<ISqlTableSource> EnumerateLevelSources(SqlTableSource tableSource)

Parameters

tableSource SqlTableSource

Returns

IEnumerable<ISqlTableSource>

EnumerateLevelTables(SelectQuery)

public static IEnumerable<SqlTable> EnumerateLevelTables(SelectQuery selectQuery)

Parameters

selectQuery SelectQuery

Returns

IEnumerable<SqlTable>

EvaluateBoolExpression(IQueryElement, EvaluationContext, bool?)

public static bool? EvaluateBoolExpression(this IQueryElement expr, EvaluationContext context, bool? defaultValue = null)

Parameters

expr IQueryElement
context EvaluationContext
defaultValue bool?

Returns

bool?

EvaluateExpression(IQueryElement, EvaluationContext)

public static object? EvaluateExpression(this IQueryElement expr, EvaluationContext context)

Parameters

expr IQueryElement
context EvaluationContext

Returns

object

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 SelectQuery
match Func<SqlJoinedTable, bool>

Returns

SqlJoinedTable

GenerateEquality(ISqlExpression, ISqlExpression, bool)

public static SqlCondition GenerateEquality(ISqlExpression field1, ISqlExpression field2, bool compareNullsAsValues)

Parameters

field1 ISqlExpression
field2 ISqlExpression
compareNullsAsValues bool

Returns

SqlCondition

GetBoolValue(ISqlExpression, EvaluationContext)

public static bool? GetBoolValue(ISqlExpression expression, EvaluationContext context)

Parameters

expression ISqlExpression
context EvaluationContext

Returns

bool?

GetColumnDescriptor(ISqlExpression?)

Returns ColumnDescriptor for expr.

public static ColumnDescriptor? GetColumnDescriptor(ISqlExpression? expr)

Parameters

expr ISqlExpression

Tested SQL Expression.

Returns

ColumnDescriptor

Associated column descriptor or null.

GetDbDataType(ISqlExpression?)

public static DbDataType GetDbDataType(ISqlExpression? expr)

Parameters

expr ISqlExpression

Returns

DbDataType

GetDeleteTable(SqlDeleteStatement)

public static SqlTable? GetDeleteTable(this SqlDeleteStatement deleteStatement)

Parameters

deleteStatement SqlDeleteStatement

Returns

SqlTable

GetExpressionType(ISqlExpression)

public static DbDataType GetExpressionType(this ISqlExpression expr)

Parameters

expr ISqlExpression

Returns

DbDataType

GetParameterValue(SqlParameter, IReadOnlyParameterValues?)

public static SqlParameterValue GetParameterValue(this SqlParameter parameter, IReadOnlyParameterValues? parameterValues)

Parameters

parameter SqlParameter
parameterValues IReadOnlyParameterValues

Returns

SqlParameterValue

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 SqlExpression
checkNullability bool

Returns

ISqlExpression

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

SqlTable

GetUsedSources(ISqlExpression, HashSet<ISqlTableSource>)

Retrieves which sources are used in the rootexpression

public static void GetUsedSources(ISqlExpression root, HashSet<ISqlTableSource> foundSources)

Parameters

root ISqlExpression

Expression to analyze.

foundSources HashSet<ISqlTableSource>

Output container for detected sources/

GetValueConverter(ISqlExpression?)

Returns IValueConverter for expr.

public static IValueConverter? GetValueConverter(ISqlExpression? expr)

Parameters

expr ISqlExpression

Tested 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

bool

HasQueryParameters(ISqlExpression)

public static bool HasQueryParameters(ISqlExpression expression)

Parameters

expression ISqlExpression

Returns

bool

IsAggregationFunction(IQueryElement)

public static bool IsAggregationFunction(IQueryElement expr)

Parameters

expr IQueryElement

Returns

bool

IsAggregationOrWindowFunction(IQueryElement)

public static bool IsAggregationOrWindowFunction(IQueryElement expr)

Parameters

expr IQueryElement

Returns

bool

IsComplexExpression(ISqlExpression)

public static bool IsComplexExpression(this ISqlExpression expr)

Parameters

expr ISqlExpression

Returns

bool

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 ISqlExpression

Tested expression.

Returns

bool

IsConstantFast(ISqlExpression)

public static bool IsConstantFast(ISqlExpression expr)

Parameters

expr ISqlExpression

Returns

bool

IsDependsOn(IQueryElement, IQueryElement, HashSet<IQueryElement>?)

public static bool IsDependsOn(IQueryElement testedRoot, IQueryElement onElement, HashSet<IQueryElement>? elementsToIgnore = null)

Parameters

testedRoot IQueryElement
onElement IQueryElement
elementsToIgnore HashSet<IQueryElement>

Returns

bool

IsDependsOn(IQueryElement, HashSet<ISqlTableSource>, HashSet<IQueryElement>?)

public static bool IsDependsOn(IQueryElement testedRoot, HashSet<ISqlTableSource> onSources, HashSet<IQueryElement>? elementsToIgnore = null)

Parameters

testedRoot IQueryElement
onSources HashSet<ISqlTableSource>
elementsToIgnore HashSet<IQueryElement>

Returns

bool

IsEqualTables(SqlTable?, SqlTable?)

public static bool IsEqualTables(SqlTable? table1, SqlTable? table2)

Parameters

table1 SqlTable
table2 SqlTable

Returns

bool

IsExpression(ISqlExpression)

Returns true if it is anything except Field or Column.

public static bool IsExpression(ISqlExpression expr)

Parameters

expr ISqlExpression

Tested 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

bool

IsTransitiveExpression(SqlExpression, bool)

public static bool IsTransitiveExpression(SqlExpression sqlExpression, bool checkNullability)

Parameters

sqlExpression SqlExpression
checkNullability bool

Returns

bool

IsWindowFunction(IQueryElement)

public static bool IsWindowFunction(IQueryElement expr)

Parameters

expr IQueryElement

Returns

bool

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 TContext

joinFunc context object.

statement SqlStatement

Source 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 SelectQuery
joinedTable SqlJoinedTable
movedConditions 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 SelectQuery

Analyzed query.

forExpression ISqlExpression

Expression that has to be enveloped by column.

inProjection bool

If '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

bool

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

SelectQuery

ShouldCheckForNull(ISqlExpression)

public static bool ShouldCheckForNull(this ISqlExpression expr)

Parameters

expr ISqlExpression

Returns

bool

SuggestDbDataType(ISqlExpression)

public static DbDataType? SuggestDbDataType(ISqlExpression expr)

Parameters

expr ISqlExpression

Returns

DbDataType?

ToDebugString(IQueryElement)

public static string ToDebugString(this IQueryElement expr)

Parameters

expr IQueryElement

Returns

string

TransformExpressionIndexes<TContext>(TContext, string, Func<TContext, int, int>)

public static string TransformExpressionIndexes<TContext>(TContext context, string expression, Func<TContext, int, int> transformFunc)

Parameters

context TContext
expression string
transformFunc Func<TContext, int, int>

Returns

string

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 SelectQuery

Input 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 SelectQuery
flags SqlProviderFlags

Returns

bool

TryEvaluateExpression(IQueryElement, EvaluationContext, out object?)

public static bool TryEvaluateExpression(this IQueryElement expr, EvaluationContext context, out object? result)

Parameters

expr IQueryElement
context EvaluationContext
result object

Returns

bool

TryRemoveDistinct(SelectQuery, QueryInformation)

Detects when we can remove order

public static bool TryRemoveDistinct(SelectQuery selectQuery, QueryInformation information)

Parameters

selectQuery SelectQuery
information QueryInformation

Returns

bool

UnwrapExpression(ISqlExpression, bool)

public static ISqlExpression UnwrapExpression(ISqlExpression expr, bool checkNullability)

Parameters

expr ISqlExpression
checkNullability bool

Returns

ISqlExpression

ValidateTable(SelectQuery, ISqlTableSource)

public static bool ValidateTable(SelectQuery selectQuery, ISqlTableSource table)

Parameters

selectQuery SelectQuery
table ISqlTableSource

Returns

bool

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 TStatement

Statement which may contain tested query

queryToWrap SelectQuery

Tells which select query needs enveloping

allowMutation bool

Wrapped 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 TContext

onWrap and wrapTest context object.

statement TStatement
wrapTest 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 bool

Wrapped query can be not recreated for performance considerations.

withStack bool

Must be set to true, if wrapTest 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 and wrapTest 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 TContext

onWrap and wrapTest context object.

statement TStatement

Statement 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 bool

Wrapped query can be not recreated for performance considerations.

withStack bool

Must be set to true, if wrapTest 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 and wrapTest context object.