1 package org.hibernate.hql.ast; 3 4 import java.util.ArrayList ; 5 import java.util.LinkedList ; 6 import java.util.List ; 7 8 import antlr.RecognitionException; 9 import antlr.collections.AST; 10 import org.hibernate.QueryException; 11 import org.hibernate.dialect.function.SQLFunction; 12 import org.hibernate.engine.SessionFactoryImplementor; 13 import org.hibernate.hql.antlr.SqlGeneratorBase; 14 import org.hibernate.hql.ast.tree.MethodNode; 15 16 22 public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter { 23 26 private ParseErrorHandler parseErrorHandler; 27 28 35 private SqlWriter writer = new DefaultWriter(); 36 37 private SessionFactoryImplementor sessionFactory; 38 39 private LinkedList outputStack = new LinkedList (); 40 41 protected void out(String s) { 42 writer.clause( s ); 43 } 44 45 protected void commaBetweenParameters(String comma) { 46 writer.commaBetweenParameters( comma ); 47 } 48 49 public void reportError(RecognitionException e) { 50 parseErrorHandler.reportError( e ); } 52 53 public void reportError(String s) { 54 parseErrorHandler.reportError( s ); } 56 57 public void reportWarning(String s) { 58 parseErrorHandler.reportWarning( s ); 59 } 60 61 public ParseErrorHandler getParseErrorHandler() { 62 return parseErrorHandler; 63 } 64 65 public SqlGenerator(SessionFactoryImplementor sfi) { 66 super(); 67 parseErrorHandler = new ErrorCounter(); 68 sessionFactory = sfi; 69 } 70 71 public String getSQL() { 72 return getStringBuffer().toString(); 73 } 74 75 protected void optionalSpace() { 76 int c = getLastChar(); 77 switch ( c ) { 78 case -1: 79 return; 80 case ' ': 81 return; 82 case ')': 83 return; 84 case '(': 85 return; 86 default: 87 out( " " ); 88 } 89 } 90 91 protected void beginFunctionTemplate(AST m, AST i) { 92 MethodNode methodNode = ( MethodNode ) m; 93 SQLFunction template = methodNode.getSQLFunction(); 94 if ( template == null ) { 95 super.beginFunctionTemplate( m, i ); 97 } 98 else { 99 outputStack.addFirst( writer ); 101 writer = new FunctionArguments(); 102 } 103 } 104 105 protected void endFunctionTemplate(AST m) { 106 MethodNode methodNode = ( MethodNode ) m; 107 SQLFunction template = methodNode.getSQLFunction(); 108 if ( template == null ) { 109 super.endFunctionTemplate( m ); 110 } 111 else { 112 FunctionArguments functionArguments = ( FunctionArguments ) writer; writer = ( SqlWriter ) outputStack.removeFirst(); 115 out( template.render( functionArguments.getArgs(), sessionFactory ) ); 116 } 117 } 118 119 121 124 interface SqlWriter { 125 void clause(String clause); 126 127 134 void commaBetweenParameters(String comma); 135 } 136 137 141 class FunctionArguments implements SqlWriter { 142 private int argInd; 143 private final List args = new ArrayList ( 3 ); 144 145 public void clause(String clause) { 146 if ( argInd == args.size() ) { 147 args.add( clause ); 148 } 149 else { 150 args.set( argInd, args.get( argInd ) + clause ); 151 } 152 } 153 154 public void commaBetweenParameters(String comma) { 155 ++argInd; 156 } 157 158 public List getArgs() { 159 return args; 160 } 161 } 162 163 166 class DefaultWriter implements SqlWriter { 167 public void clause(String clause) { 168 getStringBuffer().append( clause ); 169 } 170 171 public void commaBetweenParameters(String comma) { 172 getStringBuffer().append( comma ); 173 } 174 } 175 176 public static void panic() { 177 throw new QueryException( "TreeWalker: panic" ); 178 } 179 } 180 | Popular Tags |