KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.TimestampOperatorNode
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.TypeId;
25 import org.apache.derby.iapi.types.DataValueFactory;
26 import org.apache.derby.iapi.types.DataTypeDescriptor;
27 import org.apache.derby.iapi.services.compiler.MethodBuilder;
28 import org.apache.derby.iapi.error.StandardException;
29
30 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
31
32 import org.apache.derby.iapi.reference.ClassName;
33 import org.apache.derby.iapi.reference.SQLState;
34
35 import org.apache.derby.iapi.services.classfile.VMOpcode;
36
37 import java.sql.Types JavaDoc;
38
39 import java.util.Vector JavaDoc;
40
41 /**
42  * The TimestampOperatorNode class implements the timestamp( date, time) function.
43  */

44
45 public class TimestampOperatorNode extends BinaryOperatorNode
46 {
47
48     /**
49      * Initailizer for a TimestampOperatorNode.
50      *
51      * @param date The date
52      * @param time The time
53      */

54
55     public void init( Object JavaDoc date,
56                       Object JavaDoc time)
57     {
58         leftOperand = (ValueNode) date;
59         rightOperand = (ValueNode) time;
60         operator = "timestamp";
61         methodName = "getTimestamp";
62     }
63
64
65     /**
66      * Bind this expression. This means binding the sub-expressions,
67      * as well as figuring out what the return type is for this expression.
68      *
69      * @param fromList The FROM list for the query this
70      * expression is in, for binding columns.
71      * @param subqueryList The subquery list being built as we find SubqueryNodes
72      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
73      *
74      * @return The new top of the expression tree.
75      *
76      * @exception StandardException Thrown on error
77      */

78
79     public ValueNode bindExpression(
80         FromList fromList, SubqueryList subqueryList,
81         Vector JavaDoc aggregateVector)
82             throws StandardException
83     {
84         leftOperand = leftOperand.bindExpression(fromList, subqueryList,
85             aggregateVector);
86         rightOperand = rightOperand.bindExpression(fromList, subqueryList,
87             aggregateVector);
88
89         //Set the type if there is a parameter involved here
90
if (leftOperand.requiresTypeFromContext())
91             leftOperand.setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor( Types.DATE));
92         //Set the type if there is a parameter involved here
93
if (rightOperand.requiresTypeFromContext())
94             rightOperand.setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor( Types.TIME));
95
96         TypeId leftTypeId = leftOperand.getTypeId();
97         TypeId rightTypeId = rightOperand.getTypeId();
98         if( !(leftOperand.requiresTypeFromContext() || leftTypeId.isStringTypeId() || leftTypeId.getJDBCTypeId() == Types.DATE))
99             throw StandardException.newException(SQLState.LANG_BINARY_OPERATOR_NOT_SUPPORTED,
100                                                  operator, leftTypeId.getSQLTypeName(), rightTypeId.getSQLTypeName());
101         if( !(rightOperand.requiresTypeFromContext() || rightTypeId.isStringTypeId() || rightTypeId.getJDBCTypeId() == Types.TIME))
102             throw StandardException.newException(SQLState.LANG_BINARY_OPERATOR_NOT_SUPPORTED,
103                                                  operator, leftTypeId.getSQLTypeName(), rightTypeId.getSQLTypeName());
104         setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor( Types.TIMESTAMP));
105         return genSQLJavaSQLTree();
106     } // end of bindExpression
107

108     /**
109      * Do code generation for this binary operator.
110      *
111      * @param acb The ExpressionClassBuilder for the class we're generating
112      * @param mb The method the code to place the code
113      *
114      *
115      * @exception StandardException Thrown on error
116      */

117
118     public void generateExpression(ExpressionClassBuilder acb,
119                                             MethodBuilder mb)
120         throws StandardException
121     {
122         acb.pushDataValueFactory(mb);
123         leftOperand.generateExpression(acb, mb);
124         mb.cast( ClassName.DataValueDescriptor);
125         rightOperand.generateExpression(acb, mb);
126         mb.cast( ClassName.DataValueDescriptor);
127         mb.callMethod( VMOpcode.INVOKEINTERFACE, null, methodName, ClassName.DateTimeDataValue, 2);
128     } // end of generateExpression
129
}
130
Popular Tags