org.deegree.sqldialect.filter
Class AbstractWhereBuilder

java.lang.Object
  extended by org.deegree.sqldialect.filter.AbstractWhereBuilder
Direct Known Subclasses:
H2WhereBuilder, MSSQLWhereBuilder, PostGISWhereBuilder

public abstract class AbstractWhereBuilder
extends Object

Base class for creating SQL predicates from Filter expressions. Such an expression restricts an SQL ResultSet to those rows that contain objects that match the given filter. Also handles the creation of ORDER BY clauses.

Note that the generated WHERE and ORDER-BY expressions are sometimes not sufficient to guarantee that a ResultSet only contains the targeted objects and/or keeps the requested order. This happens when the ValueReferences used in the filter/sort criteria are not mappable to columns in the database or the contained XPath expressions are not mappable to an equivalent SQL expression. In these cases, one or both of the methods getPostFilter()/getPostSortCriteria() return not null and the objects extracted from the corresponding ResultSet must be filtered/sorted in memory to guarantee the requested constraints/order.

TODO: Implement partial backend filtering / sorting. Currently, filtering / sorting is performed completely by the database or by the post filter / criteria (if any property name has been encountered that could not be mapped).

Version:
$Revision: 31370 $, $Date: 2011-07-28 19:37:13 +0200 (Do, 28. Jul 2011) $
Author:
Markus Schneider, last edited by: $Author: mschneider $

Field Summary
protected  TableAliasManager aliasManager
          Keeps track of all generated table aliases.
protected  SQLDialect dialect
           
protected  OperatorFilter filter
           
protected  PropertyNameMapper mapper
           
protected  SQLExpression orderByClause
           
protected  OperatorFilter postFilter
           
protected  SortProperty[] postSortCrit
           
protected  List<PropertyNameMapping> propNameMappingList
          Keeps track of all successfully mapped property names.
protected  SortProperty[] sortCrit
           
protected  SQLExpression whereClause
           
 
Constructor Summary
protected AbstractWhereBuilder(SQLDialect dialect, PropertyNameMapper mapper, OperatorFilter filter, SortProperty[] sortCrit)
          Creates a new AbstractWhereBuilder instance.
 
Method Summary
protected  void assertNotMultiValued(SQLExpression expr)
          Ensures that the given SQLExpression is not an SQLExpression that is multi-valued.
protected  void build(boolean allowPartialMappings)
          Invokes the building of the internal variables that store filter and sort criteria.
 TableAliasManager getAliasManager()
          Returns the TableAliasManager that keeps track of the used table aliases.
 List<PropertyNameMapping> getMappedPropertyNames()
          Returns the mappings of all ValueReferences from the filter / sort criteria that have been mapped to the relational model.
 SQLExpression getOrderBy()
          Returns the expression for the SQL-ORDER-BY clause.
 OperatorFilter getPostFilter()
          Returns a Filter that contains all constraints from the input filter that could not be expressed in the WHERE clause.
 SortProperty[] getPostSortCriteria()
          Returns the sort criteria that contains all parts from the input sort criteria that could not be expressed in the ORDER-BY clause.
 SQLExpression getWhere()
          Returns the expression for the SQL-WHERE clause.
protected  SQLExpression toProtoSQL(ComparisonOperator op)
          Translates the given ComparisonOperator into an SQLExpression.
protected  SQLExpression toProtoSQL(Expression expr)
          Translates the given Expression into an SQLExpression.
protected  SQLExpression toProtoSQL(Expression expr, boolean assertNotMultiValued)
           
protected  SQLExpression toProtoSQL(Function function)
          Translates the given Function into an SQLExpression.
protected  SQLExpression toProtoSQL(Literal<?> literal)
          Translates the given Literal into an SQLExpression.
protected  SQLOperation toProtoSQL(LogicalOperator op)
          Translates the given LogicalOperator into an SQLOperation.
protected  SQLExpression toProtoSQL(Operator op)
          Translates the given Operator into an SQLExpression.
protected  SQLOperation toProtoSQL(PropertyIsLike op)
          Translates the given PropertyIsLike into an SQLOperation.
protected  SQLExpression toProtoSQL(SortProperty[] sortCrits)
          Translates the given SortProperty array into an SQLExpression.
