|
| 1 | +/* |
| 2 | + * Copyright 2006-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 | +package org.springframework.batch.infrastructure.item.database.support; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.springframework.batch.infrastructure.item.database.Order; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Rahul Kumar |
| 28 | + */ |
| 29 | +class SparkSqlPagingQueryProviderTests extends AbstractSqlPagingQueryProviderTests { |
| 30 | + |
| 31 | + SparkSqlPagingQueryProviderTests() { |
| 32 | + pagingQueryProvider = new SparkSqlPagingQueryProvider(); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + @Override |
| 37 | + void testGenerateFirstPageQuery() { |
| 38 | + String sql = "SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY id ASC LIMIT 100"; |
| 39 | + String s = pagingQueryProvider.generateFirstPageQuery(pageSize); |
| 40 | + assertEquals(sql, s); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + @Override |
| 45 | + void testGenerateRemainingPagesQuery() { |
| 46 | + String sql = "SELECT id, name, age FROM foo WHERE (bar = 1) AND ((id > ?)) ORDER BY id ASC LIMIT 100"; |
| 47 | + String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize); |
| 48 | + assertEquals(sql, s); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + @Test |
| 53 | + void testGenerateFirstPageQueryWithGroupBy() { |
| 54 | + pagingQueryProvider.setGroupClause("dep"); |
| 55 | + String sql = "SELECT id, name, age FROM foo WHERE bar = 1 GROUP BY dep ORDER BY id ASC LIMIT 100"; |
| 56 | + String s = pagingQueryProvider.generateFirstPageQuery(pageSize); |
| 57 | + assertEquals(sql, s); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + @Test |
| 62 | + void testGenerateRemainingPagesQueryWithGroupBy() { |
| 63 | + pagingQueryProvider.setGroupClause("dep"); |
| 64 | + 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"; |
| 65 | + String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize); |
| 66 | + assertEquals(sql, s); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testFirstPageSqlWithAliases() { |
| 71 | + Map<String, Order> sorts = new HashMap<>(); |
| 72 | + sorts.put("owner.id", Order.ASCENDING); |
| 73 | + |
| 74 | + this.pagingQueryProvider = new SparkSqlPagingQueryProvider(); |
| 75 | + this.pagingQueryProvider.setSelectClause("SELECT owner.id as ownerid, first_name, last_name, dog_name "); |
| 76 | + this.pagingQueryProvider.setFromClause("FROM dog_owner owner INNER JOIN dog ON owner.id = dog.id "); |
| 77 | + this.pagingQueryProvider.setSortKeys(sorts); |
| 78 | + |
| 79 | + String firstPage = this.pagingQueryProvider.generateFirstPageQuery(5); |
| 80 | + String remainingPagesQuery = this.pagingQueryProvider.generateRemainingPagesQuery(5); |
| 81 | + |
| 82 | + assertEquals( |
| 83 | + "SELECT owner.id as ownerid, first_name, last_name, dog_name FROM dog_owner owner INNER JOIN dog ON owner.id = dog.id ORDER BY owner.id ASC LIMIT 5", |
| 84 | + firstPage); |
| 85 | + assertEquals( |
| 86 | + "SELECT owner.id as ownerid, first_name, last_name, dog_name FROM dog_owner owner INNER JOIN dog ON owner.id = dog.id WHERE ((owner.id > ?)) ORDER BY owner.id ASC LIMIT 5", |
| 87 | + remainingPagesQuery); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + String getFirstPageSqlWithMultipleSortKeys() { |
| 92 | + return "SELECT id, name, age FROM foo WHERE bar = 1 ORDER BY name ASC, id DESC LIMIT 100"; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + String getRemainingSqlWithMultipleSortKeys() { |
| 97 | + return "SELECT id, name, age FROM foo WHERE (bar = 1) AND ((name > ?) OR (name = ? AND id < ?)) ORDER BY name ASC, id DESC LIMIT 100"; |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments