Class BasicSqlOptimizer
- Namespace
- LinqToDB.SqlProvider
- Assembly
- linq2db.dll
public class BasicSqlOptimizer : ISqlOptimizer
- Inheritance
-
BasicSqlOptimizer
- Implements
- Derived
- Extension Methods
Constructors
BasicSqlOptimizer(SqlProviderFlags)
protected BasicSqlOptimizer(SqlProviderFlags sqlProviderFlags)
Parameters
sqlProviderFlags
SqlProviderFlags
Fields
StandardLikeCharactersToEscape
protected static string[] StandardLikeCharactersToEscape
Field Value
- string[]
Properties
CanCompareSearchConditions
public virtual bool CanCompareSearchConditions { get; }
Property Value
LikeCharactersToEscape
Characters with special meaning in LIKE predicate (defined by LikeCharactersToEscape) that should be escaped to be used as matched character.
Default: ["%", "_", "?", "*", "#", "[", "]"]
.
public virtual string[] LikeCharactersToEscape { get; }
Property Value
- string[]
LikeEscapeCharacter
Escape sequence/character to escape special characters in LIKE predicate (defined by LikeCharactersToEscape).
Default: "~"
.
public virtual string LikeEscapeCharacter { get; }
Property Value
LikeIsEscapeSupported
Should be true
for provider with LIKE ... ESCAPE
modifier support.
Default: true
.
public virtual bool LikeIsEscapeSupported { get; }
Property Value
LikePatternParameterSupport
public virtual bool LikePatternParameterSupport { get; }
Property Value
LikeValueParameterSupport
public virtual bool LikeValueParameterSupport { get; }
Property Value
LikeWildcardCharacter
public virtual string LikeWildcardCharacter { get; }
Property Value
SqlProviderFlags
public SqlProviderFlags SqlProviderFlags { get; }
Property Value
Methods
Add(ISqlExpression, ISqlExpression, Type)
public ISqlExpression Add(ISqlExpression expr1, ISqlExpression expr2, Type type)
Parameters
expr1
ISqlExpressionexpr2
ISqlExpressiontype
Type
Returns
Add(ISqlExpression, int)
public ISqlExpression Add(ISqlExpression expr1, int value)
Parameters
expr1
ISqlExpressionvalue
int
Returns
Add<T>(ISqlExpression, ISqlExpression)
public ISqlExpression Add<T>(ISqlExpression expr1, ISqlExpression expr2)
Parameters
expr1
ISqlExpressionexpr2
ISqlExpression
Returns
Type Parameters
T
AlternativeConvertToBoolean(SqlFunction, DataOptions, int)
protected ISqlExpression? AlternativeConvertToBoolean(SqlFunction func, DataOptions dataOptions, int paramNumber)
Parameters
func
SqlFunctiondataOptions
DataOptionsparamNumber
int
Returns
CheckAliases(SqlStatement, int)
protected void CheckAliases(SqlStatement statement, int maxLen)
Parameters
statement
SqlStatementmaxLen
int
ConvertBetweenPredicate(Between)
public virtual ISqlPredicate ConvertBetweenPredicate(SqlPredicate.Between between)
Parameters
between
SqlPredicate.Between
Returns
ConvertBooleanExprToCase(ISqlExpression)
protected ISqlExpression ConvertBooleanExprToCase(ISqlExpression expression)
Parameters
expression
ISqlExpression
Returns
ConvertCoalesceToBinaryFunc(SqlFunction, string, bool)
protected ISqlExpression ConvertCoalesceToBinaryFunc(SqlFunction func, string funcName, bool supportsParameters = true)
Parameters
func
SqlFunctionfuncName
stringsupportsParameters
bool
Returns
ConvertConversion(SqlFunction)
Implements CONVERT function converter.
protected virtual ISqlExpression ConvertConversion(SqlFunction func)
Parameters
func
SqlFunction
Returns
ConvertCountSubQuery(SelectQuery)
public virtual bool ConvertCountSubQuery(SelectQuery subQuery)
Parameters
subQuery
SelectQuery
Returns
ConvertElement(MappingSchema, DataOptions, IQueryElement?, OptimizationContext)
Converts query element to specific provider dialect.
public virtual IQueryElement? ConvertElement(MappingSchema mappingSchema, DataOptions dataOptions, IQueryElement? element, OptimizationContext context)
Parameters
mappingSchema
MappingSchemadataOptions
DataOptionselement
IQueryElementcontext
OptimizationContext
Returns
ConvertExpressionImpl(ISqlExpression, ConvertVisitor<RunOptimizationContext>)
public virtual ISqlExpression ConvertExpressionImpl(ISqlExpression expression, ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext> visitor)
Parameters
expression
ISqlExpressionvisitor
ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext>
Returns
ConvertFunction(SqlFunction)
protected virtual ISqlExpression ConvertFunction(SqlFunction func)
Parameters
func
SqlFunction
Returns
ConvertFunctionParameters(SqlFunction, bool)
protected SqlFunction ConvertFunctionParameters(SqlFunction func, bool withParameters = false)
Parameters
func
SqlFunctionwithParameters
bool
Returns
ConvertInListPredicate(MappingSchema, InList, EvaluationContext)
public virtual ISqlPredicate ConvertInListPredicate(MappingSchema mappingSchema, SqlPredicate.InList p, EvaluationContext context)
Parameters
mappingSchema
MappingSchemap
SqlPredicate.InListcontext
EvaluationContext
Returns
ConvertLikePredicate(MappingSchema, Like, EvaluationContext)
LIKE predicate interceptor. Default implementation does nothing.
public virtual ISqlPredicate ConvertLikePredicate(MappingSchema mappingSchema, SqlPredicate.Like predicate, EvaluationContext context)
Parameters
mappingSchema
MappingSchemaCurrent mapping schema.
predicate
SqlPredicate.LikeLIKE predicate.
context
EvaluationContextParameters evaluation context for current query.
Returns
- ISqlPredicate
Preprocessed predicate.
ConvertPredicateImpl(ISqlPredicate, ConvertVisitor<RunOptimizationContext>)
public virtual ISqlPredicate ConvertPredicateImpl(ISqlPredicate predicate, ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext> visitor)
Parameters
predicate
ISqlPredicatevisitor
ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext>
Returns
ConvertSearchStringPredicate(SearchString, ConvertVisitor<RunOptimizationContext>)
public virtual ISqlPredicate ConvertSearchStringPredicate(SqlPredicate.SearchString predicate, ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext> visitor)
Parameters
predicate
SqlPredicate.SearchStringvisitor
ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext>
Returns
ConvertSearchStringPredicateViaLike(SearchString, ConvertVisitor<RunOptimizationContext>)
protected ISqlPredicate ConvertSearchStringPredicateViaLike(SqlPredicate.SearchString predicate, ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext> visitor)
Parameters
predicate
SqlPredicate.SearchStringvisitor
ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext>
Returns
ConvertSkipTake(MappingSchema, DataOptions, SelectQuery, OptimizationContext, out ISqlExpression?, out ISqlExpression?)
Corrects skip/take for specific DataProvider
public virtual void ConvertSkipTake(MappingSchema mappingSchema, DataOptions dataOptions, SelectQuery selectQuery, OptimizationContext optimizationContext, out ISqlExpression? takeExpr, out ISqlExpression? skipExpr)
Parameters
mappingSchema
MappingSchemadataOptions
DataOptionsselectQuery
SelectQueryoptimizationContext
OptimizationContexttakeExpr
ISqlExpressionskipExpr
ISqlExpression
CorrectUnionOrderBy(SqlStatement)
protected virtual SqlStatement CorrectUnionOrderBy(SqlStatement statement)
Parameters
statement
SqlStatement
Returns
CorrectUpdateTable(SqlUpdateStatement)
Corrects situation when update table is located in JOIN clause. Usually it is generated by associations.
protected SqlUpdateStatement CorrectUpdateTable(SqlUpdateStatement statement)
Parameters
statement
SqlUpdateStatementStatement to examine.
Returns
- SqlUpdateStatement
Corrected statement.
CreateSqlValue(object?, DbDataType, params ISqlExpression[])
public static ISqlExpression CreateSqlValue(object? value, DbDataType dbDataType, params ISqlExpression[] basedOn)
Parameters
value
objectdbDataType
DbDataTypebasedOn
ISqlExpression[]
Returns
CreateSqlValue(object?, SqlBinaryExpression)
public static ISqlExpression CreateSqlValue(object? value, SqlBinaryExpression be)
Parameters
value
objectbe
SqlBinaryExpression
Returns
Dec(ISqlExpression)
public ISqlExpression Dec(ISqlExpression expr1)
Parameters
expr1
ISqlExpression
Returns
Div(ISqlExpression, ISqlExpression, Type)
public ISqlExpression Div(ISqlExpression expr1, ISqlExpression expr2, Type type)
Parameters
expr1
ISqlExpressionexpr2
ISqlExpressiontype
Type
Returns
Div(ISqlExpression, int)
public ISqlExpression Div(ISqlExpression expr1, int value)
Parameters
expr1
ISqlExpressionvalue
int
Returns
Div<T>(ISqlExpression, ISqlExpression)
public ISqlExpression Div<T>(ISqlExpression expr1, ISqlExpression expr2)
Parameters
expr1
ISqlExpressionexpr2
ISqlExpression
Returns
Type Parameters
T
EscapeLikeCharacters(ISqlExpression, ref ISqlExpression?)
public virtual ISqlExpression EscapeLikeCharacters(ISqlExpression expression, ref ISqlExpression? escape)
Parameters
expression
ISqlExpressionescape
ISqlExpression
Returns
EscapeLikeCharacters(string, string)
public virtual string EscapeLikeCharacters(string str, string escape)
Parameters
Returns
EscapeLikePattern(string)
Implements LIKE pattern escaping logic for provider without ESCAPE clause support (LikeIsEscapeSupported is false
).
Default logic prefix characters from LikeCharactersToEscape with LikeEscapeCharacter.
protected virtual string EscapeLikePattern(string str)
Parameters
str
stringRaw pattern value.
Returns
- string
Escaped pattern value.
Finalize(MappingSchema, SqlStatement, DataOptions)
Finalizes query.
public virtual SqlStatement Finalize(MappingSchema mappingSchema, SqlStatement statement, DataOptions dataOptions)
Parameters
mappingSchema
MappingSchemastatement
SqlStatementdataOptions
DataOptions
Returns
- SqlStatement
Query which is ready for optimization.
FinalizeStatement(SqlStatement, EvaluationContext, DataOptions)
public virtual SqlStatement FinalizeStatement(SqlStatement statement, EvaluationContext context, DataOptions dataOptions)
Parameters
statement
SqlStatementcontext
EvaluationContextdataOptions
DataOptions
Returns
FindUpdateTable(SelectQuery, SqlTable)
protected SqlTable? FindUpdateTable(SelectQuery selectQuery, SqlTable tableToFind)
Parameters
selectQuery
SelectQuerytableToFind
SqlTable
Returns
FixEmptySelect(SqlStatement)
protected virtual void FixEmptySelect(SqlStatement statement)
Parameters
statement
SqlStatement
FixSetOperationNulls(SqlStatement)
protected virtual SqlStatement FixSetOperationNulls(SqlStatement statement)
Parameters
statement
SqlStatement
Returns
FloorBeforeConvert(SqlFunction)
protected ISqlExpression FloorBeforeConvert(SqlFunction func)
Parameters
func
SqlFunction
Returns
GenerateEscapeReplacement(ISqlExpression, ISqlExpression)
public static ISqlExpression GenerateEscapeReplacement(ISqlExpression expression, ISqlExpression character)
Parameters
expression
ISqlExpressioncharacter
ISqlExpression
Returns
GetAlternativeDelete(SqlDeleteStatement, DataOptions)
protected SqlDeleteStatement GetAlternativeDelete(SqlDeleteStatement deleteStatement, DataOptions dataOptions)
Parameters
deleteStatement
SqlDeleteStatementdataOptions
DataOptions
Returns
GetAlternativeUpdate(SqlUpdateStatement, DataOptions)
protected SqlUpdateStatement GetAlternativeUpdate(SqlUpdateStatement updateStatement, DataOptions dataOptions)
Parameters
updateStatement
SqlUpdateStatementdataOptions
DataOptions
Returns
GetAlternativeUpdatePostgreSqlite(SqlUpdateStatement, DataOptions)
protected SqlStatement GetAlternativeUpdatePostgreSqlite(SqlUpdateStatement statement, DataOptions dataOptions)
Parameters
statement
SqlUpdateStatementdataOptions
DataOptions
Returns
GetMainTableSource(SelectQuery)
protected SqlTableSource? GetMainTableSource(SelectQuery selectQuery)
Parameters
selectQuery
SelectQuery
Returns
GetMaxDisplaySize(SqlDataType)
protected virtual int? GetMaxDisplaySize(SqlDataType type)
Parameters
type
SqlDataType
Returns
- int?
GetMaxLength(SqlDataType)
protected virtual int? GetMaxLength(SqlDataType type)
Parameters
type
SqlDataType
Returns
- int?
GetMaxPrecision(SqlDataType)
protected virtual int? GetMaxPrecision(SqlDataType type)
Parameters
type
SqlDataType
Returns
- int?
GetMaxScale(SqlDataType)
protected virtual int? GetMaxScale(SqlDataType type)
Parameters
type
SqlDataType
Returns
- int?
HasParameters(ISqlExpression)
protected static bool HasParameters(ISqlExpression expr)
Parameters
expr
ISqlExpression
Returns
Inc(ISqlExpression)
public ISqlExpression Inc(ISqlExpression expr1)
Parameters
expr1
ISqlExpression
Returns
IsAggregationFunction(IQueryElement)
public static bool IsAggregationFunction(IQueryElement expr)
Parameters
expr
IQueryElement
Returns
IsBooleanParameter(ISqlExpression, int, int)
protected static bool IsBooleanParameter(ISqlExpression expr, int count, int i)
Parameters
expr
ISqlExpressioncount
inti
int
Returns
IsDateDataOffsetType(ISqlExpression)
protected static bool IsDateDataOffsetType(ISqlExpression expr)
Parameters
expr
ISqlExpression
Returns
IsDateDataType(ISqlExpression, string)
protected static bool IsDateDataType(ISqlExpression expr, string dateName)
Parameters
expr
ISqlExpressiondateName
string
Returns
IsDateTime2Type(ISqlExpression, string)
protected static bool IsDateTime2Type(ISqlExpression expr, string typeName)
Parameters
expr
ISqlExpressiontypeName
string
Returns
IsDateTimeType(ISqlExpression, string)
protected static bool IsDateTimeType(ISqlExpression expr, string typeName)
Parameters
expr
ISqlExpressiontypeName
string
Returns
IsParameterDependedElement(IQueryElement)
public virtual bool IsParameterDependedElement(IQueryElement element)
Parameters
element
IQueryElement
Returns
IsParameterDependedQuery(SelectQuery)
public virtual bool IsParameterDependedQuery(SelectQuery query)
Parameters
query
SelectQuery
Returns
IsParameterDependent(SqlStatement)
Examine query for parameter dependency.
public bool IsParameterDependent(SqlStatement statement)
Parameters
statement
SqlStatement
Returns
IsSmallDateTimeType(ISqlExpression, string)
protected static bool IsSmallDateTimeType(ISqlExpression expr, string typeName)
Parameters
expr
ISqlExpressiontypeName
string
Returns
IsTimeDataType(ISqlExpression)
protected static bool IsTimeDataType(ISqlExpression expr)
Parameters
expr
ISqlExpression
Returns
Mul(ISqlExpression, ISqlExpression, Type)
public ISqlExpression Mul(ISqlExpression expr1, ISqlExpression expr2, Type type)
Parameters
expr1
ISqlExpressionexpr2
ISqlExpressiontype
Type
Returns
Mul(ISqlExpression, int)
public ISqlExpression Mul(ISqlExpression expr1, int value)
Parameters
expr1
ISqlExpressionvalue
int
Returns
Mul<T>(ISqlExpression, ISqlExpression)
public ISqlExpression Mul<T>(ISqlExpression expr1, ISqlExpression expr2)
Parameters
expr1
ISqlExpressionexpr2
ISqlExpression
Returns
Type Parameters
T
NeedsEnvelopingForUpdate(SelectQuery)
protected bool NeedsEnvelopingForUpdate(SelectQuery query)
Parameters
query
SelectQuery
Returns
OptimizeAggregates(SqlStatement)
public SqlStatement OptimizeAggregates(SqlStatement statement)
Parameters
statement
SqlStatement
Returns
OptimizeBinaryExpression(SqlBinaryExpression, EvaluationContext)
public virtual ISqlExpression OptimizeBinaryExpression(SqlBinaryExpression be, EvaluationContext context)
Parameters
be
SqlBinaryExpressioncontext
EvaluationContext
Returns
OptimizeElement(MappingSchema, DataOptions, IQueryElement?, OptimizationContext, bool)
public IQueryElement? OptimizeElement(MappingSchema mappingSchema, DataOptions dataOptions, IQueryElement? element, OptimizationContext optimizationContext, bool withConversion)
Parameters
mappingSchema
MappingSchemadataOptions
DataOptionselement
IQueryElementoptimizationContext
OptimizationContextwithConversion
bool
Returns
OptimizeExpression(ISqlExpression, ConvertVisitor<RunOptimizationContext>)
public virtual ISqlExpression OptimizeExpression(ISqlExpression expression, ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext> convertVisitor)
Parameters
expression
ISqlExpressionconvertVisitor
ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext>
Returns
OptimizeFunction(SqlFunction, EvaluationContext)
public virtual ISqlExpression OptimizeFunction(SqlFunction func, EvaluationContext context)
Parameters
func
SqlFunctioncontext
EvaluationContext
Returns
OptimizeJoins(SqlStatement)
public void OptimizeJoins(SqlStatement statement)
Parameters
statement
SqlStatement
OptimizePredicate(ISqlPredicate, EvaluationContext, DataOptions)
public virtual ISqlPredicate OptimizePredicate(ISqlPredicate predicate, EvaluationContext context, DataOptions dataOptions)
Parameters
predicate
ISqlPredicatecontext
EvaluationContextdataOptions
DataOptions
Returns
OptimizeQueryElement(ConvertVisitor<RunOptimizationContext>, IQueryElement)
public virtual IQueryElement OptimizeQueryElement(ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext> visitor, IQueryElement element)
Parameters
visitor
ConvertVisitor<BasicSqlOptimizer.RunOptimizationContext>element
IQueryElement
Returns
OptimizeRowExprExpr(ExprExpr, EvaluationContext)
protected ISqlPredicate OptimizeRowExprExpr(SqlPredicate.ExprExpr predicate, EvaluationContext context)
Parameters
predicate
SqlPredicate.ExprExprcontext
EvaluationContext
Returns
OptimizeRowInList(InList)
protected virtual ISqlPredicate OptimizeRowInList(SqlPredicate.InList predicate)
Parameters
predicate
SqlPredicate.InList
Returns
OptimizeUpdateSubqueries(SqlStatement, DataOptions)
protected virtual SqlStatement OptimizeUpdateSubqueries(SqlStatement statement, DataOptions dataOptions)
Parameters
statement
SqlStatementdataOptions
DataOptions
Returns
ReplaceDistinctOrderByWithRowNumber(SqlStatement, Func<SelectQuery, bool>)
Alternative mechanism how to prevent loosing sorting in Distinct queries.
protected SqlStatement ReplaceDistinctOrderByWithRowNumber(SqlStatement statement, Func<SelectQuery, bool> queryFilter)
Parameters
statement
SqlStatementStatement which may contain Distinct queries.
queryFilter
Func<SelectQuery, bool>Query filter predicate to determine if query needs processing.
Returns
- SqlStatement
The same
statement
or modified statement when transformation has been performed.
ReplaceTakeSkipWithRowNumber(SqlStatement, bool, bool)
Replaces pagination by Window function ROW_NUMBER().
protected SqlStatement ReplaceTakeSkipWithRowNumber(SqlStatement statement, bool supportsEmptyOrderBy, bool onlySubqueries)
Parameters
statement
SqlStatementStatement which may contain take/skip modifiers.
supportsEmptyOrderBy
boolIndicates that database supports OVER () syntax.
onlySubqueries
boolIndicates when transformation needed only for subqueries.
Returns
- SqlStatement
The same
statement
or modified statement when transformation has been performed.
ReplaceTakeSkipWithRowNumber<TContext>(TContext, SqlStatement, Func<TContext, SelectQuery, bool>, bool)
Replaces pagination by Window function ROW_NUMBER().
protected SqlStatement ReplaceTakeSkipWithRowNumber<TContext>(TContext context, SqlStatement statement, Func<TContext, SelectQuery, bool> predicate, bool supportsEmptyOrderBy)
Parameters
context
TContextpredicate
context object.statement
SqlStatementStatement which may contain take/skip modifiers.
predicate
Func<TContext, SelectQuery, bool>Indicates when the transformation is needed
supportsEmptyOrderBy
boolIndicates that database supports OVER () syntax.
Returns
- SqlStatement
The same
statement
or modified statement when transformation has been performed.
Type Parameters
TContext
RowComparisonFallback(Operator, SqlRow, SqlRow, EvaluationContext)
protected ISqlPredicate RowComparisonFallback(SqlPredicate.Operator op, SqlRow row1, SqlRow row2, EvaluationContext context)
Parameters
op
SqlPredicate.Operatorrow1
SqlRowrow2
SqlRowcontext
EvaluationContext
Returns
RowIsNullFallback(SqlRow, bool)
protected ISqlPredicate RowIsNullFallback(SqlRow row, bool isNot)
Parameters
Returns
SeparateDistinctFromPagination(SqlStatement, Func<SelectQuery, bool>)
Moves Distinct query into another subquery. Useful when preserving ordering is required, because some providers do not support DISTINCT ORDER BY.
-- before
SELECT DISTINCT TAKE 10 c1, c2
FROM A
ORDER BY c1
-- after
SELECT TAKE 10 B.c1, B.c2
FROM
(
SELECT DISTINCT c1, c2
FROM A
) B
ORDER BY B.c1
protected SqlStatement SeparateDistinctFromPagination(SqlStatement statement, Func<SelectQuery, bool> queryFilter)
Parameters
statement
SqlStatementStatement which may contain take/skip and Distinct modifiers.
queryFilter
Func<SelectQuery, bool>Query filter predicate to determine if query needs processing.
Returns
- SqlStatement
The same
statement
or modified statement when transformation has been performed.
Sub(ISqlExpression, ISqlExpression, Type)
public ISqlExpression Sub(ISqlExpression expr1, ISqlExpression expr2, Type type)
Parameters
expr1
ISqlExpressionexpr2
ISqlExpressiontype
Type
Returns
Sub(ISqlExpression, int)
public ISqlExpression Sub(ISqlExpression expr1, int value)
Parameters
expr1
ISqlExpressionvalue
int
Returns
Sub<T>(ISqlExpression, ISqlExpression)
public ISqlExpression Sub<T>(ISqlExpression expr1, ISqlExpression expr2)
Parameters
expr1
ISqlExpressionexpr2
ISqlExpression
Returns
Type Parameters
T
TransformStatement(SqlStatement, DataOptions)
Used for correcting statement and should return new statement if changes were made.
public virtual SqlStatement TransformStatement(SqlStatement statement, DataOptions dataOptions)
Parameters
statement
SqlStatementdataOptions
DataOptions
Returns
TryConvertToValue(ISqlExpression, EvaluationContext)
protected static ISqlExpression TryConvertToValue(ISqlExpression expr, EvaluationContext context)
Parameters
expr
ISqlExpressioncontext
EvaluationContext