Browse files

Merge pull request #68 from mminella/BATCH-1691

BATCH-1691: Implemented group by functionality not impacted by paging
  • Loading branch information...
2 parents 920d0e5 + 6380491 commit 84ca7cbc40189fb15719c9360400e093f03bcb06 @mminella mminella committed Nov 8, 2012
Showing with 309 additions and 130 deletions.
  1. +50 −7 ...-tests/src/test/java/org/springframework/batch/item/database/JdbcPagingQueryIntegrationTests.java
  2. +1 −0 ...ests/src/test/java/org/springframework/batch/item/database/JdbcPagingRestartIntegrationTests.java
  3. +1 −0 ...ucture-tests/src/test/resources/org/springframework/batch/item/database/init-foo-schema-derby.sql
  4. +1 −0 ...cture-tests/src/test/resources/org/springframework/batch/item/database/init-foo-schema-hsqldb.sql
  5. +1 −0 ...cture-tests/src/test/resources/org/springframework/batch/item/database/init-foo-schema-oracle.sql
  6. +1 −0 ...ure-tests/src/test/resources/org/springframework/batch/item/database/init-foo-schema-postgres.sql
  7. +6 −1 ...src/main/java/org/springframework/batch/item/database/support/AbstractSqlPagingQueryProvider.java
  8. +9 −2 ...ructure/src/main/java/org/springframework/batch/item/database/support/Db2PagingQueryProvider.java
  9. +11 −5 ...cture/src/main/java/org/springframework/batch/item/database/support/DerbyPagingQueryProvider.java
  10. +10 −2 ...ucture/src/main/java/org/springframework/batch/item/database/support/HsqlPagingQueryProvider.java
  11. +9 −2 ...cture/src/main/java/org/springframework/batch/item/database/support/MySqlPagingQueryProvider.java
  12. +0 −1 ...ture/src/main/java/org/springframework/batch/item/database/support/OraclePagingQueryProvider.java
  13. +8 −1 ...re/src/main/java/org/springframework/batch/item/database/support/PostgresPagingQueryProvider.java
  14. +61 −3 ...astructure/src/main/java/org/springframework/batch/item/database/support/SqlPagingQueryUtils.java
  15. +10 −2 ...e/src/main/java/org/springframework/batch/item/database/support/SqlServerPagingQueryProvider.java
  16. +13 −7 ...rc/main/java/org/springframework/batch/item/database/support/SqlWindowingPagingQueryProvider.java
  17. +9 −2 ...ture/src/main/java/org/springframework/batch/item/database/support/SybasePagingQueryProvider.java
  18. +17 −14 ...re/src/test/java/org/springframework/batch/item/database/support/Db2PagingQueryProviderTests.java
  19. +13 −13 .../src/test/java/org/springframework/batch/item/database/support/DerbyPagingQueryProviderTests.java
  20. +1 −1 ...e/src/test/java/org/springframework/batch/item/database/support/HsqlPagingQueryProviderTests.java
  21. +1 −1 .../src/test/java/org/springframework/batch/item/database/support/MySqlPagingQueryProviderTests.java
  22. +9 −9 ...src/test/java/org/springframework/batch/item/database/support/OraclePagingQueryProviderTests.java
  23. +1 −1 ...c/test/java/org/springframework/batch/item/database/support/PostgresPagingQueryProviderTests.java
  24. +8 −8 ...cture/src/test/java/org/springframework/batch/item/database/support/SqlPagingQueryUtilsTests.java
  25. +17 −14 .../test/java/org/springframework/batch/item/database/support/SqlServerPagingQueryProviderTests.java
  26. +24 −20 ...st/java/org/springframework/batch/item/database/support/SqlWindowingPagingQueryProviderTests.java
  27. +17 −14 ...src/test/java/org/springframework/batch/item/database/support/SybasePagingQueryProviderTests.java
