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 | |
17 | package org.springframework.batch.item.database.support; |
18 | |
19 | import org.springframework.jdbc.support.JdbcUtils; |
20 | import org.springframework.batch.item.database.PagingQueryProvider; |
21 | import org.springframework.dao.InvalidDataAccessResourceUsageException; |
22 | |
23 | import javax.sql.DataSource; |
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 | * @since 2.0 |
35 | */ |
36 | public class DerbyPagingQueryProvider extends SqlWindowingPagingQueryProvider { |
37 | |
38 | private String version; |
39 | |
40 | @Override |
41 | public void init(DataSource dataSource) throws Exception { |
42 | super.init(dataSource); |
43 | version = JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductVersion").toString(); |
44 | if ("10.4.1.3".compareTo(version) > 0) { |
45 | throw new InvalidDataAccessResourceUsageException("Apache Derby version " + version + " is not supported by this class, Only version 10.4.1.3 or later is supported"); |
46 | } |
47 | } |
48 | |
49 | @Override |
50 | protected String getOverClause() { |
51 | return ""; |
52 | } |
53 | |
54 | protected String getOverSubstituteClauseStart() { |
55 | return " FROM (SELECT " + getSelectClause(); |
56 | } |
57 | |
58 | protected String getOverSubstituteClauseEnd() { |
59 | return " " + super.getOverClause() + ") AS TMP_ORDERED"; |
60 | } |
61 | |
62 | } |