1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 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.
36
36
*
37
37
* @author Thomas Risberg
38
38
* @author Juergen Hoeller
39
+ * @author Yanming Zhou
39
40
* @since 2.0
40
41
*/
41
42
public abstract class NamedParameterUtils {
@@ -83,7 +84,7 @@ public static ParsedSql parseSqlStatement(final String sql) {
83
84
Assert .notNull (sql , "SQL must not be null" );
84
85
85
86
Set <String > namedParameters = new HashSet <>();
86
- String sqlToUse = sql ;
87
+ StringBuilder sqlToUse = new StringBuilder ( sql ) ;
87
88
List <ParameterHolder > parameterList = new ArrayList <>();
88
89
89
90
char [] statement = sql .toCharArray ();
@@ -155,7 +156,7 @@ public static ParsedSql parseSqlStatement(final String sql) {
155
156
int j = i + 1 ;
156
157
if (j < statement .length && statement [j ] == ':' ) {
157
158
// escaped ":" should be skipped
158
- sqlToUse = sqlToUse . substring ( 0 , i - escapes ) + sqlToUse . substring ( i - escapes + 1 );
159
+ sqlToUse . deleteCharAt ( i - escapes );
159
160
escapes ++;
160
161
i = i + 2 ;
161
162
continue ;
@@ -174,7 +175,7 @@ public static ParsedSql parseSqlStatement(final String sql) {
174
175
}
175
176
i ++;
176
177
}
177
- ParsedSql parsedSql = new ParsedSql (sqlToUse );
178
+ ParsedSql parsedSql = new ParsedSql (sqlToUse . toString () );
178
179
for (ParameterHolder ph : parameterList ) {
179
180
parsedSql .addNamedParameter (ph .getParameterName (), ph .getStartIndex (), ph .getEndIndex ());
180
181
}
@@ -346,8 +347,14 @@ public static Object[] buildValueArray(
346
347
String paramName = paramNames .get (i );
347
348
try {
348
349
SqlParameter param = findParameter (declaredParams , paramName , i );
349
- paramArray [i ] = (param != null ? new SqlParameterValue (param , paramSource .getValue (paramName )) :
350
- SqlParameterSourceUtils .getTypedValue (paramSource , paramName ));
350
+ Object paramValue = paramSource .getValue (paramName );
351
+ if (paramValue instanceof SqlParameterValue ) {
352
+ paramArray [i ] = paramValue ;
353
+ }
354
+ else {
355
+ paramArray [i ] = (param != null ? new SqlParameterValue (param , paramValue ) :
356
+ SqlParameterSourceUtils .getTypedValue (paramSource , paramName ));
357
+ }
351
358
}
352
359
catch (IllegalArgumentException ex ) {
353
360
throw new InvalidDataAccessApiUsageException (
0 commit comments