1 16 17 package org.springframework.jdbc.core.namedparam; 18 19 import java.util.List ; 20 import java.util.Map ; 21 22 import javax.sql.DataSource ; 23 24 import org.springframework.dao.DataAccessException; 25 import org.springframework.dao.support.DataAccessUtils; 26 import org.springframework.jdbc.core.ColumnMapRowMapper; 27 import org.springframework.jdbc.core.JdbcOperations; 28 import org.springframework.jdbc.core.JdbcTemplate; 29 import org.springframework.jdbc.core.PreparedStatementCreatorFactory; 30 import org.springframework.jdbc.core.RowCallbackHandler; 31 import org.springframework.jdbc.core.RowMapper; 32 import org.springframework.jdbc.core.RowMapperResultSetExtractor; 33 import org.springframework.jdbc.core.SingleColumnRowMapper; 34 import org.springframework.jdbc.core.SqlRowSetResultSetExtractor; 35 import org.springframework.jdbc.support.KeyHolder; 36 import org.springframework.jdbc.support.rowset.SqlRowSet; 37 import org.springframework.util.Assert; 38 39 58 public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations { 59 60 63 private final JdbcOperations classicJdbcTemplate; 64 65 66 71 public NamedParameterJdbcTemplate(DataSource dataSource) { 72 Assert.notNull(dataSource, "The [dataSource] argument cannot be null."); 73 this.classicJdbcTemplate = new JdbcTemplate(dataSource); 74 } 75 76 81 public NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate) { 82 Assert.notNull(classicJdbcTemplate, "The [classicJdbcTemplate] argument cannot be null."); 83 this.classicJdbcTemplate = classicJdbcTemplate; 84 } 85 86 90 public JdbcOperations getJdbcOperations() { 91 return this.classicJdbcTemplate; 92 } 93 94 95 99 public void query(String sql, SqlParameterSource paramSource, RowCallbackHandler rch) 100 throws DataAccessException { 101 102 ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); 103 Object [] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); 104 int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); 105 String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); 106 getJdbcOperations().query(sqlToUse, args, argTypes, rch); 107 } 108 109 public void query(String sql, Map paramMap, RowCallbackHandler rch) throws DataAccessException { 110 query(sql, new MapSqlParameterSource(paramMap), rch); 111 } 112 113 public List query(String sql, SqlParameterSource paramSource, RowMapper rowMapper) 114 throws DataAccessException { 115 116 ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); 117 Object [] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); 118 int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); 119 String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); 120 return (List ) getJdbcOperations().query( 121 sqlToUse, args, argTypes, new RowMapperResultSetExtractor(rowMapper)); 122 } 123 124 public List query(String sql, Map paramMap, RowMapper rowMapper) throws DataAccessException { 125 return query(sql, new MapSqlParameterSource(paramMap), rowMapper); 126 } 127 128 public Object queryForObject(String sql, SqlParameterSource paramSource, RowMapper rowMapper) 129 throws DataAccessException { 130 131 ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); 132 Object [] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); 133 int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); 134 String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); 135 List results = (List ) getJdbcOperations().query( 136 sqlToUse, args, argTypes, new RowMapperResultSetExtractor(rowMapper, 1)); 137 return DataAccessUtils.requiredSingleResult(results); 138 } 139 140 public Object queryForObject(String sql, Map paramMap, RowMapper rowMapper) throws DataAccessException { 141 return queryForObject(sql, new MapSqlParameterSource(paramMap), rowMapper); 142 } 143 144 public Object queryForObject(String sql, SqlParameterSource paramSource, Class requiredType) 145 throws DataAccessException { 146 return queryForObject(sql, paramSource, new SingleColumnRowMapper(requiredType)); 147 } 148 149 public Object queryForObject(String sql, Map paramMap, Class requiredType) throws DataAccessException { 150 return queryForObject(sql, paramMap, new SingleColumnRowMapper(requiredType)); 151 } 152 153 public Map queryForMap(String sql, SqlParameterSource paramSource) throws DataAccessException { 154 return (Map ) queryForObject(sql, paramSource, new ColumnMapRowMapper()); 155 } 156 157 public Map queryForMap(String sql, Map paramMap) throws DataAccessException { 158 return (Map ) queryForObject(sql, paramMap, new ColumnMapRowMapper()); 159 } 160 161 public long queryForLong(String sql, SqlParameterSource paramSource) throws DataAccessException { 162 Number number = (Number ) queryForObject(sql, paramSource, Number .class); 163 return (number != null ? number.longValue() : 0); 164 } 165 166 public long queryForLong(String sql, Map paramMap) throws DataAccessException { 167 return queryForLong(sql, new MapSqlParameterSource(paramMap)); 168 } 169 170 public int queryForInt(String sql, SqlParameterSource paramSource) throws DataAccessException { 171 Number number = (Number ) queryForObject(sql, paramSource, Number .class); 172 return (number != null ? number.intValue() : 0); 173 } 174 175 public int queryForInt(String sql, Map paramMap) throws DataAccessException { 176 return queryForInt(sql, new MapSqlParameterSource(paramMap)); 177 } 178 179 public List queryForList(String sql, SqlParameterSource paramSource, Class elementType) 180 throws DataAccessException { 181 return query(sql, paramSource, new SingleColumnRowMapper(elementType)); 182 } 183 184 public List queryForList(String sql, Map paramMap, Class elementType) throws DataAccessException { 185 return queryForList(sql, new MapSqlParameterSource(paramMap), elementType); 186 } 187 188 public List queryForList(String sql, SqlParameterSource paramSource) throws DataAccessException { 189 return query(sql, paramSource, new ColumnMapRowMapper()); 190 } 191 192 public List queryForList(String sql, Map paramMap) throws DataAccessException { 193 return queryForList(sql, new MapSqlParameterSource(paramMap)); 194 } 195 196 public SqlRowSet queryForRowSet(String sql, SqlParameterSource paramSource) throws DataAccessException { 197 ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); 198 Object [] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); 199 int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); 200 String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); 201 return (SqlRowSet) getJdbcOperations().query(sqlToUse, args, argTypes, new SqlRowSetResultSetExtractor()); 202 } 203 204 public SqlRowSet queryForRowSet(String sql, Map paramMap) throws DataAccessException { 205 return queryForRowSet(sql, new MapSqlParameterSource(paramMap)); 206 } 207 208 209 213 public int update(String sql, SqlParameterSource paramSource) throws DataAccessException { 214 ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); 215 Object [] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); 216 int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); 217 String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); 218 return getJdbcOperations().update(sqlToUse, args, argTypes); 219 } 220 221 public int update(String sql, Map paramMap) throws DataAccessException { 222 return update(sql, new MapSqlParameterSource(paramMap)); 223 } 224 225 public int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder) 226 throws DataAccessException { 227 228 return update(sql, paramSource, generatedKeyHolder, null); 229 } 230 231 public int update( 232 String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String [] keyColumnNames) 233 throws DataAccessException { 234 235 ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql); 236 Object [] args = NamedParameterUtils.buildValueArray(parsedSql, paramSource); 237 int[] argTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, paramSource); 238 String sqlToUse = NamedParameterUtils.substituteNamedParameters(sql, paramSource); 239 PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, argTypes); 240 if (keyColumnNames != null) { 241 pscf.setGeneratedKeysColumnNames(keyColumnNames); 242 } 243 else { 244 pscf.setReturnGeneratedKeys(true); 245 } 246 return getJdbcOperations().update(pscf.newPreparedStatementCreator(args), generatedKeyHolder); 247 } 248 249 } 250 | Popular Tags |