Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd6e4eb8

Browse files
committed
Add IDENTITY() and SCOPE_IDENTITY() to LEGACY mode
1 parent1c0ca27 commitd6e4eb8

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+
packageorg.h2.mode;
7+
8+
importjava.util.HashMap;
9+
10+
importorg.h2.engine.SessionLocal;
11+
importorg.h2.expression.Expression;
12+
importorg.h2.message.DbException;
13+
importorg.h2.value.TypeInfo;
14+
importorg.h2.value.Value;
15+
16+
/**
17+
* This class implements some legacy functions not available in Regular mode.
18+
*/
19+
publicclassFunctionsLegacyextendsModeFunction {
20+
21+
privatestaticfinalHashMap<String,FunctionInfo>FUNCTIONS =newHashMap<>();
22+
23+
privatestaticfinalintIDENTITY =6001;
24+
25+
privatestaticfinalintSCOPE_IDENTITY =IDENTITY +1;
26+
27+
static {
28+
FUNCTIONS.put("IDENTITY",newFunctionInfo("IDENTITY",IDENTITY,0,Value.BIGINT,true,false));
29+
FUNCTIONS.put("SCOPE_IDENTITY",
30+
newFunctionInfo("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+
publicstaticFunctionsLegacygetFunction(StringupperName) {
41+
FunctionInfoinfo =FUNCTIONS.get(upperName);
42+
if (info !=null) {
43+
returnnewFunctionsLegacy(info);
44+
}
45+
returnnull;
46+
}
47+
48+
privateFunctionsLegacy(FunctionInfoinfo) {
49+
super(info);
50+
}
51+
52+
@Override
53+
publicValuegetValue(SessionLocalsession) {
54+
switch (info.type) {
55+
caseIDENTITY:
56+
caseSCOPE_IDENTITY:
57+
returnsession.getLastIdentity().convertTo(type);
58+
default:
59+
throwDbException.getInternalError("type=" +info.type);
60+
}
61+
}
62+
63+
@Override
64+
publicExpressionoptimize(SessionLocalsession) {
65+
type =TypeInfo.getTypeInfo(info.returnDataType);
66+
returnthis;
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
privatestaticModeFunctiongetCompatibilityModeFunction(Stringname,ModeEnummodeEnum) {
5151
switch (modeEnum) {
52+
caseLEGACY:
53+
returnFunctionsLegacy.getFunction(name);
5254
caseDB2:
5355
caseDerby:
5456
returnFunctionsDB2Derby.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+
CREATETABLETEST(IDBIGINT GENERATED BY DEFAULTAS IDENTITY, VINT);
7+
> ok
8+
9+
INSERT INTO TEST(V)VALUES10;
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)VALUES20;
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+
DROPTABLE 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)

[8]ページ先頭

©2009-2025 Movatter.jp