1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
32
32
33
33
/**
34
34
* Helper methods for named parameter parsing.
35
- * Only intended for internal use within Spring's JDBC framework.
35
+ *
36
+ * <p>Only intended for internal use within Spring's JDBC framework.
36
37
*
37
38
* @author Thomas Risberg
38
39
* @author Juergen Hoeller
@@ -112,11 +113,13 @@ public static ParsedSql parseSqlStatement(final String sql) {
112
113
while (j < statement .length && !('}' == statement [j ])) {
113
114
j ++;
114
115
if (':' == statement [j ] || '{' == statement [j ]) {
115
- throw new InvalidDataAccessApiUsageException ("Parameter name contains invalid character '" + statement [j ] + "' at position " + i + " in statement " + sql );
116
+ throw new InvalidDataAccessApiUsageException ("Parameter name contains invalid character '" +
117
+ statement [j ] + "' at position " + i + " in statement: " + sql );
116
118
}
117
119
}
118
120
if (j >= statement .length ) {
119
- throw new InvalidDataAccessApiUsageException ("Non-terminated named parameter declaration at position " + i + " in statement " + sql );
121
+ throw new InvalidDataAccessApiUsageException (
122
+ "Non-terminated named parameter declaration at position " + i + " in statement: " + sql );
120
123
}
121
124
if (j - i > 3 ) {
122
125
parameter = sql .substring (i + 2 , j );
@@ -165,8 +168,9 @@ public static ParsedSql parseSqlStatement(final String sql) {
165
168
return parsedSql ;
166
169
}
167
170
168
- private static int addNamedParameter (List <ParameterHolder > parameterList , int totalParameterCount , int escapes , int i , int j ,
169
- String parameter ) {
171
+ private static int addNamedParameter (
172
+ List <ParameterHolder > parameterList , int totalParameterCount , int escapes , int i , int j , String parameter ) {
173
+
170
174
parameterList .add (new ParameterHolder (parameter , i - escapes , j - escapes ));
171
175
totalParameterCount ++;
172
176
return totalParameterCount ;
@@ -229,18 +233,18 @@ private static int skipCommentsAndQuotes(char[] statement, int position) {
229
233
}
230
234
231
235
/**
232
- * Parse the SQL statement and locate any placeholders or named parameters.
233
- * Named parameters are substituted for a JDBC placeholder and any select list
234
- * is expanded to the required number of placeholders. Select lists may contain
235
- * an array of objects and in that case the placeholders will be grouped and
236
- * enclosed with parantheses. This allows for the use of "expression lists" in
237
- * the SQL statement like:<br/>
238
- * select id, name, state from table where (name, age) in (('John', 35), ('Ann', 50))
239
- * <p>The parameter values passed in are used to determine the number of
240
- * placeholder to be used for a select list. Select lists should be limited
241
- * to 100 or fewer elements. A larger number of elements is not guaramteed to
242
- * be supported by the database and is strictly vendor-dependent.
243
- * @param parsedSql the parsed represenation of the SQL statement
236
+ * Parse the SQL statement and locate any placeholders or named parameters. Named
237
+ * parameters are substituted for a JDBC placeholder, and any select list is expanded
238
+ * to the required number of placeholders. Select lists may contain an array of
239
+ * objects, and in that case the placeholders will be grouped and enclosed with
240
+ * parentheses. This allows for the use of "expression lists" in the SQL statement
241
+ * like: <br /><br />
242
+ * {@code select id, name, state from table where (name, age) in (('John', 35), ('Ann', 50))}
243
+ * <p>The parameter values passed in are used to determine the number of placeholders to
244
+ * be used for a select list. Select lists should be limited to 100 or fewer elements.
245
+ * A larger number of elements is not guaranteed to be supported by the database and
246
+ * is strictly vendor-dependent.
247
+ * @param parsedSql the parsed representation of the SQL statement
244
248
* @param paramSource the source for named parameters
245
249
* @return the SQL statement with substituted parameters
246
250
* @see #parseSqlStatement
@@ -255,7 +259,7 @@ public static String substituteNamedParameters(ParsedSql parsedSql, SqlParameter
255
259
int [] indexes = parsedSql .getParameterIndexes (i );
256
260
int startIndex = indexes [0 ];
257
261
int endIndex = indexes [1 ];
258
- actualSql .append (originalSql . substring ( lastIndex , startIndex ) );
262
+ actualSql .append (originalSql , lastIndex , startIndex );
259
263
if (paramSource != null && paramSource .hasValue (paramName )) {
260
264
Object value = paramSource .getValue (paramName );
261
265
if (value instanceof SqlParameterValue ) {
@@ -295,7 +299,7 @@ public static String substituteNamedParameters(ParsedSql parsedSql, SqlParameter
295
299
}
296
300
lastIndex = endIndex ;
297
301
}
298
- actualSql .append (originalSql . substring ( lastIndex , originalSql .length () ));
302
+ actualSql .append (originalSql , lastIndex , originalSql .length ());
299
303
return actualSql .toString ();
300
304
}
301
305
@@ -314,10 +318,10 @@ public static Object[] buildValueArray(
314
318
Object [] paramArray = new Object [parsedSql .getTotalParameterCount ()];
315
319
if (parsedSql .getNamedParameterCount () > 0 && parsedSql .getUnnamedParameterCount () > 0 ) {
316
320
throw new InvalidDataAccessApiUsageException (
317
- "You can't mix named and traditional ? placeholders. You have " +
321
+ "Not allowed to mix named and traditional ? placeholders. You have " +
318
322
parsedSql .getNamedParameterCount () + " named parameter(s) and " +
319
- parsedSql .getUnnamedParameterCount () + " traditonal placeholder(s) in [ " +
320
- parsedSql .getOriginalSql () + "]" );
323
+ parsedSql .getUnnamedParameterCount () + " traditional placeholder(s) in statement: " +
324
+ parsedSql .getOriginalSql ());
321
325
}
322
326
List <String > paramNames = parsedSql .getParameterNames ();
323
327
for (int i = 0 ; i < paramNames .size (); i ++) {
@@ -408,15 +412,12 @@ public static List<SqlParameter> buildSqlParameterList(ParsedSql parsedSql, SqlP
408
412
List <String > paramNames = parsedSql .getParameterNames ();
409
413
List <SqlParameter > params = new LinkedList <SqlParameter >();
410
414
for (String paramName : paramNames ) {
411
- SqlParameter param = new SqlParameter (
412
- paramName ,
413
- paramSource .getSqlType (paramName ),
414
- paramSource .getTypeName (paramName ));
415
- params .add (param );
415
+ params .add (new SqlParameter (paramName , paramSource .getSqlType (paramName ), paramSource .getTypeName (paramName )));
416
416
}
417
417
return params ;
418
418
}
419
419
420
+
420
421
//-------------------------------------------------------------------------
421
422
// Convenience methods operating on a plain SQL String
422
423
//-------------------------------------------------------------------------
@@ -463,28 +464,32 @@ public static Object[] buildValueArray(String sql, Map<String, ?> paramMap) {
463
464
return buildValueArray (parsedSql , new MapSqlParameterSource (paramMap ), null );
464
465
}
465
466
467
+
466
468
private static class ParameterHolder {
467
- private String parameterName ;
468
- private int startIndex ;
469
- private int endIndex ;
469
+
470
+ private final String parameterName ;
471
+
472
+ private final int startIndex ;
473
+
474
+ private final int endIndex ;
470
475
471
476
public ParameterHolder (String parameterName , int startIndex , int endIndex ) {
472
- super ();
473
477
this .parameterName = parameterName ;
474
478
this .startIndex = startIndex ;
475
479
this .endIndex = endIndex ;
476
480
}
477
481
478
482
public String getParameterName () {
479
- return parameterName ;
483
+ return this . parameterName ;
480
484
}
481
485
482
486
public int getStartIndex () {
483
- return startIndex ;
487
+ return this . startIndex ;
484
488
}
485
489
486
490
public int getEndIndex () {
487
- return endIndex ;
491
+ return this . endIndex ;
488
492
}
489
493
}
494
+
490
495
}
0 commit comments