KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.UnaryDateTimestampOperatorNode
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.types.DataValueFactory;
25 import org.apache.derby.iapi.types.DataTypeDescriptor;
26 import org.apache.derby.iapi.types.DataValueDescriptor;
27 import org.apache.derby.iapi.types.DateTimeDataValue;
28 import org.apache.derby.iapi.services.compiler.MethodBuilder;
29 import org.apache.derby.iapi.error.StandardException;
30
31 import org.apache.derby.iapi.sql.compile.C_NodeTypes;
32
33 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
34
35 import org.apache.derby.iapi.reference.ClassName;
36 import org.apache.derby.iapi.reference.SQLState;
37
38 import org.apache.derby.iapi.services.classfile.VMOpcode;
39 import org.apache.derby.iapi.services.sanity.SanityManager;
40
41 import java.sql.Types JavaDoc;
42
43 import java.util.Vector JavaDoc;
44
45 /**
46  * This class implements the timestamp( x) and date(x) functions.
47  *
48  * These two functions implement a few special cases of string conversions beyond the normal string to
49  * date/timestamp casts.
50  */

51 public class UnaryDateTimestampOperatorNode extends UnaryOperatorNode
52 {
53     private static final String JavaDoc TIMESTAMP_METHOD_NAME = "getTimestamp";
54     private static final String JavaDoc DATE_METHOD_NAME = "getDate";
55     
56     /**
57      * @param operand The operand of the function
58      * @param targetType The type of the result. Timestamp or Date.
59      *
60      * @exception StandardException Thrown on error
61      */

62
63     public void init( Object JavaDoc operand, Object JavaDoc targetType)
64         throws StandardException
65     {
66         setType( (DataTypeDescriptor) targetType);
67         switch( getTypeServices().getJDBCTypeId())
68         {
69         case Types.DATE:
70             super.init( operand, "date", DATE_METHOD_NAME);
71             break;
72
73         case Types.TIMESTAMP:
74             super.init( operand, "timestamp", TIMESTAMP_METHOD_NAME);
75             break;
76
77         default:
78             if( SanityManager.DEBUG)
79                 SanityManager.NOTREACHED();
80             super.init( operand);
81         }
82     }
83     
84     /**
85      * Called by UnaryOperatorNode.bindExpression.
86      *
87      * If the operand is a constant then evaluate the function at compile time. Otherwise,
88      * if the operand input type is the same as the output type then discard this node altogether.
89      * If the function is "date" and the input is a timestamp then change this node to a cast.
90      *
91      * @param fromList The FROM list for the query this
92      * expression is in, for binding columns.
93      * @param subqueryList The subquery list being built as we find SubqueryNodes
94      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
95      *
96      * @return The new top of the expression tree.
97      *
98      * @exception StandardException Thrown on error
99      */

100     protected ValueNode bindUnaryOperator(
101                     FromList fromList, SubqueryList subqueryList,
102                     Vector JavaDoc aggregateVector)
103                 throws StandardException
104     {
105         boolean isIdentity = false; // Is this function the identity operator?
106
boolean operandIsNumber = false;
107         
108         super.bindUnaryOperator( fromList, subqueryList, aggregateVector);
109         DataTypeDescriptor operandType = operand.getTypeServices();
110         switch( operandType.getJDBCTypeId())
111         {
112         case Types.BIGINT:
113         case Types.INTEGER:
114         case Types.SMALLINT:
115         case Types.TINYINT:
116         case Types.DECIMAL:
117         case Types.NUMERIC:
118         case Types.DOUBLE:
119         case Types.FLOAT:
120             if( TIMESTAMP_METHOD_NAME.equals( methodName))
121                 invalidOperandType();
122             operandIsNumber = true;
123             break;
124             
125         case Types.CHAR:
126         case Types.VARCHAR:
127             break;
128
129         case Types.DATE:
130             if( TIMESTAMP_METHOD_NAME.equals( methodName))
131                 invalidOperandType();
132             isIdentity = true;
133             break;
134             
135         case Types.NULL:
136             break;
137            
138         case Types.TIMESTAMP:
139             if( TIMESTAMP_METHOD_NAME.equals( methodName))
140                 isIdentity = true;
141             break;
142
143         default:
144             invalidOperandType();
145         }
146        
147         if( operand instanceof ConstantNode)
148         {
149             DataValueFactory dvf = getLanguageConnectionContext().getDataValueFactory();
150             DataValueDescriptor sourceValue = ((ConstantNode) operand).getValue();
151             DataValueDescriptor destValue = null;
152             if( sourceValue.isNull())
153             {
154                 destValue = (TIMESTAMP_METHOD_NAME.equals( methodName))
155                 ? dvf.getNullTimestamp( (DateTimeDataValue) null)
156                 : dvf.getNullDate( (DateTimeDataValue) null);
157             }
158             else
159             {
160                 destValue = (TIMESTAMP_METHOD_NAME.equals( methodName))
161                   ? dvf.getTimestamp( sourceValue) : dvf.getDate( sourceValue);
162             }
163             return (ValueNode) getNodeFactory().getNode( C_NodeTypes.USERTYPE_CONSTANT_NODE,
164                                                          destValue, getContextManager());
165         }
166
167         if( isIdentity)
168             return operand;
169         return this;
170     } // end of bindUnaryOperator
171

172     private void invalidOperandType() throws StandardException
173     {
174         throw StandardException.newException( SQLState.LANG_UNARY_FUNCTION_BAD_TYPE,
175                                               getOperatorString(), getOperand().getTypeServices().getSQLstring());
176     }
177
178     /**
179      * Do code generation for this unary operator.
180      *
181      * @param acb The ExpressionClassBuilder for the class we're generating
182      * @param mb The method the expression will go into
183      *
184      *
185      * @exception StandardException Thrown on error
186      */

187
188     public void generateExpression( ExpressionClassBuilder acb,
189                                     MethodBuilder mb)
190         throws StandardException
191     {
192         acb.pushDataValueFactory( mb);
193         operand.generateExpression( acb, mb);
194         mb.cast( ClassName.DataValueDescriptor);
195         mb.callMethod( VMOpcode.INVOKEINTERFACE, (String JavaDoc) null, methodName, getTypeCompiler().interfaceName(), 1);
196     } // end of generateExpression
197
}
198
Popular Tags