001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.CurrentDatetime
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.execute;
023:
024: /* can't import due to name overlap:
025: import java.util.Date;
026: */
027: import java.sql.Date;
028: import java.sql.Time;
029: import java.sql.Timestamp;
030:
031: /**
032: CurrentDatetime provides execution support for ensuring
033: that the current datetime is evaluated only once for a
034: statement. The same value is returned for every
035: CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP in the
036: statement.
037: <p>
038: This is expected to be used by an activation and its
039: result set, and so 'forget' must be called whenever you
040: want to reuse the CurrentDatetime object for additional
041: executions of the statement.
042:
043: @author ames
044: */
045: public class CurrentDatetime {
046:
047: /**
048: Holds the current datetime on the first evaluation of a current function
049: in a statement, which contains all available fields.
050: */
051: private java.util.Date currentDatetime;
052: /**
053: Holds the SQL DATE version of the current datetime.
054: */
055: private Date currentDate;
056: /**
057: Holds the SQL TIME version of the current datetime.
058: */
059: private Time currentTime;
060: /**
061: Holds the SQL TIMESTAMP version of the current datetime.
062: */
063: private Timestamp currentTimestamp;
064:
065: /**
066: The constructor is public; note we wait until evaluation to
067: put any values into the fields.
068: */
069: public CurrentDatetime() {
070: }
071:
072: // class implementation
073: final private void setCurrentDatetime() {
074: if (currentDatetime == null)
075: currentDatetime = new java.util.Date();
076: }
077:
078: // class interface
079:
080: public Date getCurrentDate() {
081: if (currentDate == null) {
082: setCurrentDatetime();
083: currentDate = new Date(currentDatetime.getTime());
084: }
085: return currentDate;
086: }
087:
088: public Time getCurrentTime() {
089: if (currentTime == null) {
090: setCurrentDatetime();
091: currentTime = new Time(currentDatetime.getTime());
092: }
093: return currentTime;
094: }
095:
096: public Timestamp getCurrentTimestamp() {
097: if (currentTimestamp == null) {
098: setCurrentDatetime();
099: currentTimestamp = new Timestamp(currentDatetime.getTime());
100: }
101: return currentTimestamp;
102: }
103:
104: /**
105: This is called prior to each execution of the statement, to
106: ensure that it starts over with a new current datetime value.
107: */
108: public void forget() {
109: currentDatetime = null;
110: currentDate = null;
111: currentTime = null;
112: currentTimestamp = null;
113: }
114:
115: }
|