|
| 1 | +/* |
| 2 | + * Copyright 2004-2022 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.TypeInfo; |
| 14 | +import org.h2.value.Value; |
| 15 | + |
| 16 | +/** |
| 17 | + * This class implements some legacy functions not available in Regular mode. |
| 18 | + */ |
| 19 | +public class FunctionsLegacy extends ModeFunction { |
| 20 | + |
| 21 | + private static final HashMap<String, FunctionInfo> FUNCTIONS = new HashMap<>(); |
| 22 | + |
| 23 | + private static final int IDENTITY = 6001; |
| 24 | + |
| 25 | + private static final int SCOPE_IDENTITY = IDENTITY + 1; |
| 26 | + |
| 27 | + static { |
| 28 | + FUNCTIONS.put("IDENTITY", new FunctionInfo("IDENTITY", IDENTITY, 0, Value.BIGINT, true, false)); |
| 29 | + FUNCTIONS.put("SCOPE_IDENTITY", |
| 30 | + new FunctionInfo("SCOPE_IDENTITY", SCOPE_IDENTITY, 0, Value.BIGINT, true, false)); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Returns mode-specific function for a given name, or {@code null}. |
| 35 | + * |
| 36 | + * @param upperName |
| 37 | + * the upper-case name of a function |
| 38 | + * @return the function with specified name or {@code null} |
| 39 | + */ |
| 40 | + public static FunctionsLegacy getFunction(String upperName) { |
| 41 | + FunctionInfo info = FUNCTIONS.get(upperName); |
| 42 | + if (info != null) { |
| 43 | + return new FunctionsLegacy(info); |
| 44 | + } |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + private FunctionsLegacy(FunctionInfo info) { |
| 49 | + super(info); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Value getValue(SessionLocal session) { |
| 54 | + switch (info.type) { |
| 55 | + case IDENTITY: |
| 56 | + case SCOPE_IDENTITY: |
| 57 | + return session.getLastIdentity().convertTo(type); |
| 58 | + default: |
| 59 | + throw DbException.getInternalError("type=" + info.type); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Expression optimize(SessionLocal session) { |
| 65 | + type = TypeInfo.getTypeInfo(info.returnDataType); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments