001/*
002 * Copyright 2006-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.item.database;
018
019import java.util.Map;
020import javax.sql.DataSource;
021
022
023/**
024 * Interface defining the functionality to be provided for generating paging queries for use with Paging
025 * Item Readers.
026 *
027 * @author Thomas Risberg
028 * @author Michael Minella
029 * @since 2.0
030 */
031public interface PagingQueryProvider {
032
033        /**
034         * Initialize the query provider using the provided {@link DataSource} if necessary.
035         * 
036         * @param dataSource DataSource to use for any initialization
037         * @throws Exception for errors when initializing
038         */
039        void init(DataSource dataSource) throws Exception;
040
041        /**
042         * Generate the query that will provide the first page, limited by the page size.
043         *
044         * @param pageSize number of rows to read for each page
045         * @return the generated query
046         */
047        String generateFirstPageQuery(int pageSize);
048
049        /**
050         * Generate the query that will provide the first page, limited by the page size.
051         *
052         * @param pageSize number of rows to read for each page
053         * @return the generated query
054         */
055        String generateRemainingPagesQuery(int pageSize);
056
057        /**
058         *
059         * Generate the query that will provide the jump to item query.  The itemIndex provided could be in the middle of
060         * the page and together with the page size it will be used to calculate the last index of the preceding page
061         * to be able to retrieve the sort key for this row.
062         *
063         * @param itemIndex the index for the next item to be read
064         * @param pageSize number of rows to read for each page
065         * @return the generated query
066         */
067        String generateJumpToItemQuery(int itemIndex, int pageSize);
068
069        /**
070         * The number of parameters that are declared in the query
071         * @return number of parameters
072         */
073        int getParameterCount();
074
075        /**
076         * Indicate whether the generated queries use named parameter syntax.
077         *
078         * @return true if named parameter syntax is used
079         */
080        boolean isUsingNamedParameters();
081
082        /**
083         * The sort keys.  A Map of the columns that make up the key and a Boolean indicating ascending or descending 
084         * (ascending = true). 
085         *  
086         * @return the sort keys used to order the query
087         */
088        Map<String, Order> getSortKeys();
089        
090        /**
091         * Returns either a String to be used as the named placeholder for a sort key value (based on the column name)
092         * or a ? for unnamed parameters.
093         * 
094         * @param keyName The sort key name
095         * @return The string to be used for a parameterized query.
096         */
097        String getSortKeyPlaceHolder(String keyName);
098
099        /**
100         * The sort key (unique single column name) without alias.
101         *
102         * @return the sort key used to order the query (without alias)
103         */
104        Map<String, Order> getSortKeysWithoutAliases();
105}