1 | /* |
2 | * Copyright 2002-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; |
18 | |
19 | import java.util.Map; |
20 | import java.util.HashMap; |
21 | import java.util.List; |
22 | |
23 | /** |
24 | * Helper methods for SQL statement parameter parsing. |
25 | * |
26 | * Only intended for internal use. |
27 | * |
28 | * @author Thomas Risberg |
29 | * @author Juergen Hoeller |
30 | * @since 2.0 |
31 | */ |
32 | public class JdbcParameterUtils { |
33 | |
34 | /** |
35 | * Count the occurrences of the character placeholder in an SQL string |
36 | * <code>sql</code>. The character placeholder is not counted if it appears |
37 | * within a literal, that is, surrounded by single or double quotes. This method will |
38 | * count traditional placeholders in the form of a question mark ('?') as well as |
39 | * named parameters indicated with a leading ':' or '&'. |
40 | * |
41 | * The code for this method is taken from an early version of the |
42 | * {@link org.springframework.jdbc.core.namedparam.NamedParameterUtils} |
43 | * class. That method was later removed after some refactoring, but the code |
44 | * is useful here for the Spring Batch project. The code has been altered to better |
45 | * suite the batch processing requirements. |
46 | * |
47 | * @param sql String to search in. Returns 0 if the given String is <code>null</code>. |
48 | */ |
49 | public static int countParameterPlaceholders(String sql, List<String> namedParameterHolder ) { |
50 | if (sql == null) { |
51 | return 0; |
52 | } |
53 | |
54 | char[] statement = sql.toCharArray(); |
55 | boolean withinQuotes = false; |
56 | Map<String, StringBuilder> namedParameters = new HashMap<String, StringBuilder>(); |
57 | char currentQuote = '-'; |
58 | int parameterCount = 0; |
59 | int i = 0; |
60 | while (i < statement.length) { |
61 | if (withinQuotes) { |
62 | if (statement[i] == currentQuote) { |
63 | withinQuotes = false; |
64 | currentQuote = '-'; |
65 | } |
66 | } |
67 | else { |
68 | if (statement[i] == '"' || statement[i] == '\'') { |
69 | withinQuotes = true; |
70 | currentQuote = statement[i]; |
71 | } |
72 | else { |
73 | if (statement[i] == ':' || statement[i] == '&') { |
74 | int j = i + 1; |
75 | StringBuilder parameter = new StringBuilder(); |
76 | while (j < statement.length && parameterNameContinues(statement, j)) { |
77 | parameter.append(statement[j]); |
78 | j++; |
79 | } |
80 | if (j - i > 1) { |
81 | if (!namedParameters.containsKey(parameter.toString())) { |
82 | parameterCount++; |
83 | namedParameters.put(parameter.toString(), parameter); |
84 | i = j - 1; |
85 | } |
86 | } |
87 | } |
88 | else { |
89 | if (statement[i] == '?') { |
90 | parameterCount++; |
91 | } |
92 | } |
93 | } |
94 | } |
95 | i++; |
96 | } |
97 | if (namedParameterHolder != null) { |
98 | namedParameterHolder.addAll(namedParameters.keySet()); |
99 | } |
100 | return parameterCount; |
101 | } |
102 | |
103 | /** |
104 | * Determine whether a parameter name continues at the current position, |
105 | * that is, does not end delimited by any whitespace character yet. |
106 | * @param statement the SQL statement |
107 | * @param pos the position within the statement |
108 | */ |
109 | private static boolean parameterNameContinues(char[] statement, int pos) { |
110 | return (statement[pos] != ' ' && statement[pos] != ',' && statement[pos] != ')' && |
111 | statement[pos] != '"' && statement[pos] != '\'' && statement[pos] != '|' && |
112 | statement[pos] != ';' && statement[pos] != '\n' && statement[pos] != '\r'); |
113 | } |
114 | |
115 | } |