From: Juergen H. <jho...@us...> - 2006-04-20 10:12:19
|
Update of /cvsroot/springframework/spring/src/org/springframework/jdbc/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1828/src/org/springframework/jdbc/core Modified Files: JdbcTemplate.java Added Files: ArgPreparedStatementSetter.java ArgTypePreparedStatementSetter.java Log Message: logging of ignored SQLWarnings, including SQL state and error code in the log output; factored out PreparedStatementSetter impls to package-protected classes --- NEW FILE: ArgTypePreparedStatementSetter.java --- /* * Copyright 2002-2006 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. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.jdbc.core; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; import java.util.Collection; import java.util.Iterator; import org.springframework.dao.InvalidDataAccessApiUsageException; /** * Simple adapter for PreparedStatementSetter that applies * given arrays of arguments and JDBC argument types. * * @author Juergen Hoeller */ class ArgTypePreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer { private final Object[] args; private final int[] argTypes; /** * Create a new ArgTypePreparedStatementSetter for the given arguments. * @param args * @param argTypes */ public ArgTypePreparedStatementSetter(Object[] args, int[] argTypes) { if ((args != null && argTypes == null) || (args == null && argTypes != null) || (args != null && args.length != argTypes.length)) { throw new InvalidDataAccessApiUsageException("args and argTypes parameters must match"); } this.args = args; this.argTypes = argTypes; } public void setValues(PreparedStatement ps) throws SQLException { int argIndx = 1; if (this.args != null) { for (int i = 0; i < this.args.length; i++) { Object arg = this.args[i]; if (arg instanceof Collection && this.argTypes[i] != Types.ARRAY) { Collection entries = (Collection) arg; for (Iterator it = entries.iterator(); it.hasNext();) { Object entry = it.next(); StatementCreatorUtils.setParameterValue(ps, argIndx++, this.argTypes[i], null, entry); } } else { StatementCreatorUtils.setParameterValue(ps, argIndx++, this.argTypes[i], null, arg); } } } } public void cleanupParameters() { StatementCreatorUtils.cleanupParameters(this.args); } } --- NEW FILE: ArgPreparedStatementSetter.java --- /* * Copyright 2002-2006 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. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.jdbc.core; import java.sql.PreparedStatement; import java.sql.SQLException; /** * Simple adapter for PreparedStatementSetter that applies * a given array of arguments. * * @author Juergen Hoeller */ class ArgPreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer { private final Object[] args; public ArgPreparedStatementSetter(Object[] args) { this.args = args; } public void setValues(PreparedStatement ps) throws SQLException { if (this.args != null) { for (int i = 0; i < this.args.length; i++) { StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, null, this.args[i]); } } } public void cleanupParameters() { StatementCreatorUtils.cleanupParameters(this.args); } } Index: JdbcTemplate.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/jdbc/core/JdbcTemplate.java,v retrieving revision 1.117 retrieving revision 1.118 diff -C2 -d -r1.117 -r1.118 *** JdbcTemplate.java 10 Apr 2006 04:57:05 -0000 1.117 --- JdbcTemplate.java 20 Apr 2006 10:12:09 -0000 1.118 *************** *** 21,26 **** import java.lang.reflect.Method; import java.lang.reflect.Proxy; ! import java.sql.*; ! import java.util.*; import javax.sql.DataSource; --- 21,34 ---- import java.lang.reflect.Method; import java.lang.reflect.Proxy; ! import java.sql.CallableStatement; ! import java.sql.Connection; ! import java.sql.PreparedStatement; ! import java.sql.ResultSet; ! import java.sql.SQLException; ! import java.sql.SQLWarning; ! import java.sql.Statement; ! import java.util.HashMap; ! import java.util.List; ! import java.util.Map; import javax.sql.DataSource; *************** *** 37,40 **** --- 45,49 ---- import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor; import org.springframework.jdbc.support.rowset.SqlRowSet; + import org.springframework.util.Assert; /** *************** *** 309,315 **** public Object query(final String sql, final ResultSetExtractor rse) throws DataAccessException { ! if (sql == null) { ! throw new InvalidDataAccessApiUsageException("SQL must not be null"); ! } if (logger.isDebugEnabled()) { logger.debug("Executing SQL query [" + sql + "]"); --- 318,322 ---- public Object query(final String sql, final ResultSetExtractor rse) throws DataAccessException { ! Assert.notNull(sql, "SQL must not be null"); if (logger.isDebugEnabled()) { logger.debug("Executing SQL query [" + sql + "]"); *************** *** 381,387 **** public int update(final String sql) throws DataAccessException { ! if (sql == null) { ! throw new InvalidDataAccessApiUsageException("SQL must not be null"); ! } if (logger.isDebugEnabled()) { logger.debug("Executing SQL update [" + sql + "]"); --- 388,392 ---- public int update(final String sql) throws DataAccessException { ! Assert.notNull(sql, "SQL must not be null"); if (logger.isDebugEnabled()) { logger.debug("Executing SQL update [" + sql + "]"); *************** *** 403,409 **** public int[] batchUpdate(final String[] sql) throws DataAccessException { ! if (sql == null) { ! throw new InvalidDataAccessApiUsageException("SQL must not be null"); ! } if (logger.isDebugEnabled()) { logger.debug("Executing SQL batch update of " + sql.length + " statements"); --- 408,412 ---- public int[] batchUpdate(final String[] sql) throws DataAccessException { ! Assert.notNull(sql, "SQL must not be null"); if (logger.isDebugEnabled()) { logger.debug("Executing SQL batch update of " + sql.length + " statements"); *************** *** 542,555 **** } ! public Object query(String sql, PreparedStatementSetter pss, final ResultSetExtractor rse) ! throws DataAccessException { ! if (sql == null) { ! throw new InvalidDataAccessApiUsageException("SQL may not be null"); ! } return query(new SimplePreparedStatementCreator(sql), pss, rse); } ! public Object query(String sql, Object[] args, int[] argTypes, ResultSetExtractor rse) ! throws DataAccessException { return query(sql, new ArgTypePreparedStatementSetter(args, argTypes), rse); } --- 545,553 ---- } ! public Object query(String sql, PreparedStatementSetter pss, ResultSetExtractor rse) throws DataAccessException { return query(new SimplePreparedStatementCreator(sql), pss, rse); } ! public Object query(String sql, Object[] args, int[] argTypes, ResultSetExtractor rse) throws DataAccessException { return query(sql, new ArgTypePreparedStatementSetter(args, argTypes), rse); } *************** *** 563,598 **** } ! public void query(String sql, PreparedStatementSetter pss, final RowCallbackHandler rch) ! throws DataAccessException { query(sql, pss, new RowCallbackHandlerResultSetExtractor(rch)); } ! public void query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch) ! throws DataAccessException { query(sql, new ArgTypePreparedStatementSetter(args, argTypes), rch); } ! public void query(String sql, Object[] args, RowCallbackHandler rch) ! throws DataAccessException { query(sql, new ArgPreparedStatementSetter(args), rch); } ! public List query(PreparedStatementCreator psc, RowMapper rowMapper) ! throws DataAccessException { return (List) query(psc, new RowMapperResultSetExtractor(rowMapper)); } ! public List query(String sql, PreparedStatementSetter pss, RowMapper rowMapper) ! throws DataAccessException { return (List) query(sql, pss, new RowMapperResultSetExtractor(rowMapper)); } ! public List query(String sql, Object[] args, int[] argTypes, RowMapper rowMapper) ! throws DataAccessException { return (List) query(sql, args, argTypes, new RowMapperResultSetExtractor(rowMapper)); } ! public List query(String sql, Object[] args, RowMapper rowMapper) ! throws DataAccessException { return (List) query(sql, args, new RowMapperResultSetExtractor(rowMapper)); } --- 561,589 ---- } ! public void query(String sql, PreparedStatementSetter pss, RowCallbackHandler rch) throws DataAccessException { query(sql, pss, new RowCallbackHandlerResultSetExtractor(rch)); } ! public void query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch) throws DataAccessException { query(sql, new ArgTypePreparedStatementSetter(args, argTypes), rch); } ! public void query(String sql, Object[] args, RowCallbackHandler rch) throws DataAccessException { query(sql, new ArgPreparedStatementSetter(args), rch); } ! public List query(PreparedStatementCreator psc, RowMapper rowMapper) throws DataAccessException { return (List) query(psc, new RowMapperResultSetExtractor(rowMapper)); } ! public List query(String sql, PreparedStatementSetter pss, RowMapper rowMapper) throws DataAccessException { return (List) query(sql, pss, new RowMapperResultSetExtractor(rowMapper)); } ! public List query(String sql, Object[] args, int[] argTypes, RowMapper rowMapper) throws DataAccessException { return (List) query(sql, args, argTypes, new RowMapperResultSetExtractor(rowMapper)); } ! public List query(String sql, Object[] args, RowMapper rowMapper) throws DataAccessException { return (List) query(sql, args, new RowMapperResultSetExtractor(rowMapper)); } *************** *** 600,603 **** --- 591,595 ---- public Object queryForObject(String sql, Object[] args, int[] argTypes, RowMapper rowMapper) throws DataAccessException { + List results = (List) query(sql, args, argTypes, new RowMapperResultSetExtractor(rowMapper, 1)); return DataAccessUtils.requiredUniqueResult(results); *************** *** 611,614 **** --- 603,607 ---- public Object queryForObject(String sql, Object[] args, int[] argTypes, Class requiredType) throws DataAccessException { + return queryForObject(sql, args, argTypes, getSingleColumnRowMapper(requiredType)); } *************** *** 646,655 **** } ! public List queryForList(String sql, Object[] args, int[] argTypes, Class elementType) ! throws DataAccessException { return query(sql, args, argTypes, getSingleColumnRowMapper(elementType)); } ! public List queryForList(String sql, final Object[] args, Class elementType) throws DataAccessException { return query(sql, args, getSingleColumnRowMapper(elementType)); } --- 639,647 ---- } ! public List queryForList(String sql, Object[] args, int[] argTypes, Class elementType) throws DataAccessException { return query(sql, args, argTypes, getSingleColumnRowMapper(elementType)); } ! public List queryForList(String sql, Object[] args, Class elementType) throws DataAccessException { return query(sql, args, getSingleColumnRowMapper(elementType)); } *************** *** 659,671 **** } ! public List queryForList(String sql, final Object[] args) throws DataAccessException { return query(sql, args, getColumnMapRowMapper()); } ! public SqlRowSet queryForRowSet(String sql, final Object[] args, int[] argTypes) throws DataAccessException { return (SqlRowSet) query(sql, args, argTypes, new SqlRowSetResultSetExtractor()); } ! public SqlRowSet queryForRowSet(String sql, final Object[] args) throws DataAccessException { return (SqlRowSet) query(sql, args, new SqlRowSetResultSetExtractor()); } --- 651,663 ---- } ! public List queryForList(String sql, Object[] args) throws DataAccessException { return query(sql, args, getColumnMapRowMapper()); } ! public SqlRowSet queryForRowSet(String sql, Object[] args, int[] argTypes) throws DataAccessException { return (SqlRowSet) query(sql, args, argTypes, new SqlRowSetResultSetExtractor()); } ! public SqlRowSet queryForRowSet(String sql, Object[] args) throws DataAccessException { return (SqlRowSet) query(sql, args, new SqlRowSetResultSetExtractor()); } *************** *** 736,748 **** } ! public int update(String sql, final PreparedStatementSetter pss) throws DataAccessException { return update(new SimplePreparedStatementCreator(sql), pss); } ! public int update(String sql, final Object[] args, final int[] argTypes) throws DataAccessException { return update(sql, new ArgTypePreparedStatementSetter(args, argTypes)); } ! public int update(String sql, final Object[] args) throws DataAccessException { return update(sql, new ArgPreparedStatementSetter(args)); } --- 728,740 ---- } ! public int update(String sql, PreparedStatementSetter pss) throws DataAccessException { return update(new SimplePreparedStatementCreator(sql), pss); } ! public int update(String sql, Object[] args, int[] argTypes) throws DataAccessException { return update(sql, new ArgTypePreparedStatementSetter(args, argTypes)); } ! public int update(String sql, Object[] args) throws DataAccessException { return update(sql, new ArgPreparedStatementSetter(args)); } *************** *** 834,838 **** } ! public Object execute(final String callString, CallableStatementCallback action) throws DataAccessException { return execute(new SimpleCallableStatementCreator(callString), action); } --- 826,830 ---- } ! public Object execute(String callString, CallableStatementCallback action) throws DataAccessException { return execute(new SimpleCallableStatementCreator(callString), action); } *************** *** 1023,1027 **** if (isIgnoreWarnings()) { if (logger.isWarnEnabled()) { ! logger.warn("SQLWarning ignored: " + warning); } } --- 1015,1020 ---- if (isIgnoreWarnings()) { if (logger.isWarnEnabled()) { ! logger.warn("SQLWarning ignored: SQL state '" + warning.getSQLState() + "', error code '" + ! warning.getErrorCode() + "', message [" + warning.getMessage() + "]"); } } *************** *** 1104,1113 **** * Simple adapter for PreparedStatementCreator, allowing to use a plain SQL statement. */ ! private static class SimplePreparedStatementCreator ! implements PreparedStatementCreator, SqlProvider { private final String sql; public SimplePreparedStatementCreator(String sql) { this.sql = sql; } --- 1097,1106 ---- * Simple adapter for PreparedStatementCreator, allowing to use a plain SQL statement. */ ! private static class SimplePreparedStatementCreator implements PreparedStatementCreator, SqlProvider { private final String sql; public SimplePreparedStatementCreator(String sql) { + Assert.notNull(sql, "SQL must not be null"); this.sql = sql; } *************** *** 1126,1135 **** * Simple adapter for CallableStatementCreator, allowing to use a plain SQL statement. */ ! private static class SimpleCallableStatementCreator ! implements CallableStatementCreator, SqlProvider { private final String callString; public SimpleCallableStatementCreator(String callString) { this.callString = callString; } --- 1119,1128 ---- * Simple adapter for CallableStatementCreator, allowing to use a plain SQL statement. */ ! private static class SimpleCallableStatementCreator implements CallableStatementCreator, SqlProvider { private final String callString; public SimpleCallableStatementCreator(String callString) { + Assert.notNull(callString, "Call string must not be null"); this.callString = callString; } *************** *** 1147,1220 **** /** - * Simple adapter for PreparedStatementSetter that applies - * a given array of arguments. - */ - private static class ArgPreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer { - - private final Object[] args; - - public ArgPreparedStatementSetter(Object[] args) { - this.args = args; - } - - public void setValues(PreparedStatement ps) throws SQLException { - if (this.args != null) { - for (int i = 0; i < this.args.length; i++) { - StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, null, this.args[i]); - } - } - } - - public void cleanupParameters() { - StatementCreatorUtils.cleanupParameters(this.args); - } - } - - - /** - * Simple adapter for PreparedStatementSetter that applies - * given arrays of arguments and JDBC argument types. - */ - private static class ArgTypePreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer { - - private final Object[] args; - - private final int[] argTypes; - - public ArgTypePreparedStatementSetter(Object[] args, int[] argTypes) { - if ((args != null && argTypes == null) || (args == null && argTypes != null) || - (args != null && args.length != argTypes.length)) { - throw new InvalidDataAccessApiUsageException("args and argTypes parameters must match"); - } - this.args = args; - this.argTypes = argTypes; - } - - public void setValues(PreparedStatement ps) throws SQLException { - int argIndx = 1; - if (this.args != null) { - for (int i = 0; i < this.args.length; i++) { - Object arg = this.args[i]; - if (arg instanceof Collection && this.argTypes[i] != Types.ARRAY) { - Collection entries = (Collection) arg; - for (Iterator it = entries.iterator(); it.hasNext();) { - Object entry = it.next(); - StatementCreatorUtils.setParameterValue(ps, argIndx++, this.argTypes[i], null, entry); - } - } - else { - StatementCreatorUtils.setParameterValue(ps, argIndx++, this.argTypes[i], null, arg); - } - } - } - } - - public void cleanupParameters() { - StatementCreatorUtils.cleanupParameters(this.args); - } - } - - - /** * Adapter to enable use of a RowCallbackHandler inside a ResultSetExtractor. * <p>Uses a regular ResultSet, so we have to be careful when using it: --- 1140,1143 ---- |