8000 Add IDENTITY() and SCOPE_IDENTITY() to LEGACY mode · h2database/h2database@d6e4eb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit d6e4eb8

Browse files
committed
Add IDENTITY() and SCOPE_IDENTITY() to LEGACY mode
1 parent 1c0ca27 commit d6e4eb8

File tree

8 files changed

+106
-5
lines changed

8 files changed

+106
-5
lines changed

h2/src/docsrc/html/changelog.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ <h1>Change Log</h1>
2121

2222
<h2>Next Version (unreleased)</h2>
2323
<ul>
24+
<li>PR #3381: Add IDENTITY() and SCOPE_IDENTITY() to LEGACY mode
25+
</li>
2426
<li>Issue #3376: Data cannot be read after insert of clob data > MAX_LENGTH_INPLACE_LOB with data change delta table
2527
</li>
2628
<li>PR #3377: Add -webExternalNames setting and fix WebServer.getConnection()

h2/src/docsrc/html/features.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ <h3>LEGACY Compatibility Mode</h3>
883883
</li><li>Attempt to reference a non-unique set of columns from a referential constraint
884884
will create an UNIQUE constraint on them automatically.
885885
</li><li>Unsafe comparison operators between numeric and boolean values are allowed.
886+
</li><li>IDENTITY() and SCOPE_IDENTIY() are supported, but both are implemented like SCOPE_IDENTITY()
886887
</li></ul>
887888

888889
<h3>DB2 Compatibility Mode</h3>

h2/src/main/org/h2/engine/Mode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ public enum CharPadding {
478478
// Legacy identity and sequence features
479479
mode.identityClause = true;
480480
mode.updateSequenceOnManualIdentityInsertion = true;
481+
mode.takeInsertedIdentity = true;
481482
mode.identityColumnsHaveDefaultOnNull = true;
482483
mode.nextvalAndCurrvalPseudoColumns = true;
483484
// Legacy DML features
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

h2/src/main/org/h2/mode/ModeFunction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public static ModeFunction getFunction(Database database, String name) {
4949

5050
private static ModeFunction getCompatibilityModeFunction(String name, ModeEnum modeEnum) {
5151
switch (modeEnum) {
52+
case LEGACY:
53+
return FunctionsLegacy.getFunction(name);
5254
case DB2:
5355
case Derby:
5456
return FunctionsDB2Derby.getFunction(name);

h2/src/test/org/h2/test/scripts/TestScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void test() throws Exception {
206206
"file-read", "file-write", "greatest", "h2version", "identity",
207207
"ifnull", "last-insert-id", "least", "link-schema", "lock-mode", "lock-timeout",
208208
"memory-free", "memory-used", "nextval", "nullif", "nvl2",
209-
"readonly", "rownum", "scope-identity", "session-id",
209+
"readonly", "rownum", "session-id",
210210
"table", "transaction-id", "trim_array", "truncate-value", "unnest" }) {
211211
testScript("functions/system/" + s + ".sql");
212212
}

h2/src/test/org/h2/test/scripts/functions/system/identity.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,33 @@
22
-- and the EPL 1.0 (https://h2database.com/html/license.html).
33
-- Initial Developer: H2 Group
44
--
5+
6+
CREATE TABLE TEST(ID BIGINT GENERATED BY DEFAULT AS IDENTITY, V INT);
7+
> ok
8+
9+
INSERT INTO TEST(V) VALUES 10;
10+
> update count: 1
11+
12+
VALUES IDENTITY();
13+
> exception FUNCTION_NOT_FOUND_1
14+
15+
VALUES SCOPE_IDENTITY();
16+
> exception FUNCTION_NOT_FOUND_1
17+
18+
SET MODE LEGACY;
19+
> ok
20+
21+
INSERT INTO TEST(V) VALUES 20;
22+
> update count: 1
23+
24+
VALUES IDENTITY();
25+
>> 2
26+
27+
VALUES SCOPE_IDENTITY();
28+
>> 2
29+
30+
SET MODE REGULAR;
31+
> ok
32+
33+
DROP TABLE TEST;
34+
> ok

h2/src/test/org/h2/test/scripts/functions/system/scope-identity.sql

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0