Use ArrayList instead of LinkedList for known size
Issue: SPR-16378
This commit is contained in:
@@ -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.
|
||||
*
|
||||
* <p>Parameters may be anonymous, in which case "name" is {@code null}.
|
||||
* <p>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<SqlParameter> sqlTypesToAnonymousParameterList(int... types) {
|
||||
List<SqlParameter> result = new LinkedList<SqlParameter>();
|
||||
List<SqlParameter> result;
|
||||
if (types != null) {
|
||||
result = new ArrayList<SqlParameter>(types.length);
|
||||
for (int type : types) {
|
||||
result.add(new SqlParameter(type));
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = new LinkedList<SqlParameter>();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String> 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<SqlParameter> buildSqlParameterList(ParsedSql parsedSql, SqlParameterSource paramSource) {
|
||||
List<String> paramNames = parsedSql.getParameterNames();
|
||||
List<SqlParameter> params = new LinkedList<SqlParameter>();
|
||||
List<SqlParameter> params = new ArrayList<SqlParameter>(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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user