~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/project/core/src/main/java/org/hibernate/hql/classic/PreprocessingParser.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Hibernate, Relational Persistence for Idiomatic Java
3
 
 *
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.
8
 
 *
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.
12
 
 *
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
16
 
 * for more details.
17
 
 *
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
23
 
 *
24
 
 */
25
 
package org.hibernate.hql.classic;
26
 
 
27
 
import org.hibernate.QueryException;
28
 
import org.hibernate.hql.CollectionProperties;
29
 
import org.hibernate.util.StringHelper;
30
 
 
31
 
import java.util.HashSet;
32
 
import java.util.Map;
33
 
import java.util.Set;
34
 
 
35
 
/**
36
 
 *
37
 
 */
38
 
public class PreprocessingParser implements Parser {
39
 
 
40
 
        private static final Set HQL_OPERATORS;
41
 
 
42
 
        static {
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" );
59
 
        }
60
 
 
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;
67
 
 
68
 
        public PreprocessingParser(Map replacements) {
69
 
                this.replacements = replacements;
70
 
        }
71
 
 
72
 
        public void token(String token, QueryTranslatorImpl q) throws QueryException {
73
 
 
74
 
                //handle quoted strings
75
 
                if ( quoted ) {
76
 
                        quotedString.append( token );
77
 
                }
78
 
                if ( "'".equals( token ) ) {
79
 
                        if ( quoted ) {
80
 
                                token = quotedString.toString();
81
 
                        }
82
 
                        else {
83
 
                                quotedString = new StringBuffer( 20 ).append( token );
84
 
                        }
85
 
                        quoted = !quoted;
86
 
                }
87
 
                if ( quoted ) return;
88
 
 
89
 
                //ignore whitespace
90
 
                if ( ParserHelper.isWhitespace( token ) ) return;
91
 
 
92
 
                //do replacements
93
 
                String substoken = ( String ) replacements.get( token );
94
 
                token = ( substoken == null ) ? token : substoken;
95
 
 
96
 
                //handle HQL2 collection syntax
97
 
                if ( currentCollectionProp != null ) {
98
 
                        if ( "(".equals( token ) ) {
99
 
                                return;
100
 
                        }
101
 
                        else if ( ")".equals( token ) ) {
102
 
                                currentCollectionProp = null;
103
 
                                return;
104
 
                        }
105
 
                        else {
106
 
                                token = StringHelper.qualify( token, currentCollectionProp );
107
 
                        }
108
 
                }
109
 
                else {
110
 
                        String prop = CollectionProperties.getNormalizedPropertyName( token.toLowerCase() );
111
 
                        if ( prop != null ) {
112
 
                                currentCollectionProp = prop;
113
 
                                return;
114
 
                        }
115
 
                }
116
 
 
117
 
 
118
 
                //handle <=, >=, !=, is not, not between, not in
119
 
                if ( lastToken == null ) {
120
 
                        lastToken = token;
121
 
                }
122
 
                else {
123
 
                        String doubleToken = ( token.length() > 1 ) ?
124
 
                                        lastToken + ' ' + token :
125
 
                                        lastToken + token;
126
 
                        if ( HQL_OPERATORS.contains( doubleToken.toLowerCase() ) ) {
127
 
                                parser.token( doubleToken, q );
128
 
                                lastToken = null;
129
 
                        }
130
 
                        else {
131
 
                                parser.token( lastToken, q );
132
 
                                lastToken = token;
133
 
                        }
134
 
                }
135
 
 
136
 
        }
137
 
 
138
 
        public void start(QueryTranslatorImpl q) throws QueryException {
139
 
                quoted = false;
140
 
                parser.start( q );
141
 
        }
142
 
 
143
 
        public void end(QueryTranslatorImpl q) throws QueryException {
144
 
                if ( lastToken != null ) parser.token( lastToken, q );
145
 
                parser.end( q );
146
 
                lastToken = null;
147
 
                currentCollectionProp = null;
148
 
        }
149
 
 
150
 
}
151
 
 
152
 
 
153
 
 
154
 
 
155
 
 
156