--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.execute.CurrentDatetime\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.execute;\r
+\r
+/* can't import due to name overlap:\r
+import java.util.Date;\r
+*/\r
+import java.sql.Date;\r
+import java.sql.Time;\r
+import java.sql.Timestamp;\r
+\r
+/**\r
+ CurrentDatetime provides execution support for ensuring\r
+ that the current datetime is evaluated only once for a\r
+ statement. The same value is returned for every\r
+ CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP in the\r
+ statement.\r
+ <p>\r
+ This is expected to be used by an activation and its\r
+ result set, and so 'forget' must be called whenever you\r
+ want to reuse the CurrentDatetime object for additional\r
+ executions of the statement.\r
+\r
+ */\r
+public class CurrentDatetime {\r
+\r
+ /**\r
+ Holds the current datetime on the first evaluation of a current function\r
+ in a statement, which contains all available fields.\r
+ */\r
+ private java.util.Date currentDatetime;\r
+ /**\r
+ Holds the SQL DATE version of the current datetime.\r
+ */\r
+ private Date currentDate;\r
+ /**\r
+ Holds the SQL TIME version of the current datetime.\r
+ */\r
+ private Time currentTime;\r
+ /**\r
+ Holds the SQL TIMESTAMP version of the current datetime.\r
+ */\r
+ private Timestamp currentTimestamp;\r
+\r
+ /**\r
+ The constructor is public; note we wait until evaluation to\r
+ put any values into the fields.\r
+ */\r
+ public CurrentDatetime() {\r
+ }\r
+\r
+ // class implementation\r
+ final private void setCurrentDatetime() {\r
+ if (currentDatetime == null)\r
+ currentDatetime = new java.util.Date();\r
+ }\r
+\r
+ // class interface\r
+\r
+ public Date getCurrentDate() {\r
+ if (currentDate == null) {\r
+ setCurrentDatetime();\r
+ currentDate = new Date(currentDatetime.getTime());\r
+ }\r
+ return currentDate;\r
+ }\r
+\r
+ public Time getCurrentTime() {\r
+ if (currentTime == null) {\r
+ setCurrentDatetime();\r
+ currentTime = new Time(currentDatetime.getTime());\r
+ }\r
+ return currentTime;\r
+ }\r
+\r
+ public Timestamp getCurrentTimestamp() {\r
+ if (currentTimestamp == null) {\r
+ setCurrentDatetime();\r
+ currentTimestamp = new Timestamp(currentDatetime.getTime());\r
+ }\r
+ return currentTimestamp;\r
+ }\r
+\r
+ /**\r
+ This is called prior to each execution of the statement, to\r
+ ensure that it starts over with a new current datetime value.\r
+ */\r
+ public void forget() {\r
+ currentDatetime = null;\r
+ currentDate = null;\r
+ currentTime = null;\r
+ currentTimestamp = null;\r
+ }\r
+\r
+}\r