public class BasicSqlQueryParser extends Object implements QueryParser
QueryParser
implementation that parses a subset of SQL select and set queries.
This grammar is equivalent to the SQL grammar as defined by the JCR 2.0 specification, with some useful additions:
... (UNION|INTERSECT|EXCEPT) [ALL] ...
" to combine and merge results from multiple queriesSELECT DISTINCT ...
" to remove duplicatesLIMIT count [OFFSET number]
" clauses to control the number of results returned as well as the number of rows
that should be skippedFULL OUTER JOIN
" and "CROSS JOIN
"DEPTH([<selectorName>])
", "PATH([<selectorName>])
", "ID([<selectorName>])
", and "CHILDCOUNT([<selectorName>])
" that
enables placing constraints on the node depth, path, and identifier, respectively, and which can be used in a manner similar to "
NAME([<selectorName>])
" and "LOCALNAME([<selectorName>])
. Note in each of these cases, the
selector name is optional if there is only one selector in the query.REFERENCE([<selectorName>.]<propertyName>])
" that
enables placing constraints on one or all reference properties, and which can be used in a manner similar to "
PropertyValue([<selectorName>.]<propertyName>)
". Note in each of these cases, the
selector name is optional if there is only one selector in the query, and that the property name can be excluded
if the constraint should apply to all reference properties.<dynamicOperand> [NOT] IN (<staticOperand> {, <staticOperand>})
"<dynamicOperand> [NOT] BETWEEN <lowerBoundStaticOperand> [EXCLUSIVE] AND
<upperBoundStaticOperand> [EXCLUSIVE]
"
WHERE
criteria and ORDER BY
clauses: "WHERE <dynamicOperand> + <dynamicOperand> ...
" or "ORDER BY (<dynamicOperand> + <dynamicOperand>) [ASC]
".
Note that standard operator precedence is used, but grouping by (potentially nested) parentheses is also supported.
This section defines the complete grammar for the SQL dialect supported by this parser.
QueryCommand ::= Query | SetQuery SetQuery ::= Query ('UNION'|'INTERSECT'|'EXCEPT') [ALL] Query { ('UNION'|'INTERSECT'|'EXCEPT') [ALL] Query } Query ::= 'SELECT' ['DISINCT'] columns 'FROM' Source ['WHERE' Constraint] ['ORDER BY' orderings] [Limit]
Source ::= Selector | Join Selector ::= nodeTypeName ['AS' selectorName] nodeTypeName ::= Name
Join ::= left [JoinType] 'JOIN' right 'ON' JoinCondition // If JoinType is omitted INNER is assumed. left ::= Source right ::= Source JoinType ::= Inner | LeftOuter | RightOuter | FullOuter | Cross Inner ::= 'INNER' ['JOIN'] LeftOuter ::= 'LEFT JOIN' | 'OUTER JOIN' | 'LEFT OUTER JOIN' RightOuter ::= 'RIGHT OUTER' ['JOIN'] RightOuter ::= 'FULL OUTER' ['JOIN'] RightOuter ::= 'CROSS' ['JOIN'] JoinCondition ::= EquiJoinCondition | SameNodeJoinCondition | ChildNodeJoinCondition | DescendantNodeJoinCondition
EquiJoinCondition ::= selector1Name'.'property1Name '=' selector2Name'.'property2Name selector1Name ::= selectorName selector2Name ::= selectorName property1Name ::= propertyName property2Name ::= propertyName
SameNodeJoinCondition ::= 'ISSAMENODE(' selector1Name ',' selector2Name [',' selector2Path] ')' selector2Path ::= Path
ChildNodeJoinCondition ::= 'ISCHILDNODE(' childSelectorName ',' parentSelectorName ')' childSelectorName ::= selectorName parentSelectorName ::= selectorName
DescendantNodeJoinCondition ::= 'ISDESCENDANTNODE(' descendantSelectorName ',' ancestorSelectorName ')' descendantSelectorName ::= selectorName ancestorSelectorName ::= selectorName
Constraint ::= ConstraintItem | '(' ConstraintItem ')' ConstraintItem ::= And | Or | Not | Comparison | Between | PropertyExistence | SetConstraint | FullTextSearch | SameNode | ChildNode | DescendantNode
And ::= constraint1 'AND' constraint2 constraint1 ::= Constraint constraint2 ::= Constraint
Or ::= constraint1 'OR' constraint2
Not ::= 'NOT' Constraint
Comparison ::= DynamicOperand Operator StaticOperand Operator ::= '=' | '!=' | '<' | '<=' | '>' | '>=' | 'LIKE' | 'NOT LIKE'
Between ::= DynamicOperand ['NOT'] 'BETWEEN' lowerBound ['EXCLUSIVE'] 'AND' upperBound ['EXCLUSIVE'] lowerBound ::= StaticOperand upperBound ::= StaticOperand
PropertyExistence ::= selectorName'.'propertyName 'IS' ['NOT'] 'NULL' | propertyName 'IS' ['NOT'] 'NULL' /* If only one selector exists in this query */
SetConstraint ::= selectorName'.'propertyName ['NOT'] 'IN' | propertyName ['NOT'] 'IN' /* If only one selector exists in this query */ '(' firstStaticOperand {',' additionalStaticOperand } ')' firstStaticOperand ::= StaticOperand additionalStaticOperand ::= StaticOperand
FullTextSearch ::= 'CONTAINS(' ([selectorName'.']propertyName | selectorName'.*')
',' ''' fullTextSearchExpression''' ')'
/* If only one selector exists in this query, explicit specification of the selectorName
preceding the propertyName is optional */
fullTextSearchExpression ::= /* a full-text search expression, see FullTextSearchParser
*/
SameNode ::= 'ISSAMENODE(' [selectorName ','] Path ')' /* If only one selector exists in this query, explicit specification of the selectorName preceding the propertyName is optional */
ChildNode ::= 'ISCHILDNODE(' [selectorName ','] Path ')' /* If only one selector exists in this query, explicit specification of the selectorName preceding the propertyName is optional */
DescendantNode ::= 'ISDESCENDANTNODE(' [selectorName ','] Path ')' /* If only one selector exists in this query, explicit specification of the selectorName preceding the propertyName is optional */
Name ::= '[' quotedName ']' | '[' simpleName ']' | simpleName quotedName ::= /* A JCR Name (see the JCR specification) */ simpleName ::= /* A JCR Name that contains only SQL-legal characters (namely letters, digits, and underscore) */ Path ::= '[' quotedPath ']' | '[' simplePath ']' | simplePath quotedPath ::= /* A JCR Path that contains non-SQL-legal characters */ simplePath ::= /* A JCR Path (rather Name) that contains only SQL-legal characters (namely letters, digits, and underscore) */
StaticOperand ::= Literal | BindVariableValue
Literal ::= CastLiteral | UncastLiteral CastLiteral ::= 'CAST(' UncastLiteral ' AS ' PropertyType ')' PropertyType ::= 'STRING' | 'BINARY' | 'DATE' | 'LONG' | 'DOUBLE' | 'DECIMAL' | 'BOOLEAN' | 'NAME' | 'PATH' | 'REFERENCE' | 'WEAKREFERENCE' | 'URI' UncastLiteral ::= UnquotedLiteral | ''' UnquotedLiteral ''' | '"' UnquotedLiteral '"' UnquotedLiteral ::= /* String form of a JCR Value, as defined in the JCR specification */
BindVariableValue ::= '$'bindVariableName bindVariableName ::= /* A string that conforms to the JCR Name syntax, though the prefix does not need to be a registered namespace prefix. */
DynamicOperand ::= PropertyValue | ReferenceValue | Length | NodeName | NodeLocalName | NodePath | NodeDepth | NodeId | FullTextSearchScore | LowerCase | UpperCase | Arithmetic | '(' DynamicOperand ')'
PropertyValue ::= [selectorName'.'] propertyName /* If only one selector exists in this query, explicit specification of the selectorName preceding the propertyName is optional */
ReferenceValue ::= 'REFERENCE(' selectorName '.' propertyName ')' | 'REFERENCE(' selectorName ')' | 'REFERENCE()' | /* If only one selector exists in this query, explicit specification of the selectorName preceding the propertyName is optional. Also, the property name may be excluded if the constraint should apply to any reference property. */
Length ::= 'LENGTH(' PropertyValue ')'
NodeName ::= 'NAME(' [selectorName] ')' /* If only one selector exists in this query, explicit specification of the selectorName is optional */
NodeLocalName ::= 'LOCALNAME(' [selectorName] ')' /* If only one selector exists in this query, explicit specification of the selectorName is optional */
NodePath ::= 'PATH(' [selectorName] ')' /* If only one selector exists in this query, explicit specification of the selectorName is optional */
NodeDepth ::= 'DEPTH(' [selectorName] ')' /* If only one selector exists in this query, explicit specification of the selectorName is optional */
NodeId ::= 'ID(' [selectorName] ')' /* If only one selector exists in this query, explicit specification of the selectorName is optional */
ChildCount ::= 'CHILDCOUNT(' [selectorName] ')' /* If only one selector exists in this query, explicit specification of the selectorName is optional */
FullTextSearchScore ::= 'SCORE(' [selectorName] ')' /* If only one selector exists in this query, explicit specification of the selectorName is optional */
LowerCase ::= 'LOWER(' DynamicOperand ')'
UpperCase ::= 'UPPER(' DynamicOperand ')'
Arithmetic ::= DynamicOperand ('+'|'-'|'*'|'/') DynamicOperand
orderings ::= Ordering {',' Ordering} Ordering ::= DynamicOperand [Order] Order ::= 'ASC' | 'DESC'
columns ::= (Column ',' {Column}) | '*' Column ::= ([selectorName'.']propertyName ['AS' columnName]) | (selectorName'.*') /* If only one selector exists in this query, explicit specification of the selectorName preceding the propertyName is optional */ selectorName ::= Name propertyName ::= Name columnName ::= Name
Limit ::= 'LIMIT' count [ 'OFFSET' offset ] count ::= /* Positive integer value */ offset ::= /* Non-negative integer value */
Modifier and Type | Class and Description |
---|---|
static class |
BasicSqlQueryParser.SqlTokenizer
A
TokenStream.Tokenizer implementation that parses words, quoted phrases, comments, and
symbols. |
Constructor and Description |
---|
BasicSqlQueryParser() |
public static final String LANGUAGE
public String getLanguage()
QueryParser
getLanguage
in interface QueryParser
public QueryCommand parseQuery(String query, TypeSystem typeSystem)
QueryParser
QueryCommand
.parseQuery
in interface QueryParser
query
- the query in string form; may not be nulltypeSystem
- the type system used by the query; may not be nullprotected QueryCommand parseQueryCommand(TokenStream tokens, TypeSystem typeSystem)
protected Query parseQuery(TokenStream tokens, TypeSystem typeSystem)
protected SetQuery parseSetQuery(TokenStream tokens, QueryCommand leftHandSide, TypeSystem typeSystem)
protected List<org.modeshape.jcr.query.parse.ColumnExpression> parseSelect(TokenStream tokens, AtomicBoolean isDistinct, TypeSystem typeSystem)
protected Source parseFrom(TokenStream tokens, TypeSystem typeSystem)
protected JoinCondition parseJoinCondition(TokenStream tokens, TypeSystem typeSystem)
protected Constraint parseWhere(TokenStream tokens, TypeSystem typeSystem, Source source)
protected Constraint parseConstraint(TokenStream tokens, TypeSystem typeSystem, Source source)
protected List<StaticOperand> parseInClause(TokenStream tokens, TypeSystem typeSystem)
protected FullTextSearch.Term parseFullTextSearchExpression(String expression, Position startOfExpression)
protected Operator parseComparisonOperator(TokenStream tokens)
protected List<Ordering> parseOrderBy(TokenStream tokens, TypeSystem typeSystem, Source source)
protected Ordering parseOrdering(TokenStream tokens, TypeSystem typeSystem, Source source)
protected Constraint parsePropertyExistance(TokenStream tokens, TypeSystem typeSystem, Source source)
protected StaticOperand parseStaticOperand(TokenStream tokens, TypeSystem typeSystem)
protected BindVariableName parseBindVariableName(TokenStream tokens, TypeSystem typeSystem)
protected Subquery subquery(QueryCommand queryCommand)
protected Literal parseLiteral(TokenStream tokens, TypeSystem typeSystem)
protected Object parseLiteralValue(TokenStream tokens, TypeSystem typeSystem)
protected DynamicOperand parseDynamicOperand(TokenStream tokens, TypeSystem typeSystem, Source source)
protected PropertyValue parsePropertyValue(TokenStream tokens, TypeSystem typeSystem, Source source)
protected ReferenceValue parseReferenceValue(TokenStream tokens, TypeSystem typeSystem, Source source)
protected Limit parseLimit(TokenStream tokens)
protected String removeBracketsAndQuotes(String text, Position position)
text
- the input text; may not be nullposition
- the position of the text; may not be nulltext
if there were no square brackets or
quotesprotected String removeBracketsAndQuotes(String text, boolean recursive, Position position)
text
- the input text; may not be nullrecursive
- true if more than one pair of quotes, double-quotes, or square brackets should be removed, or false if
just the first pair should be removedposition
- the position of the text; may not be nulltext
if there were no square brackets or
quotesprotected NamedSelector parseNamedSelector(TokenStream tokens, TypeSystem typeSystem)
protected SelectorName parseSelectorName(TokenStream tokens, TypeSystem typeSystem)
protected String parsePath(TokenStream tokens, TypeSystem typeSystem)
protected String parseName(TokenStream tokens, TypeSystem typeSystem)
protected String parseName(String token, TypeSystem typeSystem, Position position)
protected Query query(Source source, Constraint constraint, List<? extends Ordering> orderings, List<? extends Column> columns, Limit limit, boolean distinct)
protected SetQuery setQuery(QueryCommand leftQuery, SetQuery.Operation operation, QueryCommand rightQuery, boolean all)
protected Length length(PropertyValue propertyValue)
protected LowerCase lowerCase(DynamicOperand operand)
protected UpperCase upperCase(DynamicOperand operand)
protected Cast cast(DynamicOperand operand, String desiredType)
protected NodeName nodeName(SelectorName selector)
protected NodeLocalName nodeLocalName(SelectorName selector)
protected NodeDepth nodeDepth(SelectorName selector)
protected NodeId nodeId(SelectorName selector)
protected NodePath nodePath(SelectorName selector)
protected ChildCount childCount(SelectorName selector)
protected EquiJoinCondition equiJoinCondition(SelectorName selector1, String property1, SelectorName selector2, String property2)
protected DescendantNodeJoinCondition descendantNodeJoinCondition(SelectorName ancestor, SelectorName descendant)
protected ChildNodeJoinCondition childNodeJoinCondition(SelectorName parent, SelectorName child)
protected SameNodeJoinCondition sameNodeJoinCondition(SelectorName selector1, SelectorName selector2)
protected SameNodeJoinCondition sameNodeJoinCondition(SelectorName selector1, SelectorName selector2, String path)
protected Limit limit(int rowCount, int offset)
protected Column column(SelectorName selectorName, String propertyName, String columnName)
protected Join join(Source left, JoinType joinType, Source right, JoinCondition joinCondition)
protected Not not(Constraint constraint)
protected And and(Constraint constraint1, Constraint constraint2)
protected Or or(Constraint constraint1, Constraint constraint2)
protected Between between(DynamicOperand operand, StaticOperand lowerBound, StaticOperand upperBound, boolean lowerInclusive, boolean upperInclusive)
protected SetCriteria setCriteria(DynamicOperand operand, Collection<? extends StaticOperand> values)
protected FullTextSearch fullTextSearch(SelectorName name, String propertyName, String expression, FullTextSearch.Term term)
protected FullTextSearch fullTextSearch(SelectorName name, String propertyName, StaticOperand expression) throws RepositoryException
RepositoryException
protected SameNode sameNode(SelectorName name, String path)
protected ChildNode childNode(SelectorName name, String path)
protected DescendantNode descendantNode(SelectorName name, String path)
protected Comparison comparison(DynamicOperand left, Operator operator, StaticOperand right)
protected Ordering ordering(DynamicOperand operand, Order order, NullOrder nullOrder)
protected PropertyExistence propertyExistence(SelectorName selector, String propertyName)
protected FullTextSearchScore fullTextSearchScore(SelectorName selector)
protected ArithmeticOperand arithmeticOperand(DynamicOperand leftOperand, ArithmeticOperator operator, DynamicOperand rightOperand)
protected PropertyValue propertyValue(SelectorName selector, String propertyName)
protected ReferenceValue referenceValue(SelectorName selector)
protected ReferenceValue referenceValue(SelectorName selector, String propertyName)
protected BindVariableName bindVariableName(String variableName)
protected Literal literal(TypeSystem typeSystem, Object value) throws ValueFormatException
ValueFormatException
Copyright © 2008–2016 JBoss, a division of Red Hat. All rights reserved.