Introduced public ArgumentPreparedStatementSetter and ArgumentTypePreparedStatementSetter classes

Issue: SPR-10375
This commit is contained in:
Juergen Hoeller 2013-03-19 11:28:25 +01:00
parent 6b4c29cd24
commit cc4f1328ee
3 changed files with 25 additions and 24 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,12 +20,12 @@ import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
/** /**
* Simple adapter for PreparedStatementSetter that applies * Simple adapter for {@link PreparedStatementSetter} that applies a given array of arguments.
* a given array of arguments.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 3.2.3
*/ */
class ArgPreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer { public class ArgumentPreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer {
private final Object[] args; private final Object[] args;
@ -34,7 +34,7 @@ class ArgPreparedStatementSetter implements PreparedStatementSetter, ParameterDi
* Create a new ArgPreparedStatementSetter for the given arguments. * Create a new ArgPreparedStatementSetter for the given arguments.
* @param args the arguments to set * @param args the arguments to set
*/ */
public ArgPreparedStatementSetter(Object[] args) { public ArgumentPreparedStatementSetter(Object[] args) {
this.args = args; this.args = args;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,17 +20,17 @@ import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types; import java.sql.Types;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
/** /**
* Simple adapter for PreparedStatementSetter that applies * Simple adapter for {@link PreparedStatementSetter} that applies
* given arrays of arguments and JDBC argument types. * given arrays of arguments and JDBC argument types.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 3.2.3
*/ */
class ArgTypePreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer { public class ArgumentTypePreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer {
private final Object[] args; private final Object[] args;
@ -42,7 +42,7 @@ class ArgTypePreparedStatementSetter implements PreparedStatementSetter, Paramet
* @param args the arguments to set * @param args the arguments to set
* @param argTypes the corresponding SQL types of the arguments * @param argTypes the corresponding SQL types of the arguments
*/ */
public ArgTypePreparedStatementSetter(Object[] args, int[] argTypes) { public ArgumentTypePreparedStatementSetter(Object[] args, int[] argTypes) {
if ((args != null && argTypes == null) || (args == null && argTypes != null) || if ((args != null && argTypes == null) || (args == null && argTypes != null) ||
(args != null && args.length != argTypes.length)) { (args != null && args.length != argTypes.length)) {
throw new InvalidDataAccessApiUsageException("args and argTypes parameters must match"); throw new InvalidDataAccessApiUsageException("args and argTypes parameters must match");
@ -59,12 +59,10 @@ class ArgTypePreparedStatementSetter implements PreparedStatementSetter, Paramet
Object arg = this.args[i]; Object arg = this.args[i];
if (arg instanceof Collection && this.argTypes[i] != Types.ARRAY) { if (arg instanceof Collection && this.argTypes[i] != Types.ARRAY) {
Collection entries = (Collection) arg; Collection entries = (Collection) arg;
for (Iterator it = entries.iterator(); it.hasNext();) { for (Object entry : entries) {
Object entry = it.next();
if (entry instanceof Object[]) { if (entry instanceof Object[]) {
Object[] valueArray = ((Object[]) entry); Object[] valueArray = ((Object[]) entry);
for (int k = 0; k < valueArray.length; k++) { for (Object argValue : valueArray) {
Object argValue = valueArray[k];
doSetValue(ps, parameterPosition, this.argTypes[i], argValue); doSetValue(ps, parameterPosition, this.argTypes[i], argValue);
parameterPosition++; parameterPosition++;
} }
@ -94,6 +92,7 @@ class ArgTypePreparedStatementSetter implements PreparedStatementSetter, Paramet
*/ */
protected void doSetValue(PreparedStatement ps, int parameterPosition, int argType, Object argValue) protected void doSetValue(PreparedStatement ps, int parameterPosition, int argType, Object argValue)
throws SQLException { throws SQLException {
StatementCreatorUtils.setParameterValue(ps, parameterPosition, argType, argValue); StatementCreatorUtils.setParameterValue(ps, parameterPosition, argType, argValue);
} }

View File

@ -1291,24 +1291,26 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
} }
/** /**
* Create a new ArgPreparedStatementSetter using the args passed in. This method allows the * Create a new arg-based PreparedStatementSetter using the args passed in.
* creation to be overridden by sub-classes. * <p>By default, we'll create an {@link ArgumentPreparedStatementSetter}.
* This method allows for the creation to be overridden by subclasses.
* @param args object array with arguments * @param args object array with arguments
* @return the new PreparedStatementSetter * @return the new PreparedStatementSetter to use
*/ */
protected PreparedStatementSetter newArgPreparedStatementSetter(Object[] args) { protected PreparedStatementSetter newArgPreparedStatementSetter(Object[] args) {
return new ArgPreparedStatementSetter(args); return new ArgumentPreparedStatementSetter(args);
} }
/** /**
* Create a new ArgTypePreparedStatementSetter using the args and argTypes passed in. * Create a new arg-type-based PreparedStatementSetter using the args and types passed in.
* This method allows the creation to be overridden by sub-classes. * <p>By default, we'll create an {@link ArgumentTypePreparedStatementSetter}.
* This method allows for the creation to be overridden by subclasses.
* @param args object array with arguments * @param args object array with arguments
* @param argTypes int array of SQLTypes for arguments * @param argTypes int array of SQLTypes for the associated arguments
* @return the new PreparedStatementSetter * @return the new PreparedStatementSetter to use
*/ */
protected PreparedStatementSetter newArgTypePreparedStatementSetter(Object[] args, int[] argTypes) { protected PreparedStatementSetter newArgTypePreparedStatementSetter(Object[] args, int[] argTypes) {
return new ArgTypePreparedStatementSetter(args, argTypes); return new ArgumentTypePreparedStatementSetter(args, argTypes);
} }
/** /**