~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/hql/ast/SqlASTFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id: SqlASTFactory.java 10060 2006-06-28 02:53:39Z steve.ebersole@jboss.com $
 
2
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.BinaryArithmeticOperatorNode;
 
10
import org.hibernate.hql.ast.tree.BinaryLogicOperatorNode;
 
11
import org.hibernate.hql.ast.tree.Case2Node;
 
12
import org.hibernate.hql.ast.tree.CaseNode;
 
13
import org.hibernate.hql.ast.tree.CollectionFunction;
 
14
import org.hibernate.hql.ast.tree.ConstructorNode;
 
15
import org.hibernate.hql.ast.tree.CountNode;
 
16
import org.hibernate.hql.ast.tree.DeleteStatement;
 
17
import org.hibernate.hql.ast.tree.DotNode;
 
18
import org.hibernate.hql.ast.tree.FromClause;
 
19
import org.hibernate.hql.ast.tree.FromElement;
 
20
import org.hibernate.hql.ast.tree.IdentNode;
 
21
import org.hibernate.hql.ast.tree.ImpliedFromElement;
 
22
import org.hibernate.hql.ast.tree.IndexNode;
 
23
import org.hibernate.hql.ast.tree.InitializeableNode;
 
24
import org.hibernate.hql.ast.tree.InsertStatement;
 
25
import org.hibernate.hql.ast.tree.IntoClause;
 
26
import org.hibernate.hql.ast.tree.LiteralNode;
 
27
import org.hibernate.hql.ast.tree.MethodNode;
 
28
import org.hibernate.hql.ast.tree.OrderByClause;
 
29
import org.hibernate.hql.ast.tree.ParameterNode;
 
30
import org.hibernate.hql.ast.tree.QueryNode;
 
31
import org.hibernate.hql.ast.tree.SelectClause;
 
32
import org.hibernate.hql.ast.tree.SelectExpressionImpl;
 
33
import org.hibernate.hql.ast.tree.SqlFragment;
 
34
import org.hibernate.hql.ast.tree.SqlNode;
 
35
import org.hibernate.hql.ast.tree.UnaryArithmeticNode;
 
36
import org.hibernate.hql.ast.tree.UpdateStatement;
 
37
import org.hibernate.hql.ast.tree.BetweenOperatorNode;
 
38
import org.hibernate.hql.ast.tree.UnaryLogicOperatorNode;
 
39
import org.hibernate.hql.ast.tree.InLogicOperatorNode;
 
40
import org.hibernate.hql.ast.tree.JavaConstantNode;
 
41
import org.hibernate.hql.ast.tree.SessionFactoryAwareNode;
 
42
import org.hibernate.hql.ast.tree.BooleanLiteralNode;
 
43
 
 
44
import java.lang.reflect.Constructor;
 
45
 
 
46
/**
 
47
 * Custom AST factory the intermediate tree that causes ANTLR to create specialized
 
48
 * AST nodes, given the AST node type (from HqlSqlTokenTypes).   HqlSqlWalker registers
 
49
 * this factory with itself when it is initialized.
 
50
 *
 
51
 * @author Joshua
 
52
 */
 
