KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > DefaultNode


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.DefaultNode
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.services.context.ContextManager;
25
26 import org.apache.derby.iapi.services.compiler.MethodBuilder;
27
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29
30 import org.apache.derby.iapi.sql.compile.CompilerContext;
31 import org.apache.derby.iapi.sql.compile.Parser;
32 import org.apache.derby.iapi.sql.compile.C_NodeTypes;
33
34 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
35
36 import org.apache.derby.iapi.types.TypeId;
37
38 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
39 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
40 import org.apache.derby.iapi.sql.dictionary.DefaultDescriptor;
41 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
42
43 import org.apache.derby.iapi.error.StandardException;
44
45 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
46
47 import org.apache.derby.catalog.types.DefaultInfoImpl;
48
49 import java.util.Vector JavaDoc;
50
51 /**
52  * DefaultNode represents a column/parameter default.
53  */

54 public class DefaultNode extends ValueNode
55 {
56     private String JavaDoc columnName;
57     private String JavaDoc defaultText;
58     private ValueNode defaultTree;
59
60     /**
61      * Initializer for a column/parameter default.
62      *
63      * @param defaultTree Query tree for default
64      * @param defaultText The text of the default.
65      */

66     public void init(
67                     Object JavaDoc defaultTree,
68                     Object JavaDoc defaultText)
69     {
70         this.defaultTree = (ValueNode) defaultTree;
71         this.defaultText = (String JavaDoc) defaultText;
72     }
73
74     /**
75      * Initializer for insert/update
76      *
77      */

78     public void init(Object JavaDoc columnName)
79     {
80         this.columnName = (String JavaDoc) columnName;
81     }
82
83     /**
84       * Get the text of the default.
85       */

86     public String JavaDoc getDefaultText()
87     {
88         return defaultText;
89     }
90
91     /**
92      * Get the query tree for the default.
93      *
94      * @return The query tree for the default.
95      */

96     ValueNode getDefaultTree()
97     {
98         return defaultTree;
99     }
100
101     /**
102      * Convert this object to a String. See comments in QueryTreeNode.java
103      * for how this should be done for tree printing.
104      *
105      * @return This object as a String
106      */

107
108     public String JavaDoc toString()
109     {
110         if (SanityManager.DEBUG)
111         {
112             return "defaultTree: " + defaultTree + "\n" +
113                    "defaultText: " + defaultText + "\n" +
114                 super.toString();
115         }
116         else
117         {
118             return "";
119         }
120     }
121
122     /**
123      * Bind this expression. This means binding the sub-expressions,
124      * as well as figuring out what the return type is for this expression.
125      * In this case, there are no sub-expressions, and the return type
126      * is already known, so this is just a stub.
127      *
128      * @param fromList The FROM list for the query this
129      * expression is in, for binding columns.
130      * @param subqueryList The subquery list being built as we find SubqueryNodes
131      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
132      *
133      * @return The new top of the expression tree.
134      *
135      * @exception StandardException Thrown on failure
136      */

137     public ValueNode bindExpression(FromList fromList, SubqueryList subqueryList,
138             Vector JavaDoc aggregateVector)
139         throws StandardException
140     {
141         ColumnDescriptor cd;
142         TableDescriptor td;
143
144         if (SanityManager.DEBUG)
145         {
146             SanityManager.ASSERT(fromList.size() != 0,
147                 "fromList expected to be non-empty");
148             if (! (fromList.elementAt(0) instanceof FromBaseTable))
149             {
150                 SanityManager.THROWASSERT(
151                     "fromList.elementAt(0) expected to be instanceof FromBaseTable, not " +
152                     fromList.elementAt(0).getClass().getName());
153             }
154
155         }
156         // Get the TableDescriptor for the target table
157
td = ((FromBaseTable) fromList.elementAt(0)).getTableDescriptor();
158
159         // Get the ColumnDescriptor for the column
160
cd = td.getColumnDescriptor(columnName);
161         if (SanityManager.DEBUG)
162         {
163             SanityManager.ASSERT(cd != null,
164                 "cd expected to be non-null");
165         }
166
167         /* If we have the default text, then parse and bind it and
168          * return the tree.
169          */

170         DefaultInfoImpl defaultInfo = (DefaultInfoImpl) cd.getDefaultInfo();
171         if (defaultInfo != null)
172         {
173             String JavaDoc defaultText = defaultInfo.getDefaultText();
174             ValueNode defaultTree = parseDefault(defaultText, getLanguageConnectionContext(),
175                                                    getCompilerContext());
176
177             /* Query is dependent on the DefaultDescriptor */
178             DefaultDescriptor defaultDescriptor = cd.getDefaultDescriptor(
179                                                     getDataDictionary());
180             getCompilerContext().createDependency(defaultDescriptor);
181
182             return defaultTree.bindExpression(
183                                     fromList,
184                                     subqueryList,
185                                     aggregateVector);
186         }
187         else
188         {
189             // Default is null
190
ValueNode nullNode = (ValueNode) getNodeFactory().getNode(
191                                         C_NodeTypes.UNTYPED_NULL_CONSTANT_NODE,
192                                         getContextManager());
193             return nullNode;
194         }
195     }
196
197     /**
198       * Parse a default and turn it into a query tree.
199       *
200       * @param defaultText Text of Default.
201       * @param lcc LanguageConnectionContext
202       * @param cc CompilerContext
203       *
204       * @return The parsed default as a query tree.
205       *
206       * @exception StandardException Thrown on failure
207       */

208     public static ValueNode parseDefault
209     (
210         String JavaDoc defaultText,
211         LanguageConnectionContext lcc,
212         CompilerContext cc
213     )
214         throws StandardException
215     {
216         Parser p;
217         ValueNode defaultTree;
218
219         /* Get a Statement to pass to the parser */
220
221         /* We're all set up to parse. We have to build a compilable SQL statement
222          * before we can parse - So, we goober up a VALUES defaultText.
223          */

224         String JavaDoc values = "VALUES " + defaultText;
225         
226         /*
227         ** Get a new compiler context, so the parsing of the select statement
228         ** doesn't mess up anything in the current context (it could clobber
229         ** the ParameterValueSet, for example).
230         */

231         CompilerContext newCC = lcc.pushCompilerContext();
232
233         p = newCC.getParser();
234
235         
236         /* Finally, we can call the parser */
237         // Since this is always nested inside another SQL statement, so topLevel flag
238
// should be false
239
QueryTreeNode qt = p.parseStatement(values);
240         if (SanityManager.DEBUG)
241         {
242             if (! (qt instanceof CursorNode))
243             {
244                 SanityManager.THROWASSERT(
245                     "qt expected to be instanceof CursorNode, not " +
246                     qt.getClass().getName());
247             }
248             CursorNode cn = (CursorNode) qt;
249             if (! (cn.getResultSetNode() instanceof RowResultSetNode))
250             {
251                 SanityManager.THROWASSERT(
252                     "cn.getResultSetNode() expected to be instanceof RowResultSetNode, not " +
253                     cn.getResultSetNode().getClass().getName());
254             }
255         }
256
257         defaultTree = ((ResultColumn)
258                             ((CursorNode) qt).getResultSetNode().getResultColumns().elementAt(0)).
259                                     getExpression();
260
261         lcc.popCompilerContext(newCC);
262
263         return defaultTree;
264     }
265
266     /**
267      * @exception StandardException Thrown on failure
268      */

269     public void generateExpression(ExpressionClassBuilder acb,
270                                             MethodBuilder mb)
271         throws StandardException
272     {
273         if (SanityManager.DEBUG)
274         {
275             SanityManager.THROWASSERT(
276                 "generateExpression not expected to be called");
277         }
278     }
279
280     /**
281      * @inheritDoc
282      */

283     protected boolean isEquivalent(ValueNode other)
284     {
285         return false;
286     }
287 }
288
Popular Tags