Skip to content

Commit 39f24f0

Browse files
fmbenhassinesbrannen
authored andcommitted
Introduce DataFieldMaxValueIncrementer for MariaDB
Closes gh-29379
1 parent c3fca0a commit 39f24f0

File tree

2 files changed

+77
-1
lines changed

2 files changed

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

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
2929
import org.springframework.jdbc.support.incrementer.HanaSequenceMaxValueIncrementer;
3030
import org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer;
31+
import org.springframework.jdbc.support.incrementer.MariaDBSequenceMaxValueIncrementer;
3132
import org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer;
3233
import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;
3334
import org.springframework.jdbc.support.incrementer.PostgresSequenceMaxValueIncrementer;
@@ -43,6 +44,7 @@
4344
*
4445
* @author Juergen Hoeller
4546
* @author Sam Brannen
47+
* @author Mahmoud Ben Hassine
4648
* @since 27.02.2004
4749
*/
4850
class DataFieldMaxValueIncrementerTests {
@@ -210,4 +212,26 @@ void postgresSequenceMaxValueIncrementer() throws SQLException {
210212
verify(connection, times(2)).close();
211213
}
212214

215+
@Test
216+
void mariaDBSequenceMaxValueIncrementer() throws SQLException {
217+
given(dataSource.getConnection()).willReturn(connection);
218+
given(connection.createStatement()).willReturn(statement);
219+
given(statement.executeQuery("select next value for myseq")).willReturn(resultSet);
220+
given(resultSet.next()).willReturn(true);
221+
given(resultSet.getLong(1)).willReturn(10L, 12L);
222+
223+
MariaDBSequenceMaxValueIncrementer incrementer = new MariaDBSequenceMaxValueIncrementer();
224+
incrementer.setDataSource(dataSource);
225+
incrementer.setIncrementerName("myseq");
226+
incrementer.setPaddingLength(5);
227+
incrementer.afterPropertiesSet();
228+
229+
assertThat(incrementer.nextStringValue()).isEqualTo("00010");
230+
assertThat(incrementer.nextIntValue()).isEqualTo(12);
231+
232+
verify(resultSet, times(2)).close();
233+
verify(statement, times(2)).close();
234+
verify(connection, times(2)).close();
235+
}
236+
213237
}

0 commit comments

Comments
 (0)