2
* Hibernate, Relational Persistence for Idiomatic Java
4
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5
* indicated by the @author tags or express copyright attribution
6
* statements applied by the authors. All third-party contributions are
7
* distributed under license by Red Hat Middleware LLC.
9
* This copyrighted material is made available to anyone wishing to use, modify,
10
* copy, or redistribute it subject to the terms and conditions of the GNU
11
* Lesser General Public License, as published by the Free Software Foundation.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with this distribution; if not, write to:
20
* Free Software Foundation, Inc.
21
* 51 Franklin Street, Fifth Floor
22
* Boston, MA 02110-1301 USA
25
package org.hibernate.hql.classic;
27
import org.hibernate.QueryException;
28
import org.hibernate.hql.CollectionProperties;
29
import org.hibernate.util.StringHelper;
31
import java.util.HashSet;
38
public class PreprocessingParser implements Parser {
40
private static final Set HQL_OPERATORS;
43
HQL_OPERATORS = new HashSet();
44
HQL_OPERATORS.add( "<=" );
45
HQL_OPERATORS.add( ">=" );
46
HQL_OPERATORS.add( "=>" );
47
HQL_OPERATORS.add( "=<" );
48
HQL_OPERATORS.add( "!=" );
49
HQL_OPERATORS.add( "<>" );
50
HQL_OPERATORS.add( "!#" );
51
HQL_OPERATORS.add( "!~" );
52
HQL_OPERATORS.add( "!<" );
53
HQL_OPERATORS.add( "!>" );
54
HQL_OPERATORS.add( "is not" );
55
HQL_OPERATORS.add( "not like" );
56
HQL_OPERATORS.add( "not in" );
57
HQL_OPERATORS.add( "not between" );
58
HQL_OPERATORS.add( "not exists" );
61
private Map replacements;
62
private boolean quoted;
63
private StringBuffer quotedString;
64
private ClauseParser parser = new ClauseParser();
65
private String lastToken;
66
private String currentCollectionProp;
68
public PreprocessingParser(Map replacements) {
69
this.replacements = replacements;
72
public void token(String token, QueryTranslatorImpl q) throws QueryException {
74
//handle quoted strings
76
quotedString.append( token );
78
if ( "'".equals( token ) ) {
80
token = quotedString.toString();
83
quotedString = new StringBuffer( 20 ).append( token );
90
if ( ParserHelper.isWhitespace( token ) ) return;
93
String substoken = ( String ) replacements.get( token );
94
token = ( substoken == null ) ? token : substoken;
96
//handle HQL2 collection syntax
97
if ( currentCollectionProp != null ) {
98
if ( "(".equals( token ) ) {
101
else if ( ")".equals( token ) ) {
102
currentCollectionProp = null;
106
token = StringHelper.qualify( token, currentCollectionProp );
110
String prop = CollectionProperties.getNormalizedPropertyName( token.toLowerCase() );
111
if ( prop != null ) {
112
currentCollectionProp = prop;
118
//handle <=, >=, !=, is not, not between, not in
119
if ( lastToken == null ) {
123
String doubleToken = ( token.length() > 1 ) ?
124
lastToken + ' ' + token :
126
if ( HQL_OPERATORS.contains( doubleToken.toLowerCase() ) ) {
127
parser.token( doubleToken, q );
131
parser.token( lastToken, q );
138
public void start(QueryTranslatorImpl q) throws QueryException {
143
public void end(QueryTranslatorImpl q) throws QueryException {
144
if ( lastToken != null ) parser.token( lastToken, q );
147
currentCollectionProp = null;