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 static final String MINIMAL_DERBY_VERSION = "10.4.1.3"; |
40 | |
41 | @Override |
42 | public void init(DataSource dataSource) throws Exception { |
43 | super.init(dataSource); |
44 | String version = JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductVersion").toString(); |
45 | if (!isDerbyVersionSupported(version)) { |
46 | throw new InvalidDataAccessResourceUsageException("Apache Derby version " + version + " is not supported by this class, Only version " + MINIMAL_DERBY_VERSION + " or later is supported"); |
47 | } |
48 | } |
49 | |
50 | // derby version numbering is M.m.f.p [ {alpha|beta} ] see http://db.apache.org/derby/papers/versionupgrade.html#Basic+Numbering+Scheme |
51 | private boolean isDerbyVersionSupported(String version) { |
52 | String[] minimalVersionParts = MINIMAL_DERBY_VERSION.split("\\."); |
53 | String[] versionParts = version.split("[\\. ]"); |
54 | for (int i = 0; i < minimalVersionParts.length; i++) { |
55 | int minimalVersionPart = Integer.valueOf(minimalVersionParts[i]); |
56 | int versionPart = Integer.valueOf(versionParts[i]); |
57 | if (versionPart < minimalVersionPart) { |
58 | return false; |
59 | } else if (versionPart > minimalVersionPart) { |
60 | return true; |
61 | } |
62 | } |
63 | return true; |
64 | } |
65 | |
66 | @Override |
67 | protected String getOrderedQueryAlias() { |
68 | return "TMP_ORDERED"; |
69 | } |
70 | |
71 | @Override |
72 | protected String getOverClause() { |
73 | return ""; |
74 | } |
75 | |
76 | @Override |
77 | protected String getOverSubstituteClauseStart() { |
78 | return " FROM (SELECT " + getSelectClause(); |
79 | } |
80 | |
81 | @Override |
82 | protected String getOverSubstituteClauseEnd() { |
83 | return " ) AS " + getOrderedQueryAlias(); |
84 | } |
85 | |
86 | } |