1 16 17 package org.springframework.jdbc.object; 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.RowMapper; 27 import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; 28 import org.springframework.jdbc.core.namedparam.NamedParameterUtils; 29 30 54 public abstract class SqlQuery extends SqlOperation { 55 56 57 private int rowsExpected = 0; 58 59 60 65 public SqlQuery() { 66 } 67 68 74 public SqlQuery(DataSource ds, String sql) { 75 setDataSource(ds); 76 setSql(sql); 77 } 78 79 80 85 public void setRowsExpected(int rowsExpected) { 86 this.rowsExpected = rowsExpected; 87 } 88 89 92 public int getRowsExpected() { 93 return rowsExpected; 94 } 95 96 97 108 public List execute(Object [] params, Map context) throws DataAccessException { 109 validateParameters(params); 110 RowMapper rowMapper = newRowMapper(params, context); 111 return getJdbcTemplate().query(newPreparedStatementCreator(params), rowMapper); 112 } 113 114 120 public List execute(Object [] params) throws DataAccessException { 121 return execute(params, null); 122 } 123 124 128 public List execute(Map context) throws DataAccessException { 129 return execute((Object []) null, context); 130 } 131 132 135 public List execute() throws DataAccessException { 136 return execute((Object []) null); 137 } 138 139 144 public List execute(int p1, Map context) throws DataAccessException { 145 return execute(new Object [] {new Integer (p1)}, context); 146 } 147 148 152 public List execute(int p1) throws DataAccessException { 153 return execute(p1, null); 154 } 155 156 162 public List execute(int p1, int p2, Map context) throws DataAccessException { 163 return execute(new Object [] {new Integer (p1), new Integer (p2)}, context); 164 } 165 166 171 public List execute(int p1, int p2) throws DataAccessException { 172 return execute(p1, p2, null); 173 } 174 175 180 public List execute(long p1, Map context) throws DataAccessException { 181 return execute(new Object [] {new Long (p1)}, context); 182 } 183 184 188 public List execute(long p1) throws DataAccessException { 189 return execute(p1, null); 190 } 191 192 197 public List execute(String p1, Map context) throws DataAccessException { 198 return execute(new Object [] {p1}, context); 199 } 200 201 205 public List execute(String p1) throws DataAccessException { 206 return execute(p1, null); 207 } 208 209 221 public List executeByNamedParam(Map paramMap, Map context) throws DataAccessException { 222 validateNamedParameters(paramMap); 223 Object [] parameters = NamedParameterUtils.buildValueArray(getSql(), paramMap); 224 RowMapper rowMapper = newRowMapper(parameters, context); 225 String sqlToUse = NamedParameterUtils.substituteNamedParameters(getSql(), new MapSqlParameterSource(paramMap)); 226 return getJdbcTemplate().query(newPreparedStatementCreator(sqlToUse, parameters), rowMapper); 227 } 228 229 235 public List executeByNamedParam(Map paramMap) throws DataAccessException { 236 return executeByNamedParam(paramMap, null); 237 } 238 239 240 248 public Object findObject(Object [] params, Map context) throws DataAccessException { 249 List results = execute(params, context); 250 return DataAccessUtils.singleResult(results); 251 } 252 253 256 public Object findObject(Object [] params) throws DataAccessException { 257 return findObject(params, null); 258 } 259 260 264 public Object findObject(int p1, Map context) throws DataAccessException { 265 return findObject(new Object [] {new Integer (p1)}, context); 266 } 267 268 271 public Object findObject(int p1) throws DataAccessException { 272 return findObject(p1, null); 273 } 274 275 279 public Object findObject(int p1, int p2, Map context) throws DataAccessException { 280 return findObject(new Object [] {new Integer (p1), new Integer (p2)}, context); 281 } 282 283 286 public Object findObject(int p1, int p2) throws DataAccessException { 287 return findObject(p1, p2, null); 288 } 289 290 294 public Object findObject(long p1, Map context) throws DataAccessException { 295 return findObject(new Object [] {new Long (p1)}, context); 296 } 297 298 301 public Object findObject(long p1) throws DataAccessException { 302 return findObject(p1, null); 303 } 304 305 309 public Object findObject(String p1, Map context) throws DataAccessException { 310 return findObject(new Object [] {p1}, context); 311 } 312 313 316 public Object findObject(String p1) throws DataAccessException { 317 return findObject(p1, null); 318 } 319 320 331 public Object findObjectByNamedParam(Map paramMap, Map context) throws DataAccessException { 332 List results = executeByNamedParam(paramMap, context); 333 return DataAccessUtils.singleResult(results); 334 } 335 336 342 public Object findObjectByNamedParam(Map paramMap) throws DataAccessException { 343 return findObjectByNamedParam(paramMap, null); 344 } 345 346 347 358 protected abstract RowMapper newRowMapper(Object [] parameters, Map context); 359 360 } 361 | Popular Tags |