| 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.mode; |
| 7 |
|
| 8 |
import org.h2.engine.SessionLocal; |
| 9 |
import org.h2.expression.ExpressionVisitor; |
| 10 |
import org.h2.expression.Operation0; |
| 11 |
import org.h2.expression.function.NamedExpression; |
| 12 |
import org.h2.util.DateTimeUtils; |
| 13 |
import org.h2.util.TimeZoneProvider; |
| 14 |
import org.h2.value.TypeInfo; |
| 15 |
import org.h2.value.Value; |
| 16 |
import org.h2.value.ValueTimestamp; |
| 17 |
import org.h2.value.ValueTimestampTimeZone; |
| 18 |
|
| 19 |
/** |
| 20 |
* Current datetime value function. |
| 21 |
*/ |
| 22 |
final class CompatibilityDateTimeValueFunction extends Operation0 implements NamedExpression { |
| 23 |
|
| 24 |
/** |
| 25 |
* The function "SYSDATE" |
| 26 |
*/ |
| 27 |
static final int SYSDATE = 0; |
| 28 |
|
| 29 |
/** |
| 30 |
* The function "SYSTIMESTAMP" |
| 31 |
*/ |
| 32 |
static final int SYSTIMESTAMP = 1; |
| 33 |
|
| 34 |
private static final String[] NAMES = { "SYSDATE", "SYSTIMESTAMP" }; |
| 35 |
|
| 36 |
private final int function, scale; |
| 37 |
|
| 38 |
private final TypeInfo type; |
| 39 |
|
| 40 |
CompatibilityDateTimeValueFunction(int function, int scale) { |
| 41 |
this.function = function; |
| 42 |
this.scale = scale; |
| 43 |
if (function == SYSDATE) { |
| 44 |
type = TypeInfo.getTypeInfo(Value.TIMESTAMP, 0L, 0, null); |
| 45 |
} else { |
| 46 |
type = TypeInfo.getTypeInfo(Value.TIMESTAMP_TZ, 0L, scale, null); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public Value getValue(SessionLocal session) { |
| 52 |
ValueTimestampTimeZone v = session.currentTimestamp(); |
| 53 |
long dateValue = v.getDateValue(); |
| 54 |
long timeNanos = v.getTimeNanos(); |
| 55 |
int offsetSeconds = v.getTimeZoneOffsetSeconds(); |
| 56 |
int newOffset = TimeZoneProvider.getDefault() |
| 57 |
.getTimeZoneOffsetUTC(DateTimeUtils.getEpochSeconds(dateValue, timeNanos, offsetSeconds)); |
| 58 |
if (offsetSeconds != newOffset) { |
| 59 |
v = DateTimeUtils.timestampTimeZoneAtOffset(dateValue, timeNanos, offsetSeconds, newOffset); |
| 60 |
} |
| 61 |
if (function == SYSDATE) { |
| 62 |
return ValueTimestamp.fromDateValueAndNanos(v.getDateValue(), |
| 63 |
v.getTimeNanos() / DateTimeUtils.NANOS_PER_SECOND * DateTimeUtils.NANOS_PER_SECOND); |
| 64 |
} |
| 65 |
return v.castTo(type, session); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 70 |
builder.append(getName()); |
| 71 |
if (scale >= 0) { |
| 72 |
builder.append('(').append(scale).append(')'); |
| 73 |
} |
| 74 |
return builder; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 79 |
switch (visitor.getType()) { |
| 80 |
case ExpressionVisitor.DETERMINISTIC: |
| 81 |
return false; |
| 82 |
} |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public TypeInfo getType() { |
| 88 |
return type; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public int getCost() { |
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public String getName() { |
| 98 |
return NAMES[function]; |
| 99 |
} |
| 100 |
|
| 101 |
} |