| 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 java.util.HashMap; |
| 9 |
|
| 10 |
import org.h2.api.ErrorCode; |
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.expression.Expression; |
| 13 |
import org.h2.expression.ValueExpression; |
| 14 |
import org.h2.expression.function.DateTimeFunction; |
| 15 |
import org.h2.message.DbException; |
| 16 |
import org.h2.value.TypeInfo; |
| 17 |
import org.h2.value.Value; |
| 18 |
import org.h2.value.ValueNull; |
| 19 |
import org.h2.value.ValueUuid; |
| 20 |
|
| 21 |
/** |
| 22 |
* Functions for {@link org.h2.engine.Mode.ModeEnum#Oracle} compatibility mode. |
| 23 |
*/ |
| 24 |
public final class FunctionsOracle extends ModeFunction { |
| 25 |
|
| 26 |
private static final int ADD_MONTHS = 2001; |
| 27 |
|
| 28 |
private static final int SYS_GUID = ADD_MONTHS + 1; |
| 29 |
|
| 30 |
private static final int TO_DATE = SYS_GUID + 1; |
| 31 |
|
| 32 |
private static final int TO_TIMESTAMP = TO_DATE + 1; |
| 33 |
|
| 34 |
private static final int TO_TIMESTAMP_TZ = TO_TIMESTAMP + 1; |
| 35 |
|
| 36 |
private static final HashMap<String, FunctionInfo> FUNCTIONS = new HashMap<>(); |
| 37 |
|
| 38 |
static { |
| 39 |
FUNCTIONS.put("ADD_MONTHS", |
| 40 |
new FunctionInfo("ADD_MONTHS", ADD_MONTHS, 2, Value.TIMESTAMP, true, true)); |
| 41 |
FUNCTIONS.put("SYS_GUID", |
| 42 |
new FunctionInfo("SYS_GUID", SYS_GUID, 0, Value.VARBINARY, false, false)); |
| 43 |
FUNCTIONS.put("TO_DATE", |
| 44 |
new FunctionInfo("TO_DATE", TO_DATE, VAR_ARGS, Value.TIMESTAMP, true, true)); |
| 45 |
FUNCTIONS.put("TO_TIMESTAMP", |
| 46 |
new FunctionInfo("TO_TIMESTAMP", TO_TIMESTAMP, VAR_ARGS, Value.TIMESTAMP, true, true)); |
| 47 |
FUNCTIONS.put("TO_TIMESTAMP_TZ", |
| 48 |
new FunctionInfo("TO_TIMESTAMP_TZ", TO_TIMESTAMP_TZ, VAR_ARGS, Value.TIMESTAMP_TZ, true, true)); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns mode-specific function for a given name, or {@code null}. |
| 53 |
* |
| 54 |
* @param upperName |
| 55 |
* the upper-case name of a function |
| 56 |
* @return the function with specified name or {@code null} |
| 57 |
*/ |
| 58 |
public static FunctionsOracle getFunction(String upperName) { |
| 59 |
FunctionInfo info = FUNCTIONS.get(upperName); |
| 60 |
return info != null ? new FunctionsOracle(info) : null; |
| 61 |
} |
| 62 |
|
| 63 |
private FunctionsOracle(FunctionInfo info) { |
| 64 |
super(info); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
protected void checkParameterCount(int len) { |
| 69 |
int min = 0, max = Integer.MAX_VALUE; |
| 70 |
switch (info.type) { |
| 71 |
case TO_TIMESTAMP: |
| 72 |
case TO_TIMESTAMP_TZ: |
| 73 |
min = 1; |
| 74 |
max = 2; |
| 75 |
break; |
| 76 |
case TO_DATE: |
| 77 |
min = 1; |
| 78 |
max = 3; |
| 79 |
break; |
| 80 |
default: |
| 81 |
throw DbException.getInternalError("type=" + info.type); |
| 82 |
} |
| 83 |
if (len < min || len > max) { |
| 84 |
throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, info.name, min + ".." + max); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public Expression optimize(SessionLocal session) { |
| 90 |
boolean allConst = optimizeArguments(session); |
| 91 |
switch (info.type) { |
| 92 |
case SYS_GUID: |
| 93 |
type = TypeInfo.getTypeInfo(Value.VARBINARY, 16, 0, null); |
| 94 |
break; |
| 95 |
default: |
| 96 |
type = TypeInfo.getTypeInfo(info.returnDataType); |
| 97 |
} |
| 98 |
if (allConst) { |
| 99 |
return ValueExpression.get(getValue(session)); |
| 100 |
} |
| 101 |
return this; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public Value getValue(SessionLocal session) { |
| 106 |
Value[] values = getArgumentsValues(session, args); |
| 107 |
if (values == null) { |
| 108 |
return ValueNull.INSTANCE; |
| 109 |
} |
| 110 |
Value v0 = getNullOrValue(session, args, values, 0); |
| 111 |
Value v1 = getNullOrValue(session, args, values, 1); |
| 112 |
Value result; |
| 113 |
switch (info.type) { |
| 114 |
case ADD_MONTHS: |
| 115 |
result = DateTimeFunction.dateadd(session, DateTimeFunction.MONTH, v1.getInt(), v0); |
| 116 |
break; |
| 117 |
case SYS_GUID: |
| 118 |
result = ValueUuid.getNewRandom().convertTo(TypeInfo.TYPE_VARBINARY); |
| 119 |
break; |
| 120 |
case TO_DATE: |
| 121 |
result = ToDateParser.toDate(session, v0.getString(), v1 == null ? null : v1.getString()); |
| 122 |
break; |
| 123 |
case TO_TIMESTAMP: |
| 124 |
result = ToDateParser.toTimestamp(session, v0.getString(), v1 == null ? null : v1.getString()); |
| 125 |
break; |
| 126 |
case TO_TIMESTAMP_TZ: |
| 127 |
result = ToDateParser.toTimestampTz(session, v0.getString(), v1 == null ? null : v1.getString()); |
| 128 |
break; |
| 129 |
default: |
| 130 |
throw DbException.getInternalError("type=" + info.type); |
| 131 |
} |
| 132 |
return result; |
| 133 |
} |
| 134 |
|
| 135 |
} |