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.scheduleand 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] ascwould 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 |
composite
Name of composite alias, if any
|
private java.lang.String |
computedColumnPrefix
Computed column prefix for property name (optional)
|
private P2JDialect |
dialect
SQL dialect which used this sort criterion; used for rtrim calls on character properties
|
private java.lang.Class<?> |
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
|
Constructor and Description |
---|
SortCriterion(P2JDialect dialect,
java.lang.String text,
java.lang.String schema,
java.lang.Class<?> dmoIface,
java.lang.Class<?> dmoClass)
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(P2JDialect dialect,
java.lang.String text,
java.lang.String schema,
java.lang.Class<?> dmoIface,
java.lang.Class<?> dmoClass,
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,
java.lang.String schema,
java.lang.Class<?> dmoIface,
java.lang.Class<?> dmoClass)
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).
|
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 |
getAnsiJoin()
If it is necessary to perform a join to support this sort criterion,
return the necessary join clause, in ANSI join format.
|
(package private) java.lang.String |
getCompositeRestrictor()
If it is necessary to perform a join to support this sort criterion,
return a where clause component which will restrict the joined
association (a list) to the intended list element.
|
(package private) java.lang.Class<?> |
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.
|
(package private) boolean |
isAscending()
Indicate whether component sorts in ascending order.
|
(package private) boolean |
isPrimaryKey()
Indicate whether this sort criterion represents the DMO's primary key.
|
static java.util.List<SortCriterion> |
parse(P2JDialect dialect,
java.lang.String sort,
java.lang.String schema,
java.lang.String dmoAlias,
java.lang.Class<?> dmoClass,
java.lang.Class<?> dmoIface,
boolean makeUnique)
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.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 useAlias,
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) HQLExpression |
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 ExpiryCache<SortCriterion.CacheKey,java.util.List<SortCriterion>> cache
SortCriterion
listsprivate static final java.lang.Object cacheLock
private final boolean ascending
true
if sort is ascending, else it is descendingprivate final java.lang.Class<?> 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 java.lang.String originalName
private java.lang.String name
private java.lang.String composite
private int subscript
private P2JDialect dialect
SortCriterion(RecordBuffer buffer, java.lang.String text, java.lang.String schema, java.lang.Class<?> dmoIface, java.lang.Class<?> dmoClass) 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.schema
- Name of database schema.dmoIface
- DMO interface.dmoClass
- DMO implementation class.PersistenceException
- if there is any error with the text representation of the sort
component provided by the caller.SortCriterion(P2JDialect dialect, java.lang.String text, java.lang.String schema, java.lang.Class<?> dmoIface, java.lang.Class<?> dmoClass) 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.schema
- Name of database schema.dmoIface
- DMO interface.dmoClass
- DMO implementation class.PersistenceException
- if there is any error with the text representation of the sort
component provided by the caller.SortCriterion(P2JDialect dialect, java.lang.String text, java.lang.String schema, java.lang.Class<?> dmoIface, java.lang.Class<?> dmoClass, boolean internStrings) throws PersistenceException
dialect
- Database dialect.text
- Text representation of sort component, as provided by query.schema
- Name of database schema.dmoIface
- DMO interface.dmoClass
- DMO implementation class.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 static java.util.List<SortCriterion> parse(P2JDialect dialect, java.lang.String sort, java.lang.String schema, java.lang.String dmoAlias, java.lang.Class<?> dmoClass, java.lang.Class<?> dmoIface, boolean makeUnique) throws PersistenceException
SortCriterion
objects. All components of
the phrase are assumed to be properties of the same DMO.dialect
- Database dialect.sort
- Sort phrase.schema
- Database schema.dmoAlias
- Alias qualifier which represents DMO in sort phrase.dmoClass
- DMO implementation class.dmoIface
- DMO interface.makeUnique
- Indicates whether the <dmoAlias>.id
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(java.lang.String sort, RecordBuffer buffer) throws PersistenceException
SortCriterion
objects. All components of
the phrase are assumed to be properties of the same DMO, which is the
DMO associated with buffer
.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>.id
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 of
the phrase are assumed to be properties of the same DMO, which is the
DMO associated with buffer
.sort
- Sort phrase.buffer
- Record buffer associated with the sort phrase.makeUnique
- Indicates whether the <dmoAlias>.id
criterion should be appended to the returned criteria list.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.Object
boolean 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 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<?> 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 useAlias, 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.useAlias
- true
to qualify property names with aliases, else false
.doSpecificProcessing
- true
to perform dialect-specific processing (adding computed column prefix,
string trimming and uppercasing)java.lang.String getAnsiJoin()
This will be in the form:
join {alias}.{composite} as {alias}_{composite}for instance:
join workWeek.composite5 as workWeek_composite5
null
if not needed.java.lang.String getCompositeRestrictor()
This will be in the form:
index({alias}_{composite}) = {subscript}for instance:
index(workWeek_composite5) = 2
null
if not
needed.HQLExpression 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
.