Skip to content

Commit e1a0659

Browse files
committed
Avoid unnecessary char[] allocation in NamedParameterUtils
Issue: SPR-11042 (cherry picked from commit 1dc7ff8)
1 parent 3bfddc5 commit e1a0659

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,8 @@
3232

3333
/**
3434
* 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.
3637
*
3738
* @author Thomas Risberg
3839
* @author Juergen Hoeller
@@ -112,11 +113,13 @@ public static ParsedSql parseSqlStatement(final String sql) {
112113
while (j < statement.length && !('}' == statement[j])) {
113114
j++;
114115
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);
116118
}
117119
}
118120
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);
120123
}
121124
if (j - i > 3) {
122125
parameter = sql.substring(i + 2, j);
@@ -165,8 +168,9 @@ public static ParsedSql parseSqlStatement(final String sql) {
165168
return parsedSql;
166169
}
167170

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+
170174
parameterList.add(new ParameterHolder(parameter, i - escapes, j - escapes));
171175
totalParameterCount++;
172176
return totalParameterCount;
@@ -229,18 +233,18 @@ private static int skipCommentsAndQuotes(char[] statement, int position) {
229233
}
230234

231235
/**
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
244248
* @param paramSource the source for named parameters
245249
* @return the SQL statement with substituted parameters
246250
* @see #parseSqlStatement
@@ -255,7 +259,7 @@ public static String substituteNamedParameters(ParsedSql parsedSql, SqlParameter
255259
int[] indexes = parsedSql.getParameterIndexes(i);
256260
int startIndex = indexes[0];
257261
int endIndex = indexes[1];
258-
actualSql.append(originalSql.substring(lastIndex, startIndex));
262+
actualSql.append(originalSql, lastIndex, startIndex);
259263
if (paramSource != null && paramSource.hasValue(paramName)) {
260264
Object value = paramSource.getValue(paramName);
261265
if (value instanceof SqlParameterValue) {
@@ -295,7 +299,7 @@ public static String substituteNamedParameters(ParsedSql parsedSql, SqlParameter
295299
}
296300
lastIndex = endIndex;
297301
}
298-
actualSql.append(originalSql.substring(lastIndex, originalSql.length()));
302+
actualSql.append(originalSql, lastIndex, originalSql.length());
299303
return actualSql.toString();
300304
}
301305

@@ -314,10 +318,10 @@ public static Object[] buildValueArray(
314318
Object[] paramArray = new Object[parsedSql.getTotalParameterCount()];
315319
if (parsedSql.getNamedParameterCount() > 0 && parsedSql.getUnnamedParameterCount() > 0) {
316320
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 " +
318322
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());
321325
}
322326
List<String> paramNames = parsedSql.getParameterNames();
323327
for (int i = 0; i < paramNames.size(); i++) {
@@ -408,15 +412,12 @@ public static List<SqlParameter> buildSqlParameterList(ParsedSql parsedSql, SqlP
408412
List<String> paramNames = parsedSql.getParameterNames();
409413
List<SqlParameter> params = new LinkedList<SqlParameter>();
410414
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)));
416416
}
417417
return params;
418418
}
419419

420+
420421
//-------------------------------------------------------------------------
421422
// Convenience methods operating on a plain SQL String
422423
//-------------------------------------------------------------------------
@@ -463,28 +464,32 @@ public static Object[] buildValueArray(String sql, Map<String, ?> paramMap) {
463464
return buildValueArray(parsedSql, new MapSqlParameterSource(paramMap), null);
464465
}
465466

467+
466468
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;
470475

471476
public ParameterHolder(String parameterName, int startIndex, int endIndex) {
472-
super();
473477
this.parameterName = parameterName;
474478
this.startIndex = startIndex;
475479
this.endIndex = endIndex;
476480
}
477481

478482
public String getParameterName() {
479-
return parameterName;
483+
return this.parameterName;
480484
}
481485

482486
public int getStartIndex() {
483-
return startIndex;
487+
return this.startIndex;
484488
}
485489

486490
public int getEndIndex() {
487-
return endIndex;
491+
return this.endIndex;
488492
}
489493
}
494+
490495
}

0 commit comments

Comments
 (0)