public abstract class BaseSearchProcessor
extends java.lang.Object
ISearch
.
generateQL()
- is used for getting the actual search
results.generateRowCountQL()
- is used for getting just the number
of results.select _it from com.example.Cat _it where _it.age > :p1 and _it.name != :p2 parameter list: [3, 'Mittens']This is an abstract class. A subclass must be used to implement individual query languages. Currently only HQL query language is supported (
com.googlecode.genericdao.dao.hibernate.HibernateSearchToQLProcessor
). The that
implementation could be used for EQL query language as well with no or minor
modifications.Modifier and Type | Class and Description |
---|---|
protected static class |
BaseSearchProcessor.AliasNode |
protected static class |
BaseSearchProcessor.SearchContext |
Modifier and Type | Field and Description |
---|---|
protected java.util.regex.Pattern |
INJECTION_CHECK
Regex pattern for a valid property name/path.
|
protected MetadataUtil |
metadataUtil |
protected int |
qlType |
protected static int |
QLTYPE_EQL |
protected static int |
QLTYPE_HQL |
protected static java.lang.String |
ROOT_PATH |
protected java.lang.String |
rootAlias |
Modifier | Constructor and Description |
---|---|
protected |
BaseSearchProcessor(int qlType,
MetadataUtil metadataUtil) |
Modifier and Type | Method and Description |
---|---|
protected Filter |
addExplicitNullChecks(Filter filter)
Used by
negate(Filter) . |
protected void |
appendCustomExpression(java.lang.StringBuilder sb,
BaseSearchProcessor.SearchContext ctx,
java.lang.String expression)
append a custom expression to the string builder, replacing any
property tokens (i.e "{prop}") with a reference to the property.
|
protected void |
appendCustomExpression(java.lang.StringBuilder sb,
BaseSearchProcessor.SearchContext ctx,
java.lang.String expression,
java.util.List<?> values)
append a custom expression to the string builder, replacing any
property tokens (i.e "{prop}") with a reference to the property and
value tokens (i.e.
|
protected void |
applyFetches(BaseSearchProcessor.SearchContext ctx,
java.util.List<java.lang.String> fetches,
java.util.List<Field> fields)
Apply the fetch list to the alias tree in the search context.
|
protected java.util.List<java.lang.String> |
checkAndCleanFetches(java.util.List<java.lang.String> fetches)
Check for injection attack in property strings.
|
protected java.util.List<Field> |
checkAndCleanFields(java.util.List<Field> fields)
Check for injection attack in property strings.
|
protected java.util.List<Filter> |
checkAndCleanFilters(java.util.List<Filter> filters)
Check for injection attack in property strings.
|
protected java.util.List<Sort> |
checkAndCleanSorts(java.util.List<Sort> sorts)
Check for injection attack in property strings.
|
protected java.lang.String |
filterToQL(BaseSearchProcessor.SearchContext ctx,
Filter filter)
Recursively generate the QL fragment for a given search filter option.
|
protected java.lang.String |
generateFromClause(BaseSearchProcessor.SearchContext ctx,
boolean doEagerFetching)
Internal method for generating from clause.
|
protected java.lang.String |
generateJoins(BaseSearchProcessor.SearchContext ctx,
boolean doEagerFetching)
Internal method for generating the join portion of the from clause.
|
protected java.lang.String |
generateOrderByClause(BaseSearchProcessor.SearchContext ctx,
java.util.List<Sort> sorts)
Internal method for generating order by clause.
|
java.lang.String |
generateQL(java.lang.Class<?> entityClass,
ISearch search,
java.util.List<java.lang.Object> paramList)
Generate the QL string for a given search.
|
java.lang.String |
generateRowCountQL(java.lang.Class<?> entityClass,
ISearch search,
java.util.List<java.lang.Object> paramList)
Generate the QL string that will query the total number of results from a
given search (paging is ignored).
|
protected java.lang.String |
generateSelectClause(BaseSearchProcessor.SearchContext ctx,
java.util.List<Field> fields,
boolean distinct)
Internal method for generating the select clause based on the fields of
the given search.
|
protected java.lang.String |
generateSimpleAllOrSome(BaseSearchProcessor.SearchContext ctx,
java.lang.String property,
Filter filter,
java.lang.String operation)
In the case of simple ALL/SOME/NONE filters, a simpler hql syntax is used
(which is also compatible with collections of values).
|
protected java.lang.String |
generateSubquery(BaseSearchProcessor.SearchContext ctx,
java.lang.String property,
Filter filter)
Generate a QL string for a subquery on the given property that uses the
given filter.
|
protected java.lang.String |
generateWhereClause(BaseSearchProcessor.SearchContext ctx,
java.util.List<Filter> filters,
boolean isDisjunction)
Internal method for generating where clause for given search.
|
protected BaseSearchProcessor.AliasNode |
getAliasForPathIfItExists(BaseSearchProcessor.SearchContext ctx,
java.lang.String path)
Given a full path to an entity (ex.
|
Filter |
getFilterFromExample(java.lang.Object example) |
Filter |
getFilterFromExample(java.lang.Object example,
ExampleOptions options) |
MetadataUtil |
getMetadataUtil()
The MetadataUtil used by this search processor.
|
protected BaseSearchProcessor.AliasNode |
getOrCreateAlias(BaseSearchProcessor.SearchContext ctx,
java.lang.String path,
boolean setFetch)
Given a full path to an entity (ex.
|
protected java.lang.String |
getPathRef(BaseSearchProcessor.SearchContext ctx,
java.lang.String path)
Given a full path to a property (ex.
|
protected Filter |
negate(Filter filter)
Return a filter that negates the given filter.
|
protected java.lang.String |
param(BaseSearchProcessor.SearchContext ctx,
java.lang.Object value)
Add value to paramList and return the named parameter string ":pX".
|
protected java.lang.Object |
prepareValue(java.lang.Class<?> rootClass,
java.lang.String property,
java.lang.Object value,
boolean isCollection)
Convert a property value to the expected type for that property.
|
protected void |
securityCheckProperty(java.lang.String property)
Used by
securityCheck() to check a property string for
injection attack. |
protected void |
setFetchOnAliasNodeAndAllAncestors(BaseSearchProcessor.AliasNode node) |
void |
setRootAlias(java.lang.String alias)
This is the string used to represent the root entity of the search within
the query.
|
protected java.lang.String[] |
splitPath(BaseSearchProcessor.SearchContext ctx,
java.lang.String path)
Split a path into two parts.
|
protected static int QLTYPE_HQL
protected static int QLTYPE_EQL
protected int qlType
protected MetadataUtil metadataUtil
protected java.lang.String rootAlias
protected static final java.lang.String ROOT_PATH
protected java.util.regex.Pattern INJECTION_CHECK
protected BaseSearchProcessor(int qlType, MetadataUtil metadataUtil)
public MetadataUtil getMetadataUtil()
public void setRootAlias(java.lang.String alias)
"_it"
. It may be necessary
to use a different alias if there are entities in the data model with the
name or property "_it".public java.lang.String generateQL(java.lang.Class<?> entityClass, ISearch search, java.util.List<java.lang.Object> paramList)
public java.lang.String generateRowCountQL(java.lang.Class<?> entityClass, ISearch search, java.util.List<java.lang.Object> paramList)
protected java.lang.String generateSelectClause(BaseSearchProcessor.SearchContext ctx, java.util.List<Field> fields, boolean distinct)
protected void applyFetches(BaseSearchProcessor.SearchContext ctx, java.util.List<java.lang.String> fetches, java.util.List<Field> fields)
protected java.lang.String generateFromClause(BaseSearchProcessor.SearchContext ctx, boolean doEagerFetching)
fetches
if
doEagerFetching
is true
. NOTE: When
using eager fetching, applyFetches()
must be executed first.protected java.lang.String generateJoins(BaseSearchProcessor.SearchContext ctx, boolean doEagerFetching)
fetches
if doEagerFetching
is true
. NOTE: When using eager fetching, applyFetches()
must be executed first.protected java.lang.String generateOrderByClause(BaseSearchProcessor.SearchContext ctx, java.util.List<Sort> sorts)
protected java.lang.String generateWhereClause(BaseSearchProcessor.SearchContext ctx, java.util.List<Filter> filters, boolean isDisjunction)
protected java.lang.String filterToQL(BaseSearchProcessor.SearchContext ctx, Filter filter)
protected java.lang.String generateSubquery(BaseSearchProcessor.SearchContext ctx, java.lang.String property, Filter filter)
ctx
- - a new context just for this sub-queryproperty
- - the property of the main query that points to the collection
on which to queryfilter
- - the filter to use for the where clause of the sub-queryprotected java.lang.String generateSimpleAllOrSome(BaseSearchProcessor.SearchContext ctx, java.lang.String property, Filter filter, java.lang.String operation)
In the case of simple ALL/SOME/NONE filters, a simpler hql syntax is used (which is also compatible with collections of values). Simple filters include ALL/SOME/NONE filters with exactly one sub-filter where that filter applies to the elements of the collection directly (as opposed to their properties) and the operator is =, !=, <, <=, >, or >=.
For example:
Filter.some("some_collection_of_strings", Filter.equal("", "Bob") Filter.all("some_collection_of_numbers", Filter.greaterThan(null, 23)If the filter meets these criteria as a simple ALL/SOME/NONE filter, the QL string for the filter will be returned. If not,
null
is
returned.ctx
- - the context of the SOME/ALL/NONE filterproperty
- - the property of the SOME/ALL/NONE filterfilter
- - the sub-filter that is the value of the SOME/ALL/NONE filteroperation
- - a string used to fill in the collection operation. The value
should be either "some" or "all".protected java.lang.Object prepareValue(java.lang.Class<?> rootClass, java.lang.String property, java.lang.Object value, boolean isCollection)
isCollection
- true
if the value is a collection of values, for
example with IN and NOT_IN operators.protected Filter addExplicitNullChecks(Filter filter)
negate(Filter)
. There's a complication with null values
in the database so that !(x == 1) is not the opposite of (x == 1). Rather
!(x == 1 and x != null) is the same as (x == 1). This method applies the
null check explicitly to all filters included in the given filter tree.protected void appendCustomExpression(java.lang.StringBuilder sb, BaseSearchProcessor.SearchContext ctx, java.lang.String expression)
protected void appendCustomExpression(java.lang.StringBuilder sb, BaseSearchProcessor.SearchContext ctx, java.lang.String expression, java.util.List<?> values)
protected java.lang.String param(BaseSearchProcessor.SearchContext ctx, java.lang.Object value)
protected java.lang.String getPathRef(BaseSearchProcessor.SearchContext ctx, java.lang.String path)
protected java.lang.String[] splitPath(BaseSearchProcessor.SearchContext ctx, java.lang.String path)
protected BaseSearchProcessor.AliasNode getOrCreateAlias(BaseSearchProcessor.SearchContext ctx, java.lang.String path, boolean setFetch)
protected BaseSearchProcessor.AliasNode getAliasForPathIfItExists(BaseSearchProcessor.SearchContext ctx, java.lang.String path)
null
if none.protected void setFetchOnAliasNodeAndAllAncestors(BaseSearchProcessor.AliasNode node)
protected java.util.List<Field> checkAndCleanFields(java.util.List<Field> fields)
protected java.util.List<java.lang.String> checkAndCleanFetches(java.util.List<java.lang.String> fetches)
protected java.util.List<Sort> checkAndCleanSorts(java.util.List<Sort> sorts)
protected java.util.List<Filter> checkAndCleanFilters(java.util.List<Filter> filters)
protected void securityCheckProperty(java.lang.String property)
securityCheck()
to check a property string for
injection attack.public Filter getFilterFromExample(java.lang.Object example)
public Filter getFilterFromExample(java.lang.Object example, ExampleOptions options)