| 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: Daniel Gredler |
| 5 |
*/ |
| 6 |
package org.h2.mode; |
| 7 |
|
| 8 |
import static java.lang.String.format; |
| 9 |
|
| 10 |
import java.util.List; |
| 11 |
|
| 12 |
import org.h2.engine.SessionLocal; |
| 13 |
import org.h2.util.DateTimeUtils; |
| 14 |
import org.h2.util.TimeZoneProvider; |
| 15 |
import org.h2.value.ValueTimestamp; |
| 16 |
import org.h2.value.ValueTimestampTimeZone; |
| 17 |
|
| 18 |
/** |
| 19 |
* Emulates Oracle's TO_DATE function.<br> |
| 20 |
* This class holds and handles the input data form the TO_DATE-method |
| 21 |
*/ |
| 22 |
public final class ToDateParser { |
| 23 |
|
| 24 |
private final SessionLocal session; |
| 25 |
|
| 26 |
private final String unmodifiedInputStr; |
| 27 |
private final String unmodifiedFormatStr; |
| 28 |
private final ConfigParam functionName; |
| 29 |
private String inputStr; |
| 30 |
private String formatStr; |
| 31 |
|
| 32 |
private boolean doyValid = false, absoluteDayValid = false, |
| 33 |
hour12Valid = false, |
| 34 |
timeZoneHMValid = false; |
| 35 |
|
| 36 |
private boolean bc; |
| 37 |
|
| 38 |
private long absoluteDay; |
| 39 |
|
| 40 |
private int year, month, day = 1; |
| 41 |
|
| 42 |
private int dayOfYear; |
| 43 |
|
| 44 |
private int hour, minute, second, nanos; |
| 45 |
|
| 46 |
private int hour12; |
| 47 |
|
| 48 |
private boolean isAM = true; |
| 49 |
|
| 50 |
private TimeZoneProvider timeZone; |
| 51 |
|
| 52 |
private int timeZoneHour, timeZoneMinute; |
| 53 |
|
| 54 |
private int currentYear, currentMonth; |
| 55 |
|
| 56 |
/** |
| 57 |
* @param session the database session |
| 58 |
* @param functionName one of [TO_DATE, TO_TIMESTAMP] (both share the same |
| 59 |
* code) |
| 60 |
* @param input the input date with the date-time info |
| 61 |
* @param format the format of date-time info |
| 62 |
*/ |
| 63 |
private ToDateParser(SessionLocal session, ConfigParam functionName, String input, String format) { |
| 64 |
this.session = session; |
| 65 |
this.functionName = functionName; |
| 66 |
inputStr = input.trim(); |
| 67 |
// Keep a copy |
| 68 |
unmodifiedInputStr = inputStr; |
| 69 |
if (format == null || format.isEmpty()) { |
| 70 |
// default Oracle format. |
| 71 |
formatStr = functionName.getDefaultFormatStr(); |
| 72 |
} else { |
| 73 |
formatStr = format.trim(); |
| 74 |
} |
| 75 |
// Keep a copy |
| 76 |
unmodifiedFormatStr = formatStr; |
| 77 |
} |
| 78 |
|
| 79 |
private static ToDateParser getTimestampParser(SessionLocal session, ConfigParam param, String input, |
| 80 |
String format) { |
| 81 |
ToDateParser result = new ToDateParser(session, param, input, format); |
| 82 |
parse(result); |
| 83 |
return result; |
| 84 |
} |
| 85 |
|
| 86 |
private ValueTimestamp getResultingValue() { |
| 87 |
long dateValue; |
| 88 |
if (absoluteDayValid) { |
| 89 |
dateValue = DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay); |
| 90 |
} else { |
| 91 |
int year = this.year; |
| 92 |
if (year == 0) { |
| 93 |
year = getCurrentYear(); |
| 94 |
} |
| 95 |
if (bc) { |
| 96 |
year = 1 - year; |
| 97 |
} |
| 98 |
if (doyValid) { |
| 99 |
dateValue = DateTimeUtils.dateValueFromAbsoluteDay( |
| 100 |
DateTimeUtils.absoluteDayFromYear(year) + dayOfYear - 1); |
| 101 |
} else { |
| 102 |
int month = this.month; |
| 103 |
if (month == 0) { |
| 104 |
// Oracle uses current month as default |
| 105 |
month = getCurrentMonth(); |
| 106 |
} |
| 107 |
dateValue = DateTimeUtils.dateValue(year, month, day); |
| 108 |
} |
| 109 |
} |
| 110 |
int hour; |
| 111 |
if (hour12Valid) { |
| 112 |
hour = hour12 % 12; |
| 113 |
if (!isAM) { |
| 114 |
hour += 12; |
| 115 |
} |
| 116 |
} else { |
| 117 |
hour = this.hour; |
| 118 |
} |
| 119 |
long timeNanos = ((((hour * 60) + minute) * 60) + second) * 1_000_000_000L + nanos; |
| 120 |
return ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos); |
| 121 |
} |
| 122 |
|
| 123 |
private ValueTimestampTimeZone getResultingValueWithTimeZone() { |
| 124 |
ValueTimestamp ts = getResultingValue(); |
| 125 |
long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos(); |
| 126 |
int offset; |
| 127 |
if (timeZoneHMValid) { |
| 128 |
offset = (timeZoneHour * 60 + ((timeZoneHour >= 0) ? timeZoneMinute : -timeZoneMinute)) * 60; |
| 129 |
} else { |
| 130 |
offset = (timeZone != null ? timeZone : session.currentTimeZone()) |
| 131 |
.getTimeZoneOffsetLocal(dateValue, timeNanos); |
| 132 |
} |
| 133 |
return ValueTimestampTimeZone.fromDateValueAndNanos(dateValue, ts.getTimeNanos(), offset); |
| 134 |
} |
| 135 |
|
| 136 |
String getInputStr() { |
| 137 |
return inputStr; |
| 138 |
} |
| 139 |
|
| 140 |
String getFormatStr() { |
| 141 |
return formatStr; |
| 142 |
} |
| 143 |
|
| 144 |
String getFunctionName() { |
| 145 |
return functionName.name(); |
| 146 |
} |
| 147 |
|
| 148 |
private void queryCurrentYearAndMonth() { |
| 149 |
long dateValue = session.currentTimestamp().getDateValue(); |
| 150 |
currentYear = DateTimeUtils.yearFromDateValue(dateValue); |
| 151 |
currentMonth = DateTimeUtils.monthFromDateValue(dateValue); |
| 152 |
} |
| 153 |
|
| 154 |
int getCurrentYear() { |
| 155 |
if (currentYear == 0) { |
| 156 |
queryCurrentYearAndMonth(); |
| 157 |
} |
| 158 |
return currentYear; |
| 159 |
} |
| 160 |
|
| 161 |
int getCurrentMonth() { |
| 162 |
if (currentMonth == 0) { |
| 163 |
queryCurrentYearAndMonth(); |
| 164 |
} |
| 165 |
return currentMonth; |
| 166 |
} |
| 167 |
|
| 168 |
void setAbsoluteDay(int absoluteDay) { |
| 169 |
doyValid = false; |
| 170 |
absoluteDayValid = true; |
| 171 |
this.absoluteDay = absoluteDay; |
| 172 |
} |
| 173 |
|
| 174 |
void setBC(boolean bc) { |
| 175 |
doyValid = false; |
| 176 |
absoluteDayValid = false; |
| 177 |
this.bc = bc; |
| 178 |
} |
| 179 |
|
| 180 |
void setYear(int year) { |
| 181 |
doyValid = false; |
| 182 |
absoluteDayValid = false; |
| 183 |
this.year = year; |
| 184 |
} |
| 185 |
|
| 186 |
void setMonth(int month) { |
| 187 |
doyValid = false; |
| 188 |
absoluteDayValid = false; |
| 189 |
this.month = month; |
| 190 |
if (year == 0) { |
| 191 |
year = 1970; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
void setDay(int day) { |
| 196 |
doyValid = false; |
| 197 |
absoluteDayValid = false; |
| 198 |
this.day = day; |
| 199 |
if (year == 0) { |
| 200 |
year = 1970; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
void setDayOfYear(int dayOfYear) { |
| 205 |
doyValid = true; |
| 206 |
absoluteDayValid = false; |
| 207 |
this.dayOfYear = dayOfYear; |
| 208 |
} |
| 209 |
|
| 210 |
void setHour(int hour) { |
| 211 |
hour12Valid = false; |
| 212 |
this.hour = hour; |
| 213 |
} |
| 214 |
|
| 215 |
void setMinute(int minute) { |
| 216 |
this.minute = minute; |
| 217 |
} |
| 218 |
|
| 219 |
void setSecond(int second) { |
| 220 |
this.second = second; |
| 221 |
} |
| 222 |
|
| 223 |
void setNanos(int nanos) { |
| 224 |
this.nanos = nanos; |
| 225 |
} |
| 226 |
|
| 227 |
void setAmPm(boolean isAM) { |
| 228 |
hour12Valid = true; |
| 229 |
this.isAM = isAM; |
| 230 |
} |
| 231 |
|
| 232 |
void setHour12(int hour12) { |
| 233 |
hour12Valid = true; |
| 234 |
this.hour12 = hour12; |
| 235 |
} |
| 236 |
|
| 237 |
void setTimeZone(TimeZoneProvider timeZone) { |
| 238 |
timeZoneHMValid = false; |
| 239 |
this.timeZone = timeZone; |
| 240 |
} |
| 241 |
|
| 242 |
void setTimeZoneHour(int timeZoneHour) { |
| 243 |
timeZoneHMValid = true; |
| 244 |
this.timeZoneHour = timeZoneHour; |
| 245 |
} |
| 246 |
|
| 247 |
void setTimeZoneMinute(int timeZoneMinute) { |
| 248 |
timeZoneHMValid = true; |
| 249 |
this.timeZoneMinute = timeZoneMinute; |
| 250 |
} |
| 251 |
|
| 252 |
private boolean hasToParseData() { |
| 253 |
return !formatStr.isEmpty(); |
| 254 |
} |
| 255 |
|
| 256 |
private void removeFirstChar() { |
| 257 |
if (!formatStr.isEmpty()) { |
| 258 |
formatStr = formatStr.substring(1); |
| 259 |
} |
| 260 |
if (!inputStr.isEmpty()) { |
| 261 |
inputStr = inputStr.substring(1); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
private static ToDateParser parse(ToDateParser p) { |
| 266 |
while (p.hasToParseData()) { |
| 267 |
List<ToDateTokenizer.FormatTokenEnum> tokenList = |
| 268 |
ToDateTokenizer.FormatTokenEnum.getTokensInQuestion(p.getFormatStr()); |
| 269 |
if (tokenList == null) { |
| 270 |
p.removeFirstChar(); |
| 271 |
continue; |
| 272 |
} |
| 273 |
boolean foundAnToken = false; |
| 274 |
for (ToDateTokenizer.FormatTokenEnum token : tokenList) { |
| 275 |
if (token.parseFormatStrWithToken(p)) { |
| 276 |
foundAnToken = true; |
| 277 |
break; |
| 278 |
} |
| 279 |
} |
| 280 |
if (!foundAnToken) { |
| 281 |
p.removeFirstChar(); |
| 282 |
} |
| 283 |
} |
| 284 |
return p; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Remove a token from a string. |
| 289 |
* |
| 290 |
* @param inputFragmentStr the input fragment |
| 291 |
* @param formatFragment the format fragment |
| 292 |
*/ |
| 293 |
void remove(String inputFragmentStr, String formatFragment) { |
| 294 |
if (inputFragmentStr != null && inputStr.length() >= inputFragmentStr.length()) { |
| 295 |
inputStr = inputStr.substring(inputFragmentStr.length()); |
| 296 |
} |
| 297 |
if (formatFragment != null && formatStr.length() >= formatFragment.length()) { |
| 298 |
formatStr = formatStr.substring(formatFragment.length()); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public String toString() { |
| 304 |
int inputStrLen = inputStr.length(); |
| 305 |
int orgInputLen = unmodifiedInputStr.length(); |
| 306 |
int currentInputPos = orgInputLen - inputStrLen; |
| 307 |
int restInputLen = inputStrLen <= 0 ? inputStrLen : inputStrLen - 1; |
| 308 |
|
| 309 |
int orgFormatLen = unmodifiedFormatStr.length(); |
| 310 |
int currentFormatPos = orgFormatLen - formatStr.length(); |
| 311 |
|
| 312 |
return format("\n %s('%s', '%s')", functionName, unmodifiedInputStr, unmodifiedFormatStr) |
| 313 |
+ format("\n %s^%s , %s^ <-- Parsing failed at this point", |
| 314 |
format("%" + (functionName.name().length() + currentInputPos) + "s", ""), |
| 315 |
restInputLen <= 0 ? "" : format("%" + restInputLen + "s", ""), |
| 316 |
currentFormatPos <= 0 ? "" : format("%" + currentFormatPos + "s", "")); |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Parse a string as a timestamp with the given format. |
| 321 |
* |
| 322 |
* @param session the database session |
| 323 |
* @param input the input |
| 324 |
* @param format the format |
| 325 |
* @return the timestamp |
| 326 |
*/ |
| 327 |
public static ValueTimestamp toTimestamp(SessionLocal session, String input, String format) { |
| 328 |
ToDateParser parser = getTimestampParser(session, ConfigParam.TO_TIMESTAMP, input, format); |
| 329 |
return parser.getResultingValue(); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Parse a string as a timestamp with the given format. |
| 334 |
* |
| 335 |
* @param session the database session |
| 336 |
* @param input the input |
| 337 |
* @param format the format |
| 338 |
* @return the timestamp |
| 339 |
*/ |
| 340 |
public static ValueTimestampTimeZone toTimestampTz(SessionLocal session, String input, String format) { |
| 341 |
ToDateParser parser = getTimestampParser(session, ConfigParam.TO_TIMESTAMP_TZ, input, format); |
| 342 |
return parser.getResultingValueWithTimeZone(); |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Parse a string as a date with the given format. |
| 347 |
* |
| 348 |
* @param session the database session |
| 349 |
* @param input the input |
| 350 |
* @param format the format |
| 351 |
* @return the date as a timestamp |
| 352 |
*/ |
| 353 |
public static ValueTimestamp toDate(SessionLocal session, String input, String format) { |
| 354 |
ToDateParser parser = getTimestampParser(session, ConfigParam.TO_DATE, input, format); |
| 355 |
return parser.getResultingValue(); |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* The configuration of the date parser. |
| 360 |
*/ |
| 361 |
private enum ConfigParam { |
| 362 |
TO_DATE("DD MON YYYY"), |
| 363 |
TO_TIMESTAMP("DD MON YYYY HH:MI:SS"), |
| 364 |
TO_TIMESTAMP_TZ("DD MON YYYY HH:MI:SS TZR"); |
| 365 |
|
| 366 |
private final String defaultFormatStr; |
| 367 |
ConfigParam(String defaultFormatStr) { |
| 368 |
this.defaultFormatStr = defaultFormatStr; |
| 369 |
} |
| 370 |
String getDefaultFormatStr() { |
| 371 |
return defaultFormatStr; |
| 372 |
} |
| 373 |
|
| 374 |
} |
| 375 |
|
| 376 |
} |