1 package org.hibernate.hql; 3 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.hibernate.MappingException; 7 import org.hibernate.engine.SessionFactoryImplementor; 8 import org.hibernate.hql.classic.ParserHelper; 9 import org.hibernate.util.StringHelper; 10 11 import java.util.ArrayList ; 12 import java.util.HashSet ; 13 import java.util.Set ; 14 15 22 public final class QuerySplitter { 23 24 private static final Log log = LogFactory.getLog( QuerySplitter.class ); 25 26 private static final Set BEFORE_CLASS_TOKENS = new HashSet (); 27 private static final Set NOT_AFTER_CLASS_TOKENS = new HashSet (); 28 29 static { 30 BEFORE_CLASS_TOKENS.add( "from" ); 31 BEFORE_CLASS_TOKENS.add( "," ); 33 NOT_AFTER_CLASS_TOKENS.add( "in" ); 34 NOT_AFTER_CLASS_TOKENS.add( "from" ); 36 NOT_AFTER_CLASS_TOKENS.add( ")" ); 37 } 38 39 43 private QuerySplitter() { 44 } 45 46 50 public static String [] concreteQueries(String query, SessionFactoryImplementor factory) throws MappingException { 51 52 56 58 String [] tokens = StringHelper.split( StringHelper.WHITESPACE + "(),", query, true ); 59 if ( tokens.length == 0 ) return new String []{query}; ArrayList placeholders = new ArrayList (); 61 ArrayList replacements = new ArrayList (); 62 StringBuffer templateQuery = new StringBuffer ( 40 ); 63 int count = 0; 64 String last = null; 65 int nextIndex = 0; 66 String next = null; 67 boolean isSelectClause = false; 68 69 templateQuery.append( tokens[0] ); 70 if ( "select".equals( tokens[0].toLowerCase() ) ) isSelectClause = true; 71 72 for ( int i = 1; i < tokens.length; i++ ) { 73 74 if ( !ParserHelper.isWhitespace( tokens[i - 1] ) ) last = tokens[i - 1].toLowerCase(); 76 77 if ( "from".equals( tokens[i].toLowerCase() ) ) isSelectClause = false; 79 80 String token = tokens[i]; 81 if ( !ParserHelper.isWhitespace( token ) || last == null ) { 82 83 if ( nextIndex <= i ) { 85 for ( nextIndex = i + 1; nextIndex < tokens.length; nextIndex++ ) { 86 next = tokens[nextIndex].toLowerCase(); 87 if ( !ParserHelper.isWhitespace( next ) ) break; 88 } 89 } 90 91 boolean process = !isSelectClause && 92 isJavaIdentifier( token ) && 93 isPossiblyClassName( last, next ); 94 95 if (process) { 96 String importedClassName = getImportedClass( token, factory ); 97 if ( importedClassName != null ) { 98 String [] implementors = factory.getImplementors( importedClassName ); 99 String placeholder = "$clazz" + count++ + "$"; 100 if ( implementors != null ) { 101 placeholders.add( placeholder ); 102 replacements.add( implementors ); 103 } 104 token = placeholder; } 106 } 107 108 } 109 110 templateQuery.append( token ); 111 112 } 113 String [] results = StringHelper.multiply( templateQuery.toString(), placeholders.iterator(), replacements.iterator() ); 114 if ( results.length == 0 ) log.warn( "no persistent classes found for query class: " + query ); 115 return results; 116 } 117 118 private static boolean isPossiblyClassName(String last, String next) { 119 return "class".equals( last ) || ( 120 BEFORE_CLASS_TOKENS.contains( last ) && 121 !NOT_AFTER_CLASS_TOKENS.contains( next ) 122 ); 123 } 124 125 private static boolean isJavaIdentifier(String token) { 126 return Character.isJavaIdentifierStart( token.charAt( 0 ) ); 127 } 128 129 public static String getImportedClass(String name, SessionFactoryImplementor factory) { 130 return factory.getImportedClassName( name ); 131 } 132 } 133 | Popular Tags |