001/*
002 * Copyright 2014 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.support;
018
019import org.springframework.util.StringUtils;
020
021/**
022 * SQLite implementation of a {@link org.springframework.batch.item.database.PagingQueryProvider} using database specific
023 * features.
024 *
025 * @author Luke Taylor
026 * @since 3.0.0
027 */
028public class SqlitePagingQueryProvider extends AbstractSqlPagingQueryProvider {
029        /* (non-Javadoc)
030         * @see org.springframework.batch.item.database.support.AbstractSqlPagingQueryProvider#generateFirstPageQuery(int)
031         */
032        @Override
033        public String generateFirstPageQuery(int pageSize) {
034                return SqlPagingQueryUtils.generateLimitSqlQuery(this, false, buildLimitClause(pageSize));
035        }
036
037        /* (non-Javadoc)
038         * @see org.springframework.batch.item.database.support.AbstractSqlPagingQueryProvider#generateRemainingPagesQuery(int)
039         */
040        @Override
041        public String generateRemainingPagesQuery(int pageSize) {
042                if(StringUtils.hasText(getGroupClause())) {
043                        return SqlPagingQueryUtils.generateLimitGroupedSqlQuery(this, true, buildLimitClause(pageSize));
044                }
045                else {
046                        return SqlPagingQueryUtils.generateLimitSqlQuery(this, true, buildLimitClause(pageSize));
047                }
048        }
049
050        /* (non-Javadoc)
051         * @see org.springframework.batch.item.database.support.AbstractSqlPagingQueryProvider#generateJumpToItemQuery(int, int)
052         */
053        @Override
054        public String generateJumpToItemQuery(int itemIndex, int pageSize) {
055                int page = itemIndex / pageSize;
056                int offset = (page * pageSize) - 1;
057                offset = offset<0 ? 0 : offset;
058
059                String limitClause = new StringBuilder().append("LIMIT ").append(offset).append(", 1").toString();
060                return SqlPagingQueryUtils.generateLimitJumpToQuery(this, limitClause);
061        }
062
063        private String buildLimitClause(int pageSize) {
064                return new StringBuilder().append("LIMIT ").append(pageSize).toString();
065        }
066}
067