Browse Source

Use ArrayList instead of LinkedList for known size

Issue: SPR-16378
pull/1610/merge
Juergen Hoeller 8 years ago
parent
commit
64af3a0f64
  1. 19
      spring-jdbc/src/main/java/org/springframework/jdbc/core/SqlParameter.java
  2. 26
      spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java

19
spring-jdbc/src/main/java/org/springframework/jdbc/core/SqlParameter.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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; @@ -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; @@ -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 { @@ -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;
}

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

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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;
}

Loading…
Cancel
Save