1 package org.hibernate.hql.classic; 3 4 import org.hibernate.QueryException; 5 import org.hibernate.hql.CollectionProperties; 6 import org.hibernate.util.StringHelper; 7 8 import java.util.HashSet ; 9 import java.util.Map ; 10 import java.util.Set ; 11 12 15 public class PreprocessingParser implements Parser { 16 17 private static final Set HQL_OPERATORS; 18 19 static { 20 HQL_OPERATORS = new HashSet (); 21 HQL_OPERATORS.add( "<=" ); 22 HQL_OPERATORS.add( ">=" ); 23 HQL_OPERATORS.add( "=>" ); 24 HQL_OPERATORS.add( "=<" ); 25 HQL_OPERATORS.add( "!=" ); 26 HQL_OPERATORS.add( "<>" ); 27 HQL_OPERATORS.add( "!#" ); 28 HQL_OPERATORS.add( "!~" ); 29 HQL_OPERATORS.add( "!<" ); 30 HQL_OPERATORS.add( "!>" ); 31 HQL_OPERATORS.add( "is not" ); 32 HQL_OPERATORS.add( "not like" ); 33 HQL_OPERATORS.add( "not in" ); 34 HQL_OPERATORS.add( "not between" ); 35 HQL_OPERATORS.add( "not exists" ); 36 } 37 38 private Map replacements; 39 private boolean quoted; 40 private StringBuffer quotedString; 41 private ClauseParser parser = new ClauseParser(); 42 private String lastToken; 43 private String currentCollectionProp; 44 45 public PreprocessingParser(Map replacements) { 46 this.replacements = replacements; 47 } 48 49 public void token(String token, QueryTranslatorImpl q) throws QueryException { 50 51 if ( quoted ) { 53 quotedString.append( token ); 54 } 55 if ( "'".equals( token ) ) { 56 if ( quoted ) { 57 token = quotedString.toString(); 58 } 59 else { 60 quotedString = new StringBuffer ( 20 ).append( token ); 61 } 62 quoted = !quoted; 63 } 64 if ( quoted ) return; 65 66 if ( ParserHelper.isWhitespace( token ) ) return; 68 69 String substoken = ( String ) replacements.get( token ); 71 token = ( substoken == null ) ? token : substoken; 72 73 if ( currentCollectionProp != null ) { 75 if ( "(".equals( token ) ) { 76 return; 77 } 78 else if ( ")".equals( token ) ) { 79 currentCollectionProp = null; 80 return; 81 } 82 else { 83 token = StringHelper.qualify( token, currentCollectionProp ); 84 } 85 } 86 else { 87 String prop = CollectionProperties.getNormalizedPropertyName( token.toLowerCase() ); 88 if ( prop != null ) { 89 currentCollectionProp = prop; 90 return; 91 } 92 } 93 94 95 if ( lastToken == null ) { 97 lastToken = token; 98 } 99 else { 100 String doubleToken = ( token.length() > 1 ) ? 101 lastToken + ' ' + token : 102 lastToken + token; 103 if ( HQL_OPERATORS.contains( doubleToken.toLowerCase() ) ) { 104 parser.token( doubleToken, q ); 105 lastToken = null; 106 } 107 else { 108 parser.token( lastToken, q ); 109 lastToken = token; 110 } 111 } 112 113 } 114 115 public void start(QueryTranslatorImpl q) throws QueryException { 116 quoted = false; 117 parser.start( q ); 118 } 119 120 public void end(QueryTranslatorImpl q) throws QueryException { 121 if ( lastToken != null ) parser.token( lastToken, q ); 122 parser.end( q ); 123 lastToken = null; 124 currentCollectionProp = null; 125 } 126 127 } 128 129 130 131 132 133 134 | Popular Tags |