| 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.engine.SessionLocal; |
| 11 |
import org.h2.expression.Expression; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.value.ExtTypeInfoNumeric; |
| 14 |
import org.h2.value.TypeInfo; |
| 15 |
import org.h2.value.Value; |
| 16 |
|
| 17 |
/** |
| 18 |
* Functions for {@link org.h2.engine.Mode.ModeEnum#DB2} and |
| 19 |
* {@link org.h2.engine.Mode.ModeEnum#Derby} compatibility modes. |
| 20 |
*/ |
| 21 |
public final class FunctionsDB2Derby extends ModeFunction { |
| 22 |
|
| 23 |
private static final int IDENTITY_VAL_LOCAL = 5001; |
| 24 |
|
| 25 |
private static final HashMap<String, FunctionInfo> FUNCTIONS = new HashMap<>(); |
| 26 |
|
| 27 |
private static final TypeInfo IDENTITY_VAL_LOCAL_TYPE = TypeInfo.getTypeInfo(Value.NUMERIC, 31, 0, |
| 28 |
ExtTypeInfoNumeric.DECIMAL); |
| 29 |
|
| 30 |
static { |
| 31 |
FUNCTIONS.put("IDENTITY_VAL_LOCAL", |
| 32 |
new FunctionInfo("IDENTITY_VAL_LOCAL", IDENTITY_VAL_LOCAL, 0, Value.BIGINT, true, false)); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns mode-specific function for a given name, or {@code null}. |
| 37 |
* |
| 38 |
* @param upperName |
| 39 |
* the upper-case name of a function |
| 40 |
* @return the function with specified name or {@code null} |
| 41 |
*/ |
| 42 |
public static FunctionsDB2Derby getFunction(String upperName) { |
| 43 |
FunctionInfo info = FUNCTIONS.get(upperName); |
| 44 |
return info != null ? new FunctionsDB2Derby(info) : null; |
| 45 |
} |
| 46 |
|
| 47 |
private FunctionsDB2Derby(FunctionInfo info) { |
| 48 |
super(info); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public Value getValue(SessionLocal session) { |
| 53 |
switch (info.type) { |
| 54 |
case IDENTITY_VAL_LOCAL: |
| 55 |
return session.getLastIdentity().convertTo(type); |
| 56 |
default: |
| 57 |
throw DbException.getInternalError("type=" + info.type); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public Expression optimize(SessionLocal session) { |
| 63 |
switch (info.type) { |
| 64 |
case IDENTITY_VAL_LOCAL: |
| 65 |
type = IDENTITY_VAL_LOCAL_TYPE; |
| 66 |
break; |
| 67 |
default: |
| 68 |
throw DbException.getInternalError("type=" + info.type); |
| 69 |
} |
| 70 |
return this; |
| 71 |
} |
| 72 |
|
| 73 |
} |