BATCH-1749: Updated for sorts to be more intuitive
This commit is contained in:
@@ -31,6 +31,7 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStreamException;
|
||||
import org.springframework.batch.item.database.support.Order;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
@@ -326,7 +327,7 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme
|
||||
private class PagingRowMapper implements RowMapper {
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
startAfterValues = new LinkedHashMap<String, Object>();
|
||||
for (Map.Entry<String, Boolean> sortKey : queryProvider.getSortKeys().entrySet()) {
|
||||
for (Map.Entry<String, Order> sortKey : queryProvider.getSortKeys().entrySet()) {
|
||||
startAfterValues.put(sortKey.getKey(), rs.getObject(sortKey.getKey()));
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@ import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.item.database.support.Order;
|
||||
|
||||
/**
|
||||
* Interface defining the functionality to be provided for generating paging queries for use with Paging
|
||||
* Item Readers.
|
||||
@@ -84,7 +86,7 @@ public interface PagingQueryProvider {
|
||||
*
|
||||
* @return the sort keys used to order the query
|
||||
*/
|
||||
Map<String, Boolean> getSortKeys();
|
||||
Map<String, Order> getSortKeys();
|
||||
|
||||
/**
|
||||
* Returns either a String to be used as the named placeholder for a sort key value (based on the column name)
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
package org.springframework.batch.item.database.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@@ -57,7 +57,7 @@ public abstract class AbstractSqlPagingQueryProvider implements PagingQueryProvi
|
||||
|
||||
private String whereClause;
|
||||
|
||||
private Map<String, Boolean> sortKeys = new TreeMap<String, Boolean>();
|
||||
private Map<String, Order> sortKeys = new LinkedHashMap<String, Order>();
|
||||
|
||||
private String groupClause;
|
||||
|
||||
@@ -136,7 +136,7 @@ public abstract class AbstractSqlPagingQueryProvider implements PagingQueryProvi
|
||||
/**
|
||||
* @param sortKeys key to use to sort and limit page content
|
||||
*/
|
||||
public void setSortKeys(Map<String, Boolean> sortKeys) {
|
||||
public void setSortKeys(Map<String, Order> sortKeys) {
|
||||
this.sortKeys = sortKeys;
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ public abstract class AbstractSqlPagingQueryProvider implements PagingQueryProvi
|
||||
*
|
||||
* @return sortKey key to use to sort and limit page content
|
||||
*/
|
||||
public Map<String, Boolean> getSortKeys() {
|
||||
public Map<String, Order> getSortKeys() {
|
||||
return sortKeys;
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ public class OraclePagingQueryProvider extends AbstractSqlPagingQueryProvider {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
String prefix = "";
|
||||
|
||||
for (Map.Entry<String, Boolean> sortKey : this.getSortKeys().entrySet()) {
|
||||
for (Map.Entry<String, Order> sortKey : this.getSortKeys().entrySet()) {
|
||||
sql.append(prefix);
|
||||
prefix = ", ";
|
||||
sql.append(sortKey.getKey());
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 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;
|
||||
|
||||
/**
|
||||
* The direction of the sort in an ORDER BY clause.
|
||||
*
|
||||
* @author Michael Minella
|
||||
*/
|
||||
public enum Order {
|
||||
ASCENDING, DESCENDING
|
||||
}
|
||||
@@ -18,8 +18,8 @@ package org.springframework.batch.item.database.support;
|
||||
import static org.springframework.batch.support.DatabaseType.DB2;
|
||||
import static org.springframework.batch.support.DatabaseType.DB2ZOS;
|
||||
import static org.springframework.batch.support.DatabaseType.DERBY;
|
||||
import static org.springframework.batch.support.DatabaseType.HSQL;
|
||||
import static org.springframework.batch.support.DatabaseType.H2;
|
||||
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;
|
||||
@@ -27,6 +27,7 @@ import static org.springframework.batch.support.DatabaseType.SQLSERVER;
|
||||
import static org.springframework.batch.support.DatabaseType.SYBASE;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@@ -46,6 +47,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Dave Syer
|
||||
* @author Michael Minella
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class SqlPagingQueryProviderFactoryBean implements FactoryBean {
|
||||
|
||||
private DataSource dataSource;
|
||||
@@ -60,9 +62,7 @@ public class SqlPagingQueryProviderFactoryBean implements FactoryBean {
|
||||
|
||||
private String groupClause;
|
||||
|
||||
private Map<String, Boolean> sortKeys;
|
||||
|
||||
private boolean ascending = true;
|
||||
private Map<String, Order> sortKeys;
|
||||
|
||||
private Map<DatabaseType, AbstractSqlPagingQueryProvider> providers = new HashMap<DatabaseType, AbstractSqlPagingQueryProvider>();
|
||||
|
||||
@@ -125,15 +125,17 @@ public class SqlPagingQueryProviderFactoryBean implements FactoryBean {
|
||||
/**
|
||||
* @param sortKey the sortKey to set
|
||||
*/
|
||||
public void setSortKeys(Map<String, Boolean> sortKeys) {
|
||||
public void setSortKeys(Map<String, Order> sortKeys) {
|
||||
this.sortKeys = sortKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ascending
|
||||
*/
|
||||
public void setAscending(boolean ascending) {
|
||||
this.ascending = ascending;
|
||||
|
||||
public void setSortKey(String key) {
|
||||
Assert.doesNotContain(key, ",", "String setter is valid for a single ASC key only");
|
||||
|
||||
Map<String, Order> keys = new LinkedHashMap<String, Order>();
|
||||
keys.put(key, Order.ASCENDING);
|
||||
|
||||
this.sortKeys = keys;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -190,14 +190,14 @@ public class SqlPagingQueryUtils {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String prefix = "";
|
||||
|
||||
for (Map.Entry<String, Boolean> sortKey : provider.getSortKeys().entrySet()) {
|
||||
for (Map.Entry<String, Order> sortKey : provider.getSortKeys().entrySet()) {
|
||||
builder.append(prefix);
|
||||
|
||||
prefix = ", ";
|
||||
|
||||
builder.append(sortKey.getKey());
|
||||
|
||||
if(sortKey.getValue() != null && !sortKey.getValue()) {
|
||||
if(sortKey.getValue() != null && sortKey.getValue() == Order.DESCENDING) {
|
||||
builder.append(" DESC");
|
||||
}
|
||||
else {
|
||||
@@ -216,7 +216,7 @@ public class SqlPagingQueryUtils {
|
||||
*/
|
||||
public static void buildSortConditions(
|
||||
AbstractSqlPagingQueryProvider provider, StringBuilder sql) {
|
||||
List<Map.Entry<String, Boolean>> keys = new ArrayList<Map.Entry<String,Boolean>>(provider.getSortKeys().entrySet());
|
||||
List<Map.Entry<String, Order>> keys = new ArrayList<Map.Entry<String,Order>>(provider.getSortKeys().entrySet());
|
||||
List<String> clauses = new ArrayList<String>();
|
||||
|
||||
for(int i = 0; i < keys.size(); i++) {
|
||||
@@ -226,7 +226,7 @@ public class SqlPagingQueryUtils {
|
||||
for(int j = 0; j < i; j++) {
|
||||
clause.append(prefix);
|
||||
prefix = " AND ";
|
||||
Entry<String, Boolean> entry = keys.get(j);
|
||||
Entry<String, Order> entry = keys.get(j);
|
||||
clause.append(entry.getKey());
|
||||
clause.append(" = ");
|
||||
clause.append(provider.getSortKeyPlaceHolder(entry.getKey()));
|
||||
@@ -237,7 +237,7 @@ public class SqlPagingQueryUtils {
|
||||
}
|
||||
clause.append(keys.get(i).getKey());
|
||||
|
||||
if(keys.get(i).getValue() != null && !keys.get(i).getValue()) {
|
||||
if(keys.get(i).getValue() != null && keys.get(i).getValue() == Order.DESCENDING) {
|
||||
clause.append(" < ");
|
||||
}
|
||||
else {
|
||||
@@ -267,7 +267,7 @@ public class SqlPagingQueryUtils {
|
||||
|
||||
String prefix = "";
|
||||
|
||||
for (Map.Entry<String, Boolean> sortKey : provider.getSortKeys().entrySet()) {
|
||||
for (Map.Entry<String, Order> sortKey : provider.getSortKeys().entrySet()) {
|
||||
select.append(prefix);
|
||||
|
||||
prefix = ", ";
|
||||
|
||||
@@ -115,7 +115,7 @@ public class SqlWindowingPagingQueryProvider extends AbstractSqlPagingQueryProvi
|
||||
|
||||
private void buildSortKeySelect(StringBuilder sql) {
|
||||
String prefix = "";
|
||||
for (Map.Entry<String, Boolean> sortKey : getSortKeys().entrySet()) {
|
||||
for (Map.Entry<String, Order> sortKey : getSortKeys().entrySet()) {
|
||||
sql.append(prefix);
|
||||
prefix = ", ";
|
||||
sql.append(sortKey.getKey());
|
||||
|
||||
Reference in New Issue
Block a user