Class DefaultSqlExpressionConverter
- All Implemented Interfaces:
de.calamanari.adl.cnv.ExpressionConverter<de.calamanari.adl.irl.CoreExpression, QueryTemplateWithParameters>, de.calamanari.adl.irl.CoreExpressionVisitor
CoreExpressions into SQL.- Author:
- Karl Eilebrecht
-
Field Summary
Fields inherited from class de.calamanari.adl.cnv.AbstractExpressionConverter
contextStack -
Constructor Summary
ConstructorsConstructorDescriptionDefaultSqlExpressionConverter(DataBinding dataBinding, de.calamanari.adl.Flag... flags) DefaultSqlExpressionConverter(DataBinding dataBinding, Map<String, Serializable> globalVariables, de.calamanari.adl.Flag... flags) DefaultSqlExpressionConverter(DataBinding dataBinding, Map<String, Serializable> globalVariables, Set<de.calamanari.adl.Flag> flags) DefaultSqlExpressionConverter(DataBinding dataBinding, Set<de.calamanari.adl.Flag> flags) DefaultSqlExpressionConverter(Supplier<? extends SqlConversionContext> contextSupplier, DataBinding dataBinding, Map<String, Serializable> globalVariables, Set<de.calamanari.adl.Flag> flags) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddExtraAliasWithClauseElements(List<String> withClauseElements) Adds further WITH-clause elements to the list.protected voidaddMainWithClauseElement(List<String> withClauseElements) This is the base query if (and only if) an alias has been chosen as base queryprotected voidaddOnRestrictionIfApplicable(TableMetaInfo table, ExpressionAlias alias, Set<de.calamanari.adl.irl.CoreExpression> expressionCandidates) This method tests the given alias, and if the alias matches the table and certain criteria is fulfilled the expression will be added to the candidates which can later refine the ON-condition.protected voidHere we add the joins to connect tables and (if defined) aliases from the WITH-clause to the base queryprotected voidappendFurtherJoinOnRestrictions(StringBuilder sb, String tableOrAliasName, List<de.calamanari.adl.irl.CoreExpression> onRestrictions) If the given list of expressions is not empty, each will be expressed as a further ON-restriction of the join, added and combined with ANDprotected voidThis method appends the global WHERE-condition (the part after the 'WHERE'), composed inAbstractSqlExpressionConverter.whereClause()to the given builderprotected voidappendJoin(StringBuilder sb, String tableOrAliasName, String refIdColumnName, List<de.calamanari.adl.irl.CoreExpression> onRestrictions) Appends the join for a table or alias with optional restrictions (ON-condition)protected voidSetup of the SELECT-FROM part of the query (everything before the joins)protected voidAppends the with-clause elements to the queryprotected List<ExpressionAlias> Collects further aliases.protected Stringprotected StringdetectJoinType(String tableOrAliasName, List<de.calamanari.adl.irl.CoreExpression> onRestrictions) The decision whether a join will be created as INNER JOIN or LEFT OUTER JOIN works as follows: If possible it will be an INNER JOIN If the table is referenced in the where-clause If the table contains all IDs and the expression (condition of the join) does not incorrectly limit the result set. else: LEFT OUTER JOINprotected voidIt can happen that we detect during the WITH-clause setup that there is neither a main table nor a primary alias, not even a combination (UNION) of aliases that can serve as base query.protected voidDetermines and sets the main table to base the query on (if possible).protected List<de.calamanari.adl.irl.CoreExpression> filterApplicableOnConditionExpressions(Set<de.calamanari.adl.irl.CoreExpression> expressionCandidates) An ON-condition is a logical OR of all the conditions of the aliases' where clauses, which is not always plausible.protected List<de.calamanari.adl.irl.CoreExpression> findOnRestrictions(TableMetaInfo table) This method consolidates all available conditions related to a given table to construct a consolidated super-condition for the join (ON)protected QueryTemplateWithParametersvoidinit()Methods inherited from class AbstractSqlExpressionConverter
aliasesInWhereClause, aliasHelper, appendAliasExistenceCheckWithExtraNullCheckIfRequired, appendAliasGroupToBaseQuery, appendAliasQuery, appendAliasUnionBaseQuery, appendAllTableUnionBaseQuery, appendFilterColumnConditions, appendFullTableQuery, appendMatchCondition, appendToAliasConditionClause, appendToGlobalWhereClause, augmentationListener, conditionFactory, createAliasHelper, createCoreExpressionSqlHelper, createMatchConditionFactory, dataBinding, enterCombinedExpression, enterNegationExpression, exitCombinedExpression, expressionHelper, flags, getAugmentationListener, getIdColumnName, getInitialFlags, getInitialVariables, getMainIdColumnName, getMainTable, getProcessContext, getQueryType, getStyle, globalVariables, handleMatchExpression, handleSpecialSetExpression, prepareRootExpression, setAugmentationListener, setIdColumnName, setMainTable, setQueryType, setStyle, stats, style, tablesInWhereClause, whereClauseMethods inherited from class de.calamanari.adl.cnv.AbstractCoreExpressionConverter
createConversionException, exitNegationExpression, traverse, visit, visit, visit, visitMethods inherited from class de.calamanari.adl.cnv.AbstractExpressionConverter
convert, createNewContext, decreaseNormalizedDepth, getContext, getCurrentDepth, getNormalizedDepth, getParentContext, getRootContext, getRootExpression, increaseNormalizedDepth, popContext, pushContext, setContextPreparator
-
Constructor Details
-
DefaultSqlExpressionConverter
public DefaultSqlExpressionConverter(Supplier<? extends SqlConversionContext> contextSupplier, DataBinding dataBinding, Map<String, Serializable> globalVariables, Set<de.calamanari.adl.Flag> flags) - Parameters:
contextSupplier- custom context supplierdataBinding- physical data bindingglobalVariables- initial global variablesflags- initial flags
-
DefaultSqlExpressionConverter
public DefaultSqlExpressionConverter(DataBinding dataBinding, Map<String, Serializable> globalVariables, Set<de.calamanari.adl.Flag> flags) - Parameters:
dataBinding- physical data bindingglobalVariables- initial global variablesflags- initial flags
-
DefaultSqlExpressionConverter
public DefaultSqlExpressionConverter(DataBinding dataBinding, Map<String, Serializable> globalVariables, de.calamanari.adl.Flag... flags) - Parameters:
dataBinding- physical data bindingglobalVariables- initial global variablesflags- initial flags
-
DefaultSqlExpressionConverter
- Parameters:
dataBinding- physical data bindingflags- initial flags
-
DefaultSqlExpressionConverter
- Parameters:
dataBinding- physical data bindingflags- initial flags
-
-
Method Details
-
finishResult
- Specified by:
finishResultin classde.calamanari.adl.cnv.AbstractExpressionConverter<de.calamanari.adl.irl.CoreExpression, QueryTemplateWithParameters, SqlConversionContext>
-
init
public void init()- Overrides:
initin classAbstractSqlExpressionConverter<SqlConversionContext>
-
createSqlQueryTemplate
- Returns:
- the textual template with named parameters
-
appendWithClauseIfRequired
Appends the with-clause elements to the query- Parameters:
sb-
-
appendMainSelectFrom
Setup of the SELECT-FROM part of the query (everything before the joins)- Parameters:
sb-
-
addMainWithClauseElement
-
addExtraAliasWithClauseElements
-
determineMainTable
protected void determineMainTable()Determines and sets the main table to base the query on (if possible).Most of the time we can start the query from one of the tables referenced in the query.
This is either the only table or the table of the primary alias, but only if the global where-clause references the table name (and not the corresponding alias).
The latter (no primary table) can happen if only existence check aliases are referenced in the where-clause.If the method is successful, then subsequent calls to
AbstractSqlExpressionConverter.getMainTable()return the main table. -
determineAuxiliaryMainTable
protected void determineAuxiliaryMainTable()It can happen that we detect during the WITH-clause setup that there is neither a main table nor a primary alias, not even a combination (UNION) of aliases that can serve as base query.
The only way to fix this is choosing some table with all IDs to start the query.Resolution order in this case is:
- Primary table from configuration.
- First table with
TableNature.containsAllIds()=true from the tables referenced in the current query. - First table with
TableNature.containsAllIds()=true from the configuration.
ConversionException.- Throws:
de.calamanari.adl.ConversionException- if the resolution was not successful
-
detectJoinType
protected String detectJoinType(String tableOrAliasName, List<de.calamanari.adl.irl.CoreExpression> onRestrictions) The decision whether a join will be created as INNER JOIN or LEFT OUTER JOIN works as follows:- If possible it will be an INNER JOIN
- If the table is referenced in the where-clause
- If the table contains all IDs and the expression (condition of the join) does not incorrectly limit the result set.
- else: LEFT OUTER JOIN
- Parameters:
tableOrAliasName-onRestrictions-- Returns:
- String with the JOIN-type
SqlFormatConstants.INNER_JOINorSqlFormatConstants.LEFT_OUTER_JOIN
-
findOnRestrictions
This method consolidates all available conditions related to a given table to construct a consolidated super-condition for the join (ON)The super-condition is a logical OR. Sometimes such a condition cannot be found so that the table will be joined without any condition.
- Parameters:
table-- Returns:
- list of conditions to be OR'd or empty list if there is no reasonable super-condition
-
addOnRestrictionIfApplicable
protected void addOnRestrictionIfApplicable(TableMetaInfo table, ExpressionAlias alias, Set<de.calamanari.adl.irl.CoreExpression> expressionCandidates) This method tests the given alias, and if the alias matches the table and certain criteria is fulfilled the expression will be added to the candidates which can later refine the ON-condition.- Parameters:
table- target tablealias- to be testedexpressionCandidates- to add the alias expression if applicable- See Also:
-
filterApplicableOnConditionExpressions
protected List<de.calamanari.adl.irl.CoreExpression> filterApplicableOnConditionExpressions(Set<de.calamanari.adl.irl.CoreExpression> expressionCandidates) An ON-condition is a logical OR of all the conditions of the aliases' where clauses, which is not always plausible.For example, it does not make any sense to include a condition if there is also IS NOT UNKNOWN included for the same argName.
Should there be an IS UNKNOWN OR IS NOT UNKOWN for the same argName, there is no possible ON-conditionThis method checks the plausibility and returns a simplified list as the lowest common denominator.
- Parameters:
expressionCandidates-- Returns:
- list of on-conditions or empty list if there is none
-
appendJoin
protected void appendJoin(StringBuilder sb, String tableOrAliasName, String refIdColumnName, List<de.calamanari.adl.irl.CoreExpression> onRestrictions) Appends the join for a table or alias with optional restrictions (ON-condition)- Parameters:
sb-tableOrAliasName-refIdColumnName-onRestrictions- ON-condition
-
appendFurtherJoinOnRestrictions
protected void appendFurtherJoinOnRestrictions(StringBuilder sb, String tableOrAliasName, List<de.calamanari.adl.irl.CoreExpression> onRestrictions) If the given list of expressions is not empty, each will be expressed as a further ON-restriction of the join, added and combined with AND- Parameters:
sb-onRestrictions-
-
collectFurtherRequiredAliases
Collects further aliases.These are aliases other than the primary alias which are referenced in the where-clause, so they MUST be included in WITH and/or joins.
This method is required for the WITH-clause creation and for the joins
- Returns:
- list of aliases
-
appendAliasJoinsIfRequired
Here we add the joins to connect tables and (if defined) aliases from the WITH-clause to the base query- Parameters:
sb- final sql script
-
appendGlobalWhereCondition
This method appends the global WHERE-condition (the part after the 'WHERE'), composed inAbstractSqlExpressionConverter.whereClause()to the given builderIt performs a last check if the table filter conditions for the main table are present and adds these if required.
- Parameters:
sb- the final sql script
-