diff --git a/h2/src/docsrc/html/changelog.html b/h2/src/docsrc/html/changelog.html
index 1159bb8242..5385ea66c7 100644
--- a/h2/src/docsrc/html/changelog.html
+++ b/h2/src/docsrc/html/changelog.html
@@ -21,6 +21,8 @@
Change Log
Next Version (unreleased)
+- PR #3381: Add IDENTITY() and SCOPE_IDENTITY() to LEGACY mode
+
- Issue #3376: Data cannot be read after insert of clob data > MAX_LENGTH_INPLACE_LOB with data change delta table
- PR #3377: Add -webExternalNames setting and fix WebServer.getConnection()
diff --git a/h2/src/docsrc/html/features.html b/h2/src/docsrc/html/features.html
index 76302932e6..5d1f7c7f22 100644
--- a/h2/src/docsrc/html/features.html
+++ b/h2/src/docsrc/html/features.html
@@ -883,6 +883,7 @@
LEGACY Compatibility Mode
- Attempt to reference a non-unique set of columns from a referential constraint
will create an UNIQUE constraint on them automatically.
- Unsafe comparison operators between numeric and boolean values are allowed.
+
- IDENTITY() and SCOPE_IDENTITY() are supported, but both are implemented like SCOPE_IDENTITY()
DB2 Compatibility Mode
diff --git a/h2/src/main/org/h2/engine/Mode.java b/h2/src/main/org/h2/engine/Mode.java
index c28d35617b..26f875b976 100644
--- a/h2/src/main/org/h2/engine/Mode.java
+++ b/h2/src/main/org/h2/engine/Mode.java
@@ -478,6 +478,7 @@ public enum CharPadding {
// Legacy identity and sequence features
mode.identityClause = true;
mode.updateSequenceOnManualIdentityInsertion = true;
+ mode.takeInsertedIdentity = true;
mode.identityColumnsHaveDefaultOnNull = true;
mode.nextvalAndCurrvalPseudoColumns = true;
// Legacy DML features
diff --git a/h2/src/main/org/h2/mode/FunctionsLegacy.java b/h2/src/main/org/h2/mode/FunctionsLegacy.java
new file mode 100644
index 0000000000..64df770078
--- /dev/null
+++ b/h2/src/main/org/h2/mode/FunctionsLegacy.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2004-2022 H2 Group. Multiple-Licensed under the MPL 2.0,
+ * and the EPL 1.0 (https://h2database.com/html/license.html).
+ * Initial Developer: H2 Group
+ */
+package org.h2.mode;
+
+import java.util.HashMap;
+
+import org.h2.engine.SessionLocal;
+import org.h2.expression.Expression;
+import org.h2.message.DbException;
+import org.h2.value.TypeInfo;
+import org.h2.value.Value;
+
+/**
+ * This class implements some legacy functions not available in Regular mode.
+ */
+public class FunctionsLegacy extends ModeFunction {
+
+ private static final HashMap FUNCTIONS = new HashMap<>();
+
+ private static final int IDENTITY = 6001;
+
+ private static final int SCOPE_IDENTITY = IDENTITY + 1;
+
+ static {
+ FUNCTIONS.put("IDENTITY", new FunctionInfo("IDENTITY", IDENTITY, 0, Value.BIGINT, true, false));
+ FUNCTIONS.put("SCOPE_IDENTITY",
+ new FunctionInfo("SCOPE_IDENTITY", SCOPE_IDENTITY, 0, Value.BIGINT, true, false));
+ }
+
+ /**
+ * Returns mode-specific function for a given name, or {@code null}.
+ *
+ * @param upperName
+ * the upper-case name of a function
+ * @return the function with specified name or {@code null}
+ */
+ public static FunctionsLegacy getFunction(String upperName) {
+ FunctionInfo info = FUNCTIONS.get(upperName);
+ if (info != null) {
+ return new FunctionsLegacy(info);
+ }
+ return null;
+ }
+
+ private FunctionsLegacy(FunctionInfo info) {
+ super(info);
+ }
+
+ @Override
+ public Value getValue(SessionLocal session) {
+ switch (info.type) {
+ case IDENTITY:
+ case SCOPE_IDENTITY:
+ return session.getLastIdentity().convertTo(type);
+ default:
+ throw DbException.getInternalError("type=" + info.type);
+ }
+ }
+
+ @Override
+ public Expression optimize(SessionLocal session) {
+ type = TypeInfo.getTypeInfo(info.returnDataType);
+ return this;
+ }
+
+}
diff --git a/h2/src/main/org/h2/mode/ModeFunction.java b/h2/src/main/org/h2/mode/ModeFunction.java
index 141945f513..59f212242e 100644
--- a/h2/src/main/org/h2/mode/ModeFunction.java
+++ b/h2/src/main/org/h2/mode/ModeFunction.java
@@ -49,6 +49,8 @@ public static ModeFunction getFunction(Database database, String name) {
private static ModeFunction getCompatibilityModeFunction(String name, ModeEnum modeEnum) {
switch (modeEnum) {
+ case LEGACY:
+ return FunctionsLegacy.getFunction(name);
case DB2:
case Derby:
return FunctionsDB2Derby.getFunction(name);
diff --git a/h2/src/test/org/h2/test/scripts/TestScript.java b/h2/src/test/org/h2/test/scripts/TestScript.java
index 6d53098810..0e7686b693 100644
--- a/h2/src/test/org/h2/test/scripts/TestScript.java
+++ b/h2/src/test/org/h2/test/scripts/TestScript.java
@@ -206,7 +206,7 @@ public void test() throws Exception {
"file-read", "file-write", "greatest", "h2version", "identity",
"ifnull", "last-insert-id", "least", "link-schema", "lock-mode", "lock-timeout",
"memory-free", "memory-used", "nextval", "nullif", "nvl2",
- "readonly", "rownum", "scope-identity", "session-id",
+ "readonly", "rownum", "session-id",
"table", "transaction-id", "trim_array", "truncate-value", "unnest" }) {
testScript("functions/system/" + s + ".sql");
}
diff --git a/h2/src/test/org/h2/test/scripts/functions/system/identity.sql b/h2/src/test/org/h2/test/scripts/functions/system/identity.sql
index e04598e7c1..4d692e68d5 100644
--- a/h2/src/test/org/h2/test/scripts/functions/system/identity.sql
+++ b/h2/src/test/org/h2/test/scripts/functions/system/identity.sql
@@ -2,3 +2,33 @@
-- and the EPL 1.0 (https://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
+
+CREATE TABLE TEST(ID BIGINT GENERATED BY DEFAULT AS IDENTITY, V INT);
+> ok
+
+INSERT INTO TEST(V) VALUES 10;
+> update count: 1
+
+VALUES IDENTITY();
+> exception FUNCTION_NOT_FOUND_1
+
+VALUES SCOPE_IDENTITY();
+> exception FUNCTION_NOT_FOUND_1
+
+SET MODE LEGACY;
+> ok
+
+INSERT INTO TEST(V) VALUES 20;
+> update count: 1
+
+VALUES IDENTITY();
+>> 2
+
+VALUES SCOPE_IDENTITY();
+>> 2
+
+SET MODE REGULAR;
+> ok
+
+DROP TABLE TEST;
+> ok
diff --git a/h2/src/test/org/h2/test/scripts/functions/system/scope-identity.sql b/h2/src/test/org/h2/test/scripts/functions/system/scope-identity.sql
deleted file mode 100644
index e04598e7c1..0000000000
--- a/h2/src/test/org/h2/test/scripts/functions/system/scope-identity.sql
+++ /dev/null
@@ -1,4 +0,0 @@
--- Copyright 2004-2022 H2 Group. Multiple-Licensed under the MPL 2.0,
--- and the EPL 1.0 (https://h2database.com/html/license.html).
--- Initial Developer: H2 Group
---