public final class SortCriterion
extends java.lang.Object
character property. Provides static
convenience methods to parse sort criteria and to produce an HQL-compliant
order by clause.
Two forms of the property name are stored: the name and the
orginalName. For non-indexed properties, these will be the
same. For indexed properties, name is the expanded name
after considering the ANSI join needed to support the sort criterion;
originalName is the name as originally given before parsing.
For instance, the sort criterion person.schedule[0] asc would
have the following name:
person_composite5.schedule
and the following originalName:
person.schedule[0]
The from clause would be augmented with
...join person.composite5 as person_composite5...
and the where clause in this case would be augmented with
...[and/where] index(person_composite5) = 0...
Thus, given a DMO with a schedule field of extent 5, a basic
HQL statement of
from Person as person order by person.schedule[0] asc
would be expanded to
from PersonImpl as person
join person.composite5 as person_composite5
where (index(person_composite5) = 0)
order by person.empNum asc,
upper(rtrim(person_composite5.schedule)) asc
(assuming schedule is a case-insensitive character field of
extent 5).
Note that this class does not actually refactor the full HQL statement; it provides the sort criteria helper methods that allow the various query implementations to do so.
| Modifier and Type | Class and Description |
|---|---|
private static class |
SortCriterion.CacheKey
Key for cached criterion lists.
|
| Modifier and Type | Field and Description |
|---|---|
private java.lang.String |
alias
DMO alias qualifier, if any
|
private boolean |
ascending
true if sort is ascending, else it is descending |
private static ExpiryCache<SortCriterion.CacheKey,java.util.List<SortCriterion>> |
cache
Shared cache of immutable
SortCriterion lists |
private static java.lang.Object |
cacheLock
Lock for cache
|
private java.lang.String |
computedColumnPrefix
Computed column prefix for property name (optional)
|
private Dialect |
dialect
SQL dialect which used this sort criterion; used for rtrim calls on character properties
|
private java.lang.Class<? extends Record> |
dmoClass
DMO implementation class containing the sort property
|
private boolean |
ignoreCase
Is case ignored (for character property only)?
|
private boolean |
isCharacter
Is property of type character?
|
private java.lang.reflect.Method |
method
Getter method associated with sort property
|
private java.lang.String |
name
Sort property name, usually qualified
|
private java.lang.String |
originalName
Sort property name, including subscript if any, usually qualified
|
private java.lang.String |
propertyName
Unqualified property name
|
private int |
subscript
Subscript value, if any
|
private java.lang.String |
unqualifiedName
Cache the unqualified name.
|
private static boolean |
WHERE_WITH_NULLS
Flag indicating if the 'withNulls' is activated in
toWhereExpression(boolean, boolean). |
| Constructor and Description |
|---|
SortCriterion(Dialect dialect,
java.lang.String text,
DmoMeta dmoInfo)
Constructor which parses original sort component text, deriving the
property name, sort direction, getter method for the property, case
sensitivity of the property (character type only), and possibly a
subscript value (for extent fields) and a composite alias name (for
extent fields which are converted to use composite inner classes within
the DMO).
|
SortCriterion(Dialect dialect,
java.lang.String text,
DmoMeta dmoInfo,
boolean internStrings)
Constructor which parses original sort component text, deriving the
property name, sort direction, getter method for the property, case
sensitivity of the property (character type only), and possibly a
subscript value (for extent fields) and a composite alias name (for
extent fields which are converted to use composite inner classes within
the DMO).
|
SortCriterion(RecordBuffer buffer,
java.lang.String text)
Constructor which parses original sort component text, deriving the property name, sort
direction, getter method for the property, case sensitivity of the property (character type
only), and possibly a subscript value (for extent fields) and a composite alias name (for
extent fields which are converted to use composite inner classes within the DMO).
|
SortCriterion(SortCriterion other,
java.lang.String alias)
Specialized constructor which takes an existing
SortCriterion and create a duplicate of it but
with changed alias. |
| Modifier and Type | Method and Description |
|---|---|
(package private) static java.lang.String |
assembleHQLNoPreamble(java.util.List<SortCriterion> sortCriteria,
boolean invert)
Assemble an HQL "order by" clause, sans the "order
by" preamble, from the given list of sort criteria, optionally
inverting the sort direction of the sort criteria.
|
(package private) java.lang.String |
getAlias()
Get the DMO alias.
|
(package private) java.lang.Class<? extends Record> |
getDMOClass()
Get the DMO implementation class containing the sort property for this criterion.
|
(package private) java.lang.reflect.Method |
getMethod()
Get the getter method associated with the property behind this component.
|
(package private) java.lang.String |
getName()
Get the name of the property behind this component.
|
(package private) java.lang.String |
getOriginalName()
Get the name of the property behind this component.
|
java.lang.String |
getUnqualifiedName()
Get the unqualified name of the property backing this component.
|
static void |
initializeCache()
Initializes the cache using CacheManager.
|
(package private) boolean |
isAscending()
Indicate whether component sorts in ascending order.
|
static boolean |
isCompatible(java.lang.String sort,
java.lang.String dmoAlias)
Check if this sort clause contains a property of a specific DMO.
|
(package private) boolean |
isPrimaryKey()
Indicate whether this sort criterion represents the DMO's primary key.
|
static java.util.List<SortCriterion> |
parse(Dialect dialect,
java.lang.String sort,
java.lang.String dmoAlias,
DmoMeta dmoInfo,
boolean makeUnique)
Parse a sort phrase (not including the
"order by" preamble) into a list of SortCriterion
objects. |
static java.util.List<SortCriterion> |
parse(Dialect dialect,
java.lang.String sort,
java.lang.String dmoAlias,
DmoMeta dmoInfo,
boolean makeUnique,
boolean forceUniqueAsc)
Parse a sort phrase (not including the
"order by" preamble) into a list of SortCriterion
objects. |
static java.util.List<SortCriterion> |
parse(java.lang.String sort,
RecordBuffer buffer)
Parse a sort phrase (not including the "order by" preamble)
into a list of
SortCriterion objects. |
(package private) static java.util.List<SortCriterion> |
parse(java.lang.String sort,
RecordBuffer buffer,
boolean makeUnique)
Parse a sort phrase (not including the "order by" preamble)
into a list of
SortCriterion objects. |
(package private) static java.util.List<SortCriterion> |
parse(java.lang.String sort,
RecordBuffer buffer,
boolean makeUnique,
boolean forceUniqueAsc)
Parse a sort phrase (not including the "order by" preamble)
into a list of
SortCriterion objects. |
(package private) static java.lang.String |
toRawSortPhrase(java.util.List<SortCriterion> sortCriteria)
Generate a raw sort phrase from the given list of criteria, in the simple form:
|
(package private) java.lang.String |
toSortExpression(boolean invert)
Get a normalized string representation of the sort component.
|
(package private) java.lang.String |
toSortExpression(boolean invert,
boolean useTableAlias,
boolean uniqueAlias,
boolean doSpecificProcessing)
Get a normalized string representation of the sort component.
|
java.lang.String |
toString()
Get a normalized string representation of the sort component.
|
(package private) FQLExpression |
toWhereExpression(boolean rangeMatch,
boolean invert)
Get a string representation of this sort component which is suitable
to append to a base where clause when augmenting that where clause
to make it more specific.
|
private static final boolean WHERE_WITH_NULLS
toWhereExpression(boolean, boolean).private static final java.lang.Object cacheLock
private static ExpiryCache<SortCriterion.CacheKey,java.util.List<SortCriterion>> cache
SortCriterion listsprivate final boolean ascending
true if sort is ascending, else it is descendingprivate final java.lang.Class<? extends Record> dmoClass
private final java.lang.reflect.Method method
private final boolean isCharacter
private final boolean ignoreCase
private final java.lang.String computedColumnPrefix
private final java.lang.String alias
private final java.lang.String propertyName
private final java.lang.String originalName
private final java.lang.String name
private final int subscript
private final java.lang.String unqualifiedName
private final Dialect dialect
SortCriterion(RecordBuffer buffer, java.lang.String text) throws PersistenceException
This constructor should be used only for short-lived instances of this class. It does not intern the numerous string fields (an expensive operation), so instances created by this constructor should not be cached in memory, because the contents of these strings will be duplicated many times.
buffer - Record buffer.text - Text representation of sort component, as provided by query.PersistenceException - if there is any error with the text representation of the sort
component provided by the caller.SortCriterion(Dialect dialect, java.lang.String text, DmoMeta dmoInfo) throws PersistenceException
This constructor should be used only for long-lived instances of this class. It interns the numerous string fields (an expensive operation), so instances created by this constructor can be cached in memory, without duplicating the contents of these strings.
dialect - Database dialect.text - Text representation of sort component, as provided by query.dmoInfo - Metadata for the table.PersistenceException - if there is any error with the text representation of the sort
component provided by the caller.SortCriterion(Dialect dialect, java.lang.String text, DmoMeta dmoInfo, boolean internStrings) throws PersistenceException
dialect - Database dialect.text - Text representation of sort component, as provided by query.dmoInfo - Metadata for the table.internStrings - true to intern string fields, else false.PersistenceException - if there is any error with the text representation of the sort
component provided by the caller.public SortCriterion(SortCriterion other, java.lang.String alias)
SortCriterion and create a duplicate of it but
with changed alias. This is used when the buffer is bound (see BIND mode for table parameters)
under a different local variable name.other - The source SortCriterion to copy.alias - The new alias to be used.public static boolean isCompatible(java.lang.String sort,
java.lang.String dmoAlias)
throws PersistenceException
sort - An atomic sort clause containing a quantified property and optionally a sorting direction.dmoAlias - The alias of the DMO against which the compatibility should be checked.true if the provided sort clause related to the DMO.PersistenceException - This is thrown when the formating itself is wrong. If the name is not quantified, it doesn't
mean it is not compatible, but that the sort clause reached an illegal state.public static void initializeCache()
public static java.util.List<SortCriterion> parse(Dialect dialect, java.lang.String sort, java.lang.String dmoAlias, DmoMeta dmoInfo, boolean makeUnique) throws PersistenceException
"order by" preamble) into a list of SortCriterion
objects. All components are recommended to be properties of the same DMO. Otherwise, the alias of
each sort component is checked.dialect - Database dialect.sort - Sort phrase.dmoAlias - Alias qualifier which represents DMO in sort phrase.dmoInfo - Metadata for the table.makeUnique - Indicates whether the <dmoAlias>.recid criterion should be appended to the returned
criteria list.SortCriterion objects.PersistenceException - if there is any error parsing the given string.public static java.util.List<SortCriterion> parse(Dialect dialect, java.lang.String sort, java.lang.String dmoAlias, DmoMeta dmoInfo, boolean makeUnique, boolean forceUniqueAsc) throws PersistenceException
"order by" preamble) into a list of SortCriterion
objects. All components are recommended to be properties of the same DMO. Otherwise, the alias of
each sort component is checked.dialect - Database dialect.sort - Sort phrase.dmoAlias - Alias qualifier which represents DMO in sort phrase.dmoInfo - Metadata for the table.makeUnique - Indicates whether the <dmoAlias>.recid criterion should be appended to the returned
criteria list.forceUniqueAsc - Forces the recid to be asc, and not follow the last component direction, when 'makeUnique'.SortCriterion objects.PersistenceException - if there is any error parsing the given string.public static java.util.List<SortCriterion> parse(java.lang.String sort, RecordBuffer buffer) throws PersistenceException
SortCriterion objects. All components are recommended to
be properties of the same DMO. Otherwise, the alias of each sort component is checked.sort - Sort phrase.buffer - Record buffer associated with the sort phrase.SortCriterion objects. This list should
never be empty; even if sort is null or empty,
a single criterion of <dmoAlias>.recid will be
created.PersistenceException - if there is any error parsing the given string.static java.util.List<SortCriterion> parse(java.lang.String sort, RecordBuffer buffer, boolean makeUnique) throws PersistenceException
SortCriterion objects. All components are recommended to
be properties of the same DMO. Otherwise, the alias of each sort component is checked.sort - Sort phrase.buffer - Record buffer associated with the sort phrase.makeUnique - Indicates whether the <dmoAlias>.recid
criterion should be appended to the returned criteria list.SortCriterion objects.PersistenceException - if there is any error parsing the given string.static java.util.List<SortCriterion> parse(java.lang.String sort, RecordBuffer buffer, boolean makeUnique, boolean forceUniqueAsc) throws PersistenceException
SortCriterion objects. All components are recommended to
be properties of the same DMO. Otherwise, the alias of each sort component is checked.sort - Sort phrase.buffer - Record buffer associated with the sort phrase.makeUnique - Indicates whether the <dmoAlias>.recid
criterion should be appended to the returned criteria list.forceUniqueAsc - Forces the recid to be asc, and not follow the last component direction, when 'makeUnique'.SortCriterion objects.PersistenceException - if there is any error parsing the given string.static java.lang.String assembleHQLNoPreamble(java.util.List<SortCriterion> sortCriteria, boolean invert)
sortCriteria - List of SortCriterion objects.invert - true to reverse the sort direction of all sort
criteria; false to use the original sort
directions of the sort criteria.static java.lang.String toRawSortPhrase(java.util.List<SortCriterion> sortCriteria)
{name} {direction} [, {name} {direction}]
Character fields are not enclosed in any upper() or rtrim() functions.
sortCriteria - List of sort criteria.public java.lang.String getUnqualifiedName()
public java.lang.String toString()
{name} {direction}
For example:
customer.accountNumber asc
If this component represents a case-insensitive sort on a character property, the property
name will be enclosed in the upper database function, as in:
upper(customer.name) asc
toString in class java.lang.Objectboolean isPrimaryKey()
true if this sort criterion represents the DMO's
primary key, else false.java.lang.String getOriginalName()
This will usually be the same as the name returned by getName(), except in the case of an extent field. In that case, this
method will return the name with the original subscripting.
java.lang.String getAlias()
java.lang.String getName()
This will usually be the same as the name returned by getOriginalName(), except in the case of an extent field. In that
case, this method will return the expanded name, in the form:
{alias}_composite{num}_{original_name_sans_subscript}
boolean isAscending()
true if ascending, false if
descending.java.lang.Class<? extends Record> getDMOClass()
java.lang.reflect.Method getMethod()
java.lang.String toSortExpression(boolean invert)
{name} {direction}
For example:
customer.accountNumber asc
If this component represents a case-insensitive sort on a character
property, the property name will be enclosed in the
upper database function, as in:
upper(customer.name) asc
If the invert parameter is true, the sort
direction will be reversed, as in:
upper(customer.name) desc
invert - true to reverse the sort direction.java.lang.String toSortExpression(boolean invert,
boolean useTableAlias,
boolean uniqueAlias,
boolean doSpecificProcessing)
{name} {direction}
For example:
customer.accountNumber ascIf this component represents a case-insensitive sort on a character property and
doSpecificProcessing is set, the property name will be enclosed in the upper
database function, as in:
upper(customer.name) ascIf the
invert parameter is true, the sort direction will be reversed, as in:
upper(customer.name) desc
invert - true to reverse the sort direction.useTableAlias - true to qualify property names with aliases, else false.uniqueAlias - if true, a unique alias will be generated for use in subselects.doSpecificProcessing - true to perform dialect-specific processing (adding computed column prefix,
string trimming and uppercasing)FQLExpression toWhereExpression(boolean rangeMatch, boolean invert)
customer.accountNumber ascwould be represented as:
customer.accountNumber = ?for an equality match; as:
customer.accountNumber > ?for a normal range match; and as:
customer.accountNumber < ?for an inverted range match.
Case-insensitive character properties are wrapped in the
upper database function, as in:
upper(customer.name) = ?
rangeMatch - true if a range match expression should be
created; false to create an equality match
expression.invert - true to reverse polarity of a range match
expression, else false.