1 package org.hibernate.hql.ast; 3 4 import antlr.ASTFactory; 5 import antlr.Token; 6 import antlr.collections.AST; 7 import org.hibernate.hql.antlr.HqlSqlTokenTypes; 8 import org.hibernate.hql.ast.tree.AggregateNode; 9 import org.hibernate.hql.ast.tree.ArithmeticNode; 10 import org.hibernate.hql.ast.tree.Case2Node; 11 import org.hibernate.hql.ast.tree.CaseNode; 12 import org.hibernate.hql.ast.tree.CollectionFunction; 13 import org.hibernate.hql.ast.tree.ConstructorNode; 14 import org.hibernate.hql.ast.tree.CountNode; 15 import org.hibernate.hql.ast.tree.DeleteStatement; 16 import org.hibernate.hql.ast.tree.DotNode; 17 import org.hibernate.hql.ast.tree.FromClause; 18 import org.hibernate.hql.ast.tree.FromElement; 19 import org.hibernate.hql.ast.tree.IdentNode; 20 import org.hibernate.hql.ast.tree.ImpliedFromElement; 21 import org.hibernate.hql.ast.tree.IndexNode; 22 import org.hibernate.hql.ast.tree.InitializeableNode; 23 import org.hibernate.hql.ast.tree.InsertStatement; 24 import org.hibernate.hql.ast.tree.IntoClause; 25 import org.hibernate.hql.ast.tree.LiteralNode; 26 import org.hibernate.hql.ast.tree.MethodNode; 27 import org.hibernate.hql.ast.tree.OrderByClause; 28 import org.hibernate.hql.ast.tree.ParameterNode; 29 import org.hibernate.hql.ast.tree.QueryNode; 30 import org.hibernate.hql.ast.tree.SelectClause; 31 import org.hibernate.hql.ast.tree.SelectExpressionImpl; 32 import org.hibernate.hql.ast.tree.SqlFragment; 33 import org.hibernate.hql.ast.tree.SqlNode; 34 import org.hibernate.hql.ast.tree.UnaryNode; 35 import org.hibernate.hql.ast.tree.UpdateStatement; 36 37 import java.lang.reflect.Constructor ; 38 39 47 public class SqlASTFactory extends ASTFactory implements HqlSqlTokenTypes { 48 private HqlSqlWalker walker; 49 50 55 public SqlASTFactory(HqlSqlWalker walker) { 56 super(); 57 this.walker = walker; 58 } 59 60 66 public Class getASTNodeType(int tokenType) { 67 switch ( tokenType ) { 68 case SELECT: 69 case QUERY: 70 return QueryNode.class; 71 case UPDATE: 72 return UpdateStatement.class; 73 case DELETE: 74 return DeleteStatement.class; 75 case INSERT: 76 return InsertStatement.class; 77 case INTO: 78 return IntoClause.class; 79 case FROM: 80 return FromClause.class; 81 case FROM_FRAGMENT: 82 return FromElement.class; 83 case IMPLIED_FROM: 84 return ImpliedFromElement.class; 85 case DOT: 86 return DotNode.class; 87 case INDEX_OP: 88 return IndexNode.class; 89 case ALIAS_REF: 91 case IDENT: 92 return IdentNode.class; 93 case SQL_TOKEN: 94 return SqlFragment.class; 95 case METHOD_CALL: 96 return MethodNode.class; 97 case ELEMENTS: 98 case INDICES: 99 return CollectionFunction.class; 100 case SELECT_CLAUSE: 101 return SelectClause.class; 102 case SELECT_EXPR: 103 return SelectExpressionImpl.class; 104 case AGGREGATE: 105 return AggregateNode.class; 106 case COUNT: 107 return CountNode.class; 108 case CONSTRUCTOR: 109 return ConstructorNode.class; 110 case NUM_INT: 111 case NUM_FLOAT: 112 case NUM_LONG: 113 case NUM_DOUBLE: 114 case QUOTED_STRING: 115 case TRUE: 116 case FALSE: 117 return LiteralNode.class; 118 case ORDER: 119 return OrderByClause.class; 120 case PLUS: 121 case MINUS: 122 case STAR: 123 case DIV: 124 return ArithmeticNode.class; 125 case UNARY_MINUS: 126 case UNARY_PLUS: 127 return UnaryNode.class; 128 case CASE2: 129 return Case2Node.class; 130 case CASE: 131 return CaseNode.class; 132 case PARAM: 133 case NAMED_PARAM: 134 return ParameterNode.class; 135 default: 136 return SqlNode.class; 137 } } 139 140 protected AST createUsingCtor(Token token, String className) { 141 Class c; 142 AST t; 143 try { 144 c = Class.forName( className ); 145 Class [] tokenArgType = new Class []{antlr.Token.class}; 146 Constructor ctor = c.getConstructor( tokenArgType ); 147 if ( ctor != null ) { 148 t = ( AST ) ctor.newInstance( new Object []{token} ); initializeSqlNode( t ); 150 } 151 else { 152 t = create( c ); 155 } 156 } 157 catch ( Exception e ) { 158 throw new IllegalArgumentException ( "Invalid class or can't make instance, " + className ); 159 } 160 return t; 161 } 162 163 private void initializeSqlNode(AST t) { 164 if ( t instanceof InitializeableNode ) { 166 InitializeableNode initializeableNode = ( InitializeableNode ) t; 167 initializeableNode.initialize( walker ); 168 } 169 } 170 171 177 protected AST create(Class c) { 178 AST t; 179 try { 180 t = ( AST ) c.newInstance(); initializeSqlNode( t ); 182 } 183 catch ( Exception e ) { 184 error( "Can't create AST Node " + c.getName() ); 185 return null; 186 } 187 return t; 188 } 189 190 } 191 | Popular Tags |