Skip to content

Commit

Permalink
Use ArrayList instead of LinkedList for known size
Browse files Browse the repository at this point in the history
Issue: SPR-16378
  • Loading branch information
jhoeller committed Jan 19, 2018
1 parent d7959ed commit 64af3a0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.jdbc.core;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -24,7 +25,7 @@
/**
* 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
Expand All @@ -34,16 +35,16 @@
*/
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;


Expand Down Expand Up @@ -177,12 +178,16 @@ public boolean isResultsParameter() {
* 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,7 +20,6 @@
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;
Expand Down Expand Up @@ -232,7 +231,6 @@ private static int skipCommentsAndQuotes(char[] statement, int position) {
// character sequence ending comment or quote not found
return statement.length;
}

}
}
return position;
Expand All @@ -257,8 +255,11 @@ private static int skipCommentsAndQuotes(char[] statement, int position) {
*/
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);
Expand All @@ -282,26 +283,26 @@ public static String substituteNamedParameters(ParsedSql parsedSql, SqlParameter
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;
}
Expand Down Expand Up @@ -416,9 +417,10 @@ public static int[] buildSqlTypeArray(ParsedSql parsedSql, SqlParameterSource pa
*/
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;
}
Expand Down

0 comments on commit 64af3a0

Please sign in to comment.