1
1
/**
2
- * Copyright 2009-2020 the original author or authors.
3
- * <p>
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
- * <p>
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- * <p>
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.
2
+ * Copyright 2009-2020 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
15
*/
16
16
package org .apache .ibatis .jdbc ;
17
17
@@ -57,9 +57,7 @@ public void setUseGeneratedKeySupport(boolean useGeneratedKeySupport) {
57
57
*
58
58
* @param sql The SQL
59
59
* @param args The arguments to be set on the statement.
60
- *
61
60
* @return The row expected.
62
- *
63
61
* @throws SQLException If less or more than one row is returned
64
62
*/
65
63
public Map <String , Object > selectOne (String sql , Object ... args ) throws SQLException {
@@ -75,15 +73,13 @@ public Map<String, Object> selectOne(String sql, Object... args) throws SQLExcep
75
73
*
76
74
* @param sql The SQL
77
75
* @param args The arguments to be set on the statement.
78
- *
79
76
* @return The list of rows expected.
80
- *
81
77
* @throws SQLException If statement preparation or execution fails
82
78
*/
83
79
public List <Map <String , Object >> selectAll (String sql , Object ... args ) throws SQLException {
84
- try (PreparedStatement ps = connection .prepareStatement (sql ); ) {
80
+ try (PreparedStatement ps = connection .prepareStatement (sql )) {
85
81
setParameters (ps , args );
86
- try (ResultSet rs = ps .executeQuery (); ) {
82
+ try (ResultSet rs = ps .executeQuery ()) {
87
83
return getResults (rs );
88
84
}
89
85
}
@@ -94,9 +90,7 @@ public List<Map<String, Object>> selectAll(String sql, Object... args) throws SQ
94
90
*
95
91
* @param sql The SQL
96
92
* @param args The arguments to be set on the statement.
97
- *
98
93
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
99
- *
100
94
* @throws SQLException If statement preparation or execution fails
101
95
*/
102
96
public int insert (String sql , Object ... args ) throws SQLException {
@@ -111,7 +105,7 @@ public int insert(String sql, Object... args) throws SQLException {
111
105
setParameters (ps , args );
112
106
ps .executeUpdate ();
113
107
if (useGeneratedKeySupport ) {
114
- try (ResultSet generatedKeys = ps .getGeneratedKeys (); ) {
108
+ try (ResultSet generatedKeys = ps .getGeneratedKeys ()) {
115
109
List <Map <String , Object >> keys = getResults (generatedKeys );
116
110
if (keys .size () == 1 ) {
117
111
Map <String , Object > key = keys .get (0 );
@@ -144,13 +138,11 @@ public int insert(String sql, Object... args) throws SQLException {
144
138
*
145
139
* @param sql The SQL
146
140
* @param args The arguments to be set on the statement.
147
- *
148
141
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
149
- *
150
142
* @throws SQLException If statement preparation or execution fails
151
143
*/
152
144
public int update (String sql , Object ... args ) throws SQLException {
153
- try (PreparedStatement ps = connection .prepareStatement (sql ); ) {
145
+ try (PreparedStatement ps = connection .prepareStatement (sql )) {
154
146
setParameters (ps , args );
155
147
return ps .executeUpdate ();
156
148
}
@@ -161,9 +153,7 @@ public int update(String sql, Object... args) throws SQLException {
161
153
*
162
154
* @param sql The SQL
163
155
* @param args The arguments to be set on the statement.
164
- *
165
156
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
166
- *
167
157
* @throws SQLException If statement preparation or execution fails
168
158
*/
169
159
public int delete (String sql , Object ... args ) throws SQLException {
@@ -175,11 +165,10 @@ public int delete(String sql, Object... args) throws SQLException {
175
165
* Good for DDL
176
166
*
177
167
* @param sql The SQL
178
- *
179
168
* @throws SQLException If statement preparation or execution fails
180
169
*/
181
170
public void run (String sql ) throws SQLException {
182
- try (Statement stmt = connection .createStatement (); ) {
171
+ try (Statement stmt = connection .createStatement ()) {
183
172
stmt .execute (sql );
184
173
}
185
174
}
@@ -199,8 +188,7 @@ public void closeConnection() {
199
188
private void setParameters (PreparedStatement ps , Object ... args ) throws SQLException {
200
189
for (int i = 0 , n = args .length ; i < n ; i ++) {
201
190
if (args [i ] == null ) {
202
- throw new SQLException (
203
- "SqlRunner requires an instance of Null to represent typed null values for JDBC compatibility" );
191
+ throw new SQLException ("SqlRunner requires an instance of Null to represent typed null values for JDBC compatibility" );
204
192
} else if (args [i ] instanceof Null ) {
205
193
((Null ) args [i ]).getTypeHandler ().setParameter (ps , i + 1 , null , ((Null ) args [i ]).getJdbcType ());
206
194
} else {
@@ -214,9 +202,6 @@ private void setParameters(PreparedStatement ps, Object... args) throws SQLExcep
214
202
}
215
203
}
216
204
217
- /**
218
- * ResultSet should't was closed in here,it should be closed in caller
219
- */
220
205
private List <Map <String , Object >> getResults (ResultSet rs ) throws SQLException {
221
206
List <Map <String , Object >> list = new ArrayList <>();
222
207
List <String > columns = new ArrayList <>();
0 commit comments