protected abstract  SQLOperation toProtoSQL(SpatialOperator op)
          Translates the given SpatialOperator into an SQLOperation.
protected  SQLExpression toProtoSQL(ValueReference propName)
          Translates the given ValueReference into an SQLExpression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dialect

protected final SQLDialect dialect

mapper

protected final PropertyNameMapper mapper

filter

protected final OperatorFilter filter

sortCrit

protected final SortProperty[] sortCrit

aliasManager

protected final TableAliasManager aliasManager
Keeps track of all generated table aliases.


propNameMappingList

protected final List<PropertyNameMapping> propNameMappingList
Keeps track of all successfully mapped property names.


whereClause

protected SQLExpression whereClause

orderByClause

protected SQLExpression orderByClause

postFilter

protected OperatorFilter postFilter

postSortCrit

protected SortProperty[] postSortCrit
Constructor Detail

AbstractWhereBuilder

protected AbstractWhereBuilder(SQLDialect dialect,
                               PropertyNameMapper mapper,
                               OperatorFilter filter,
                               SortProperty[] sortCrit)
                        throws FilterEvaluationException
Creates a new AbstractWhereBuilder instance.

Parameters:
dialect - SQL dialect, can be null (TODO refactor code, so not null is always used)
mapper - provides the mapping from ValueReferences to DB columns, must not be null
filter - Filter to use for generating the WHERE clause, can be null
sortCrit - criteria to use for generating the ORDER-BY clause, can be null
Throws:
FilterEvaluationException - if the filter contains invalid ValueReferences
Method Detail

build

protected void build(boolean allowPartialMappings)
              throws FilterEvaluationException,
                     UnmappableException
Invokes the building of the internal variables that store filter and sort criteria.

Parameters:
allowPartialMappings - if false, any unmappable expression will cause an UnmappableException to be thrown
Throws:
FilterEvaluationException
UnmappableException - if allowPartialMappings is false and an expression could not be mapped to the db

getWhere

public SQLExpression getWhere()
Returns the expression for the SQL-WHERE clause.

Returns:
the WHERE clause, can be null

getOrderBy

public SQLExpression getOrderBy()
Returns the expression for the SQL-ORDER-BY clause.

Returns:
the ORDER-BY clause, can be null

getPostFilter

public OperatorFilter getPostFilter()
Returns a Filter that contains all constraints from the input filter that could not be expressed in the WHERE clause.

Returns:
filter to apply on the objects from the ResultSet, may be null (no post-filtering necessary)

getPostSortCriteria

public SortProperty[] getPostSortCriteria()
Returns the sort criteria that contains all parts from the input sort criteria that could not be expressed in the ORDER-BY clause.

Returns:
sort criteria to apply on the objects from the ResultSet, may be null (no post-sorting necessary)

getAliasManager

public TableAliasManager getAliasManager()
Returns the TableAliasManager that keeps track of the used table aliases.

The returned manager may also be used for generating additional aliases that are needed for creating the final SQL statement.

Returns:
the table alias manager, never null

getMappedPropertyNames

public List<PropertyNameMapping> getMappedPropertyNames()
Returns the mappings of all ValueReferences from the filter / sort criteria that have been mapped to the relational model.

Returns:
the successful mappings, can be empty but never null

toProtoSQL

protected SQLExpression toProtoSQL(Operator op)
                            throws UnmappableException,
                                   FilterEvaluationException
Translates the given Operator into an SQLExpression.

Parameters:
op - operator to be translated, must not be null
Returns:
corresponding SQL expression, never null
Throws:
UnmappableException - if translation is not possible (usually due to unmappable property names)
FilterEvaluationException - if the filter contains invalid ValueReferences

toProtoSQL

protected SQLExpression toProtoSQL(ComparisonOperator op)
                            throws UnmappableException,
                                   FilterEvaluationException
Translates the given ComparisonOperator into an SQLExpression.

Parameters:
op - comparison operator to be translated, must not be null
Returns:
corresponding SQL expression, never null
Throws:
UnmappableException - if translation is not possible (usually due to unmappable property names)
FilterEvaluationException - if the filter contains invalid ValueReferences

toProtoSQL

