1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.jdbc.core;
  17. import java.util.List;
  18. import java.util.Map;
  19. import org.springframework.dao.DataAccessException;
  20. /**
  21. * Interface that specifies a basic set of JDBC operations.
  22. * Implemented by JdbcTemplate. Not often used, but a useful option
  23. * to enhance testability, as it can easily be mocked or stubbed.
  24. *
  25. * <p>Alternatively, the standard JDBC infrastructure can be mocked.
  26. * However, mocking this interface constitutes significantly less work.
  27. *
  28. * @author Rod Johnson
  29. * @author Juergen Hoeller
  30. * @version $Id: JdbcOperations.java,v 1.10 2004/05/27 14:46:26 jhoeller Exp $
  31. * @see JdbcTemplate
  32. */
  33. public interface JdbcOperations {
  34. //-------------------------------------------------------------------------
  35. // Methods dealing with static SQL (java.sql.Statement)
  36. //-------------------------------------------------------------------------
  37. /**
  38. * Execute the action specified by the given action object within a JDBC
  39. * Statement. Allows for returning a result object, i.e. a domain object
  40. * or a collection of domain objects.
  41. * @param action callback object that specifies the action
  42. * @return a result object returned by the action, or null
  43. * @throws DataAccessException if there is any problem
  44. */
  45. Object execute(StatementCallback action) throws DataAccessException;
  46. /**
  47. * Issue a single SQL execute, typically a DDL statement.
  48. * @param sql static SQL to execute
  49. * @throws DataAccessException if there is any problem
  50. */
  51. void execute(String sql) throws DataAccessException;
  52. /**
  53. * Execute a query given static SQL, reading the ResultSet with a
  54. * ResultSetExtractor.
  55. * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to execute
  56. * a static query with a PreparedStatement, use the overloaded query method
  57. * with a null PreparedStatementSetter as a parameter.
  58. * @param sql SQL query to execute
  59. * @param rse object that will extract all rows of results
  60. * @return an arbitrary result object, as returned by the ResultSetExtractor
  61. * @throws DataAccessException if there is any problem executing the query
  62. * @see #query(String, PreparedStatementSetter, ResultSetExtractor)
  63. */
  64. Object query(String sql, ResultSetExtractor rse) throws DataAccessException;
  65. /**
  66. * Execute a query given static SQL, reading the ResultSet on a per-row
  67. * basis with a RowCallbackHandler (potentially implementing the ResultReader
  68. * sub-interface that provides a result List).
  69. * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to execute
  70. * a static query with a PreparedStatement, use the overloaded query method
  71. * with null as PreparedStatementSetter argument.
  72. * @param sql SQL query to execute
  73. * @param rch object that will extract results
  74. * @return the result List in case of a ResultReader, or null else
  75. * @throws DataAccessException if there is any problem executing the query
  76. * @see #query(String, PreparedStatementSetter, RowCallbackHandler)
  77. */
  78. List query(String sql, RowCallbackHandler rch) throws DataAccessException;
  79. /**
  80. * Execute a query for a result list, given static SQL.
  81. * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to execute
  82. * a static query with a PreparedStatement, use the overloaded queryForList
  83. * method with null as argument array.
  84. * <p>This method is useful for running static SQL with a known outcome.
  85. * The results will be mapped to an ArrayList (one entry for each row) of
  86. * HashMaps (one entry for each column using the column name as the key).
  87. * @param sql SQL query to execute
  88. * @return an ArrayList that contains a HashMap per row
  89. * @throws DataAccessException if there is any problem executing the query
  90. * @see #queryForList(String, Object[])
  91. */
  92. List queryForList(String sql) throws DataAccessException;
  93. /**
  94. * Execute a query for a result object, given static SQL.
  95. * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to execute
  96. * a static query with a PreparedStatement, use the overloaded queryForObject
  97. * method with null as argument array.
  98. * <p>This method is useful for running static SQL with a known outcome.
  99. * The query is expected to be a single row/single column query; the returned
  100. * result will be directly mapped to the corresponding object type.
  101. * @param sql SQL query to execute
  102. * @param requiredType the type that the result object is expected to match
  103. * @return the result object of the required type, or null in case of SQL NULL
  104. * @throws DataAccessException if there is any problem executing the query
  105. * @see #queryForObject(String, Object[], Class)
  106. */
  107. Object queryForObject(String sql, Class requiredType) throws DataAccessException;
  108. /**
  109. * Execute a query that results in a long value, given static SQL.
  110. * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to execute
  111. * a static query with a PreparedStatement, use the overloaded queryForLong
  112. * method with null as argument array.
  113. * <p>This method is useful for running static SQL with a known outcome.
  114. * The query is expected to be a single row/single column query that results
  115. * in a long value.
  116. * @param sql SQL query to execute
  117. * @return the long value, or 0 in case of SQL NULL
  118. * @throws DataAccessException if there is any problem executing the query
  119. * @see #queryForLong(String, Object[])
  120. */
  121. long queryForLong(String sql) throws DataAccessException;
  122. /**
  123. * Execute a query that results in an int value, given static SQL.
  124. * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to execute
  125. * a static query with a PreparedStatement, use the overloaded queryForInt
  126. * method with null as argument array.
  127. * <p>This method is useful for running static SQL with a known outcome.
  128. * The query is expected to be a single row/single column query that results
  129. * in an int value.
  130. * @param sql SQL query to execute
  131. * @return the int value, or 0 in case of SQL NULL
  132. * @throws DataAccessException if there is any problem executing the query
  133. * @see #queryForInt(String, Object[])
  134. */
  135. int queryForInt(String sql) throws DataAccessException;
  136. /**
  137. * Issue a single SQL update.
  138. * @param sql static SQL to execute
  139. * @return the number of rows affected
  140. * @throws DataAccessException if there is any problem.
  141. */
  142. int update(String sql) throws DataAccessException;
  143. //-------------------------------------------------------------------------
  144. // Methods dealing with prepared statements
  145. //-------------------------------------------------------------------------
  146. /**
  147. * Execute the action specified by the given action object within a JDBC
  148. * PreparedStatement. Allows for returning a result object, i.e. a domain
  149. * object or a collection of domain objects.
  150. * @param psc object that can create a PreparedStatement given a Connection
  151. * @param action callback object that specifies the action
  152. * @return a result object returned by the action, or null
  153. * @throws DataAccessException if there is any problem
  154. */
  155. Object execute(PreparedStatementCreator psc, PreparedStatementCallback action)
  156. throws DataAccessException;
  157. /**
  158. * Execute the action specified by the given action object within a JDBC
  159. * PreparedStatement. Allows for returning a result object, i.e. a domain
  160. * object or a collection of domain objects.
  161. * @param sql SQL to execute
  162. * @param action callback object that specifies the action
  163. * @return a result object returned by the action, or null
  164. * @throws DataAccessException if there is any problem
  165. */
  166. Object execute(String sql, PreparedStatementCallback action)
  167. throws DataAccessException;
  168. /**
  169. * Query using a prepared statement.
  170. * @param psc object that can create a PreparedStatement given a Connection
  171. * @param rse object that will extract results
  172. * @return an arbitrary result object, as returned by the ResultSetExtractor
  173. * @throws DataAccessException if there is any problem
  174. */
  175. Object query(PreparedStatementCreator psc, ResultSetExtractor rse)
  176. throws DataAccessException;
  177. /**
  178. * Query using a prepared statement, reading the ResultSet with a
  179. * ResultSetExtractor.
  180. * @param sql SQL to execute
  181. * @param pss object that knows how to set values on the prepared statement.
  182. * If this is null, the SQL will be assumed to contain no bind parameters.
  183. * Even if there are no bind parameters, this object may be used to
  184. * set fetch size and other performance options.
  185. * @param rse object that will extract results
  186. * @return an arbitrary result object, as returned by the ResultSetExtractor
  187. * @throws DataAccessException if there is any problem
  188. */
  189. Object query(String sql, PreparedStatementSetter pss, ResultSetExtractor rse)
  190. throws DataAccessException;
  191. /**
  192. * Query given SQL to create a prepared statement from SQL and a list of
  193. * arguments to bind to the query, reading the ResultSet on a per-row basis
  194. * with a RowCallbackHandler (potentially implementing the ResultReader
  195. * sub-interface that provides a result List).
  196. * @param sql SQL to execute
  197. * @param args arguments to bind to the query
  198. * @param argTypes SQL types of the arguments (constants from java.sql.Types)
  199. * @param rse object that will extract results
  200. * @return the result List in case of a ResultReader, or null else
  201. * @throws DataAccessException if the query fails
  202. * @see java.sql.Types
  203. */
  204. Object query(String sql, Object[] args, int[] argTypes, ResultSetExtractor rse)
  205. throws DataAccessException;
  206. /**
  207. * Query given SQL to create a prepared statement from SQL and a list of
  208. * arguments to bind to the query, reading the ResultSet on a per-row basis
  209. * with a RowCallbackHandler (potentially implementing the ResultReader
  210. * sub-interface that provides a result List).
  211. * @param sql SQL to execute
  212. * @param args arguments to bind to the query
  213. * (leaving it to the PreparedStatement to guess the respective SQL type)
  214. * @param rse object that will extract results
  215. * @return the result List in case of a ResultReader, or null else
  216. * @throws DataAccessException if the query fails
  217. */
  218. Object query(String sql, Object[] args, ResultSetExtractor rse)
  219. throws DataAccessException;
  220. /**
  221. * Query using a prepared statement, reading the ResultSet on a per-row
  222. * basis with a RowCallbackHandler (potentially implementing the ResultReader
  223. * sub-interface that provides a result List).
  224. * @param psc object that can create a PreparedStatement given a Connection
  225. * @param rch object that will extract results,
  226. * one row at a time
  227. * @return the result List in case of a ResultReader, or null else
  228. * @throws DataAccessException if there is any problem
  229. */
  230. List query(PreparedStatementCreator psc, RowCallbackHandler rch)
  231. throws DataAccessException;
  232. /**
  233. * Query given SQL to create a prepared statement from SQL and a
  234. * PreparedStatementSetter implementation that knows how to bind values
  235. * to the query, reading the ResultSet on a per-row basis with a
  236. * RowCallbackHandler (potentially implementing the ResultReader
  237. * sub-interface that provides a result List).
  238. * @param sql SQL to execute
  239. * @param pss object that knows how to set values on the prepared statement.
  240. * If this is null, the SQL will be assumed to contain no bind parameters.
  241. * Even if there are no bind parameters, this object may be used to
  242. * set fetch size and other performance options.
  243. * @param rch object that will extract results
  244. * @return the result List in case of a ResultReader, or null else
  245. * @throws DataAccessException if the query fails
  246. */
  247. List query(String sql, PreparedStatementSetter pss, RowCallbackHandler rch)
  248. throws DataAccessException;
  249. /**
  250. * Query given SQL to create a prepared statement from SQL and a list of
  251. * arguments to bind to the query, reading the ResultSet on a per-row basis
  252. * with a RowCallbackHandler (potentially implementing the ResultReader
  253. * sub-interface that provides a result List).
  254. * @param sql SQL to execute
  255. * @param args arguments to bind to the query
  256. * @param argTypes SQL types of the arguments (constants from java.sql.Types)
  257. * @param rch object that will extract results
  258. * @return the result List in case of a ResultReader, or null else
  259. * @throws DataAccessException if the query fails
  260. * @see java.sql.Types
  261. */
  262. List query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch)
  263. throws DataAccessException;
  264. /**
  265. * Query given SQL to create a prepared statement from SQL and a list of
  266. * arguments to bind to the query, reading the ResultSet on a per-row basis
  267. * with a RowCallbackHandler (potentially implementing the ResultReader
  268. * sub-interface that provides a result List).
  269. * @param sql SQL to execute
  270. * @param args arguments to bind to the query
  271. * (leaving it to the PreparedStatement to guess the respective SQL type)
  272. * @param rch object that will extract results
  273. * @return the result List in case of a ResultReader, or null else
  274. * @throws DataAccessException if the query fails
  275. */
  276. List query(String sql, Object[] args, RowCallbackHandler rch)
  277. throws DataAccessException;
  278. /**
  279. * Query given SQL to create a prepared statement from SQL and a
  280. * list of arguments to bind to the query, expecting a result list.
  281. * <p>This method is useful for running static SQL with a known outcome.
  282. * The results will be mapped to an ArrayList (one entry for each row) of
  283. * HashMaps (one entry for each column using the column name as the key).
  284. * @param sql SQL to execute
  285. * @param args arguments to bind to the query
  286. * (leaving it to the PreparedStatement to guess the respective SQL type)
  287. * @return an ArrayList that contains a HashMap per row
  288. * @throws DataAccessException if the query fails
  289. * @see #queryForList(String)
  290. */
  291. List queryForList(String sql, Object[] args) throws DataAccessException;
  292. /**
  293. * Query given SQL to create a prepared statement from SQL and a
  294. * list of arguments to bind to the query, expecting a result object.
  295. * <p>This method is useful for running static SQL with a known outcome.
  296. * The query is expected to be a single row/single column query; the returned
  297. * result will be directly mapped to the corresponding object type.
  298. * @param sql SQL to execute
  299. * @param args arguments to bind to the query
  300. * (leaving it to the PreparedStatement to guess the respective SQL type)
  301. * @param requiredType the type that the result object is expected to match
  302. * @return the result object of the required type, or null in case of SQL NULL
  303. * @throws DataAccessException if the query fails
  304. * @see #queryForObject(String, Class)
  305. */
  306. Object queryForObject(String sql, Object[] args, Class requiredType)
  307. throws DataAccessException;
  308. /**
  309. * Query given SQL to create a prepared statement from SQL and a
  310. * list of arguments to bind to the query, resulting in a long value.
  311. * <p>This method is useful for running static SQL with a known outcome.
  312. * The query is expected to be a single row/single column query that results
  313. * in a long value.
  314. * @param sql SQL to execute
  315. * @param args arguments to bind to the query
  316. * (leaving it to the PreparedStatement to guess the respective SQL type)
  317. * @return the long value, or 0 in case of SQL NULL
  318. * @throws DataAccessException if the query fails
  319. * @see #queryForLong(String)
  320. */
  321. long queryForLong(String sql, Object[] args) throws DataAccessException;
  322. /**
  323. * Query given SQL to create a prepared statement from SQL and a
  324. * list of arguments to bind to the query, resulting in an int value.
  325. * <p>This method is useful for running static SQL with a known outcome.
  326. * The query is expected to be a single row/single column query that results
  327. * in an int value.
  328. * @param sql SQL to execute
  329. * @param args arguments to bind to the query
  330. * (leaving it to the PreparedStatement to guess the respective SQL type)
  331. * @return the int value, or 0 in case of SQL NULL
  332. * @throws DataAccessException if the query fails
  333. * @see #queryForInt(String)
  334. */
  335. int queryForInt(String sql, Object[] args) throws DataAccessException;
  336. /**
  337. * Issue an update using a PreparedStatementCreator to provide SQL and any
  338. * required parameters.
  339. * @param psc object that provides SQL and any necessary parameters
  340. * @return the number of rows affected
  341. * @throws DataAccessException if there is any problem issuing the update
  342. */
  343. int update(PreparedStatementCreator psc) throws DataAccessException;
  344. /**
  345. * Issue an update using a PreparedStatementSetter to set bind parameters,
  346. * with given SQL. Simpler than using a PreparedStatementCreator as this
  347. * method will create the PreparedStatement: The PreparedStatementSetter
  348. * just needs to set parameters.
  349. * @param sql SQL, containing bind parameters
  350. * @param pss helper that sets bind parameters. If this is null
  351. * we run an update with static SQL.
  352. * @return the number of rows affected
  353. * @throws DataAccessException if there is any problem issuing the update
  354. */
  355. int update(String sql, PreparedStatementSetter pss) throws DataAccessException;
  356. /**
  357. * Issue an update via a prepared statement, binding the given arguments.
  358. * @param sql SQL, containing bind parameters
  359. * @param args arguments to bind to the query
  360. * @param argTypes SQL types of the arguments (constants from java.sql.Types)
  361. * @return the number of rows affected
  362. * @throws DataAccessException if there is any problem issuing the update
  363. */
  364. int update(String sql, Object[] args, int[] argTypes) throws DataAccessException;
  365. /**
  366. * Issue an update via a prepared statement, binding the given arguments.
  367. * @param sql SQL, containing bind parameters
  368. * @param args arguments to bind to the query
  369. * (leaving it to the PreparedStatement to guess the respective SQL type)
  370. * @return the number of rows affected
  371. * @throws DataAccessException if there is any problem issuing the update
  372. */
  373. int update(String sql, Object[] args) throws DataAccessException;
  374. /**
  375. * Issue multiple updates on a single PreparedStatement, using JDBC 2.0
  376. * batch updates and a BatchPreparedStatementSetter to set values.
  377. * <p>Will fall back to separate updates on a single PreparedStatement
  378. * if the JDBC driver does not support batch updates.
  379. * @param sql defining PreparedStatement that will be reused.
  380. * All statements in the batch will use the same SQL.
  381. * @param pss object to set parameters on the PreparedStatement
  382. * created by this method
  383. * @return an array of the number of rows affected by each statement
  384. * @throws DataAccessException if there is any problem issuing the update
  385. */
  386. int[] batchUpdate(String sql, BatchPreparedStatementSetter pss)
  387. throws DataAccessException;
  388. //-------------------------------------------------------------------------
  389. // Methods dealing with callable statements
  390. //-------------------------------------------------------------------------
  391. /**
  392. * Execute the action specified by the given action object within a JDBC
  393. * CallableStatement. Allows for returning a result object, i.e. a domain
  394. * object or a collection of domain objects.
  395. * @param csc object that can create a CallableStatement given a Connection
  396. * @param action callback object that specifies the action
  397. * @return a result object returned by the action, or null
  398. * @throws DataAccessException if there is any problem
  399. */
  400. Object execute(CallableStatementCreator csc, CallableStatementCallback action)
  401. throws DataAccessException;
  402. /**
  403. * Execute the action specified by the given action object within a JDBC
  404. * CallableStatement. Allows for returning a result object, i.e. a domain
  405. * object or a collection of domain objects.
  406. * @param callString the SQL call string to execute
  407. * @param action callback object that specifies the action
  408. * @return a result object returned by the action, or null
  409. * @throws DataAccessException if there is any problem
  410. */
  411. Object execute(String callString, CallableStatementCallback action)
  412. throws DataAccessException;
  413. /**
  414. * Execute a SQL call using a CallableStatementCreator to provide SQL and any
  415. * required parameters.
  416. * @param csc object that provides SQL and any necessary parameters
  417. * @param declaredParameters list of declared SqlParameter objects
  418. * @return Map of extracted out parameters
  419. * @throws DataAccessException if there is any problem issuing the update
  420. */
  421. Map call(CallableStatementCreator csc, List declaredParameters)
  422. throws DataAccessException;
  423. }