Skip to content

Commit b8f71b2

Browse files
committed
Add DataFieldMaxValueIncrementer for SQLite (migrated from Spring Batch)
Closes gh-35440
1 parent 2722158 commit b8f71b2

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2002-present 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 java.sql.Connection;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
import java.sql.Statement;
23+
24+
import javax.sql.DataSource;
25+
26+
import org.springframework.dao.DataAccessResourceFailureException;
27+
import org.springframework.jdbc.datasource.DataSourceUtils;
28+
import org.springframework.jdbc.support.JdbcUtils;
29+
30+
/**
31+
* {@link DataFieldMaxValueIncrementer} that increments the maximum value of a given table with
32+
* the equivalent of an auto-increment column, using a SQLite {@code select max(rowid)} query.
33+
*
34+
* @author Luke Taylor
35+
* @author Juergen Hoeller
36+
* @since 7.0
37+
*/
38+
public class SqliteMaxValueIncrementer extends AbstractColumnMaxValueIncrementer {
39+
40+
/**
41+
* Default constructor for bean property style usage.
42+
* @see #setDataSource
43+
* @see #setIncrementerName
44+
* @see #setColumnName
45+
*/
46+
public SqliteMaxValueIncrementer() {
47+
}
48+
49+
/**
50+
* Convenience constructor.
51+
* @param dataSource the DataSource to use
52+
* @param incrementerName the name of the sequence/table to use
53+
* @param columnName the name of the column in the sequence table to use
54+
*/
55+
public SqliteMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) {
56+
super(dataSource, incrementerName, columnName);
57+
}
58+
59+
60+
@Override
61+
protected long getNextKey() {
62+
Connection con = DataSourceUtils.getConnection(getDataSource());
63+
Statement stmt = null;
64+
try {
65+
stmt = con.createStatement();
66+
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
67+
stmt.executeUpdate("insert into " + getIncrementerName() + " values(null)");
68+
ResultSet rs = stmt.executeQuery("select max(rowid) from " + getIncrementerName());
69+
if (!rs.next()) {
70+
throw new DataAccessResourceFailureException("rowid query failed after executing an update");
71+
}
72+
long nextKey = rs.getLong(1);
73+
stmt.executeUpdate("delete from " + getIncrementerName() + " where " + getColumnName() + " < " + nextKey);
74+
return nextKey;
75+
}
76+
catch (SQLException ex) {
77+
throw new DataAccessResourceFailureException("Could not obtain rowid", ex);
78+
}
79+
finally {
80+
JdbcUtils.closeStatement(stmt);
81+
DataSourceUtils.releaseConnection(con, getDataSource());
82+
}
83+
}
84+
85+
}

0 commit comments

Comments
 (0)