protected SQLOperation toProtoSQL(PropertyIsLike op)
                           throws UnmappableException,
                                  FilterEvaluationException
Translates the given PropertyIsLike into an SQLOperation.

Parameters:
op - comparison operator to be translated, must not be null
Returns:
corresponding SQL expression, never null
Throws:
UnmappableException - if translation is not possible (usually due to unmappable property names)
FilterEvaluationException - if the filter contains invalid ValueReferences

toProtoSQL

protected SQLOperation toProtoSQL(LogicalOperator op)
                           throws UnmappableException,
                                  FilterEvaluationException
Translates the given LogicalOperator into an SQLOperation.

Parameters:
op - logical operator to be translated, must not be null
Returns:
corresponding SQL expression, never null
Throws:
UnmappableException - if translation is not possible (usually due to unmappable property names)
FilterEvaluationException - if the filter contains invalid ValueReferences

toProtoSQL

protected abstract SQLOperation toProtoSQL(SpatialOperator op)
                                    throws UnmappableException,
                                           FilterEvaluationException
Translates the given SpatialOperator into an SQLOperation.

Parameters:
op - spatial operator to be translated, must not be null
Returns:
corresponding SQL expression, never null
Throws:
UnmappableException - if translation is not possible (usually due to unmappable property names)
FilterEvaluationException - if the filter contains invalid ValueReferences

toProtoSQL

protected SQLExpression toProtoSQL(Expression expr)
                            throws UnmappableException,
                                   FilterEvaluationException
Translates the given Expression into an SQLExpression.

Parameters:
expr - expression to be translated, must not be null
Returns:
corresponding SQL expression, never null
Throws:
UnmappableException - if translation is not possible (usually due to unmappable property names)
FilterEvaluationException - if the filter contains invalid ValueReferences

toProtoSQL

protected SQLExpression toProtoSQL(Expression expr,
                                   boolean assertNotMultiValued)
                            throws UnmappableException,
                                   FilterEvaluationException
Throws:
UnmappableException
FilterEvaluationException

toProtoSQL

protected SQLExpression toProtoSQL(Function function)
                            throws UnmappableException,
                                   FilterEvaluationException
Translates the given Function into an SQLExpression.

Parameters:
function - function to be translated, must not be null
Returns:
corresponding SQL expression, or null if function can not be mapped to an SQL function call
Throws:
UnmappableException - if translation is not possible (usually due to unmappable property names)
FilterEvaluationException - if the filter contains invalid ValueReferences

toProtoSQL

protected SQLExpression toProtoSQL(Literal<?> literal)
                            throws UnmappableException,
                                   FilterEvaluationException
Translates the given Literal into an SQLExpression.

The returned literal does not have any type information. Literal types are inferred in toProtoSQL(ComparisonOperator).

Parameters:
literal - literal to be translated, must not be null
Returns:
corresponding SQL expression, never null
Throws:
UnmappableException - if translation is not possible (usually due to unmappable property names)
FilterEvaluationException - if the filter contains invalid ValueReferences

toProtoSQL

protected SQLExpression toProtoSQL(ValueReference propName)
                            throws UnmappableException,
                                   FilterEvaluationException
Translates the given ValueReference into an SQLExpression.

Parameters:
expr - expression to be translated, must not be null
Returns:
corresponding SQL expression, never null
Throws:
UnmappableException - if translation is not possible (usually due to unmappable property names)
FilterEvaluationException - if the filter contains invalid ValueReferences

toProtoSQL

protected SQLExpression toProtoSQL(SortProperty[] sortCrits)
                            throws UnmappableException,
                                   FilterEvaluationException
Translates the given SortProperty array into an SQLExpression.

Parameters:
sortCrits - sort criteria to be translated, must not be null
Returns:
corresponding SQL expression, never null
Throws:
UnmappableException - if translation is not possible (usually due to unmappable property names)
FilterEvaluationException - if the filter contains invalid ValueReferences

assertNotMultiValued

protected void assertNotMultiValued(SQLExpression expr)
                             throws UnmappableException
Ensures that the given SQLExpression is not an SQLExpression that is multi-valued.

Parameters:
expr - SQL expression, must not be null
Throws:
UnmappableException


Copyright © 2011. All Rights Reserved.