KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > CurrentDatetime


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.CurrentDatetime
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.execute;
23
24 /* can't import due to name overlap:
25 import java.util.Date;
26 */

27 import java.sql.Date JavaDoc;
28 import java.sql.Time JavaDoc;
29 import java.sql.Timestamp JavaDoc;
30
31 /**
32     CurrentDatetime provides execution support for ensuring
33     that the current datetime is evaluated only once for a
34     statement. The same value is returned for every
35     CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP in the
36     statement.
37     <p>
38     This is expected to be used by an activation and its
39     result set, and so 'forget' must be called whenever you
40     want to reuse the CurrentDatetime object for additional
41     executions of the statement.
42
43     @author ames
44  */

45 public class CurrentDatetime {
46
47     /**
48         Holds the current datetime on the first evaluation of a current function
49         in a statement, which contains all available fields.
50      */

51     private java.util.Date JavaDoc currentDatetime;
52     /**
53         Holds the SQL DATE version of the current datetime.
54      */

55     private Date JavaDoc currentDate;
56     /**
57         Holds the SQL TIME version of the current datetime.
58      */

59     private Time JavaDoc currentTime;
60     /**
61         Holds the SQL TIMESTAMP version of the current datetime.
62      */

63     private Timestamp JavaDoc currentTimestamp;
64
65     /**
66         The constructor is public; note we wait until evaluation to
67         put any values into the fields.
68      */

69     public CurrentDatetime() {
70     }
71
72     // class implementation
73
final private void setCurrentDatetime() {
74         if (currentDatetime == null)
75             currentDatetime = new java.util.Date JavaDoc();
76     }
77
78     // class interface
79

80     public Date JavaDoc getCurrentDate() {
81         if (currentDate == null) {
82             setCurrentDatetime();
83             currentDate = new Date JavaDoc(currentDatetime.getTime());
84         }
85         return currentDate;
86     }
87
88     public Time JavaDoc getCurrentTime() {
89         if (currentTime == null) {
90             setCurrentDatetime();
91             currentTime = new Time JavaDoc(currentDatetime.getTime());
92         }
93         return currentTime;
94     }
95
96     public Timestamp JavaDoc getCurrentTimestamp() {
97         if (currentTimestamp == null) {
98             setCurrentDatetime();
99             currentTimestamp = new Timestamp JavaDoc(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 }
116
Popular Tags