--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.compile.CurrentDatetimeOperatorNode\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.sql.compile;\r
+\r
+import org.apache.derby.iapi.sql.dictionary.DataDictionary;\r
+\r
+import org.apache.derby.iapi.sql.compile.CompilerContext;\r
+\r
+import org.apache.derby.iapi.types.TypeId;\r
+import org.apache.derby.iapi.types.DataTypeDescriptor;\r
+\r
+import org.apache.derby.iapi.services.compiler.MethodBuilder;\r
+import org.apache.derby.iapi.services.compiler.LocalField;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import org.apache.derby.iapi.store.access.Qualifier;\r
+\r
+import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;\r
+\r
+import java.lang.reflect.Modifier;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import java.sql.Types;\r
+\r
+import java.util.Vector;\r
+\r
+/**\r
+ * The CurrentDatetimeOperator operator is for the builtin CURRENT_DATE,\r
+ * CURRENT_TIME, and CURRENT_TIMESTAMP operations.\r
+ *\r
+ */\r
+public class CurrentDatetimeOperatorNode extends ValueNode {\r
+\r
+ public static final int CURRENT_DATE = 0;\r
+ public static final int CURRENT_TIME = 1;\r
+ public static final int CURRENT_TIMESTAMP = 2;\r
+\r
+ static private final int jdbcTypeId[] = { \r
+ Types.DATE, \r
+ Types.TIME,\r
+ Types.TIMESTAMP\r
+ };\r
+ static private final String methodName[] = { // used in toString only\r
+ "CURRENT DATE",\r
+ "CURRENT TIME",\r
+ "CURRENT TIMSTAMP"\r
+ };\r
+\r
+ private int whichType;\r
+\r
+ public void init(Object whichType) {\r
+ this.whichType = ((Integer) whichType).intValue();\r
+\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.ASSERT(this.whichType >= 0 && this.whichType <= 2);\r
+ }\r
+\r
+ //\r
+ // QueryTreeNode interface\r
+ //\r
+\r
+ /**\r
+ * Binding this expression means setting the result DataTypeServices.\r
+ * In this case, the result type is based on the operation requested.\r
+ *\r
+ * @param fromList The FROM list for the statement. This parameter\r
+ * is not used in this case.\r
+ * @param subqueryList The subquery list being built as we find \r
+ * SubqueryNodes. Not used in this case.\r
+ * @param aggregateVector The aggregate vector being built as we find \r
+ * AggregateNodes. Not used in this case.\r
+ *\r
+ * @return The new top of the expression tree.\r
+ *\r
+ * @exception StandardException Thrown on error\r
+ */\r
+ public ValueNode bindExpression(FromList fromList, SubqueryList subqueryList,\r
+ Vector aggregateVector)\r
+ throws StandardException\r
+ {\r
+ checkReliability( methodName[whichType], CompilerContext.DATETIME_ILLEGAL );\r
+\r
+ setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor(\r
+ jdbcTypeId[whichType],\r
+ false /* Not nullable */\r
+ )\r
+ );\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Return the variant type for the underlying expression.\r
+ * The variant type can be:\r
+ * VARIANT - variant within a scan\r
+ * (method calls and non-static field access)\r
+ * SCAN_INVARIANT - invariant within a scan\r
+ * (column references from outer tables)\r
+ * QUERY_INVARIANT - invariant within the life of a query\r
+ * (constant expressions)\r
+ *\r
+ * @return The variant type for the underlying expression.\r
+ */\r
+ protected int getOrderableVariantType()\r
+ {\r
+ // CurrentDate, Time, Timestamp are invariant for the life of the query\r
+ return Qualifier.QUERY_INVARIANT;\r
+ }\r
+\r
+ /**\r
+ * CurrentDatetimeOperatorNode is used in expressions.\r
+ * The expression generated for it invokes a static method\r
+ * on a special Derby type to get the system time and\r
+ * wrap it in the right java.sql type, and then wrap it\r
+ * into the right shape for an arbitrary value, i.e. a column\r
+ * holder. This is very similar to what constants do.\r
+ *\r
+ * @param acb The ExpressionClassBuilder for the class being built\r
+ * @param mb The method the code to place the code\r
+ *\r
+ * @exception StandardException Thrown on error\r
+ */\r
+ public void generateExpression(ExpressionClassBuilder acb,\r
+ MethodBuilder mb)\r
+ throws StandardException\r
+ {\r
+ /*\r
+ ** First, we generate the current expression to be stuffed into\r
+ ** the right shape of holder.\r
+ */\r
+ switch (whichType) {\r
+ case CURRENT_DATE: \r
+ acb.getCurrentDateExpression(mb);\r
+ break;\r
+ case CURRENT_TIME: \r
+ acb.getCurrentTimeExpression(mb);\r
+ break;\r
+ case CURRENT_TIMESTAMP: \r
+ acb.getCurrentTimestampExpression(mb);\r
+ break;\r
+ }\r
+\r
+ acb.generateDataValue(mb, getTypeCompiler(), \r
+ getTypeServices().getCollationType(), (LocalField)null);\r
+ }\r
+\r
+ /*\r
+ print the non-node subfields\r
+ */\r
+ public String toString() {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ return super.toString()+"method = "+methodName[whichType]+"\n";\r
+ }\r
+ else\r
+ {\r
+ return "";\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * {@inheritDoc}\r
+ */\r
+ protected boolean isEquivalent(ValueNode o)\r
+ {\r
+ if (isSameNodeType(o)) \r
+ {\r
+ CurrentDatetimeOperatorNode other = (CurrentDatetimeOperatorNode)o;\r
+ return other.whichType == whichType;\r
+ }\r
+ return false;\r
+ }\r
+}\r