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