Class SelectionClauseWriter
java.lang.Object
org.apache.sis.internal.filter.Visitor<AbstractFeature,SelectionClause>
org.apache.sis.internal.sql.feature.SelectionClauseWriter
- Direct Known Subclasses:
ExtendedClauseWriter
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 inSelectionClause.- Since:
- 1.1
- Version:
- 1.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final classHandler for converting+,-,*or/filter into SQL clauses.private final classHandler for converting=,<,>,<=or>=filter into SQL clauses.private final classAppends a function name with an arbitrary number of parameters (potentially zero).private final classHandler for converting anAND,ORorNOTfilter into SQL clauses. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final SelectionClauseWriterThe default instance. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateCreates a new converter from filters/expressions to SQL.protectedCreates a new converter initialized to the same handlers than the specified converter. -
Method Summary
Modifier and TypeMethodDescriptionprotected SelectionClauseWriterCreates a new converter of the same class thanthisand initialized with the same data.(package private) final SelectionClauseWriterremoveUnsupportedFunctions(Database<?> database) Returns a writer without the functions that are unsupported by the database software.protected final voidtypeNotFound(Enum<?> type, Filter<AbstractFeature> filter, SelectionClause sql) Invoked when an unsupported filter is found.protected final voidtypeNotFound(String type, Expression<AbstractFeature, ?> expression, SelectionClause sql) Invoked when an unsupported expression is found.private booleanwrite(SelectionClause sql, Expression<? super AbstractFeature, ?> expression) Executes the registered action for the given expression.(package private) final booleanwrite(SelectionClause sql, Filter<? super AbstractFeature> filter) Executes the registered action for the given filter.protected final voidwriteBinaryOperator(SelectionClause sql, Filter<AbstractFeature> filter, String operator) Writes the expressions of a filter as a binary operator.private voidwriteParameters(SelectionClause sql, List<Expression<? super AbstractFeature, ?>> expressions, String separator, boolean binary) Writes the parameters of a function or a binary operator.Methods inherited from class org.apache.sis.internal.filter.Visitor
getExpressionHandler, getFilterHandler, removeFilterHandlers, setBinaryComparisonHandlers, setBinaryTemporalHandlers, setExpressionHandler, setFilterHandler, setLogicalHandlers, setMathHandlers, setNullAndNilHandlers, setSpatialHandlers, visit, visit
-
Field Details
-
DEFAULT
The default instance.
-
-
Constructor Details
-
SelectionClauseWriter
private SelectionClauseWriter()Creates a new converter from filters/expressions to SQL. -
SelectionClauseWriter
Creates a new converter initialized to the same handlers than the specified converter. The given source is usuallyDEFAULT.- Parameters:
source- the converter from which to copy the handlers.
-
-
Method Details
-
duplicate
Creates a new converter of the same class thanthisand 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
Returns a writer without the functions that are unsupported by the database software. If the database supports all functions, then this method returnsthis. Otherwise it returns a copy ofthiswith unsupported functions removed. This method should be invoked at most once for aDatabaseinstance.- 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:
typeNotFoundin classVisitor<AbstractFeature,SelectionClause> - Parameters:
type- the filter type which has not been found, ornullif is null.filter- the filter (may benull).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:
typeNotFoundin classVisitor<AbstractFeature,SelectionClause> - Parameters:
type- the expression type which has not been found, ornullif is null.expression- the expression (may benull).sql- where to write the result of all actions.
-
write
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 inVisitor.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.isInvalidflag, for allowing caller to short-circuit.
-
write
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 inVisitor.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.isInvalidflag, 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.
-