Skip to content

Commit f9ed072

Browse files
fmbenhassinesbrannen
authored andcommitted
Introduce DataFieldMaxValueIncrementer for SQL Server sequences
This commit introduces SqlServerSequenceMaxValueIncrementer to support SQL Server sequences, complementing the existing SqlServerMaxValueIncrementer for table-based support. Closes gh-29447
1 parent ce9e2e4 commit f9ed072

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
/**
22+
* Incrementer for SQL Server sequences.
23+
*
24+
* @author Mahmoud Ben Hassine
25+
* @since 6.0
26+
*/
27+
public class SqlServerSequenceMaxValueIncrementer extends AbstractSequenceMaxValueIncrementer {
28+
29+
/**
30+
* Default constructor for bean property style usage.
31+
* @see #setDataSource
32+
* @see #setIncrementerName
33+
*/
34+
public SqlServerSequenceMaxValueIncrementer() {
35+
}
36+
37+
/**
38+
* Convenience constructor.
39+
* @param dataSource the DataSource to use
40+
* @param incrementerName the name of the sequence to use
41+
*/
42+
public SqlServerSequenceMaxValueIncrementer(DataSource dataSource, String incrementerName) {
43+
super(dataSource, incrementerName);
44+
}
45+
46+
@Override
47+
protected String getSequenceQuery() {
48+
return "select next value for " + getIncrementerName();
49+
}
50+
51+
}

spring-jdbc/src/test/java/org/springframework/jdbc/support/DataFieldMaxValueIncrementerTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer;
3333
import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;
3434
import org.springframework.jdbc.support.incrementer.PostgresSequenceMaxValueIncrementer;
35+
import org.springframework.jdbc.support.incrementer.SqlServerSequenceMaxValueIncrementer;
3536

3637
import static org.assertj.core.api.Assertions.assertThat;
3738
import static org.mockito.BDDMockito.given;
@@ -234,4 +235,26 @@ void postgresSequenceMaxValueIncrementer() throws SQLException {
234235
verify(connection, times(2)).close();
235236
}
236237

238+
@Test
239+
void sqlServerSequenceMaxValueIncrementer() throws SQLException {
240+
given(dataSource.getConnection()).willReturn(connection);
241+
given(connection.createStatement()).willReturn(statement);
242+
given(statement.executeQuery("select next value for myseq")).willReturn(resultSet);
243+
given(resultSet.next()).willReturn(true);
244+
given(resultSet.getLong(1)).willReturn(10L, 12L);
245+
246+
SqlServerSequenceMaxValueIncrementer incrementer = new SqlServerSequenceMaxValueIncrementer();
247+
incrementer.setDataSource(dataSource);
248+
incrementer.setIncrementerName("myseq");
249+
incrementer.setPaddingLength(5);
250+
incrementer.afterPropertiesSet();
251+
252+
assertThat(incrementer.nextStringValue()).isEqualTo("00010");
253+
assertThat(incrementer.nextIntValue()).isEqualTo(12);
254+
255+
verify(resultSet, times(2)).close();
256+
verify(statement, times(2)).close();
257+
verify(connection, times(2)).close();
258+
}
259+
237260
}

0 commit comments

Comments
 (0)