KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.CurrentDatetimeOperatorNode
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.sql.dictionary.DataDictionary;
25
26 import org.apache.derby.iapi.sql.compile.CompilerContext;
27
28 import org.apache.derby.iapi.types.TypeId;
29 import org.apache.derby.iapi.types.DataTypeDescriptor;
30
31 import org.apache.derby.iapi.services.compiler.MethodBuilder;
32 import org.apache.derby.iapi.services.compiler.LocalField;
33 import org.apache.derby.iapi.services.sanity.SanityManager;
34
35 import org.apache.derby.iapi.store.access.Qualifier;
36
37 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
38
39 import java.lang.reflect.Modifier JavaDoc;
40
41 import org.apache.derby.iapi.error.StandardException;
42
43 import java.sql.Types JavaDoc;
44
45 import java.util.Vector JavaDoc;
46
47 /**
48  * The CurrentDatetimeOperator operator is for the builtin CURRENT_DATE,
49  * CURRENT_TIME, and CURRENT_TIMESTAMP operations.
50  *
51  * @author ames
52  */

53 public class CurrentDatetimeOperatorNode extends ValueNode {
54
55     public static final int CURRENT_DATE = 0;
56     public static final int CURRENT_TIME = 1;
57     public static final int CURRENT_TIMESTAMP = 2;
58
59     static private final int jdbcTypeId[] = {
60         Types.DATE,
61         Types.TIME,
62         Types.TIMESTAMP
63     };
64     static private final String JavaDoc methodName[] = { // used in toString only
65
"CURRENT DATE",
66         "CURRENT TIME",
67         "CURRENT TIMSTAMP"
68     };
69
70     private int whichType;
71
72     public void init(Object JavaDoc whichType) {
73         this.whichType = ((Integer JavaDoc) whichType).intValue();
74
75         if (SanityManager.DEBUG)
76             SanityManager.ASSERT(this.whichType >= 0 && this.whichType <= 2);
77     }
78
79     //
80
// QueryTreeNode interface
81
//
82

83     /**
84      * Binding this expression means setting the result DataTypeServices.
85      * In this case, the result type is based on the operation requested.
86      *
87      * @param fromList The FROM list for the statement. This parameter
88      * is not used in this case.
89      * @param subqueryList The subquery list being built as we find
90      * SubqueryNodes. Not used in this case.
91      * @param aggregateVector The aggregate vector being built as we find
92      * AggregateNodes. Not used in this case.
93      *
94      * @return The new top of the expression tree.
95      *
96      * @exception StandardException Thrown on error
97      */

98     public ValueNode bindExpression(FromList fromList, SubqueryList subqueryList,
99                             Vector JavaDoc aggregateVector)
100                     throws StandardException
101     {
102         checkReliability( methodName[whichType], CompilerContext.DATETIME_ILLEGAL );
103
104         setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor(
105                         jdbcTypeId[whichType],
106                         false /* Not nullable */
107                     )
108                 );
109         return this;
110     }
111
112     /**
113      * Return the variant type for the underlying expression.
114      * The variant type can be:
115      * VARIANT - variant within a scan
116      * (method calls and non-static field access)
117      * SCAN_INVARIANT - invariant within a scan
118      * (column references from outer tables)
119      * QUERY_INVARIANT - invariant within the life of a query
120      * (constant expressions)
121      *
122      * @return The variant type for the underlying expression.
123      */

124     protected int getOrderableVariantType()
125     {
126         // CurrentDate, Time, Timestamp are invariant for the life of the query
127
return Qualifier.QUERY_INVARIANT;
128     }
129
130     /**
131      * CurrentDatetimeOperatorNode is used in expressions.
132      * The expression generated for it invokes a static method
133      * on a special Cloudscape type to get the system time and
134      * wrap it in the right java.sql type, and then wrap it
135      * into the right shape for an arbitrary value, i.e. a column
136      * holder. This is very similar to what constants do.
137      *
138      * @param acb The ExpressionClassBuilder for the class being built
139      * @param mb The method the code to place the code
140      *
141      * @exception StandardException Thrown on error
142      */

143     public void generateExpression(ExpressionClassBuilder acb,
144                                             MethodBuilder mb)
145                                     throws StandardException
146     {
147         acb.pushDataValueFactory(mb);
148
149         /*
150         ** First, we generate the current expression to be stuffed into
151         ** the right shape of holder.
152         */

153         switch (whichType) {
154             case CURRENT_DATE:
155                 acb.getCurrentDateExpression(mb);
156                 break;
157             case CURRENT_TIME:
158                 acb.getCurrentTimeExpression(mb);
159                 break;
160             case CURRENT_TIMESTAMP:
161                 acb.getCurrentTimestampExpression(mb);
162                 break;
163         }
164
165         String JavaDoc fieldType = getTypeCompiler().interfaceName();
166         LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, fieldType);
167         getTypeCompiler().generateDataValue(mb, field);
168     }
169
170     /*
171         print the non-node subfields
172      */

173     public String JavaDoc toString() {
174         if (SanityManager.DEBUG)
175         {
176             return super.toString()+"method = "+methodName[whichType]+"\n";
177         }
178         else
179         {
180             return "";
181         }
182     }
183         
184         /**
185          * {@inheritDoc}
186          */

187     protected boolean isEquivalent(ValueNode o)
188     {
189         if (isSameNodeType(o))
190         {
191             CurrentDatetimeOperatorNode other = (CurrentDatetimeOperatorNode)o;
192             return other.whichType == whichType;
193         }
194         return false;
195     }
196 }
197
Popular Tags