1 /*
2 * Copyright 2006-2012 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 * http://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.batch.item.database;
18
19 import java.util.Map;
20
21 import javax.sql.DataSource;
22
23
24 /**
25 * Interface defining the functionality to be provided for generating paging queries for use with Paging
26 * Item Readers.
27 *
28 * @author Thomas Risberg
29 * @author Michael Minella
30 * @since 2.0
31 */
32 public interface PagingQueryProvider {
33
34 /**
35 * Initialize the query provider using the provided {@link DataSource} if necessary.
36 *
37 * @param dataSource DataSource to use for any initialization
38 */
39 void init(DataSource dataSource) throws Exception;
40
41 /**
42 * Generate the query that will provide the first page, limited by the page size.
43 *
44 * @param pageSize number of rows to read for each page
45 * @return the generated query
46 */
47 String generateFirstPageQuery(int pageSize);
48
49 /**
50 * Generate the query that will provide the first page, limited by the page size.
51 *
52 * @param pageSize number of rows to read for each page
53 * @return the generated query
54 */
55 String generateRemainingPagesQuery(int pageSize);
56
57 /**
58 *
59 * Generate the query that will provide the jump to item query. The itemIndex provided could be in the middle of
60 * the page and together with the page size it will be used to calculate the last index of the preceding page
61 * to be able to retrieve the sort key for this row.
62 *
63 * @param itemIndex the index for the next item to be read
64 * @param pageSize number of rows to read for each page
65 * @return the generated query
66 */
67 String generateJumpToItemQuery(int itemIndex, int pageSize);
68
69 /**
70 * The number of parameters that are declared in the query
71 * @return number of parameters
72 */
73 int getParameterCount();
74
75 /**
76 * Indicate whether the generated queries use named parameter syntax.
77 *
78 * @return true if named parameter syntax is used
79 */
80 boolean isUsingNamedParameters();
81
82 /**
83 * The sort keys. A Map of the columns that make up the key and a Boolean indicating ascending or descending
84 * (ascending = true).
85 *
86 * @return the sort keys used to order the query
87 */
88 Map<String, Order> getSortKeys();
89
90 /**
91 * Returns either a String to be used as the named placeholder for a sort key value (based on the column name)
92 * or a ? for unnamed parameters.
93 *
94 * @param keyName The sort key name
95 * @return The string to be used for a parameterized query.
96 */
97 String getSortKeyPlaceHolder(String keyName);
98 }