Class SelectionClauseWriter

java.lang.Object
org.apache.sis.internal.filter.Visitor<AbstractFeature,SelectionClause>
org.apache.sis.internal.sql.feature.SelectionClauseWriter
Direct Known Subclasses:
ExtendedClauseWriter

public class SelectionClauseWriter extends Visitor<AbstractFeature,SelectionClause>
Converter from filters/expressions to the WHERE part of SQL statement. This base class handles ANSI compliant SQL. Subclasses can add database-specific syntax.

As soon as a filter or expression is not supported by this interpreter, the writing of the SQL statement stops and next filters operations will be executed with Java code.

Implementation notes

For now, we over-use parenthesis to ensure consistent operator priority. In the future, we could evolve this component to provide more elegant transcription of filter groups.

Thread-safety

Instances of this classes shall be unmodified after construction and thus thread-safe. Information about the state of a conversion to SQL is stored in SelectionClause.
Since:
1.1
Version:
1.1
  • Field Details

  • Constructor Details

    • SelectionClauseWriter

      private SelectionClauseWriter()
      Creates a new converter from filters/expressions to SQL.
    • SelectionClauseWriter

      protected SelectionClauseWriter(SelectionClauseWriter source)
      Creates a new converter initialized to the same handlers than the specified converter. The given source is usually DEFAULT.
      Parameters:
      source - the converter from which to copy the handlers.
  • Method Details

    • duplicate

      protected SelectionClauseWriter duplicate()
      Creates a new converter of the same class than this and initialized with the same data. This method is invoked before to remove handlers for functions that are unsupported on the target database software.
      Returns:
      a converter initialized to a copy of this.
    • removeUnsupportedFunctions

      final SelectionClauseWriter removeUnsupportedFunctions(Database<?> database)
      Returns a writer without the functions that are unsupported by the database software. If the database supports all functions, then this method returns this. Otherwise it returns a copy of this with unsupported functions removed. This method should be invoked at most once for a Database instance.
      Parameters:
      database - information about the database software.
      Returns:
      a writer with unsupported functions removed.
    • typeNotFound

      protected final void typeNotFound(Enum<?> type, Filter<AbstractFeature> filter, SelectionClause sql)
      Invoked when an unsupported filter is found. The SQL string is marked as invalid and may be truncated (later) to the length that it has the last time that it was valid.
      Overrides:
      typeNotFound in class Visitor<AbstractFeature,SelectionClause>
      Parameters:
      type - the filter type which has not been found, or null if is null.
      filter - the filter (may be null).
      sql - where to write the result of all actions.
    • typeNotFound

      protected final void typeNotFound(String type, Expression<AbstractFeature,?> expression, SelectionClause sql)
      Invoked when an unsupported expression is found. The SQL string is marked as invalid and may be truncated (later) to the length that it has the last time that it was valid.
      Overrides:
      typeNotFound in class Visitor<AbstractFeature,SelectionClause>
      Parameters:
      type - the expression type which has not been found, or null if is null.
      expression - the expression (may be null).
      sql - where to write the result of all actions.
    • write

      final boolean write(SelectionClause sql, Filter<? super AbstractFeature> filter)
      Executes the registered action for the given filter.

      Note on type safety

      This method applies a theoretically unsafe cast, which is okay in the context of this class. See Note on parameterized type section in Visitor.visit(Filter, Object).
      Parameters:
      sql - where to write the result of all actions.
      filter - the filter for which to execute an action based on its type.
      Returns:
      value of SelectionClause.isInvalid flag, for allowing caller to short-circuit.
    • write

      private boolean write(SelectionClause sql, Expression<? super AbstractFeature,?> expression)
      Executes the registered action for the given expression.

      Note on type safety

      This method applies a theoretically unsafe cast, which is okay in the context of this class. See Note on parameterized type section in Visitor.visit(Filter, Object).
      Parameters:
      sql - where to write the result of all actions.
      expression - the expression for which to execute an action based on its type.
      Returns:
      value of SelectionClause.isInvalid flag, for allowing caller to short-circuit.
    • writeBinaryOperator

      protected final void writeBinaryOperator(SelectionClause sql, Filter<AbstractFeature> filter, String operator)
      Writes the expressions of a filter as a binary operator. The filter must have exactly two expressions, otherwise the SQL will be declared invalid.
      Parameters:
      sql - where to append the SQL clause.
      filter - the filter for which to append the expressions.
      operator - the operator to write between the expressions.
    • writeParameters

      private void writeParameters(SelectionClause sql, List<Expression<? super AbstractFeature,?>> expressions, String separator, boolean binary)
      Writes the parameters of a function or a binary operator.
      Parameters:
      sql - where to append the SQL clause.
      expressions - the expressions to write.
      separator - the separator to insert between expression.
      binary - whether the list of expressions shall contain exactly 2 elements.