View Javadoc

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.support;
18  
19  import java.util.Map;
20  
21  import org.springframework.batch.item.database.Order;
22  
23  /**
24   * Oracle implementation of a
25   * {@link org.springframework.batch.item.database.PagingQueryProvider} using
26   * database specific features.
27   * 
28   * @author Thomas Risberg
29   * @author Michael Minella
30   * @since 2.0
31   */
32  public class OraclePagingQueryProvider extends AbstractSqlPagingQueryProvider {
33  
34  	@Override
35  	public String generateFirstPageQuery(int pageSize) {
36  		return SqlPagingQueryUtils.generateRowNumSqlQuery(this, false, buildRowNumClause(pageSize));
37  	}
38  
39  	@Override
40  	public String generateRemainingPagesQuery(int pageSize) {
41  		return SqlPagingQueryUtils.generateRowNumSqlQuery(this, true, buildRowNumClause(pageSize));
42  	}
43  
44  	@Override
45  	public String generateJumpToItemQuery(int itemIndex, int pageSize) {
46  		int page = itemIndex / pageSize;
47  		int offset = (page * pageSize);
48  		offset = offset == 0 ? 1 : offset;
49  		String sortKeySelect = this.getSortKeySelect();
50  		return SqlPagingQueryUtils.generateRowNumSqlQueryWithNesting(this, sortKeySelect, sortKeySelect, false, "TMP_ROW_NUM = "
51  				+ offset);
52  	}
53  	
54  	private String getSortKeySelect() {
55  		StringBuilder sql = new StringBuilder();
56  		String prefix = "";
57  		
58  		for (Map.Entry<String, Order> sortKey : this.getSortKeys().entrySet()) {
59  			sql.append(prefix);
60  			prefix = ", ";
61  			sql.append(sortKey.getKey());
62  		}
63  		
64  		return sql.toString();
65  	}
66  
67  	private String buildRowNumClause(int pageSize) {
68  		return new StringBuilder().append("ROWNUM <= ").append(pageSize).toString();
69  	}
70  
71  }