Debian Codegraph @

src/main/org/h2/api/IntervalQualifier.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.api;
7
8 /**
9 * Interval qualifier.
10 */
11 public enum IntervalQualifier {
12
13 /**
14 * {@code YEAR}
15 */
16 YEAR,
17
18 /**
19 * {@code MONTH}
20 */
21 MONTH,
22
23 /**
24 * {@code DAY}
25 */
26 DAY,
27
28 /**
29 * {@code HOUR}
30 */
31 HOUR,
32
33 /**
34 * {@code MINUTE}
35 */
36 MINUTE,
37
38 /**
39 * {@code SECOND}
40 */
41 SECOND,
42
43 /**
44 * {@code YEAR TO MONTH}
45 */
46 YEAR_TO_MONTH,
47
48 /**
49 * {@code DAY TO HOUR}
50 */
51 DAY_TO_HOUR,
52
53 /**
54 * {@code DAY TO MINUTE}
55 */
56 DAY_TO_MINUTE,
57
58 /**
59 * {@code DAY TO SECOND}
60 */
61 DAY_TO_SECOND,
62
63 /**
64 * {@code HOUR TO MINUTE}
65 */
66 HOUR_TO_MINUTE,
67
68 /**
69 * {@code HOUR TO SECOND}
70 */
71 HOUR_TO_SECOND,
72
73 /**
74 * {@code MINUTE TO SECOND}
75 */
76 MINUTE_TO_SECOND;
77
78 private final String string;
79
80 /**
81 * Returns the interval qualifier with the specified ordinal value.
82 *
83 * @param ordinal
84 * Java ordinal value (0-based)
85 * @return interval qualifier with the specified ordinal value
86 */
87 public static IntervalQualifier valueOf(int ordinal) {
88 switch (ordinal) {
89 case 0:
90 return YEAR;
91 case 1:
92 return MONTH;
93 case 2:
94 return DAY;
95 case 3:
96 return HOUR;
97 case 4:
98 return MINUTE;
99 case 5:
100 return SECOND;
101 case 6:
102 return YEAR_TO_MONTH;
103 case 7:
104 return DAY_TO_HOUR;
105 case 8:
106 return DAY_TO_MINUTE;
107 case 9:
108 return DAY_TO_SECOND;
109 case 10:
110 return HOUR_TO_MINUTE;
111 case 11:
112 return HOUR_TO_SECOND;
113 case 12:
114 return MINUTE_TO_SECOND;
115 default:
116 throw new IllegalArgumentException();
117 }
118 }
119
120 private IntervalQualifier() {
121 string = name().replace('_', ' ').intern();
122 }
123
124 /**
125 * Returns whether interval with this qualifier is a year-month interval.
126 *
127 * @return whether interval with this qualifier is a year-month interval
128 */
129 public boolean isYearMonth() {
130 return this == YEAR || this == MONTH || this == YEAR_TO_MONTH;
131 }
132
133 /**
134 * Returns whether interval with this qualifier is a day-time interval.
135 *
136 * @return whether interval with this qualifier is a day-time interval
137 */
138 public boolean isDayTime() {
139 return !isYearMonth();
140 }
141
142 /**
143 * Returns whether interval with this qualifier has years.
144 *
145 * @return whether interval with this qualifier has years
146 */
147 public boolean hasYears() {
148 return this == YEAR || this == YEAR_TO_MONTH;
149 }
150
151 /**
152 * Returns whether interval with this qualifier has months.
153 *
154 * @return whether interval with this qualifier has months
155 */
156 public boolean hasMonths() {
157 return this == MONTH || this == YEAR_TO_MONTH;
158 }
159
160 /**
161 * Returns whether interval with this qualifier has days.
162 *
163 * @return whether interval with this qualifier has days
164 */
165 public boolean hasDays() {
166 switch (this) {
167 case DAY:
168 case DAY_TO_HOUR:
169 case DAY_TO_MINUTE:
170 case DAY_TO_SECOND:
171 return true;
172 default:
173 return false;
174 }
175 }
176
177 /**
178 * Returns whether interval with this qualifier has hours.
179 *
180 * @return whether interval with this qualifier has hours
181 */
182 public boolean hasHours() {
183 switch (this) {
184 case HOUR:
185 case DAY_TO_HOUR:
186 case DAY_TO_MINUTE:
187 case DAY_TO_SECOND:
188 case HOUR_TO_MINUTE:
189 case HOUR_TO_SECOND:
190 return true;
191 default:
192 return false;
193 }
194 }
195
196 /**
197 * Returns whether interval with this qualifier has minutes.
198 *
199 * @return whether interval with this qualifier has minutes
200 */
201 public boolean hasMinutes() {
202 switch (this) {
203 case MINUTE:
204 case DAY_TO_MINUTE:
205 case DAY_TO_SECOND:
206 case HOUR_TO_MINUTE:
207 case HOUR_TO_SECOND:
208 case MINUTE_TO_SECOND:
209 return true;
210 default:
211 return false;
212 }
213 }
214
215 /**
216 * Returns whether interval with this qualifier has seconds.
217 *
218 * @return whether interval with this qualifier has seconds
219 */
220 public boolean hasSeconds() {
221 switch (this) {
222 case SECOND:
223 case DAY_TO_SECOND:
224 case HOUR_TO_SECOND:
225 case MINUTE_TO_SECOND:
226 return true;
227 default:
228 return false;
229 }
230 }
231
232 /**
233 * Returns whether interval with this qualifier has multiple fields.
234 *
235 * @return whether interval with this qualifier has multiple fields
236 */
237 public boolean hasMultipleFields() {
238 return ordinal() > 5;
239 }
240
241 @Override
242 public String toString() {
243 return string;
244 }
245
246 /**
247 * Returns full type name.
248 *
249 * @param precision precision, or {@code -1}
250 * @param scale fractional seconds precision, or {@code -1}
251 * @return full type name
252 */
253 public String getTypeName(int precision, int scale) {
254 return getTypeName(new StringBuilder(), precision, scale, false).toString();
255 }
256
257 /**
258 * Appends full type name to the specified string builder.
259 *
260 * @param builder string builder
261 * @param precision precision, or {@code -1}
262 * @param scale fractional seconds precision, or {@code -1}
263 * @param qualifierOnly if {@code true}, don't add the INTERVAL prefix
264 * @return the specified string builder
265 */
266 public StringBuilder getTypeName(StringBuilder builder, int precision, int scale, boolean qualifierOnly) {
267 if (!qualifierOnly) {
268 builder.append("INTERVAL ");
269 }
270 switch (this) {
271 case YEAR:
272 case MONTH:
273 case DAY:
274 case HOUR:
275 case MINUTE:
276 builder.append(string);
277 if (precision > 0) {
278 builder.append('(').append(precision).append(')');
279 }
280 break;
281 case SECOND:
282 builder.append(string);
283 if (precision > 0 || scale >= 0) {
284 builder.append('(').append(precision > 0 ? precision : 2);
285 if (scale >= 0) {
286 builder.append(", ").append(scale);
287 }
288 builder.append(')');
289 }
290 break;
291 case YEAR_TO_MONTH:
292 builder.append("YEAR");
293 if (precision > 0) {
294 builder.append('(').append(precision).append(')');
295 }
296 builder.append(" TO MONTH");
297 break;
298 case DAY_TO_HOUR:
299 builder.append("DAY");
300 if (precision > 0) {
301 builder.append('(').append(precision).append(')');
302 }
303 builder.append(" TO HOUR");
304 break;
305 case DAY_TO_MINUTE:
306 builder.append("DAY");
307 if (precision > 0) {
308 builder.append('(').append(precision).append(')');
309 }
310 builder.append(" TO MINUTE");
311 break;
312 case DAY_TO_SECOND:
313 builder.append("DAY");
314 if (precision > 0) {
315 builder.append('(').append(precision).append(')');
316 }
317 builder.append(" TO SECOND");
318 if (scale >= 0) {
319 builder.append('(').append(scale).append(')');
320 }
321 break;
322 case HOUR_TO_MINUTE:
323 builder.append("HOUR");
324 if (precision > 0) {
325 builder.append('(').append(precision).append(')');
326 }
327 builder.append(" TO MINUTE");
328 break;
329 case HOUR_TO_SECOND:
330 builder.append("HOUR");
331 if (precision > 0) {
332 builder.append('(').append(precision).append(')');
333 }
334 builder.append(" TO SECOND");
335 if (scale >= 0) {
336 builder.append('(').append(scale).append(')');
337 }
338 break;
339 case MINUTE_TO_SECOND:
340 builder.append("MINUTE");
341 if (precision > 0) {
342 builder.append('(').append(precision).append(')');
343 }
344 builder.append(" TO SECOND");
345 if (scale >= 0) {
346 builder.append('(').append(scale).append(')');
347 }
348 }
349 return builder;
350 }
351
352 }