1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.jdbc;
17
18 import java.sql.Connection;
19 import java.sql.PreparedStatement;
20 import java.sql.ResultSet;
21 import java.sql.ResultSetMetaData;
22 import java.sql.SQLException;
23 import java.sql.Statement;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Locale;
29 import java.util.Map;
30
31 import org.apache.ibatis.io.Resources;
32 import org.apache.ibatis.type.ObjectTypeHandler;
33 import org.apache.ibatis.type.TypeHandler;
34 import org.apache.ibatis.type.TypeHandlerRegistry;
35
36
37
38
39 public class SqlRunner {
40
41 public static final int NO_GENERATED_KEY = Integer.MIN_VALUE + 1001;
42
43 private final Connection connection;
44 private final TypeHandlerRegistry typeHandlerRegistry;
45 private boolean useGeneratedKeySupport;
46
47 public SqlRunner(Connection connection) {
48 this.connection = connection;
49 this.typeHandlerRegistry = new TypeHandlerRegistry();
50 }
51
52 public void setUseGeneratedKeySupport(boolean useGeneratedKeySupport) {
53 this.useGeneratedKeySupport = useGeneratedKeySupport;
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public Map<String, Object> selectOne(String sql, Object... args) throws SQLException {
70 List<Map<String, Object>> results = selectAll(sql, args);
71 if (results.size() != 1) {
72 throw new SQLException("Statement returned " + results.size() + " results where exactly one (1) was expected.");
73 }
74 return results.get(0);
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public List<Map<String, Object>> selectAll(String sql, Object... args) throws SQLException {
91 try (PreparedStatement ps = connection.prepareStatement(sql)) {
92 setParameters(ps, args);
93 try (ResultSet rs = ps.executeQuery()) {
94 return getResults(rs);
95 }
96 }
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public int insert(String sql, Object... args) throws SQLException {
113 PreparedStatement ps;
114 if (useGeneratedKeySupport) {
115 ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
116 } else {
117 ps = connection.prepareStatement(sql);
118 }
119
120 try {
121 setParameters(ps, args);
122 ps.executeUpdate();
123 if (useGeneratedKeySupport) {
124 try (ResultSet generatedKeys = ps.getGeneratedKeys()) {
125 List<Map<String, Object>> keys = getResults(generatedKeys);
126 if (keys.size() == 1) {
127 Map<String, Object> key = keys.get(0);
128 Iterator<Object> i = key.values().iterator();
129 if (i.hasNext()) {
130 Object genkey = i.next();
131 if (genkey != null) {
132 try {
133 return Integer.parseInt(genkey.toString());
134 } catch (NumberFormatException e) {
135
136 }
137 }
138 }
139 }
140 }
141 }
142 return NO_GENERATED_KEY;
143 } finally {
144 try {
145 ps.close();
146 } catch (SQLException e) {
147
148 }
149 }
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public int update(String sql, Object... args) throws SQLException {
166 try (PreparedStatement ps = connection.prepareStatement(sql)) {
167 setParameters(ps, args);
168 return ps.executeUpdate();
169 }
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 public int delete(String sql, Object... args) throws SQLException {
186 return update(sql, args);
187 }
188
189
190
191
192
193
194
195
196
197
198 public void run(String sql) throws SQLException {
199 try (Statement stmt = connection.createStatement()) {
200 stmt.execute(sql);
201 }
202 }
203
204
205
206
207 @Deprecated
208 public void closeConnection() {
209 try {
210 connection.close();
211 } catch (SQLException e) {
212
213 }
214 }
215
216 private void setParameters(PreparedStatement ps, Object... args) throws SQLException {
217 for (int i = 0, n = args.length; i < n; i++) {
218 if (args[i] == null) {
219 throw new SQLException(
220 "SqlRunner requires an instance of Null to represent typed null values for JDBC compatibility");
221 }
222 if (args[i] instanceof Null) {
223 ((Null) args[i]).getTypeHandler().setParameter(ps, i + 1, null, ((Null) args[i]).getJdbcType());
224 } else {
225 TypeHandler typeHandler = typeHandlerRegistry.getTypeHandler(args[i].getClass());
226 if (typeHandler == null) {
227 throw new SQLException("SqlRunner could not find a TypeHandler instance for " + args[i].getClass());
228 } else {
229 typeHandler.setParameter(ps, i + 1, args[i], null);
230 }
231 }
232 }
233 }
234
235 private List<Map<String, Object>> getResults(ResultSet rs) throws SQLException {
236 List<Map<String, Object>> list = new ArrayList<>();
237 List<String> columns = new ArrayList<>();
238 List<TypeHandler<?>> typeHandlers = new ArrayList<>();
239 ResultSetMetaData rsmd = rs.getMetaData();
240 for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
241 columns.add(rsmd.getColumnLabel(i + 1));
242 try {
243 Class<?> type = Resources.classForName(rsmd.getColumnClassName(i + 1));
244 TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(type);
245 if (typeHandler == null) {
246 typeHandler = ObjectTypeHandler.INSTANCE;
247 }
248 typeHandlers.add(typeHandler);
249 } catch (Exception e) {
250 typeHandlers.add(ObjectTypeHandler.INSTANCE);
251 }
252 }
253 while (rs.next()) {
254 Map<String, Object> row = new HashMap<>();
255 for (int i = 0, n = columns.size(); i < n; i++) {
256 String name = columns.get(i);
257 TypeHandler<?> handler = typeHandlers.get(i);
258 row.put(name.toUpperCase(Locale.ENGLISH), handler.getResult(rs, name));
259 }
260 list.add(row);
261 }
262 return list;
263 }
264
265 }