KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > types > TypeInterval


1 package com.quadcap.sql.types;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.util.Calendar JavaDoc;
47
48 import java.sql.ResultSet JavaDoc;
49 import java.sql.SQLException JavaDoc;
50 import java.sql.Types JavaDoc;
51
52 import com.quadcap.util.Debug;
53 import com.quadcap.util.Util;
54
55 /**
56  * SQL <b>INTERVAL</b> types.
57  *
58  * @author Stan Bailes
59  */

60 public class TypeInterval implements Type, Externalizable JavaDoc {
61     public static final TypeInterval typeInterval = new TypeInterval();
62
63     public static final int YEAR = 0;
64     public static final int MONTH = 1;
65     public static final int DAY = 2;
66     public static final int HOUR = 3;
67     public static final int MINUTE = 4;
68     public static final int SECOND = 5;
69     public static final int NANO = 6;
70
71     static final String JavaDoc[] fieldNames = {
72     "YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND", "NANO"
73     };
74    
75     static final long[] fieldNanos = {
76     365*24*3600*1000000000L, // yr
77
30*24*3600*1000000000L, // mon
78
24*3600*1000000000L, // day
79
3600*1000000000L, // hour
80
60*1000000000L, // min
81
1000000000L, // sec
82
1L // nano
83
};
84
85     static final long[] fieldMon = {
86     12, 1, 0, 0, 0, 0, 0
87     };
88
89     static final long[] pow10 = {
90         1L, 10L, 100L,
91         1000L, 10000L, 100000L,
92         1000000L, 10000000L, 100000000L,
93         1000000000L
94     };
95     
96     int start = -1;
97     int end = -1;
98     int startPrecision = 2;
99     int secPrecision = 6;
100
101     public static int convertCalendarField(int field) throws ValueException {
102         switch (field) {
103         case Calendar.YEAR:
104             return YEAR;
105         case Calendar.MONTH:
106             return MONTH;
107         case Calendar.DAY_OF_MONTH:
108             return DAY;
109         case Calendar.HOUR_OF_DAY:
110             return HOUR;
111         case Calendar.MINUTE:
112             return MINUTE;
113         case Calendar.SECOND:
114             return SECOND;
115         }
116         throw new ValueException("Bad time component: " + field);
117     }
118     
119     public TypeInterval() {
120     }
121
122     public TypeInterval(int start, int startPrecision, int end,
123             int secPrecision)
124     throws antlr.RecognitionException
125     {
126     if (start == MONTH) {
127         throw new antlr.RecognitionException(
128                 "first interval field can't be MONTH");
129     }
130     this.start = start;
131     this.startPrecision = startPrecision;
132     this.end = end;
133     this.secPrecision = secPrecision;
134     }
135     
136     public TypeInterval(int start, int startPrecision,
137             int secPrecision) {
138     this.start = start;
139     this.end = start;
140     this.startPrecision = startPrecision;
141     this.secPrecision = secPrecision;
142     }
143
144     int getStart() { return start; }
145     int getEnd() { return end; }
146
147     int getSecPrecision() { return secPrecision; }
148
149     public String JavaDoc fieldName(int type) { return fieldNames[type]; }
150
151     static final long getNanos(int type) { return fieldNanos[type]; }
152     static final long getMon(int type) { return fieldMon[type]; }
153
154     /**
155      * Return the number by which the interval value must be multiplied
156      * to convert it to a value in nanoseconds
157      */

158     final long getMult() {
159         if (end == NANO) {
160             return 1000000000L / pow10[secPrecision];
161         } else {
162             return getNanos(end);
163         }
164     }
165                 
166     /**
167      * Return the number by which elements of field 'type' must be multiplied
168      * in order to convert them to the unit type used in this interval.
169      *
170      * units(type) * getMult() => nanoseconds per unit
171      */

172     long units(int type, boolean ym) {
173         if (ym) {
174             return getMon(type);
175         } else if (end == NANO && type == NANO) {
176             return 1;
177         } else {
178             return getNanos(type) / getMult();
179         }
180     }
181     
182     public String JavaDoc getTypeName() {
183     return toString();
184     }
185
186     public int getJDBCType() { return Types.OTHER; }
187
188     public String JavaDoc getJDBCClassName() { return "java.lang.String"; }
189
190     public int getPrecision() { return 32; }
191
192     public int getScale() { return 0; }
193
194     public int getMaxPrecision() { return 32; }
195
196     public int getMinScale() { return -1; }
197
198     public int getMaxScale() { return -1; }
199
200     public boolean isCharType() { return false; }
201
202     public boolean isCaseSensitive() { return false; }
203
204     public boolean isCurrency() { return false; }
205
206     public boolean isSigned() { return true; }
207                       
208     public String JavaDoc toString() {
209     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("INTERVAL");
210     if (start >= 0) {
211         sb.append(' ');
212         sb.append(fieldNames[start]);
213         if (start == SECOND) {
214                 if (startPrecision != 2 || secPrecision != 6) {
215                     sb.append('(');
216                     sb.append(Integer.toString(startPrecision));
217                     sb.append(',');
218                     sb.append(Integer.toString(secPrecision));
219                     sb.append(')');
220                 }
221             } else if (startPrecision != 2) {
222                 sb.append('(');
223                 sb.append(Integer.toString(startPrecision));
224                 sb.append(')');
225         }
226         int fld = start;
227         if (end >= 0 && end != NANO && end != start) {
228         sb.append(" TO ");
229         sb.append(fieldNames[end]);
230         fld = end;
231         }
232     }
233 // sb.append("[");
234
// sb.append(start);
235
// sb.append("-");
236
// sb.append(end);
237
// sb.append("]");
238
return sb.toString();
239     }
240
241     public int getDisplayWidth() { return 10; }
242
243     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc {
244     start = in.readInt();
245     end = in.readInt();
246     startPrecision = in.readInt();
247     secPrecision = in.readInt();
248     }
249     
250     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
251     out.writeInt(start);
252     out.writeInt(end);
253     out.writeInt(startPrecision);
254     out.writeInt(secPrecision);
255     }
256
257     public Value convert(Value v) throws ValueException {
258     return v.convert(this);
259     }
260
261     public String JavaDoc getCreateParams() {
262         return null;
263     }
264 }
265
Popular Tags