EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.item.database.support]

COVERAGE SUMMARY FOR SOURCE FILE [AbstractSqlPagingQueryProvider.java]

nameclass, %method, %block, %line, %
AbstractSqlPagingQueryProvider.java100% (1/1)94%  (16/17)86%  (171/198)90%  (45/50)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AbstractSqlPagingQueryProvider100% (1/1)94%  (16/17)86%  (171/198)90%  (45/50)
getParameterCount (): int 0%   (0/1)0%   (0/3)0%   (0/1)
getSortKeyWithoutAlias (): String 100% (1/1)48%  (11/23)57%  (4/7)
init (DataSource): void 100% (1/1)84%  (64/76)94%  (15/16)
AbstractSqlPagingQueryProvider (): void 100% (1/1)100% (6/6)100% (2/2)
getFromClause (): String 100% (1/1)100% (3/3)100% (1/1)
getSelectClause (): String 100% (1/1)100% (3/3)100% (1/1)
getSortKey (): String 100% (1/1)100% (3/3)100% (1/1)
getSortKeyPlaceHolder (): String 100% (1/1)100% (7/7)100% (1/1)
getWhereClause (): String 100% (1/1)100% (3/3)100% (1/1)
isAscending (): boolean 100% (1/1)100% (3/3)100% (1/1)
isUsingNamedParameters (): boolean 100% (1/1)100% (3/3)100% (1/1)
removeKeyWord (String, String): String 100% (1/1)100% (29/29)100% (5/5)
setAscending (boolean): void 100% (1/1)100% (4/4)100% (2/2)
setFromClause (String): void 100% (1/1)100% (7/7)100% (2/2)
setSelectClause (String): void 100% (1/1)100% (7/7)100% (2/2)
setSortKey (String): void 100% (1/1)100% (4/4)100% (2/2)
setWhereClause (String): void 100% (1/1)100% (14/14)100% (4/4)

