Class DefaultSqlExpressionConverter

java.lang.Object
de.calamanari.adl.cnv.AbstractExpressionConverter<de.calamanari.adl.irl.CoreExpression, QueryTemplateWithParameters, SqlConversionContext>
de.calamanari.adl.cnv.AbstractCoreExpressionConverter<QueryTemplateWithParameters, SqlConversionContext>
de.calamanari.adl.sql.cnv.AbstractSqlExpressionConverter<SqlConversionContext>
de.calamanari.adl.sql.cnv.DefaultSqlExpressionConverter
All Implemented Interfaces:
de.calamanari.adl.cnv.ExpressionConverter<de.calamanari.adl.irl.CoreExpression, QueryTemplateWithParameters>, de.calamanari.adl.irl.CoreExpressionVisitor

public class DefaultSqlExpressionConverter extends AbstractSqlExpressionConverter<SqlConversionContext>
Standard implementation for transforming CoreExpressions into SQL.
Author:
Karl Eilebrecht
  • 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 supplier
      dataBinding - physical data binding
      globalVariables - initial global variables
      flags - initial flags
    • DefaultSqlExpressionConverter

      public DefaultSqlExpressionConverter(DataBinding dataBinding, Map<String, Serializable> globalVariables, Set<de.calamanari.adl.Flag> flags)
      Parameters:
      dataBinding - physical data binding
      globalVariables - initial global variables
      flags - initial flags
    • DefaultSqlExpressionConverter

      public DefaultSqlExpressionConverter(DataBinding dataBinding, Map<String, Serializable> globalVariables, de.calamanari.adl.Flag... flags)
      Parameters:
      dataBinding - physical data binding
      globalVariables - initial global variables
      flags - initial flags
    • DefaultSqlExpressionConverter

      public DefaultSqlExpressionConverter(DataBinding dataBinding, Set<de.calamanari.adl.Flag> flags)
      Parameters:
      dataBinding - physical data binding
      flags - initial flags
    • DefaultSqlExpressionConverter

      public DefaultSqlExpressionConverter(DataBinding dataBinding, de.calamanari.adl.Flag... flags)
      Parameters:
      dataBinding - physical data binding
      flags - initial flags
  • Method Details

    • finishResult

      protected QueryTemplateWithParameters finishResult()
      Specified by:
      finishResult in class de.calamanari.adl.cnv.AbstractExpressionConverter<de.calamanari.adl.irl.CoreExpression, QueryTemplateWithParameters, SqlConversionContext>
    • init

      public void init()
      Overrides:
      init in class AbstractSqlExpressionConverter<SqlConversionContext>
    • createSqlQueryTemplate

      protected String createSqlQueryTemplate()
      Returns:
      the textual template with named parameters
    • appendWithClauseIfRequired

      protected void appendWithClauseIfRequired(StringBuilder sb)
      Appends the with-clause elements to the query
      Parameters:
      sb -
    • appendMainSelectFrom

      protected void appendMainSelectFrom(StringBuilder sb)
      Setup of the SELECT-FROM part of the query (everything before the joins)
      Parameters:
      sb -
    • addMainWithClauseElement

      protected void addMainWithClauseElement(List<String> withClauseElements)
      This is the base query if (and only if) an alias has been chosen as base query

      If present the element will be added to the list

      Parameters:
      withClauseElements -
    • addExtraAliasWithClauseElements

      protected void addExtraAliasWithClauseElements(List<String> withClauseElements)
      Adds further WITH-clause elements to the list. These are the elements for existence checks in case of IS UNKNOWN or multi-row attributes.
      Parameters:
      withClauseElements -
    • 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:

      1. Primary table from configuration.
      2. First table with TableNature.containsAllIds()=true from the tables referenced in the current query.
      3. First table with TableNature.containsAllIds()=true from the configuration.
      If this resolution fails, the method throws a 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_JOIN or SqlFormatConstants.LEFT_OUTER_JOIN
    • findOnRestrictions

      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)

      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 table
      alias - to be tested
      expressionCandidates - 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-condition

      This 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

      protected List<ExpressionAlias> 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

      protected void appendAliasJoinsIfRequired(StringBuilder sb)
      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

      protected void appendGlobalWhereCondition(StringBuilder sb)
      This method appends the global WHERE-condition (the part after the 'WHERE'), composed in AbstractSqlExpressionConverter.whereClause() to the given builder

      It 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