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 javax.sql.DataSource;
20  
21  import org.springframework.batch.item.database.PagingQueryProvider;
22  import org.springframework.dao.InvalidDataAccessResourceUsageException;
23  import org.springframework.jdbc.support.JdbcUtils;
24  
25  /**
26   * Derby implementation of a  {@link PagingQueryProvider} using standard SQL:2003 windowing functions.
27   * These features are supported starting with Apache Derby version 10.4.1.3.
28   *
29   * As the OVER() function does not support the ORDER BY clause a sub query is instead used to order the results
30   * before the ROW_NUM restriction is applied
31   *
32   * @author Thomas Risberg
33   * @author David Thexton
34   * @author Michael Minella
35   * @since 2.0
36   */
37  public class DerbyPagingQueryProvider extends SqlWindowingPagingQueryProvider {
38  
39  	private String version;
40  
41  	@Override
42  	public void init(DataSource dataSource) throws Exception {
43  		super.init(dataSource);
44  		version = JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductVersion").toString();
45  		if ("10.4.1.3".compareTo(version) > 0) {
46  			throw new InvalidDataAccessResourceUsageException("Apache Derby version " + version + " is not supported by this class,  Only version 10.4.1.3 or later is supported");
47  		}
48  	}
49  	
50  	@Override
51  	protected String getOrderedQueryAlias() {
52  		return "TMP_ORDERED";
53  	}
54  
55  	@Override
56  	protected String getOverClause() {
57  		return "";
58  	}
59  
60      @Override
61  	protected String getOverSubstituteClauseStart() {
62  		return " FROM (SELECT " + getSelectClause();
63  	}
64  
65      @Override
66  	protected String getOverSubstituteClauseEnd() {
67  		return " ) AS " + getOrderedQueryAlias();
68  	}
69  
70  }