BATCH-2163: Add support for SQLite.

Adds a DatabaseType, scripts, a DataFieldMaxValueIncrementer and
a PagingQueryProvider for SQLite.
This commit is contained in:
Luke Taylor
2014-01-13 19:03:21 +00:00
committed by Michael Minella
parent f5a31a99a2
commit 2c007deaf6
17 changed files with 444 additions and 14 deletions

View File

@@ -23,6 +23,7 @@ import static org.springframework.batch.support.DatabaseType.HSQL;
import static org.springframework.batch.support.DatabaseType.MYSQL;
import static org.springframework.batch.support.DatabaseType.ORACLE;
import static org.springframework.batch.support.DatabaseType.POSTGRES;
import static org.springframework.batch.support.DatabaseType.SQLITE;
import static org.springframework.batch.support.DatabaseType.SQLSERVER;
import static org.springframework.batch.support.DatabaseType.SYBASE;
@@ -101,6 +102,9 @@ public class DefaultDataFieldMaxValueIncrementerFactory implements DataFieldMaxV
else if (databaseType == POSTGRES) {
return new PostgreSQLSequenceMaxValueIncrementer(dataSource, incrementerName);
}
else if (databaseType == SQLITE) {
return new SqliteMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName);
}
else if (databaseType == SQLSERVER) {
return new SqlServerMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName);
}

View File

@@ -23,6 +23,7 @@ import static org.springframework.batch.support.DatabaseType.HSQL;
import static org.springframework.batch.support.DatabaseType.MYSQL;
import static org.springframework.batch.support.DatabaseType.ORACLE;
import static org.springframework.batch.support.DatabaseType.POSTGRES;
import static org.springframework.batch.support.DatabaseType.SQLITE;
import static org.springframework.batch.support.DatabaseType.SQLSERVER;
import static org.springframework.batch.support.DatabaseType.SYBASE;
@@ -77,6 +78,7 @@ public class SqlPagingQueryProviderFactoryBean implements FactoryBean {
providers.put(MYSQL,new MySqlPagingQueryProvider());
providers.put(ORACLE,new OraclePagingQueryProvider());
providers.put(POSTGRES,new PostgresPagingQueryProvider());
providers.put(SQLITE, new SqlitePagingQueryProvider());
providers.put(SQLSERVER,new SqlServerPagingQueryProvider());
providers.put(SYBASE,new SybasePagingQueryProvider());
}

View File

@@ -0,0 +1,50 @@
package org.springframework.batch.item.database.support;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.incrementer.AbstractColumnMaxValueIncrementer;
/**
* Implemented as a package-private class since it is required for SQLite support, but
* should ideally be in Spring JDBC.
*
* @author Luke Taylor
*/
class SqliteMaxValueIncrementer extends AbstractColumnMaxValueIncrementer {
public SqliteMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) {
super(dataSource, incrementerName, columnName);
}
@Override
protected long getNextKey() {
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
stmt.executeUpdate("insert into " + getIncrementerName() + " values(null)");
ResultSet rs = stmt.executeQuery("select max(rowid) from " + getIncrementerName());
if (!rs.next()) {
throw new DataAccessResourceFailureException("rowid query failed after executing an update");
}
long nextKey = rs.getLong(1);
stmt.executeUpdate("delete from " + getIncrementerName() + " where " + getColumnName() + " < " + nextKey);
return nextKey;
}
catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not obtain rowid", ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2006-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.item.database.support;
import org.springframework.util.StringUtils;
/**
* SQLite implementation of a {@link org.springframework.batch.item.database.PagingQueryProvider} using database specific
* features.
*
* @author Luke Taylor
* @since 2.2.5
*/
public class SqlitePagingQueryProvider extends AbstractSqlPagingQueryProvider {
@Override
public String generateFirstPageQuery(int pageSize) {
return SqlPagingQueryUtils.generateLimitSqlQuery(this, false, buildLimitClause(pageSize));
}
@Override
public String generateRemainingPagesQuery(int pageSize) {
if(StringUtils.hasText(getGroupClause())) {
return SqlPagingQueryUtils.generateLimitGroupedSqlQuery(this, true, buildLimitClause(pageSize));
}
else {
return SqlPagingQueryUtils.generateLimitSqlQuery(this, true, buildLimitClause(pageSize));
}
}
@Override
public String generateJumpToItemQuery(int itemIndex, int pageSize) {
int page = itemIndex / pageSize;
int offset = (page * pageSize) - 1;
offset = offset<0 ? 0 : offset;
String limitClause = new StringBuilder().append("LIMIT ").append(offset).append(", 1").toString();
return SqlPagingQueryUtils.generateLimitJumpToQuery(this, limitClause);
}
private String buildLimitClause(int pageSize) {
return new StringBuilder().append("LIMIT ").append(pageSize).toString();
}
}

View File

@@ -29,24 +29,26 @@ import org.springframework.jdbc.support.MetaDataAccessException;
* Enum representing a database type, such as DB2 or oracle. The type also
* contains a product name, which is expected to be the same as the product name
* provided by the database driver's metadata.
*
*
* @author Lucas Ward
* @since 2.0
*/
public enum DatabaseType {
DERBY("Apache Derby"),
DB2("DB2"),
DB2ZOS("DB2ZOS"),
DERBY("Apache Derby"),
DB2("DB2"),
DB2ZOS("DB2ZOS"),
HSQL("HSQL Database Engine"),
SQLSERVER("Microsoft SQL Server"),
MYSQL("MySQL"),
ORACLE("Oracle"),
POSTGRES("PostgreSQL"),
SYBASE("Sybase"), H2("H2");
SYBASE("Sybase"),
H2("H2"),
SQLITE("SQLite");
private static final Map<String, DatabaseType> nameMap;
static{
nameMap = new HashMap<String, DatabaseType>();
for(DatabaseType type: values()){
@@ -56,35 +58,35 @@ public enum DatabaseType {
//A description is necessary due to the nature of database descriptions
//in metadata.
private final String productName;
private DatabaseType(String productName) {
this.productName = productName;
}
public String getProductName() {
return productName;
}
/**
* Static method to obtain a DatabaseType from the provided product name.
*
*
* @param productName
* @return DatabaseType for given product name.
* @throws IllegalArgumentException if none is found.
*/
public static DatabaseType fromProductName(String productName){
if(!nameMap.containsKey(productName)){
throw new IllegalArgumentException("DatabaseType not found for product name: [" +
throw new IllegalArgumentException("DatabaseType not found for product name: [" +
productName + "]");
}
else{
return nameMap.get(productName);
}
}
/**
* Convenience method that pulls a database product name from the DataSource's metadata.
*
*
* @param dataSource
* @return DatabaseType
* @throws MetaDataAccessException