-
Notifications
You must be signed in to change notification settings - Fork 12.9k
/
Copy pathSqlRunner.java.html
265 lines (247 loc) · 12.5 KB
/
SqlRunner.java.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>SqlRunner.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">mybatis</a> > <a href="index.source.html" class="el_package">org.apache.ibatis.jdbc</a> > <span class="el_source">SqlRunner.java</span></div><h1>SqlRunner.java</h1><pre class="source lang-java linenums">/*
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
/**
* @author Clinton Begin
*/
public class SqlRunner {
public static final int NO_GENERATED_KEY = Integer.MIN_VALUE + 1001;
private final Connection connection;
private final TypeHandlerRegistry typeHandlerRegistry;
private boolean useGeneratedKeySupport;
<span class="fc" id="L46"> public SqlRunner(Connection connection) {</span>
<span class="fc" id="L47"> this.connection = connection;</span>
<span class="fc" id="L48"> this.typeHandlerRegistry = new TypeHandlerRegistry();</span>
<span class="fc" id="L49"> }</span>
public void setUseGeneratedKeySupport(boolean useGeneratedKeySupport) {
<span class="fc" id="L52"> this.useGeneratedKeySupport = useGeneratedKeySupport;</span>
<span class="fc" id="L53"> }</span>
/**
* Executes a SELECT statement that returns one row.
*
* @param sql
* The SQL
* @param args
* The arguments to be set on the statement.
*
* @return The row expected.
*
* @throws SQLException
* If less or more than one row is returned
*/
public Map<String, Object> selectOne(String sql, Object... args) throws SQLException {
<span class="fc" id="L69"> List<Map<String, Object>> results = selectAll(sql, args);</span>
<span class="pc bpc" id="L70" title="1 of 2 branches missed."> if (results.size() != 1) {</span>
<span class="nc" id="L71"> throw new SQLException("Statement returned " + results.size() + " results where exactly one (1) was expected.");</span>
}
<span class="fc" id="L73"> return results.get(0);</span>
}
/**
* Executes a SELECT statement that returns multiple rows.
*
* @param sql
* The SQL
* @param args
* The arguments to be set on the statement.
*
* @return The list of rows expected.
*
* @throws SQLException
* If statement preparation or execution fails
*/
public List<Map<String, Object>> selectAll(String sql, Object... args) throws SQLException {
<span class="fc" id="L90"> try (PreparedStatement ps = connection.prepareStatement(sql)) {</span>
<span class="fc" id="L91"> setParameters(ps, args);</span>
<span class="fc" id="L92"> try (ResultSet rs = ps.executeQuery()) {</span>
<span class="fc" id="L93"> return getResults(rs);</span>
}
}
}
/**
* Executes an INSERT statement.
*
* @param sql
* The SQL
* @param args
* The arguments to be set on the statement.
*
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
*
* @throws SQLException
* If statement preparation or execution fails
*/
public int insert(String sql, Object... args) throws SQLException {
PreparedStatement ps;
<span class="pc bpc" id="L113" title="1 of 2 branches missed."> if (useGeneratedKeySupport) {</span>
<span class="fc" id="L114"> ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);</span>
} else {
<span class="nc" id="L116"> ps = connection.prepareStatement(sql);</span>
}
try {
<span class="fc" id="L120"> setParameters(ps, args);</span>
<span class="fc" id="L121"> ps.executeUpdate();</span>
<span class="pc bpc" id="L122" title="1 of 2 branches missed."> if (useGeneratedKeySupport) {</span>
<span class="fc" id="L123"> try (ResultSet generatedKeys = ps.getGeneratedKeys()) {</span>
<span class="fc" id="L124"> List<Map<String, Object>> keys = getResults(generatedKeys);</span>
<span class="pc bpc" id="L125" title="1 of 2 branches missed."> if (keys.size() == 1) {</span>
<span class="fc" id="L126"> Map<String, Object> key = keys.get(0);</span>
<span class="fc" id="L127"> Iterator<Object> i = key.values().iterator();</span>
<span class="pc bpc" id="L128" title="1 of 2 branches missed."> if (i.hasNext()) {</span>
<span class="fc" id="L129"> Object genkey = i.next();</span>
<span class="pc bpc" id="L130" title="1 of 2 branches missed."> if (genkey != null) {</span>
try {
<span class="fc" id="L132"> return Integer.parseInt(genkey.toString());</span>
<span class="nc" id="L133"> } catch (NumberFormatException e) {</span>
// ignore, no numeric key support
}
}
}
}
<span class="pc bpc" id="L139" title="1 of 2 branches missed."> }</span>
}
<span class="nc" id="L141"> return NO_GENERATED_KEY;</span>
} finally {
try {
<span class="fc" id="L144"> ps.close();</span>
<span class="nc" id="L145"> } catch (SQLException e) {</span>
// ignore
<span class="fc" id="L147"> }</span>
}
}
/**
* Executes an UPDATE statement.
*
* @param sql
* The SQL
* @param args
* The arguments to be set on the statement.
*
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
*
* @throws SQLException
* If statement preparation or execution fails
*/
public int update(String sql, Object... args) throws SQLException {
<span class="fc" id="L165"> try (PreparedStatement ps = connection.prepareStatement(sql)) {</span>
<span class="fc" id="L166"> setParameters(ps, args);</span>
<span class="fc" id="L167"> return ps.executeUpdate();</span>
}
}
/**
* Executes a DELETE statement.
*
* @param sql
* The SQL
* @param args
* The arguments to be set on the statement.
*
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
*
* @throws SQLException
* If statement preparation or execution fails
*/
public int delete(String sql, Object... args) throws SQLException {
<span class="fc" id="L185"> return update(sql, args);</span>
}
/**
* Executes any string as a JDBC Statement. Good for DDL
*
* @param sql
* The SQL
*
* @throws SQLException
* If statement preparation or execution fails
*/
public void run(String sql) throws SQLException {
<span class="fc" id="L198"> try (Statement stmt = connection.createStatement()) {</span>
<span class="fc" id="L199"> stmt.execute(sql);</span>
}
<span class="fc" id="L201"> }</span>
/**
* @deprecated Since 3.5.4, this method is deprecated. Please close the {@link Connection} outside of this class.
*/
@Deprecated
public void closeConnection() {
try {
<span class="nc" id="L209"> connection.close();</span>
<span class="nc" id="L210"> } catch (SQLException e) {</span>
// ignore
<span class="nc" id="L212"> }</span>
<span class="nc" id="L213"> }</span>
private void setParameters(PreparedStatement ps, Object... args) throws SQLException {
<span class="fc bfc" id="L216" title="All 2 branches covered."> for (int i = 0, n = args.length; i < n; i++) {</span>
<span class="pc bpc" id="L217" title="1 of 2 branches missed."> if (args[i] == null) {</span>
<span class="nc" id="L218"> throw new SQLException(</span>
"SqlRunner requires an instance of Null to represent typed null values for JDBC compatibility");
}
<span class="fc bfc" id="L221" title="All 2 branches covered."> if (args[i] instanceof Null) {</span>
<span class="fc" id="L222"> ((Null) args[i]).getTypeHandler().setParameter(ps, i + 1, null, ((Null) args[i]).getJdbcType());</span>
} else {
<span class="fc" id="L224"> TypeHandler typeHandler = typeHandlerRegistry.getTypeHandler(args[i].getClass());</span>
<span class="pc bpc" id="L225" title="1 of 2 branches missed."> if (typeHandler == null) {</span>
<span class="nc" id="L226"> throw new SQLException("SqlRunner could not find a TypeHandler instance for " + args[i].getClass());</span>
} else {
<span class="fc" id="L228"> typeHandler.setParameter(ps, i + 1, args[i], null);</span>
}
}
}
<span class="fc" id="L232"> }</span>
private List<Map<String, Object>> getResults(ResultSet rs) throws SQLException {
<span class="fc" id="L235"> List<Map<String, Object>> list = new ArrayList<>();</span>
<span class="fc" id="L236"> List<String> columns = new ArrayList<>();</span>
<span class="fc" id="L237"> List<TypeHandler<?>> typeHandlers = new ArrayList<>();</span>
<span class="fc" id="L238"> ResultSetMetaData rsmd = rs.getMetaData();</span>
<span class="fc bfc" id="L239" title="All 2 branches covered."> for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {</span>
<span class="fc" id="L240"> columns.add(rsmd.getColumnLabel(i + 1));</span>
try {
<span class="fc" id="L242"> Class<?> type = Resources.classForName(rsmd.getColumnClassName(i + 1));</span>
<span class="fc" id="L243"> TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(type);</span>
<span class="fc bfc" id="L244" title="All 2 branches covered."> if (typeHandler == null) {</span>
<span class="fc" id="L245"> typeHandler = typeHandlerRegistry.getTypeHandler(Object.class);</span>
}
<span class="fc" id="L247"> typeHandlers.add(typeHandler);</span>
<span class="nc" id="L248"> } catch (Exception e) {</span>
<span class="nc" id="L249"> typeHandlers.add(typeHandlerRegistry.getTypeHandler(Object.class));</span>
<span class="fc" id="L250"> }</span>
}
<span class="fc bfc" id="L252" title="All 2 branches covered."> while (rs.next()) {</span>
<span class="fc" id="L253"> Map<String, Object> row = new HashMap<>();</span>
<span class="fc bfc" id="L254" title="All 2 branches covered."> for (int i = 0, n = columns.size(); i < n; i++) {</span>
<span class="fc" id="L255"> String name = columns.get(i);</span>
<span class="fc" id="L256"> TypeHandler<?> handler = typeHandlers.get(i);</span>
<span class="fc" id="L257"> row.put(name.toUpperCase(Locale.ENGLISH), handler.getResult(rs, name));</span>
}
<span class="fc" id="L259"> list.add(row);</span>
<span class="fc" id="L260"> }</span>
<span class="fc" id="L261"> return list;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>