View Javadoc

1   package com.diasparsoftware.jdbc;
2   
3   import java.sql.*;
4   import java.util.*;
5   
6   import com.diasparsoftware.java.sql.PreparedStatementData;
7   import com.diasparsoftware.store.DataStoreException;
8   
9   public class JdbcQueryExecuter implements PreparedStatementExecuter {
10      private static final RowCountRowMapper ROW_COUNT_ROW_MAPPER = new RowCountRowMapper();
11  
12      private Connection connection;
13  
14      public JdbcQueryExecuter(Connection connection) {
15          this.connection = connection;
16      }
17  
18      public void executeDeleteStatement(
19              PreparedStatementData preparedStatementData) throws DataStoreException {
20  
21          PreparedStatement deleteStatement = null;
22  
23          try {
24              deleteStatement = connection
25                      .prepareStatement(preparedStatementData.sqlString);
26  
27              deleteStatement.clearParameters();
28  
29              int columnIndex = 1;
30              for (Iterator i = preparedStatementData.parameters.iterator(); i
31                      .hasNext(); columnIndex++) {
32  
33                  Object eachParameter = (Object) i.next();
34                  deleteStatement.setObject(columnIndex, eachParameter);
35              }
36  
37              deleteStatement.executeUpdate();
38          }
39          catch (SQLException rethrow) {
40              throw new DataStoreException(rethrow);
41          }
42          finally {
43              try {
44                  if (deleteStatement != null)
45                      deleteStatement.close();
46              }
47              catch (SQLException ignored) {
48              }
49          }
50  
51      }
52  
53      public int executeUpdateStatement(
54              PreparedStatementData preparedStatementData) {
55          PreparedStatement updateStatement = null;
56  
57          try {
58              updateStatement = connection
59                      .prepareStatement(preparedStatementData.sqlString);
60  
61              updateStatement.clearParameters();
62  
63              int columnIndex = 1;
64              for (Iterator i = preparedStatementData.parameters.iterator(); i
65                      .hasNext(); columnIndex++) {
66  
67                  Object eachParameter = (Object) i.next();
68                  updateStatement.setObject(columnIndex, eachParameter);
69              }
70  
71              return updateStatement.executeUpdate();
72          }
73          catch (SQLException rethrow) {
74              throw new DataStoreException(rethrow);
75          }
76          finally {
77              try {
78                  if (updateStatement != null)
79                      updateStatement.close();
80              }
81              catch (SQLException ignored) {
82              }
83          }
84  
85      }
86  
87      // TODO Another version of this method that accepts
88      //       the expected number of rows inserted, and
89      //       checks them.
90      public int executeInsertStatement(PreparedStatementData insertStatementData) {
91          PreparedStatement insertStatement = null;
92          try {
93              insertStatement = connection
94                      .prepareStatement(insertStatementData.sqlString);
95  
96              insertStatement.clearParameters();
97  
98              int columnIndex = 1;
99              for (Iterator i = insertStatementData.parameters.iterator(); i
100                     .hasNext(); ) {
101 
102                 Object each = (Object) i.next();
103                 insertStatement.setObject(columnIndex, each);
104 
105                 columnIndex++;
106             }
107 
108             return insertStatement.executeUpdate();
109         }
110         catch (SQLException rethrow) {
111             throw new DataStoreException(rethrow);
112         }
113         finally {
114             try {
115                 if (insertStatement != null)
116                     insertStatement.close();
117             }
118             catch (SQLException ignored) {
119             }
120         }
121     }
122 
123     public List executeSelectStatement(
124             PreparedStatementData selectStatementData, JdbcRowMapper rowMapper) {
125 
126         List result = new ArrayList();
127 
128         PreparedStatement selectStatement = null;
129         ResultSet resultSet = null;
130 
131         try {
132             selectStatement = connection
133                     .prepareStatement(selectStatementData.sqlString);
134 
135             selectStatement.clearParameters();
136 
137             JdbcUtil.setPreparedStatementParameters(selectStatement,
138                     selectStatementData.parameters);
139 
140             resultSet = selectStatement.executeQuery();
141             while (resultSet.next()) {
142                 result.add(rowMapper.makeDomainObject(resultSet));
143             }
144         }
145         catch (SQLException rethrow) {
146             throw new DataStoreException(rethrow);
147         }
148         finally {
149             try {
150                 if (resultSet != null)
151                     resultSet.close();
152 
153                 if (selectStatement != null)
154                     selectStatement.close();
155             }
156             catch (SQLException ignored) {
157             }
158         }
159 
160         return result;
161     }
162 
163     /***
164 	 * Invoke this only for SELECT statements that count rows. This
165 	 * method assumes that the database returns only a single row for
166 	 * SELECT COUNT(...) statements.
167 	 * 
168 	 * @param countStatementData
169 	 *            A SELECT COUNT(...) statement.
170 	 * @return The number of rows determined by the COUNT statement.
171 	 */
172     public int executeCountStatement(PreparedStatementData countStatementData) {
173         List rowCountResults = executeSelectStatement(countStatementData,
174                 ROW_COUNT_ROW_MAPPER);
175 
176         Object rowCountAsObject = new LinkedList(rowCountResults).get(0);
177 
178         return ((Integer) rowCountAsObject).intValue();
179     }
180 
181     public Object executeSingleRowSelectStatement(
182             PreparedStatementData selectStatementData,
183             JdbcRowMapper simpleMapper) {
184 
185         List rows = executeSelectStatement(selectStatementData, simpleMapper);
186         if (rows.isEmpty()) {
187             return null;
188         }
189         else {
190             return rows.get(0);
191         }
192     }
193 
194     public void commit() {
195         try {
196             connection.commit();
197         }
198         catch (SQLException wrapped) {
199             throw new DataStoreException("Unable to commit last operation",
200                     wrapped);
201         }
202     }
203 }