diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/SqlParameter.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/SqlParameter.java index 82a82962704..b4ce5c5bee5 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/SqlParameter.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/SqlParameter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.jdbc.core; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -24,7 +25,7 @@ import org.springframework.util.Assert; /** * Object to represent a SQL parameter definition. * - *

Parameters may be anonymous, in which case "name" is {@code null}. + *

Parameters may be anonymous in which case "name" is {@code null}. * However, all parameters must define a SQL type according to {@link java.sql.Types}. * * @author Rod Johnson @@ -34,16 +35,16 @@ import org.springframework.util.Assert; */ public class SqlParameter { - /** The name of the parameter, if any */ + // The name of the parameter, if any private String name; - /** SQL type constant from {@code java.sql.Types} */ + // SQL type constant from {@code java.sql.Types} private final int sqlType; - /** Used for types that are user-named like: STRUCT, DISTINCT, JAVA_OBJECT, named array types */ + // Used for types that are user-named like: STRUCT, DISTINCT, JAVA_OBJECT, named array types private String typeName; - /** The scale to apply in case of a NUMERIC or DECIMAL type, if any */ + // The scale to apply in case of a NUMERIC or DECIMAL type, if any private Integer scale; @@ -177,12 +178,16 @@ public class SqlParameter { * to a List of SqlParameter objects as used in this package. */ public static List sqlTypesToAnonymousParameterList(int... types) { - List result = new LinkedList(); + List result; if (types != null) { + result = new ArrayList(types.length); for (int type : types) { result.add(new SqlParameter(type)); } } + else { + result = new LinkedList(); + } return result; } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java index 7072ad96a01..6cf6fd315b7 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -232,7 +231,6 @@ public abstract class NamedParameterUtils { // character sequence ending comment or quote not found return statement.length; } - } } return position; @@ -257,8 +255,11 @@ public abstract class NamedParameterUtils { */ public static String substituteNamedParameters(ParsedSql parsedSql, SqlParameterSource paramSource) { String originalSql = parsedSql.getOriginalSql(); - StringBuilder actualSql = new StringBuilder(); List paramNames = parsedSql.getParameterNames(); + if (paramNames.isEmpty()) { + return originalSql; + } + StringBuilder actualSql = new StringBuilder(originalSql.length()); int lastIndex = 0; for (int i = 0; i < paramNames.size(); i++) { String paramName = paramNames.get(i); @@ -282,26 +283,26 @@ public abstract class NamedParameterUtils { Object entryItem = entryIter.next(); if (entryItem instanceof Object[]) { Object[] expressionList = (Object[]) entryItem; - actualSql.append("("); + actualSql.append('('); for (int m = 0; m < expressionList.length; m++) { if (m > 0) { actualSql.append(", "); } - actualSql.append("?"); + actualSql.append('?'); } - actualSql.append(")"); + actualSql.append(')'); } else { - actualSql.append("?"); + actualSql.append('?'); } } } else { - actualSql.append("?"); + actualSql.append('?'); } } else { - actualSql.append("?"); + actualSql.append('?'); } lastIndex = endIndex; } @@ -416,9 +417,10 @@ public abstract class NamedParameterUtils { */ public static List buildSqlParameterList(ParsedSql parsedSql, SqlParameterSource paramSource) { List paramNames = parsedSql.getParameterNames(); - List params = new LinkedList(); + List params = new ArrayList(paramNames.size()); for (String paramName : paramNames) { - params.add(new SqlParameter(paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName))); + params.add(new SqlParameter( + paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName))); } return params; }