53
public class SqlASTFactory extends ASTFactory implements HqlSqlTokenTypes {
 
54
        private HqlSqlWalker walker;
 
55
 
 
56
        /**
 
57
         * Create factory with a specific mapping from token type
 
58
         * to Java AST node type.  Your subclasses of ASTFactory
 
59
         * can override and reuse the map stuff.
 
60
         */
 
61
        public SqlASTFactory(HqlSqlWalker walker) {
 
62
                super();
 
63
                this.walker = walker;
 
64
        }
 
65
 
 
66
        /**
 
67
         * Returns the class for a given token type (a.k.a. AST node type).
 
68
         *
 
69
         * @param tokenType The token type.
 
70
         * @return Class - The AST node class to instantiate.
 
71
         */
 
72
        public Class getASTNodeType(int tokenType) {
 
73
                switch ( tokenType ) {
 
74
                        case SELECT:
 
75
                        case QUERY:
 
76
                                return QueryNode.class;
 
77
                        case UPDATE:
 
78
                                return UpdateStatement.class;
 
79
                        case DELETE:
 
80
                                return DeleteStatement.class;
 
81
                        case INSERT:
 
82
                                return InsertStatement.class;
 
83
                        case INTO:
 
84
                                return IntoClause.class;
 
85
                        case FROM:
 
86
                                return FromClause.class;
 
87
                        case FROM_FRAGMENT:
 
88
                                return FromElement.class;
 
89
                        case IMPLIED_FROM:
 
90
                                return ImpliedFromElement.class;
 
91
                        case DOT:
 
92
                                return DotNode.class;
 
93
                        case INDEX_OP:
 
94
                                return IndexNode.class;
 
95
                                // Alias references and identifiers use the same node class.
 
96
                        case ALIAS_REF:
 
97
                        case IDENT:
 
98
                                return IdentNode.class;
 
99
                        case SQL_TOKEN:
 
100
                                return SqlFragment.class;
 
101
                        case METHOD_CALL:
 
102
                                return MethodNode.class;
 
103
                        case ELEMENTS:
 
104
                        case INDICES:
 
105
                                return CollectionFunction.class;
 
106
                        case SELECT_CLAUSE:
 
107
                                return SelectClause.class;
 
108
                        case SELECT_EXPR:
 
109
                                return SelectExpressionImpl.class;
 
110
                        case AGGREGATE:
 
111
                                return AggregateNode.class;
 
112
                        case COUNT:
 
113
                                return CountNode.class;
 
114
                        case CONSTRUCTOR:
 
115
                                return ConstructorNode.class;
 
116
                        case NUM_INT:
 
117
                        case NUM_FLOAT:
 
118
                        case NUM_LONG:
 
119
                        case NUM_DOUBLE:
 
120
                        case QUOTED_STRING:
 
121
                                return LiteralNode.class;
 
122
                        case TRUE:
 
123
                        case FALSE:
 
124
                                return BooleanLiteralNode.class;
 
125
                        case JAVA_CONSTANT:
 
126
                                return JavaConstantNode.class;
 
127
                        case ORDER:
 
128
                                return OrderByClause.class;
 
129
                        case PLUS:
 
130
                        case MINUS:
 
131
                        case STAR:
 
132
                        case DIV:
 
133
                                return BinaryArithmeticOperatorNode.class;
 
134
                        case UNARY_MINUS:
 
135
                        case UNARY_PLUS:
 
136
                                return UnaryArithmeticNode.class;
 
137
                        case CASE2:
 
138
                                return Case2Node.class;
 
139
                        case CASE:
 
140
                                return CaseNode.class;
 
141
                        case PARAM:
 
142
                        case NAMED_PARAM:
 
143
                                return ParameterNode.class;
 
144
                        case EQ:
 
145
                        case NE:
 
146
                        case LT:
 
147
                        case GT:
 
148
                        case LE:
 
149
                        case GE:
 
150
                        case LIKE:
 
151
                        case NOT_LIKE:
 
152
                                return BinaryLogicOperatorNode.class;
 
153
                        case IN:
 
154
                        case NOT_IN:
 
155
                                return InLogicOperatorNode.class;
 
156
                        case BETWEEN:
 
157
                        case NOT_BETWEEN:
 
158
                                return BetweenOperatorNode.class;
 
159
                        case IS_NULL:
 
160
                        case IS_NOT_NULL:
 
161
                        case EXISTS:
 
162
                                return UnaryLogicOperatorNode.class;
 
163
                        default:
 
164
                                return SqlNode.class;
 
165
                } // switch
 
166
        }
 
167
 
 
168
        protected AST createUsingCtor(Token token, String className) {
 
169
                Class c;
 
170
                AST t;
 
171
                try {
 
172
                        c = Class.forName( className );
 
173
                        Class[] tokenArgType = new Class[]{antlr.Token.class};
 
174
                        Constructor ctor = c.getConstructor( tokenArgType );
 
175
                        if ( ctor != null ) {
 
176
                                t = ( AST ) ctor.newInstance( new Object[]{token} ); // make a new one
 
177
                                initializeSqlNode( t );
 
178
                        }
 
179
                        else {
 
180
                                // just do the regular thing if you can't find the ctor
 
181
                                // Your AST must have default ctor to use this.
 
182
                                t = create( c );
 
183
                        }
 
184
                }
 
185
                catch ( Exception e ) {
 
186
                        throw new IllegalArgumentException( "Invalid class or can't make instance, " + className );
 
187
                }
 
188
                return t;
 
189
        }
 
190
 
 
191
        private void initializeSqlNode(AST t) {
 
192
                // Initialize SQL nodes here.
 
193
                if ( t instanceof InitializeableNode ) {
 
194
                        InitializeableNode initializeableNode = ( InitializeableNode ) t;
 
195
                        initializeableNode.initialize( walker );
 
196
                }
 
197
                if ( t instanceof SessionFactoryAwareNode ) {
 
198
                        ( ( SessionFactoryAwareNode ) t ).setSessionFactory( walker.getSessionFactoryHelper().getFactory() );
 
199
                }
 
200
        }
 
201
 
 
202
        /**
 
203
         * Actually instantiate the AST node.
 
204
         *
 
205
         * @param c The class to instantiate.
 
206
         * @return The instantiated and initialized node.
 
207
         */
 
208
        protected AST create(Class c) {
 
209
                AST t;
 
210
                try {
 
211
                        t = ( AST ) c.newInstance(); // make a new one
 
212
                        initializeSqlNode( t );
 
213
                }
 
214
                catch ( Exception e ) {
 
215
                        error( "Can't create AST Node " + c.getName() );
 
216
                        return null;
 
217
                }
 
218
                return t;
 
219
        }
 
220
 
 
221
}