From: Juergen H. <jho...@us...> - 2006-04-20 10:32:18
|
Update of /cvsroot/springframework/spring/src/org/springframework/jdbc/core/namedparam In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17652/src/org/springframework/jdbc/core/namedparam Modified Files: NamedParameterJdbcOperations.java NamedParameterJdbcTemplate.java Log Message: refined KeyHolder-based operations Index: NamedParameterJdbcOperations.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/jdbc/core/namedparam/NamedParameterJdbcOperations.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** NamedParameterJdbcOperations.java 10 Apr 2006 14:40:12 -0000 1.2 --- NamedParameterJdbcOperations.java 20 Apr 2006 10:32:10 -0000 1.3 *************** *** 51,54 **** --- 51,58 ---- + //------------------------------------------------------------------------- + // Query operations + //------------------------------------------------------------------------- + /** * Query given SQL to create a prepared statement from SQL and a list of *************** *** 377,383 **** SqlRowSet queryForRowSet(String sql, Map paramMap) throws DataAccessException; /** * Issue an update via a prepared statement, binding the given arguments. ! * @param sql SQL, containing bind parameters * @param paramSource container of arguments and SQL types to bind to the query * @return the number of rows affected --- 381,392 ---- SqlRowSet queryForRowSet(String sql, Map paramMap) throws DataAccessException; + + //------------------------------------------------------------------------- + // Update operations + //------------------------------------------------------------------------- + /** * Issue an update via a prepared statement, binding the given arguments. ! * @param sql SQL containing named parameters * @param paramSource container of arguments and SQL types to bind to the query * @return the number of rows affected *************** *** 388,392 **** /** * Issue an update via a prepared statement, binding the given arguments. ! * @param sql SQL, containing bind parameters * @param paramMap map of parameters to bind to the query * (leaving it to the PreparedStatement to guess the corresponding SQL type) --- 397,401 ---- /** * Issue an update via a prepared statement, binding the given arguments. ! * @param sql SQL containing named parameters * @param paramMap map of parameters to bind to the query * (leaving it to the PreparedStatement to guess the corresponding SQL type) *************** *** 396,410 **** int update(String sql, Map paramMap) throws DataAccessException; ! /** ! * Issue an update via a prepared statement, binding the given arguments. ! * @param sql SQL, containing bind parameters ! * @param paramSource container of arguments and SQL types to bind to the query ! * @param generatedKeyHolder KeyHolder that will hold the generated keys ! * @param keyColumnNames names of the columns that will have keys generated for them ! * @return the number of rows affected ! * @throws org.springframework.dao.DataAccessException if there is any problem issuing the update ! */ ! int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames) throws DataAccessException; } --- 405,437 ---- int update(String sql, Map paramMap) throws DataAccessException; ! /** ! * Issue an update via a prepared statement, binding the given arguments, ! * returning generated keys. ! * @param sql SQL containing named parameters ! * @param paramSource container of arguments and SQL types to bind to the query ! * @param generatedKeyHolder KeyHolder that will hold the generated keys ! * @return the number of rows affected ! * @throws org.springframework.dao.DataAccessException if there is any problem issuing the update ! * @see SimpleSqlParameterSource ! * @see org.springframework.jdbc.support.GeneratedKeyHolder ! */ ! int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder) ! throws DataAccessException; + /** + * Issue an update via a prepared statement, binding the given arguments, + * returning generated keys. + * @param sql SQL containing named parameters + * @param paramSource container of arguments and SQL types to bind to the query + * @param generatedKeyHolder KeyHolder that will hold the generated keys + * @param keyColumnNames names of the columns that will have keys generated for them + * @return the number of rows affected + * @throws org.springframework.dao.DataAccessException if there is any problem issuing the update + * @see SimpleSqlParameterSource + * @see org.springframework.jdbc.support.GeneratedKeyHolder + */ + int update( + String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames) + throws DataAccessException; } Index: NamedParameterJdbcTemplate.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** NamedParameterJdbcTemplate.java 18 Apr 2006 10:04:24 -0000 1.3 --- NamedParameterJdbcTemplate.java 20 Apr 2006 10:32:10 -0000 1.4 *************** *** 24,30 **** import org.springframework.dao.DataAccessException; import org.springframework.dao.support.DataAccessUtils; ! import org.springframework.jdbc.core.*; ! import org.springframework.jdbc.support.rowset.SqlRowSet; import org.springframework.jdbc.support.KeyHolder; import org.springframework.util.Assert; --- 24,38 ---- import org.springframework.dao.DataAccessException; import org.springframework.dao.support.DataAccessUtils; ! import org.springframework.jdbc.core.ColumnMapRowMapper; ! import org.springframework.jdbc.core.JdbcOperations; ! import org.springframework.jdbc.core.JdbcTemplate; ! import org.springframework.jdbc.core.PreparedStatementCreatorFactory; ! import org.springframework.jdbc.core.RowCallbackHandler; ! import org.springframework.jdbc.core.RowMapper; ! import org.springframework.jdbc.core.RowMapperResultSetExtractor; ! import org.springframework.jdbc.core.SingleColumnRowMapper; ! import org.springframework.jdbc.core.SqlRowSetResultSetExtractor; import org.springframework.jdbc.support.KeyHolder; + import org.springframework.jdbc.support.rowset.SqlRowSet; import org.springframework.util.Assert; *************** *** 50,217 **** public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations { ! /** The JdbcTemplate we are wrapping */ ! private final JdbcOperations classicJdbcTemplate; ! /** ! * Create a new NamedParameterJdbcTemplate for the given {@link DataSource}. ! * <p>Creates a classic Spring {@link org.springframework.jdbc.core.JdbcTemplate} and wraps it. ! * @param dataSource the JDBC {@link DataSource} to access * @throws IllegalArgumentException if the given {@link DataSource} is <code>null</code> ! */ ! public NamedParameterJdbcTemplate(DataSource dataSource) { ! Assert.notNull(dataSource, "The [dataSource] argument cannot be null."); ! this.classicJdbcTemplate = new JdbcTemplate(dataSource); ! } ! /** ! * Create a new SimpleJdbcTemplate for the given classic Spring {@link org.springframework.jdbc.core.JdbcTemplate}. ! * @param classicJdbcTemplate the classic Spring {@link org.springframework.jdbc.core.JdbcTemplate} to wrap * @throws IllegalArgumentException if the given {@link org.springframework.jdbc.core.JdbcTemplate} is <code>null</code> ! */ ! public NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate) { ! Assert.notNull(classicJdbcTemplate, "The [classicJdbcTemplate] argument cannot be null."); ! this.classicJdbcTemplate = classicJdbcTemplate; ! } ! /** ! * Expose the classic Spring JdbcTemplate to allow invocation of ! * less commonly used methods. ! */ ! public JdbcOperations getJdbcOperations() { ! return this.classicJdbcTemplate; ! } ! public void query(String sql, SqlParameterSource paramSource, RowCallbackHandler rch) ! throws DataAccessException { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! getJdbcOperations().query(sqlToUse, args, argTypes, rch); ! } ! public void query(String sql, Map paramMap, RowCallbackHandler rch) throws DataAccessException { ! query(sql, new SimpleSqlParameterSource(paramMap), rch); ! } ! public List query(String sql, SqlParameterSource paramSource, RowMapper rowMapper) ! throws DataAccessException { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! return (List) getJdbcOperations().query( ! sqlToUse, args, argTypes, new RowMapperResultSetExtractor(rowMapper)); ! } ! public List query(String sql, Map paramMap, RowMapper rowMapper) throws DataAccessException { ! return query(sql, new SimpleSqlParameterSource(paramMap), rowMapper); ! } ! public Object queryForObject(String sql, SqlParameterSource paramSource, RowMapper rowMapper) ! throws DataAccessException { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! List results = (List) getJdbcOperations().query( ! sqlToUse, args, argTypes, new RowMapperResultSetExtractor(rowMapper, 1)); ! return DataAccessUtils.requiredUniqueResult(results); ! } ! public Object queryForObject(String sql, Map paramMap, RowMapper rowMapper) throws DataAccessException { ! return queryForObject(sql, new SimpleSqlParameterSource(paramMap), rowMapper); ! } ! public Object queryForObject(String sql, SqlParameterSource paramSource, Class requiredType) ! throws DataAccessException { ! return queryForObject(sql, paramSource, new SingleColumnRowMapper(requiredType)); ! } ! public Object queryForObject(String sql, Map paramMap, Class requiredType) throws DataAccessException { ! return queryForObject(sql, paramMap, new SingleColumnRowMapper(requiredType)); ! } ! public Map queryForMap(String sql, SqlParameterSource paramSource) throws DataAccessException { ! return (Map) queryForObject(sql, paramSource, new ColumnMapRowMapper()); ! } ! public Map queryForMap(String sql, Map paramMap) throws DataAccessException { ! return (Map) queryForObject(sql, paramMap, new ColumnMapRowMapper()); ! } ! public long queryForLong(String sql, SqlParameterSource paramSource) throws DataAccessException { ! Number number = (Number) queryForObject(sql, paramSource, Number.class); ! return (number != null ? number.longValue() : 0); ! } ! public long queryForLong(String sql, Map paramMap) throws DataAccessException { ! return queryForLong(sql, new SimpleSqlParameterSource(paramMap)); ! } ! public int queryForInt(String sql, SqlParameterSource paramSource) throws DataAccessException { ! Number number = (Number) queryForObject(sql, paramSource, Number.class); ! return (number != null ? number.intValue() : 0); ! } ! public int queryForInt(String sql, Map paramMap) throws DataAccessException { ! return queryForInt(sql, new SimpleSqlParameterSource(paramMap)); ! } ! public List queryForList(String sql, SqlParameterSource paramSource, Class elementType) ! throws DataAccessException { ! return query(sql, paramSource, new SingleColumnRowMapper(elementType)); ! } ! public List queryForList(String sql, Map paramMap, Class elementType) throws DataAccessException { ! return queryForList(sql, new SimpleSqlParameterSource(paramMap), elementType); ! } ! public List queryForList(String sql, SqlParameterSource paramSource) throws DataAccessException { ! return query(sql, paramSource, new ColumnMapRowMapper()); ! } ! public List queryForList(String sql, Map paramMap) throws DataAccessException { ! return queryForList(sql, new SimpleSqlParameterSource(paramMap)); ! } ! public SqlRowSet queryForRowSet(String sql, SqlParameterSource paramSource) throws DataAccessException { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! return (SqlRowSet) getJdbcOperations().query(sqlToUse, args, argTypes, new SqlRowSetResultSetExtractor()); ! } ! public SqlRowSet queryForRowSet(String sql, Map paramMap) throws DataAccessException { ! return queryForRowSet(sql, new SimpleSqlParameterSource(paramMap)); ! } ! public int update(String sql, SqlParameterSource paramSource) throws DataAccessException { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! return getJdbcOperations().update(sqlToUse, args, argTypes); ! } ! public int update(String sql, Map paramMap) throws DataAccessException { ! return update(sql, new SimpleSqlParameterSource(paramMap)); ! } ! public int update(String sql, SqlParameterSource paramSource, KeyHolder keyHolder, String[] keyColumnNames) { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, argTypes); ! pscf.setReturnGeneratedKeys(true); ! if (keyColumnNames != null) { ! pscf.setGeneratedKeysColumnNames(keyColumnNames); ! } ! return getJdbcOperations().update(pscf.newPreparedStatementCreator(args), keyHolder); ! } } --- 58,250 ---- public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations { ! /** ! * The JdbcTemplate we are wrapping ! */ ! private final JdbcOperations classicJdbcTemplate; ! /** ! * Create a new NamedParameterJdbcTemplate for the given {@link DataSource}. ! * <p>Creates a classic Spring {@link org.springframework.jdbc.core.JdbcTemplate} and wraps it. ! * @param dataSource the JDBC {@link DataSource} to access * @throws IllegalArgumentException if the given {@link DataSource} is <code>null</code> ! */ ! public NamedParameterJdbcTemplate(DataSource dataSource) { ! Assert.notNull(dataSource, "The [dataSource] argument cannot be null."); ! this.classicJdbcTemplate = new JdbcTemplate(dataSource); ! } ! /** ! * Create a new SimpleJdbcTemplate for the given classic Spring {@link org.springframework.jdbc.core.JdbcTemplate}. ! * @param classicJdbcTemplate the classic Spring {@link org.springframework.jdbc.core.JdbcTemplate} to wrap * @throws IllegalArgumentException if the given {@link org.springframework.jdbc.core.JdbcTemplate} is <code>null</code> ! */ ! public NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate) { ! Assert.notNull(classicJdbcTemplate, "The [classicJdbcTemplate] argument cannot be null."); ! this.classicJdbcTemplate = classicJdbcTemplate; ! } ! /** ! * Expose the classic Spring JdbcTemplate to allow invocation of ! * less commonly used methods. ! */ ! public JdbcOperations getJdbcOperations() { ! return this.classicJdbcTemplate; ! } ! //------------------------------------------------------------------------- ! // Query operations ! //------------------------------------------------------------------------- ! public void query(String sql, SqlParameterSource paramSource, RowCallbackHandler rch) ! throws DataAccessException { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! getJdbcOperations().query(sqlToUse, args, argTypes, rch); ! } ! public void query(String sql, Map paramMap, RowCallbackHandler rch) throws DataAccessException { ! query(sql, new SimpleSqlParameterSource(paramMap), rch); ! } ! public List query(String sql, SqlParameterSource paramSource, RowMapper rowMapper) ! throws DataAccessException { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! return (List) getJdbcOperations().query( ! sqlToUse, args, argTypes, new RowMapperResultSetExtractor(rowMapper)); ! } ! public List query(String sql, Map paramMap, RowMapper rowMapper) throws DataAccessException { ! return query(sql, new SimpleSqlParameterSource(paramMap), rowMapper); ! } ! public Object queryForObject(String sql, SqlParameterSource paramSource, RowMapper rowMapper) ! throws DataAccessException { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! List results = (List) getJdbcOperations().query( ! sqlToUse, args, argTypes, new RowMapperResultSetExtractor(rowMapper, 1)); ! return DataAccessUtils.requiredUniqueResult(results); ! } ! public Object queryForObject(String sql, Map paramMap, RowMapper rowMapper) throws DataAccessException { ! return queryForObject(sql, new SimpleSqlParameterSource(paramMap), rowMapper); ! } ! public Object queryForObject(String sql, SqlParameterSource paramSource, Class requiredType) ! throws DataAccessException { ! return queryForObject(sql, paramSource, new SingleColumnRowMapper(requiredType)); ! } ! public Object queryForObject(String sql, Map paramMap, Class requiredType) throws DataAccessException { ! return queryForObject(sql, paramMap, new SingleColumnRowMapper(requiredType)); ! } ! public Map queryForMap(String sql, SqlParameterSource paramSource) throws DataAccessException { ! return (Map) queryForObject(sql, paramSource, new ColumnMapRowMapper()); ! } ! public Map queryForMap(String sql, Map paramMap) throws DataAccessException { ! return (Map) queryForObject(sql, paramMap, new ColumnMapRowMapper()); ! } ! public long queryForLong(String sql, SqlParameterSource paramSource) throws DataAccessException { ! Number number = (Number) queryForObject(sql, paramSource, Number.class); ! return (number != null ? number.longValue() : 0); ! } ! public long queryForLong(String sql, Map paramMap) throws DataAccessException { ! return queryForLong(sql, new SimpleSqlParameterSource(paramMap)); ! } ! public int queryForInt(String sql, SqlParameterSource paramSource) throws DataAccessException { ! Number number = (Number) queryForObject(sql, paramSource, Number.class); ! return (number != null ? number.intValue() : 0); ! } ! public int queryForInt(String sql, Map paramMap) throws DataAccessException { ! return queryForInt(sql, new SimpleSqlParameterSource(paramMap)); ! } ! public List queryForList(String sql, SqlParameterSource paramSource, Class elementType) ! throws DataAccessException { ! return query(sql, paramSource, new SingleColumnRowMapper(elementType)); ! } ! public List queryForList(String sql, Map paramMap, Class elementType) throws DataAccessException { ! return queryForList(sql, new SimpleSqlParameterSource(paramMap), elementType); ! } ! public List queryForList(String sql, SqlParameterSource paramSource) throws DataAccessException { ! return query(sql, paramSource, new ColumnMapRowMapper()); ! } ! public List queryForList(String sql, Map paramMap) throws DataAccessException { ! return queryForList(sql, new SimpleSqlParameterSource(paramMap)); ! } ! public SqlRowSet queryForRowSet(String sql, SqlParameterSource paramSource) throws DataAccessException { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! return (SqlRowSet) getJdbcOperations().query(sqlToUse, args, argTypes, new SqlRowSetResultSetExtractor()); ! } ! ! public SqlRowSet queryForRowSet(String sql, Map paramMap) throws DataAccessException { ! return queryForRowSet(sql, new SimpleSqlParameterSource(paramMap)); ! } ! ! ! //------------------------------------------------------------------------- ! // Update operations ! //------------------------------------------------------------------------- ! ! public int update(String sql, SqlParameterSource paramSource) throws DataAccessException { ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! return getJdbcOperations().update(sqlToUse, args, argTypes); ! } ! ! public int update(String sql, Map paramMap) throws DataAccessException { ! return update(sql, new SimpleSqlParameterSource(paramMap)); ! } ! ! public int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder) ! throws DataAccessException { ! ! return update(sql, paramSource, generatedKeyHolder, null); ! } ! ! public int update( ! String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames) ! throws DataAccessException { ! ! ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); ! Object[] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); ! int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); ! String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); ! PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, argTypes); ! if (keyColumnNames != null) { ! pscf.setGeneratedKeysColumnNames(keyColumnNames); ! } ! else { ! pscf.setReturnGeneratedKeys(true); ! } ! return getJdbcOperations().update(pscf.newPreparedStatementCreator(args), generatedKeyHolder); ! } } |