forked from pgjdbc/pgjdbc
/
PGInterval.java
518 lines (461 loc) · 14 KB
/
PGInterval.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* Copyright (c) 2004, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.util;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;
/**
* This implements a class that handles the PostgreSQL interval type.
*/
public class PGInterval extends PGobject implements Serializable, Cloneable {
private int years;
private byte months;
private byte days;
private byte hours;
private byte minutes;
private int wholeSeconds;
private int microSeconds;
/**
* required by the driver.
*/
public PGInterval() {
setType("interval");
}
/**
* Initialize a interval with a given interval string representation.
*
* @param value String representated interval (e.g. '3 years 2 mons')
* @throws SQLException Is thrown if the string representation has an unknown format
* @see #setValue(String)
*/
public PGInterval(String value) throws SQLException {
this();
setValue(value);
}
private int lookAhead(String value, int position, String find) {
char [] tokens = find.toCharArray();
int found = -1;
for ( int i = 0; i < tokens.length; i++ ) {
found = value.indexOf(tokens[i], position);
if ( found > 0 ) {
return found;
}
}
return found;
}
private void parseISO8601Format(String value) {
int number = 0;
String dateValue;
String timeValue = null;
int hasTime = value.indexOf('T');
if ( hasTime > 0 ) {
/* skip over the P */
dateValue = value.substring(1,hasTime);
timeValue = value.substring(hasTime + 1);
} else {
/* skip over the P */
dateValue = value.substring(1);
}
for ( int i = 0; i < dateValue.length(); i++ ) {
int lookAhead = lookAhead(dateValue, i, "YMD");
if (lookAhead > 0) {
number = Integer.parseInt(dateValue.substring(i, lookAhead));
if (dateValue.charAt(lookAhead) == 'Y') {
setYears(number);
} else if (dateValue.charAt(lookAhead) == 'M') {
setMonths(number);
} else if (dateValue.charAt(lookAhead) == 'D') {
setDays(number);
}
i = lookAhead;
}
}
if ( timeValue != null ) {
for (int i = 0; i < timeValue.length(); i++) {
int lookAhead = lookAhead(timeValue, i, "HMS");
if (lookAhead > 0) {
number = Integer.parseInt(timeValue.substring(i, lookAhead));
if (timeValue.charAt(lookAhead) == 'H') {
setHours(number);
} else if (timeValue.charAt(lookAhead) == 'M') {
setMinutes(number);
} else if (timeValue.charAt(lookAhead) == 'S') {
setSeconds(number);
}
i = lookAhead;
}
}
}
}
/**
* Initializes all values of this interval to the specified values.
*
* @param years years
* @param months months
* @param days days
* @param hours hours
* @param minutes minutes
* @param seconds seconds
* @see #setValue(int, int, int, int, int, double)
*/
public PGInterval(int years, int months, int days, int hours, int minutes, double seconds) {
this();
setValue(years, months, days, hours, minutes, seconds);
}
/**
* Sets a interval string represented value to this instance. This method only recognize the
* format, that Postgres returns - not all input formats are supported (e.g. '1 yr 2 m 3 s').
*
* @param value String representated interval (e.g. '3 years 2 mons')
* @throws SQLException Is thrown if the string representation has an unknown format
*/
public void setValue(String value) throws SQLException {
final boolean PostgresFormat = !value.startsWith("@");
if (value.startsWith("P")) {
parseISO8601Format(value);
return;
}
// Just a simple '0'
if (!PostgresFormat && value.length() == 3 && value.charAt(2) == '0') {
setValue(0, 0, 0, 0, 0, 0.0);
return;
}
int years = 0;
int months = 0;
int days = 0;
int hours = 0;
int minutes = 0;
double seconds = 0;
try {
String valueToken = null;
value = value.replace('+', ' ').replace('@', ' ');
final StringTokenizer st = new StringTokenizer(value);
for (int i = 1; st.hasMoreTokens(); i++) {
String token = st.nextToken();
if ((i & 1) == 1) {
int endHours = token.indexOf(':');
if (endHours == -1) {
valueToken = token;
continue;
}
// This handles hours, minutes, seconds and microseconds for
// ISO intervals
int offset = (token.charAt(0) == '-') ? 1 : 0;
hours = nullSafeIntGet(token.substring(offset + 0, endHours));
minutes = nullSafeIntGet(token.substring(endHours + 1, endHours + 3));
// Pre 7.4 servers do not put second information into the results
// unless it is non-zero.
int endMinutes = token.indexOf(':', endHours + 1);
if (endMinutes != -1) {
seconds = nullSafeDoubleGet(token.substring(endMinutes + 1));
}
if (offset == 1) {
hours = -hours;
minutes = -minutes;
seconds = -seconds;
}
valueToken = null;
} else {
// This handles years, months, days for both, ISO and
// Non-ISO intervals. Hours, minutes, seconds and microseconds
// are handled for Non-ISO intervals here.
if (token.startsWith("year")) {
years = nullSafeIntGet(valueToken);
} else if (token.startsWith("mon")) {
months = nullSafeIntGet(valueToken);
} else if (token.startsWith("day")) {
days = nullSafeIntGet(valueToken);
} else if (token.startsWith("hour")) {
hours = nullSafeIntGet(valueToken);
} else if (token.startsWith("min")) {
minutes = nullSafeIntGet(valueToken);
} else if (token.startsWith("sec")) {
seconds = nullSafeDoubleGet(valueToken);
}
}
}
} catch (NumberFormatException e) {
throw new PSQLException(GT.tr("Conversion of interval failed"),
PSQLState.NUMERIC_CONSTANT_OUT_OF_RANGE, e);
}
if (!PostgresFormat && value.endsWith("ago")) {
// Inverse the leading sign
setValue(-years, -months, -days, -hours, -minutes, -seconds);
} else {
setValue(years, months, days, hours, minutes, seconds);
}
}
/**
* Set all values of this interval to the specified values.
*
* @param years years
* @param months months
* @param days days
* @param hours hours
* @param minutes minutes
* @param seconds seconds
*/
public void setValue(int years, int months, int days, int hours, int minutes, double seconds) {
setYears(years);
setMonths(months);
setDays(days);
setHours(hours);
setMinutes(minutes);
setSeconds(seconds);
}
/**
* Returns the stored interval information as a string.
*
* @return String represented interval
*/
public String getValue() {
return years + " years "
+ months + " mons "
+ days + " days "
+ hours + " hours "
+ minutes + " mins "
+ wholeSeconds + '.' + microSeconds + " secs";
}
/**
* Returns the years represented by this interval.
*
* @return years represented by this interval
*/
public int getYears() {
return years;
}
/**
* Set the years of this interval to the specified value.
*
* @param years years to set
*/
public void setYears(int years) {
this.years = years;
}
/**
* Returns the months represented by this interval.
*
* @return months represented by this interval
*/
public int getMonths() {
return months;
}
/**
* Set the months of this interval to the specified value.
*
* @param months months to set
*/
public void setMonths(int months) {
this.months = (byte) months;
}
/**
* Returns the days represented by this interval.
*
* @return days represented by this interval
*/
public int getDays() {
return days;
}
/**
* Set the days of this interval to the specified value.
*
* @param days days to set
*/
public void setDays(int days) {
this.days = (byte)days;
}
/**
* Returns the hours represented by this interval.
*
* @return hours represented by this interval
*/
public int getHours() {
return hours;
}
/**
* Set the hours of this interval to the specified value.
*
* @param hours hours to set
*/
public void setHours(int hours) {
this.hours = (byte)hours;
}
/**
* Returns the minutes represented by this interval.
*
* @return minutes represented by this interval
*/
public int getMinutes() {
return minutes;
}
/**
* Set the minutes of this interval to the specified value.
*
* @param minutes minutes to set
*/
public void setMinutes(int minutes) {
this.minutes = (byte)minutes;
}
/**
* Returns the seconds represented by this interval.
*
* @return seconds represented by this interval
*/
public double getSeconds() {
if ( microSeconds < 0) {
if ( wholeSeconds == 0 ) {
return Double.parseDouble("-0." + -microSeconds);
} else {
return Double.parseDouble("" + wholeSeconds + '.' + -microSeconds);
}
}
return Double.parseDouble("" + wholeSeconds + '.' + microSeconds );
}
public int getWholeSeconds() {
return wholeSeconds;
}
public int getMicroSeconds() {
return microSeconds;
}
/**
* Set the seconds of this interval to the specified value.
*
* @param seconds seconds to set
*/
public void setSeconds(double seconds) {
String str = Double.toString(seconds);
int decimal = str.indexOf('.');
if (decimal > 0) {
/* how many 10's do we need to multiply by to get microseconds */
String micSeconds = str.substring(decimal + 1);
int power = 6 - micSeconds.length();
microSeconds = Integer.parseInt(micSeconds) * (int)Math.pow(10,power);
wholeSeconds = Integer.parseInt(str.substring(0,decimal));
} else {
microSeconds = 0;
wholeSeconds = Integer.parseInt(str);
}
if ( seconds < 0 ) {
microSeconds = -microSeconds;
}
}
/**
* Rolls this interval on a given calendar.
*
* @param cal Calendar instance to add to
*/
public void add(Calendar cal) {
final int milliseconds = (microSeconds + ((microSeconds < 0) ? -500 : 500)) / 1000 + wholeSeconds * 1000;
cal.add(Calendar.MILLISECOND, milliseconds);
cal.add(Calendar.MINUTE, getMinutes());
cal.add(Calendar.HOUR, getHours());
cal.add(Calendar.DAY_OF_MONTH, getDays());
cal.add(Calendar.MONTH, getMonths());
cal.add(Calendar.YEAR, getYears());
}
/**
* Rolls this interval on a given date.
*
* @param date Date instance to add to
*/
public void add(Date date) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
add(cal);
date.setTime(cal.getTime().getTime());
}
/**
* Add this interval's value to the passed interval. This is backwards to what I would expect, but
* this makes it match the other existing add methods.
*
* @param interval intval to add
*/
public void add(PGInterval interval) {
interval.setYears(interval.getYears() + getYears());
interval.setMonths(interval.getMonths() + getMonths());
interval.setDays(interval.getDays() + getDays());
interval.setHours(interval.getHours() + getHours());
interval.setMinutes(interval.getMinutes() + getMinutes());
interval.setSeconds(interval.getSeconds() + getSeconds());
}
/**
* Scale this interval by an integer factor. The server can scale by arbitrary factors, but that
* would require adjusting the call signatures for all the existing methods like getDays() or
* providing our own justification of fractional intervals. Neither of these seem like a good idea
* without a strong use case.
*
* @param factor scale factor
*/
public void scale(int factor) {
setYears(factor * getYears());
setMonths(factor * getMonths());
setDays(factor * getDays());
setHours(factor * getHours());
setMinutes(factor * getMinutes());
setSeconds(factor * getSeconds());
}
/**
* Returns integer value of value or 0 if value is null.
*
* @param value integer as string value
* @return integer parsed from string value
* @throws NumberFormatException if the string contains invalid chars
*/
private static int nullSafeIntGet(String value) throws NumberFormatException {
return (value == null) ? 0 : Integer.parseInt(value);
}
/**
* Returns double value of value or 0 if value is null.
*
* @param value double as string value
* @return double parsed from string value
* @throws NumberFormatException if the string contains invalid chars
*/
private static double nullSafeDoubleGet(String value) throws NumberFormatException {
return (value == null) ? 0 : Double.parseDouble(value);
}
/**
* Returns whether an object is equal to this one or not.
*
* @param obj Object to compare with
* @return true if the two intervals are identical
*/
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (!(obj instanceof PGInterval)) {
return false;
}
final PGInterval pgi = (PGInterval) obj;
return pgi.years == years
&& pgi.months == months
&& pgi.days == days
&& pgi.hours == hours
&& pgi.minutes == minutes
&& pgi.wholeSeconds == wholeSeconds
&& pgi.microSeconds == microSeconds;
}
/**
* Returns a hashCode for this object.
*
* @return hashCode
*/
@Override
public int hashCode() {
return (((((((8 * 31 + microSeconds) * 31 + wholeSeconds) * 31 + minutes) * 31 + hours) * 31
+ days) * 31 + months) * 31 + years) * 31;
}
@Override
public Object clone() throws CloneNotSupportedException {
// squid:S2157 "Cloneables" should implement "clone
return super.clone();
}
}