Skip to content

Commit ed4e228

Browse files
hpoettkersbrannen
authored andcommitted
Support H2 1.4 & 2.0 in H2SequenceMaxValueIncrementer
Prior to this commit, H2SequenceMaxValueIncrementer only supported H2 database 1.4. This commit updates H2SequenceMaxValueIncrementer's getSequenceQuery() method so that the syntax used supports version 1.4 and 2.0 of the H2 database. This commit also updates several test schemas so that they work with H2 1.4 and 2.0 as well as HSQL. Closes gh-27870
1 parent ccf9541 commit ed4e228

File tree

8 files changed

+53
-7
lines changed

8 files changed

+53
-7
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/support/incrementer/H2SequenceMaxValueIncrementer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public H2SequenceMaxValueIncrementer(DataSource dataSource, String incrementerNa
4747

4848
@Override
4949
protected String getSequenceQuery() {
50-
return "select " + getIncrementerName() + ".nextval from dual";
50+
return "values next value for " + getIncrementerName();
5151
}
5252

5353
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.jdbc.support.incrementer;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
24+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* @author Henning Pöttker
30+
*/
31+
class H2SequenceMaxValueIncrementerTests {
32+
33+
@Test
34+
void testH2SequenceMaxValueIncrementer() {
35+
DataSource dataSource = new EmbeddedDatabaseBuilder()
36+
.setType(EmbeddedDatabaseType.H2)
37+
.addScript("classpath:/org/springframework/jdbc/support/incrementer/schema.sql")
38+
.build();
39+
H2SequenceMaxValueIncrementer incrementer = new H2SequenceMaxValueIncrementer(dataSource, "SEQ");
40+
41+
assertThat(incrementer.nextIntValue()).isEqualTo(1);
42+
assertThat(incrementer.nextStringValue()).isEqualTo("2");
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CREATE TABLE users (
2-
id INTEGER NOT NULL IDENTITY,
2+
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
33
first_name VARCHAR(50) NOT NULL,
44
last_name VARCHAR(50) NOT NULL
55
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
DROP TABLE users IF EXISTS;
22

33
CREATE TABLE users (
4-
id INTEGER NOT NULL IDENTITY,
4+
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
55
first_name VARCHAR(50) NOT NULL,
66
last_name VARCHAR(50) NOT NULL
77
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE SEQUENCE SEQ;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
DROP TABLE users IF EXISTS;
22

33
CREATE TABLE users (
4-
id INTEGER NOT NULL IDENTITY,
4+
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
55
first_name VARCHAR(50) NOT NULL,
66
last_name VARCHAR(50) NOT NULL
77
);

spring-test/src/test/resources/org/springframework/test/context/junit4/orm/db-schema.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ DROP TABLE drivers_license IF EXISTS;
22
DROP TABLE person IF EXISTS;
33

44
CREATE TABLE person (
5-
id INTEGER NOT NULL IDENTITY,
5+
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
66
name VARCHAR(50) NOT NULL,
77
drivers_license_id INTEGER NOT NULL
88
);
99
CREATE UNIQUE INDEX person_name ON person(name);
1010
CREATE UNIQUE INDEX person_drivers_license_id ON person(drivers_license_id);
1111

1212
CREATE TABLE drivers_license (
13-
id INTEGER NOT NULL IDENTITY,
13+
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
1414
license_number INTEGER NOT NULL
1515
);
1616
CREATE UNIQUE INDEX drivers_license_license_number ON drivers_license(license_number);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
CREATE TABLE enigma (
2-
id INTEGER NOT NULL IDENTITY
2+
id INTEGER GENERATED BY DEFAULT AS IDENTITY
33
);

0 commit comments

Comments
 (0)