View
57 ...rc/test/java/org/springframework/batch/item/database/JdbcPagingQueryIntegrationTests.java
@@ -36,6 +36,7 @@
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.springframework.batch.item.database.support.AbstractSqlPagingQueryProvider;
import org.springframework.batch.item.database.support.Order;
import org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
@@ -65,20 +66,23 @@
private int itemCount = 9;
private int pageSize = 2;
-
+
@Before
- public void init() {
+ public void testInit() {
jdbcTemplate = new SimpleJdbcTemplate(dataSource);
- maxId = jdbcTemplate.queryForInt("SELECT MAX(ID) from T_FOOS");
- for (int i = itemCount; i > maxId; i--) {
- jdbcTemplate.update("INSERT into T_FOOS (ID,NAME,VALUE) values (?, ?, ?)", i, "foo" + i, i);
+ String[] names = {"Foo", "Bar", "Baz", "Foo", "Bar", "Baz", "Foo", "Bar", "Baz"};
+ String[] codes = {"A", "B", "A", "B", "B", "B", "A", "B", "A"};
+ jdbcTemplate.update("DELETE from T_FOOS");
+ for(int i = 0; i < names.length; i++) {
+ jdbcTemplate.update("INSERT into T_FOOS (ID,NAME, CODE, VALUE) values (?, ?, ?, ?)", maxId, names[i], codes[i], i);
+ maxId++;
}
assertEquals(itemCount, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS"));
}
@After
public void destroy() {
- jdbcTemplate.update("DELETE from T_FOOS where ID>?", maxId);
+ jdbcTemplate.update("DELETE from T_FOOS");
}
@Test
@@ -118,12 +122,51 @@ public void testQueryFromStart() throws Exception {
assertEquals(total, count);
}
+
+ @Test
+ public void testQueryFromStartWithGroupBy() throws Exception {
+ AbstractSqlPagingQueryProvider queryProvider = (AbstractSqlPagingQueryProvider) getPagingQueryProvider();
+ Map<String, Order> sortKeys = new LinkedHashMap<String, Order>();
+ sortKeys.put("NAME", Order.ASCENDING);
+ sortKeys.put("CODE", Order.DESCENDING);
+ queryProvider.setSortKeys(sortKeys);
+ queryProvider.setSelectClause("select NAME, CODE, sum(VALUE)");
+ queryProvider.setGroupClause("NAME, CODE");
+
+ int pages = 3;
+ int count = 0;
+ int total = 5;
+
+ List<Map<String, Object>> list = jdbcTemplate.queryForList(queryProvider.generateFirstPageQuery(pageSize));
+ logger.debug("First page result: " + list);
+ assertEquals(pageSize, list.size());
+ count += pageSize;
+ Map<String, Object> oldValues = null;
+
+ while (count < total) {
+ Map<String, Object> startAfterValues = getStartAfterValues(
+ queryProvider, list);
+ assertNotSame(oldValues, startAfterValues);
+ list = jdbcTemplate.queryForList(queryProvider.generateRemainingPagesQuery(pageSize), getParameterList(null, startAfterValues).toArray());
+ count += list.size();
+
+ if(list.size() < pageSize) {
+ assertEquals(1, list.size());
+ }
+ else {
+ assertEquals(pageSize, list.size());
+ }
+ oldValues = startAfterValues;
+ }
+
+ assertEquals(total, count);
+ }
private Map<String, Object> getStartAfterValues(
PagingQueryProvider queryProvider, List<Map<String, Object>> list) {
Map<String, Object> startAfterValues = new LinkedHashMap<String, Object>();
for (Map.Entry<String, Order> sortKey : queryProvider.getSortKeys().entrySet()) {
- startAfterValues.put(sortKey.getKey(), list.get(pageSize - 1).get(sortKey.getKey()));
+ startAfterValues.put(sortKey.getKey(), list.get(list.size() - 1).get(sortKey.getKey()));
}
return startAfterValues;
}
View
1 .../test/java/org/springframework/batch/item/database/JdbcPagingRestartIntegrationTests.java
@@ -76,6 +76,7 @@ public void init() {
for (int i = itemCount; i > maxId; i--) {
jdbcTemplate.update("INSERT into T_FOOS (ID,NAME,VALUE) values (?, ?, ?)", i, "foo" + i, i);
}
+
assertEquals(itemCount, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS"));
}
View
1 ...ests/src/test/resources/org/springframework/batch/item/database/init-foo-schema-derby.sql
@@ -4,6 +4,7 @@ DROP TABLE T_WRITE_FOOS;
CREATE TABLE T_FOOS (
ID BIGINT NOT NULL,
NAME VARCHAR(45),
+ CODE VARCHAR(10),
VALUE INTEGER
);
View
1 ...sts/src/test/resources/org/springframework/batch/item/database/init-foo-schema-hsqldb.sql
@@ -4,6 +4,7 @@ DROP TABLE T_WRITE_FOOS if exists;
CREATE TABLE T_FOOS (
ID BIGINT NOT NULL,
NAME VARCHAR(45),
+ CODE VARCHAR(10),
VALUE INTEGER
);
View
1 ...sts/src/test/resources/org/springframework/batch/item/database/init-foo-schema-oracle.sql
@@ -4,6 +4,7 @@ DROP TABLE T_WRITE_FOOS;
CREATE TABLE T_FOOS (
ID NUMBER(19) NOT NULL,
NAME VARCHAR(45),
+ CODE VARCHAR(10),
VALUE NUMBER(19)
);
View
1 ...s/src/test/resources/org/springframework/batch/item/database/init-foo-schema-postgres.sql
@@ -4,6 +4,7 @@ DROP TABLE T_WRITE_FOOS;
CREATE TABLE T_FOOS (
ID BIGINT NOT NULL,
NAME VARCHAR(45),
+ CODE VARCHAR(10),
VALUE BIGINT
);
View
7 .../java/org/springframework/batch/item/database/support/AbstractSqlPagingQueryProvider.java
@@ -71,7 +71,12 @@
* @param SQL GROUP BY clause part of the SQL query string
*/
public void setGroupClause(String groupClause) {
- this.groupClause = groupClause;
+ if (StringUtils.hasText(groupClause)) {
+ this.groupClause = removeKeyWord("group by", groupClause);
+ }
+ else {
+ this.groupClause = null;
+ }
}
/**
View
11 ...src/main/java/org/springframework/batch/item/database/support/Db2PagingQueryProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2006-2008 the original author or authors.
+ * 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.
@@ -17,12 +17,14 @@
package org.springframework.batch.item.database.support;
import org.springframework.batch.item.database.PagingQueryProvider;
+import org.springframework.util.StringUtils;
/**
* DB2 implementation of a {@link PagingQueryProvider} using
* database specific features.
*
* @author Thomas Risberg
+ * @author Michael Minella
* @since 2.0
*/
public class Db2PagingQueryProvider extends SqlWindowingPagingQueryProvider {
@@ -34,7 +36,12 @@ public String generateFirstPageQuery(int pageSize) {
@Override
public String generateRemainingPagesQuery(int pageSize) {
- return SqlPagingQueryUtils.generateLimitSqlQuery(this, true, buildLimitClause(pageSize));
+ if(StringUtils.hasText(getGroupClause())) {
+ return SqlPagingQueryUtils.generateLimitGroupedSqlQuery(this, true, buildLimitClause(pageSize));
+ }
+ else {
+ return SqlPagingQueryUtils.generateLimitSqlQuery(this, true, buildLimitClause(pageSize));
+ }
}
@Override
View
16 ...c/main/java/org/springframework/batch/item/database/support/DerbyPagingQueryProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2006-2008 the original author or authors.
+ * 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.
@@ -16,11 +16,11 @@
package org.springframework.batch.item.database.support;
-import org.springframework.jdbc.support.JdbcUtils;
+import javax.sql.DataSource;
+
import org.springframework.batch.item.database.PagingQueryProvider;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
-
-import javax.sql.DataSource;
+import org.springframework.jdbc.support.JdbcUtils;
/**
* Derby implementation of a {@link PagingQueryProvider} using standard SQL:2003 windowing functions.
@@ -31,6 +31,7 @@
*
* @author Thomas Risberg
* @author David Thexton
+ * @author Michael Minella
* @since 2.0
*/
public class DerbyPagingQueryProvider extends SqlWindowingPagingQueryProvider {
@@ -47,6 +48,11 @@ public void init(DataSource dataSource) throws Exception {
}
@Override
+ protected String getOrderedQueryAlias() {
+ return "TMP_ORDERED";
+ }
+
+ @Override
protected String getOverClause() {
return "";
}
@@ -56,7 +62,7 @@ protected String getOverSubstituteClauseStart() {
}
protected String getOverSubstituteClauseEnd() {
- return " " + super.getOverClause() + ") AS TMP_ORDERED";
+ return " ) AS " + getOrderedQueryAlias();
}
}
View
12 ...rc/main/java/org/springframework/batch/item/database/support/HsqlPagingQueryProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2006-2008 the original author or authors.
+ * 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.
@@ -16,10 +16,13 @@
package org.springframework.batch.item.database.support;
+import org.springframework.util.StringUtils;
+
/**
* HSQLDB implementation of a {@link org.springframework.batch.item.database.PagingQueryProvider} using database specific features.
*
* @author Thomas Risberg
+ * @author Michael Minella
* @since 2.0
*/
public class HsqlPagingQueryProvider extends AbstractSqlPagingQueryProvider {
@@ -31,7 +34,12 @@ public String generateFirstPageQuery(int pageSize) {
@Override
public String generateRemainingPagesQuery(int pageSize) {
- return SqlPagingQueryUtils.generateTopSqlQuery(this, true, buildTopClause(pageSize));
+ if(StringUtils.hasText(getGroupClause())) {
+ return SqlPagingQueryUtils.generateGroupedTopSqlQuery(this, true, buildTopClause(pageSize));
+ }
+ else {
+ return SqlPagingQueryUtils.generateTopSqlQuery(this, true, buildTopClause(pageSize));
+ }
}
private String buildTopClause(int pageSize) {
View
11 ...c/main/java/org/springframework/batch/item/database/support/MySqlPagingQueryProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2006-2008 the original author or authors.
+ * 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.
@@ -17,11 +17,13 @@
package org.springframework.batch.item.database.support;
import org.springframework.batch.item.database.PagingQueryProvider;
+import org.springframework.util.StringUtils;
/**
* MySQL implementation of a {@link PagingQueryProvider} using database specific features.
*
* @author Thomas Risberg
+ * @author Michael Minella
* @since 2.0
*/
public class MySqlPagingQueryProvider extends AbstractSqlPagingQueryProvider {
@@ -33,7 +35,12 @@ public String generateFirstPageQuery(int pageSize) {
@Override
public String generateRemainingPagesQuery(int pageSize) {
- return SqlPagingQueryUtils.generateLimitSqlQuery(this, true, buildLimitClause(pageSize));
+ if(StringUtils.hasText(getGroupClause())) {
+ return SqlPagingQueryUtils.generateLimitGroupedSqlQuery(this, true, buildLimitClause(pageSize));
+ }
+ else {
+ return SqlPagingQueryUtils.generateLimitSqlQuery(this, true, buildLimitClause(pageSize));
+ }
}
private String buildLimitClause(int pageSize) {
View
1 .../main/java/org/springframework/batch/item/database/support/OraclePagingQueryProvider.java
@@ -32,7 +32,6 @@
@Override
public String generateFirstPageQuery(int pageSize) {
return SqlPagingQueryUtils.generateRowNumSqlQuery(this, false, buildRowNumClause(pageSize));
-
}
@Override
View
9 ...ain/java/org/springframework/batch/item/database/support/PostgresPagingQueryProvider.java
@@ -16,6 +16,8 @@
package org.springframework.batch.item.database.support;
+import org.springframework.util.StringUtils;
+
/**
* Postgres implementation of a {@link org.springframework.batch.item.database.PagingQueryProvider} using database specific features.
*
@@ -35,7 +37,12 @@ public String generateFirstPageQuery(int pageSize) {
@Override
public String generateRemainingPagesQuery(int pageSize) {
- return SqlPagingQueryUtils.generateLimitSqlQuery(this, true, buildLimitClause(pageSize));
+ if(StringUtils.hasText(getGroupClause())) {
+ return SqlPagingQueryUtils.generateLimitGroupedSqlQuery(this, true, buildLimitClause(pageSize));
+ }
+ else {
+ return SqlPagingQueryUtils.generateLimitSqlQuery(this, true, buildLimitClause(pageSize));
+ }
}
private String buildLimitClause(int pageSize) {
View
64 ...re/src/main/java/org/springframework/batch/item/database/support/SqlPagingQueryUtils.java
@@ -58,6 +58,34 @@ public static String generateLimitSqlQuery(AbstractSqlPagingQueryProvider provid
}
/**
+ * Generate SQL query string using a LIMIT clause
+ *
+ * @param provider {@link AbstractSqlPagingQueryProvider} providing the
+ * implementation specifics
+ * @param remainingPageQuery is this query for the ramining pages (true) as
+ * opposed to the first page (false)
+ * @param limitClause the implementation specific limit clause to be used
+ * @return the generated query
+ */
+ public static String generateLimitGroupedSqlQuery(AbstractSqlPagingQueryProvider provider, boolean remainingPageQuery,
+ String limitClause) {
+ StringBuilder sql = new StringBuilder();
+ sql.append("SELECT * ");
+ sql.append(" FROM (");
+ sql.append("SELECT ").append(provider.getSelectClause());
+ sql.append(" FROM ").append(provider.getFromClause());
+ sql.append(provider.getWhereClause() == null ? "" : " WHERE " + provider.getWhereClause());
+ buildGroupByClause(provider, sql);
+ sql.append(") AS MAIN_QRY ");
+ sql.append("WHERE ");
+ buildSortConditions(provider, sql);
+ sql.append(" ORDER BY ").append(buildSortClause(provider));
+ sql.append(" " + limitClause);
+
+ return sql.toString();
+ }
+
+ /**
* Generate SQL query string using a TOP clause
*
* @param provider {@link AbstractSqlPagingQueryProvider} providing the
@@ -80,6 +108,32 @@ public static String generateTopSqlQuery(AbstractSqlPagingQueryProvider provider
}
/**
+ * Generate SQL query string using a TOP clause
+ *
+ * @param provider {@link AbstractSqlPagingQueryProvider} providing the
+ * implementation specifics
+ * @param remainingPageQuery is this query for the ramining pages (true) as
+ * opposed to the first page (false)
+ * @param topClause the implementation specific top clause to be used
+ * @return the generated query
+ */
+ public static String generateGroupedTopSqlQuery(AbstractSqlPagingQueryProvider provider, boolean remainingPageQuery,
+ String topClause) {
+ StringBuilder sql = new StringBuilder();
+ sql.append("SELECT ").append(topClause).append(" * FROM (");
+ sql.append("SELECT ").append(provider.getSelectClause());
+ sql.append(" FROM ").append(provider.getFromClause());
+ sql.append(provider.getWhereClause() == null ? "" : " WHERE " + provider.getWhereClause());
+ buildGroupByClause(provider, sql);
+ sql.append(") AS MAIN_QRY ");
+ sql.append("WHERE ");
+ buildSortConditions(provider, sql);
+ sql.append(" ORDER BY ").append(buildSortClause(provider));
+
+ return sql.toString();
+ }
+
+ /**
* Generate SQL query string using a ROW_NUM condition
*
* @param provider {@link AbstractSqlPagingQueryProvider} providing the
@@ -109,12 +163,16 @@ public static String generateRowNumSqlQuery(AbstractSqlPagingQueryProvider provi
public static String generateRowNumSqlQuery(AbstractSqlPagingQueryProvider provider, String selectClause,
boolean remainingPageQuery, String rowNumClause) {
StringBuilder sql = new StringBuilder();
- sql.append("SELECT * FROM (SELECT ").append(selectClause).append(", ROWNUM as TMP_ROW_NUM");
+ sql.append("SELECT * FROM (SELECT ").append(selectClause);
sql.append(" FROM ").append(provider.getFromClause());
- buildWhereClause(provider, remainingPageQuery, sql);
+ sql.append(provider.getWhereClause() == null ? "" : " WHERE " + provider.getWhereClause());
buildGroupByClause(provider, sql);
sql.append(" ORDER BY ").append(buildSortClause(provider));
sql.append(") WHERE ").append(rowNumClause);
+ if(remainingPageQuery) {
+ sql.append(" AND ");
+ buildSortConditions(provider, sql);
+ }
return sql.toString();
@@ -130,7 +188,7 @@ public static String generateRowNumSqlQueryWithNesting(AbstractSqlPagingQueryPro
StringBuilder sql = new StringBuilder();
sql.append("SELECT ").append(outerSelectClause).append(" FROM (SELECT ").append(outerSelectClause)
- .append(", ROWNUM as TMP_ROW_NUM");
+ .append(", ").append(StringUtils.hasText(provider.getGroupClause()) ? "MIN(ROWNUM) as TMP_ROW_NUM" : "ROWNUM as TMP_ROW_NUM");
sql.append(" FROM (SELECT ").append(innerSelectClause).append(" FROM ").append(provider.getFromClause());
buildWhereClause(provider, remainingPageQuery, sql);
buildGroupByClause(provider, sql);
View
12 ...in/java/org/springframework/batch/item/database/support/SqlServerPagingQueryProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2006-2008 the original author or authors.
+ * 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.
@@ -16,12 +16,15 @@
package org.springframework.batch.item.database.support;
+import org.springframework.util.StringUtils;
+
/**
* Sql Server implementation of a
* {@link org.springframework.batch.item.database.PagingQueryProvider} using
* database specific features.
*
* @author Thomas Risberg
+ * @author Michael Minella
* @since 2.0
*/
public class SqlServerPagingQueryProvider extends SqlWindowingPagingQueryProvider {
@@ -33,7 +36,12 @@ public String generateFirstPageQuery(int pageSize) {
@Override
public String generateRemainingPagesQuery(int pageSize) {
- return SqlPagingQueryUtils.generateTopSqlQuery(this, true, buildTopClause(pageSize));
+ if(StringUtils.hasText(getGroupClause())) {
+ return SqlPagingQueryUtils.generateGroupedTopSqlQuery(this, true, buildTopClause(pageSize));
+ }
+ else {
+ return SqlPagingQueryUtils.generateTopSqlQuery(this, true, buildTopClause(pageSize));
+ }
}
@Override
View
20 ...java/org/springframework/batch/item/database/support/SqlWindowingPagingQueryProvider.java
@@ -35,7 +35,7 @@
public String generateFirstPageQuery(int pageSize) {
StringBuilder sql = new StringBuilder();
sql.append("SELECT * FROM ( ");
- sql.append("SELECT ").append(getSelectClause()).append(", ");
+ sql.append("SELECT ").append(StringUtils.hasText(getOrderedQueryAlias()) ? getOrderedQueryAlias() + ".*, " : "*, ");
sql.append("ROW_NUMBER() OVER (").append(getOverClause());
sql.append(") AS ROW_NUMBER");
sql.append(getOverSubstituteClauseStart());
@@ -45,10 +45,15 @@ public String generateFirstPageQuery(int pageSize) {
sql.append(getOverSubstituteClauseEnd());
sql.append(") ").append(getSubQueryAlias()).append("WHERE ").append(extractTableAlias()).append(
"ROW_NUMBER <= ").append(pageSize);
-
+ sql.append(" ORDER BY ").append(SqlPagingQueryUtils.buildSortClause(this));
+
return sql.toString();
}
+ protected String getOrderedQueryAlias() {
+ return "";
+ }
+
protected Object getSubQueryAlias() {
return "AS TMP_SUB ";
}
@@ -65,23 +70,23 @@ protected Object extractTableAlias() {
public String generateRemainingPagesQuery(int pageSize) {
StringBuilder sql = new StringBuilder();
sql.append("SELECT * FROM ( ");
- sql.append("SELECT ").append(getSelectClause()).append(", ");
+ sql.append("SELECT ").append(StringUtils.hasText(getOrderedQueryAlias()) ? getOrderedQueryAlias() + ".*, " : "*, ");
sql.append("ROW_NUMBER() OVER (").append(getOverClause());
sql.append(") AS ROW_NUMBER");
sql.append(getOverSubstituteClauseStart());
sql.append(" FROM ").append(getFromClause());
- sql.append(" WHERE ");
if (getWhereClause() != null) {
+ sql.append(" WHERE ");
sql.append(getWhereClause());
- sql.append(" AND ");
}
- SqlPagingQueryUtils.buildSortConditions(this, sql);
-
sql.append(getGroupClause() == null ? "" : " GROUP BY " + getGroupClause());
sql.append(getOverSubstituteClauseEnd());
sql.append(") ").append(getSubQueryAlias()).append("WHERE ").append(extractTableAlias()).append(
"ROW_NUMBER <= ").append(pageSize);
+ sql.append(" AND ");
+ SqlPagingQueryUtils.buildSortConditions(this, sql);
+ sql.append(" ORDER BY ").append(SqlPagingQueryUtils.buildSortClause(this));
return sql.toString();
}
@@ -109,6 +114,7 @@ public String generateJumpToItemQuery(int itemIndex, int pageSize) {
sql.append(getOverSubstituteClauseEnd());
sql.append(") ").append(getSubQueryAlias()).append("WHERE ").append(extractTableAlias()).append(
"ROW_NUMBER = ").append(lastRowNum);
+ sql.append(" ORDER BY ").append(SqlPagingQueryUtils.buildSortClause(this));
return sql.toString();
}
View
11 .../main/java/org/springframework/batch/item/database/support/SybasePagingQueryProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2006-2008 the original author or authors.
+ * 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.
@@ -17,12 +17,14 @@
package org.springframework.batch.item.database.support;
import org.springframework.batch.item.database.PagingQueryProvider;
+import org.springframework.util.StringUtils;
/**
* Sybase implementation of a {@link PagingQueryProvider} using
* database specific features.
*
* @author Thomas Risberg
+ * @author Michael Minella
* @since 2.0
*/
public class SybasePagingQueryProvider extends SqlWindowingPagingQueryProvider {
@@ -34,7 +36,12 @@ public String generateFirstPageQuery(int pageSize) {
@Override
public String generateRemainingPagesQuery(int pageSize) {
- return SqlPagingQueryUtils.generateTopSqlQuery(this, true, buildTopClause(pageSize));
+ if(StringUtils.hasText(getGroupClause())) {
+ return SqlPagingQueryUtils.generateGroupedTopSqlQuery(this, true, buildTopClause(pageSize));
+ }
+ else {
+ return SqlPagingQueryUtils.generateTopSqlQuery(this, true, buildTopClause(pageSize));
+ }
}
@Override
View
31 ...est/java/org/springframework/batch/item/database/support/Db2PagingQueryProviderTests.java
@@ -37,59 +37,62 @@ public void testGenerateFirstPageQuery() {
assertEquals(sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) ORDER BY id ASC FETCH FIRST 100 ROWS ONLY";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateJumpToItemQuery() {
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
assertEquals(sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateJumpToItemQueryForFirstPage() {
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateFirstPageQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
String sql = "SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC FETCH FIRST 100 ROWS ONLY";
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateRemainingPagesQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) GROUP BY dep ORDER BY id ASC FETCH FIRST 100 ROWS ONLY";
+ String sql = "SELECT * FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep) AS MAIN_QRY WHERE ((id > ?)) ORDER BY id ASC FETCH FIRST 100 ROWS ONLY";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateJumpToItemQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateJumpToItemQueryForFirstPageWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
assertEquals(sql, s);
}
@@ -106,11 +109,11 @@ public String getRemainingSqlWithMultipleSortKeys() {
@Override
public String getJumpToItemQueryWithMultipleSortKeys() {
- return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY name ASC, id DESC";
}
@Override
public String getJumpToItemQueryForFirstPageWithMultipleSortKeys() {
- return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY name ASC, id DESC";
}
}
View
26 ...t/java/org/springframework/batch/item/database/support/DerbyPagingQueryProviderTests.java
@@ -41,7 +41,7 @@
public DerbyPagingQueryProviderTests() {
pagingQueryProvider = new DerbyPagingQueryProvider();
}
-
+
@Test
public void testInit() throws Exception {
DataSource ds = createMock(DataSource.class);
@@ -85,31 +85,31 @@ public void testInitWithUnsupportedVErsion() throws Exception {
@Test
@Override
public void testGenerateFirstPageQuery() {
- String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY id ASC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ String sql = "SELECT * FROM ( SELECT TMP_ORDERED.*, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
Assert.assertEquals(sql, s);
}
@Test
@Override
public void testGenerateRemainingPagesQuery() {
- String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) ORDER BY id ASC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ String sql = "SELECT * FROM ( SELECT TMP_ORDERED.*, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 AND ((id > ?)) ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
Assert.assertEquals(sql, s);
}
@Test
@Override
public void testGenerateJumpToItemQuery() {
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY id ASC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
Assert.assertEquals(sql, s);
}
@Test
@Override
public void testGenerateJumpToItemQueryForFirstPage() {
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY id ASC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
Assert.assertEquals(sql, s);
}
@@ -139,7 +139,7 @@ public void testQueryContainsSortKeyDesc() {
@Test
public void testGenerateFirstPageQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ String sql = "SELECT * FROM ( SELECT TMP_ORDERED.*, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
assertEquals(sql, s);
}
@@ -148,7 +148,7 @@ public void testGenerateFirstPageQueryWithGroupBy() {
@Test
public void testGenerateRemainingPagesQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) GROUP BY dep ORDER BY id ASC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ String sql = "SELECT * FROM ( SELECT TMP_ORDERED.*, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 AND ((id > ?)) ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
@@ -157,7 +157,7 @@ public void testGenerateRemainingPagesQueryWithGroupBy() {
@Test
public void testGenerateJumpToItemQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
assertEquals(sql, s);
}
@@ -166,28 +166,28 @@ public void testGenerateJumpToItemQueryWithGroupBy() {
@Test
public void testGenerateJumpToItemQueryForFirstPageWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
assertEquals(sql, s);
}
@Override
public String getFirstPageSqlWithMultipleSortKeys() {
- return "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY name ASC, id DESC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ return "SELECT * FROM ( SELECT TMP_ORDERED.*, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 ORDER BY name ASC, id DESC";
}
@Override
public String getRemainingSqlWithMultipleSortKeys() {
- return "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 AND ((name > ?) OR (name = ? AND id < ?)) ORDER BY name ASC, id DESC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ return "SELECT * FROM ( SELECT TMP_ORDERED.*, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 AND ((name > ?) OR (name = ? AND id < ?)) ORDER BY name ASC, id DESC";
}
@Override
public String getJumpToItemQueryWithMultipleSortKeys() {
- return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY name ASC, id DESC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY name ASC, id DESC";
}
@Override
public String getJumpToItemQueryForFirstPageWithMultipleSortKeys() {
- return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY name ASC, id DESC) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT id, name, age FROM foo WHERE bar = 1 ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY name ASC, id DESC";
}
}
View
2 ...st/java/org/springframework/batch/item/database/support/HsqlPagingQueryProviderTests.java
@@ -71,7 +71,7 @@ public void testGenerateFirstPageQueryWithGroupBy() {
@Test
public void testGenerateRemainingPagesQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) GROUP BY dep ORDER BY id ASC";
+ String sql = "SELECT TOP 100 * FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep) AS MAIN_QRY WHERE ((id > ?)) ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
View
2 ...t/java/org/springframework/batch/item/database/support/MySqlPagingQueryProviderTests.java
@@ -71,7 +71,7 @@ public void testGenerateFirstPageQueryWithGroupBy() {
@Test
public void testGenerateRemainingPagesQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) GROUP BY dep ORDER BY id ASC LIMIT 100";
+ String sql = "SELECT * FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep) AS MAIN_QRY WHERE ((id > ?)) ORDER BY id ASC LIMIT 100";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
View
18 .../java/org/springframework/batch/item/database/support/OraclePagingQueryProviderTests.java
@@ -32,18 +32,18 @@ public OraclePagingQueryProviderTests() {
@Test
@Override
public void testGenerateFirstPageQuery() {
- String sql = "SELECT * FROM (SELECT id, name, age, ROWNUM as TMP_ROW_NUM FROM foo WHERE bar = 1 ORDER BY id ASC) WHERE ROWNUM <= 100";
+ String sql = "SELECT * FROM (SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY id ASC) WHERE ROWNUM <= 100";
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
assertEquals(sql, s);
pagingQueryProvider.setWhereClause("");
- String sql2 = "SELECT * FROM (SELECT id, name, age, ROWNUM as TMP_ROW_NUM FROM foo ORDER BY id ASC) WHERE ROWNUM <= 100";
+ String sql2 = "SELECT * FROM (SELECT id, name, age FROM foo ORDER BY id ASC) WHERE ROWNUM <= 100";
String s2 = pagingQueryProvider.generateFirstPageQuery(pageSize);
assertEquals(sql2, s2);
}
@Test @Override
public void testGenerateRemainingPagesQuery() {
- String sql = "SELECT * FROM (SELECT id, name, age, ROWNUM as TMP_ROW_NUM FROM foo WHERE bar = 1 AND ((id > ?)) ORDER BY id ASC) WHERE ROWNUM <= 100";
+ String sql = "SELECT * FROM (SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY id ASC) WHERE ROWNUM <= 100 AND ((id > ?))";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
@@ -66,7 +66,7 @@ public void testGenerateJumpToItemQueryForFirstPage() {
@Test
public void testGenerateFirstPageQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT * FROM (SELECT id, name, age, ROWNUM as TMP_ROW_NUM FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC) WHERE ROWNUM <= 100";
+ String sql = "SELECT * FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC) WHERE ROWNUM <= 100";
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
assertEquals(sql, s);
}
@@ -75,7 +75,7 @@ public void testGenerateFirstPageQueryWithGroupBy() {
@Test
public void testGenerateRemainingPagesQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT * FROM (SELECT id, name, age, ROWNUM as TMP_ROW_NUM FROM foo WHERE bar = 1 AND ((id > ?)) GROUP BY dep ORDER BY id ASC) WHERE ROWNUM <= 100";
+ String sql = "SELECT * FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC) WHERE ROWNUM <= 100 AND ((id > ?))";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
@@ -84,7 +84,7 @@ public void testGenerateRemainingPagesQueryWithGroupBy() {
@Test
public void testGenerateJumpToItemQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM (SELECT id, ROWNUM as TMP_ROW_NUM FROM (SELECT id FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC)) WHERE TMP_ROW_NUM = 100";
+ String sql = "SELECT id FROM (SELECT id, MIN(ROWNUM) as TMP_ROW_NUM FROM (SELECT id FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC)) WHERE TMP_ROW_NUM = 100";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
assertEquals(sql, s);
}
@@ -93,19 +93,19 @@ public void testGenerateJumpToItemQueryWithGroupBy() {
@Test
public void testGenerateJumpToItemQueryForFirstPageWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM (SELECT id, ROWNUM as TMP_ROW_NUM FROM (SELECT id FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC)) WHERE TMP_ROW_NUM = 1";
+ String sql = "SELECT id FROM (SELECT id, MIN(ROWNUM) as TMP_ROW_NUM FROM (SELECT id FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC)) WHERE TMP_ROW_NUM = 1";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
assertEquals(sql, s);
}
@Override
public String getFirstPageSqlWithMultipleSortKeys() {
- return "SELECT * FROM (SELECT id, name, age, ROWNUM as TMP_ROW_NUM FROM foo WHERE bar = 1 ORDER BY name ASC, id DESC) WHERE ROWNUM <= 100";
+ return "SELECT * FROM (SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY name ASC, id DESC) WHERE ROWNUM <= 100";
}
@Override
public String getRemainingSqlWithMultipleSortKeys() {
- return "SELECT * FROM (SELECT id, name, age, ROWNUM as TMP_ROW_NUM FROM foo WHERE bar = 1 AND ((name > ?) OR (name = ? AND id < ?)) ORDER BY name ASC, id DESC) WHERE ROWNUM <= 100";
+ return "SELECT * FROM (SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY name ASC, id DESC) WHERE ROWNUM <= 100 AND ((name > ?) OR (name = ? AND id < ?))";
}
@Override
View
2 ...ava/org/springframework/batch/item/database/support/PostgresPagingQueryProviderTests.java
@@ -71,7 +71,7 @@ public void testGenerateFirstPageQueryWithGroupBy() {
@Test
public void testGenerateRemainingPagesQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("id, dep");
- String sql = "SELECT id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) GROUP BY id, dep ORDER BY id ASC LIMIT 100";
+ String sql = "SELECT * FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY id, dep) AS MAIN_QRY WHERE ((id > ?)) ORDER BY id ASC LIMIT 100";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
View
16 ...c/test/java/org/springframework/batch/item/database/support/SqlPagingQueryUtilsTests.java
@@ -74,17 +74,17 @@ public void testGenerateTopSqlQuery() {
public void testGenerateRowNumSqlQuery() {
AbstractSqlPagingQueryProvider qp = new TestSqlPagingQueryProvider("FOO", "BAR", sortKeys);
assertEquals(
- "SELECT * FROM (SELECT FOO, ROWNUM as TMP_ROW_NUM FROM BAR ORDER BY ID ASC) WHERE ROWNUMBER <= 100",
+ "SELECT * FROM (SELECT FOO FROM BAR ORDER BY ID ASC) WHERE ROWNUMBER <= 100",
SqlPagingQueryUtils.generateRowNumSqlQuery(qp, false, "ROWNUMBER <= 100"));
assertEquals(
- "SELECT * FROM (SELECT FOO, ROWNUM as TMP_ROW_NUM FROM BAR WHERE ((ID > ?)) ORDER BY ID ASC) WHERE ROWNUMBER <= 100",
+ "SELECT * FROM (SELECT FOO FROM BAR ORDER BY ID ASC) WHERE ROWNUMBER <= 100 AND ((ID > ?))",
SqlPagingQueryUtils.generateRowNumSqlQuery(qp, true, "ROWNUMBER <= 100"));
qp.setWhereClause("BAZ IS NOT NULL");
assertEquals(
- "SELECT * FROM (SELECT FOO, ROWNUM as TMP_ROW_NUM FROM BAR WHERE BAZ IS NOT NULL ORDER BY ID ASC) WHERE ROWNUMBER <= 100",
+ "SELECT * FROM (SELECT FOO FROM BAR WHERE BAZ IS NOT NULL ORDER BY ID ASC) WHERE ROWNUMBER <= 100",
SqlPagingQueryUtils.generateRowNumSqlQuery(qp, false, "ROWNUMBER <= 100"));
assertEquals(
- "SELECT * FROM (SELECT FOO, ROWNUM as TMP_ROW_NUM FROM BAR WHERE BAZ IS NOT NULL AND ((ID > ?)) ORDER BY ID ASC) WHERE ROWNUMBER <= 100",
+ "SELECT * FROM (SELECT FOO FROM BAR WHERE BAZ IS NOT NULL ORDER BY ID ASC) WHERE ROWNUMBER <= 100 AND ((ID > ?))",
SqlPagingQueryUtils.generateRowNumSqlQuery(qp, true, "ROWNUMBER <= 100"));
}
@@ -116,17 +116,17 @@ public void testGenerateRowNumSqlQueryDescending() {
sortKeys.put("ID", Order.DESCENDING);
AbstractSqlPagingQueryProvider qp = new TestSqlPagingQueryProvider("FOO", "BAR", sortKeys);
assertEquals(
- "SELECT * FROM (SELECT FOO, ROWNUM as TMP_ROW_NUM FROM BAR ORDER BY ID DESC) WHERE ROWNUMBER <= 100",
+ "SELECT * FROM (SELECT FOO FROM BAR ORDER BY ID DESC) WHERE ROWNUMBER <= 100",
SqlPagingQueryUtils.generateRowNumSqlQuery(qp, false, "ROWNUMBER <= 100"));
assertEquals(
- "SELECT * FROM (SELECT FOO, ROWNUM as TMP_ROW_NUM FROM BAR WHERE ((ID < ?)) ORDER BY ID DESC) WHERE ROWNUMBER <= 100",
+ "SELECT * FROM (SELECT FOO FROM BAR ORDER BY ID DESC) WHERE ROWNUMBER <= 100 AND ((ID < ?))",
SqlPagingQueryUtils.generateRowNumSqlQuery(qp, true, "ROWNUMBER <= 100"));
qp.setWhereClause("BAZ IS NOT NULL");
assertEquals(
- "SELECT * FROM (SELECT FOO, ROWNUM as TMP_ROW_NUM FROM BAR WHERE BAZ IS NOT NULL ORDER BY ID DESC) WHERE ROWNUMBER <= 100",
+ "SELECT * FROM (SELECT FOO FROM BAR WHERE BAZ IS NOT NULL ORDER BY ID DESC) WHERE ROWNUMBER <= 100",
SqlPagingQueryUtils.generateRowNumSqlQuery(qp, false, "ROWNUMBER <= 100"));
assertEquals(
- "SELECT * FROM (SELECT FOO, ROWNUM as TMP_ROW_NUM FROM BAR WHERE BAZ IS NOT NULL AND ((ID < ?)) ORDER BY ID DESC) WHERE ROWNUMBER <= 100",
+ "SELECT * FROM (SELECT FOO FROM BAR WHERE BAZ IS NOT NULL ORDER BY ID DESC) WHERE ROWNUMBER <= 100 AND ((ID < ?))",
SqlPagingQueryUtils.generateRowNumSqlQuery(qp, true, "ROWNUMBER <= 100"));
}
View
31 ...va/org/springframework/batch/item/database/support/SqlServerPagingQueryProviderTests.java
@@ -37,59 +37,62 @@ public void testGenerateFirstPageQuery() {
assertEquals(sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateJumpToItemQuery() {
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
assertEquals(sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateJumpToItemQueryForFirstPage() {
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateFirstPageQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC";
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateRemainingPagesQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) GROUP BY dep ORDER BY id ASC";
+ String sql = "SELECT TOP 100 * FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep) AS MAIN_QRY WHERE ((id > ?)) ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateJumpToItemQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateJumpToItemQueryForFirstPageWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
assertEquals(sql, s);
}
@@ -106,11 +109,11 @@ public String getRemainingSqlWithMultipleSortKeys() {
@Override
public String getJumpToItemQueryWithMultipleSortKeys() {
- return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY name ASC, id DESC";
}
@Override
public String getJumpToItemQueryForFirstPageWithMultipleSortKeys() {
- return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY name ASC, id DESC";
}
}
View
44 ...org/springframework/batch/item/database/support/SqlWindowingPagingQueryProviderTests.java
@@ -30,87 +30,91 @@ public SqlWindowingPagingQueryProviderTests() {
pagingQueryProvider = new SqlWindowingPagingQueryProvider();
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateFirstPageQuery() {
- String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ String sql = "SELECT * FROM ( SELECT *, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
assertEquals("", sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateRemainingPagesQuery() {
- String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 AND ((id > ?))) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ String sql = "SELECT * FROM ( SELECT *, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 AND ((id > ?)) ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals("", sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateJumpToItemQuery() {
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
assertEquals("", sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateJumpToItemQueryForFirstPage() {
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
Assert.assertEquals("", sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateFirstPageQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ String sql = "SELECT * FROM ( SELECT *, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateRemainingPagesQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 AND ((id > ?)) GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ String sql = "SELECT * FROM ( SELECT *, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 AND ((id > ?)) ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateJumpToItemQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateJumpToItemQueryForFirstPageWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
assertEquals(sql, s);
}
@Override
public String getFirstPageSqlWithMultipleSortKeys() {
- return "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ return "SELECT * FROM ( SELECT *, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 ORDER BY name ASC, id DESC";
}
@Override
public String getRemainingSqlWithMultipleSortKeys() {
- return "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1 AND ((name > ?) OR (name = ? AND id < ?))) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100";
+ return "SELECT * FROM ( SELECT *, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 100 AND ((name > ?) OR (name = ? AND id < ?)) ORDER BY name ASC, id DESC";
}
@Override
public String getJumpToItemQueryWithMultipleSortKeys() {
- return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100";
+ return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 100 ORDER BY name ASC, id DESC";
}
@Override
public String getJumpToItemQueryForFirstPageWithMultipleSortKeys() {
- return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1";
+ return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER = 1 ORDER BY name ASC, id DESC";
}
}
View
31 .../java/org/springframework/batch/item/database/support/SybasePagingQueryProviderTests.java
@@ -37,59 +37,62 @@ public void testGenerateFirstPageQuery() {
assertEquals("", sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals("", sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateJumpToItemQuery() {
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER = 100";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER = 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
assertEquals("", sql, s);
}
- @Test @Override
+ @Test
+ @Override
public void testGenerateJumpToItemQueryForFirstPage() {
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER = 1";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER = 1 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
assertEquals("", sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateFirstPageQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC";
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateRemainingPagesQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1 AND ((id > ?)) GROUP BY dep ORDER BY id ASC";
+ String sql = "SELECT TOP 100 * FROM (SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep) AS MAIN_QRY WHERE ((id > ?)) ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateJumpToItemQueryWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) WHERE ROW_NUMBER = 100";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) WHERE ROW_NUMBER = 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
assertEquals(sql, s);
}
- @Override
@Test
+ @Override
public void testGenerateJumpToItemQueryForFirstPageWithGroupBy() {
pagingQueryProvider.setGroupClause("dep");
- String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) WHERE ROW_NUMBER = 1";
+ String sql = "SELECT id FROM ( SELECT id, ROW_NUMBER() OVER ( ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 GROUP BY dep) WHERE ROW_NUMBER = 1 ORDER BY id ASC";
String s = pagingQueryProvider.generateJumpToItemQuery(45, pageSize);
assertEquals(sql, s);
}
@@ -106,11 +109,11 @@ public String getRemainingSqlWithMultipleSortKeys() {
@Override
public String getJumpToItemQueryWithMultipleSortKeys() {
- return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER = 100";
+ return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER = 100 ORDER BY name ASC, id DESC";
}
@Override
public String getJumpToItemQueryForFirstPageWithMultipleSortKeys() {
- return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER = 1";
+ return "SELECT name, id FROM ( SELECT name, id, ROW_NUMBER() OVER ( ORDER BY name ASC, id DESC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER = 1 ORDER BY name ASC, id DESC";
}
}

0 comments on commit 84ca7cb

Please sign in to comment.