1/*
2 * Copyright 2006-2008 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 
17package org.springframework.batch.item.database.support;
18 
19import javax.sql.DataSource;
20 
21import org.springframework.util.Assert;
22import org.springframework.util.StringUtils;
23import org.springframework.batch.item.database.JdbcParameterUtils;
24import org.springframework.batch.item.database.PagingQueryProvider;
25import org.springframework.dao.InvalidDataAccessApiUsageException;
26 
27import java.util.List;
28import java.util.ArrayList;
29 
30/**
31 * Abstract SQL Paging Query Provider to serve as a base class for all provided
32 * SQL paging query providers.
33 * 
34 * Any implementation must provide a way to specify the select clause, from
35 * clause and optionally a where clause. In addition a way to specify a single
36 * column sort key must also be provided. This sort key will be used to provide
37 * the paging functionality. It is recommended that there should be an index for
38 * the sort key to provide better performance.
39 * 
40 * Provides properties and preparation for the mandatory "selectClause" and
41 * "fromClause" as well as for the optional "whereClause". Also provides
42 * property for the mandatory "sortKey".
43 * 
44 * @author Thomas Risberg
45 * @author Dave Syer
46 * @since 2.0
47 */
48public abstract class AbstractSqlPagingQueryProvider implements PagingQueryProvider {
49 
50        private String selectClause;
51 
52        private String fromClause;
53 
54        private String whereClause;
55 
56        private String sortKey;
57 
58        private boolean ascending = true;
59 
60        private int parameterCount;
61 
62        private boolean usingNamedParameters;
63 
64        /**
65         * @param selectClause SELECT clause part of SQL query string
66         */
67        public void setSelectClause(String selectClause) {
68                this.selectClause = removeKeyWord("select", selectClause);
69        }
70 
71        /**
72         * 
73         * @return SQL SELECT clause part of SQL query string
74         */
75        protected String getSelectClause() {
76                return selectClause;
77        }
78 
79        /**
80         * @param fromClause FROM clause part of SQL query string
81         */
82        public void setFromClause(String fromClause) {
83                this.fromClause = removeKeyWord("from", fromClause);
84        }
85 
86        /**
87         * 
88         * @return SQL FROM clause part of SQL query string
89         */
90        protected String getFromClause() {
91                return fromClause;
92        }
93 
94        /**
95         * @param whereClause WHERE clause part of SQL query string
96         */
97        public void setWhereClause(String whereClause) {
98                if (StringUtils.hasText(whereClause)) {
99                        this.whereClause = removeKeyWord("where", whereClause);
100                }
101                else {
102                        this.whereClause = null;
103                }
104        }
105 
106        /**
107         * 
108         * @return SQL WHERE clause part of SQL query string
109         */
110        protected String getWhereClause() {
111                return whereClause;
112        }
113 
114        /**
115         * @param sortKey key to use to sort and limit page content
116         */
117        public void setSortKey(String sortKey) {
118                this.sortKey = sortKey;
119        }
120 
121        /**
122         * Set the flag that signals that the sort key is applied ascending (default
123         * true).
124         * 
125         * @param ascending the ascending value to set
126         */
127        public void setAscending(boolean ascending) {
128                this.ascending = ascending;
129        }
130 
131        /**
132         * Get the flag that signals that the sort key is applied ascending.
133         * 
134         * @return the ascending flag
135         */
136        public boolean isAscending() {
137                return ascending;
138        }
139 
140        /**
141         *
142         * @return sortKey key to use to sort and limit page content
143         */
144        public String getSortKey() {
145                return sortKey;
146        }
147 
148        /**
149         *
150         * @return sortKey key to use to sort and limit page content (without alias)
151         */
152        public String getSortKeyWithoutAlias() {
153                String sortKey = getSortKey();
154                int separator = sortKey.indexOf('.');
155                if (separator > 0) {
156                        int columnIndex = separator + 1;
157                        if (columnIndex < sortKey.length()) {
158                                sortKey = sortKey.substring(columnIndex);
159                        }
160                }
161                return sortKey;
162        }
163 
164        public int getParameterCount() {
165                return parameterCount;
166        }
167 
168        public boolean isUsingNamedParameters() {
169                return usingNamedParameters;
170        }
171 
172        /**
173         * The sort key placeholder will vary depending on whether named parameters
174         * or traditional placeholders are used in query strings.
175         * 
176         * @return place holder for sortKey.
177         */
178        protected String getSortKeyPlaceHolder() {
179                return usingNamedParameters ? ":_sortKey" : "?";
180        }
181 
182        /**
183         * Check mandatory properties.
184         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
185         */
186        public void init(DataSource dataSource) throws Exception {
187                Assert.notNull(dataSource);
188                Assert.hasLength(selectClause, "selectClause must be specified");
189                Assert.hasLength(fromClause, "fromClause must be specified");
190                Assert.hasLength(sortKey, "sortKey must be specified");
191                StringBuilder sql = new StringBuilder();
192                sql.append("SELECT ").append(selectClause);
193                sql.append(" FROM ").append(fromClause);
194                if (whereClause != null) {
195                        sql.append(" WHERE ").append(whereClause);
196                }
197                List<String> namedParameters = new ArrayList<String>();
198                parameterCount = JdbcParameterUtils.countParameterPlaceholders(sql.toString(), namedParameters);
199                if (namedParameters.size() > 0) {
200                        if (parameterCount != namedParameters.size()) {
201                                throw new InvalidDataAccessApiUsageException(
202                                                "You can't use both named parameters and classic \"?\" placeholders: " + sql);
203                        }
204                        usingNamedParameters = true;
205                }
206        }
207 
208        /**
209         * Method generating the query string to be used for retrieving the first
210         * page. This method must be implemented in sub classes.
211         * 
212         * @param pageSize number of rows to read per page
213         * @return query string
214         */
215        public abstract String generateFirstPageQuery(int pageSize);
216 
217        /**
218         * Method generating the query string to be used for retrieving the pages
219         * following the first page. This method must be implemented in sub classes.
220         * 
221         * @param pageSize number of rows to read per page
222         * @return query string
223         */
224        public abstract String generateRemainingPagesQuery(int pageSize);
225 
226        /**
227         * Method generating the query string to be used for jumping to a specific
228         * item position. This method must be implemented in sub classes.
229         * 
230         * @param itemIndex the index of the item to jump to
231         * @param pageSize number of rows to read per page
232         * @return query string
233         */
234        public abstract String generateJumpToItemQuery(int itemIndex, int pageSize);
235 
236        private String removeKeyWord(String keyWord, String clause) {
237                String temp = clause.trim();
238                String keyWordString = keyWord + " ";
239                if (temp.toLowerCase().startsWith(keyWordString) && temp.length() > keyWordString.length()) {
240                        return temp.substring(keyWordString.length());
241                }
242                else {
243                        return temp;
244                }
245        }
246 
247}

[all classes][org.springframework.batch.item.database.support]
EMMA 2.0.5312 (C) Vladimir Roubtsov