@@ -36,7 +36,6 @@ public class MappedQueryImpl<T> implements MappedQuery<T>, Closeable {
36
36
37
37
final PreparedStatement statement ;
38
38
final RowMapper <T > mapper ;
39
- final ResultSet resultSet ;
40
39
41
40
/**
42
41
* Constructor.
@@ -47,7 +46,6 @@ public class MappedQueryImpl<T> implements MappedQuery<T>, Closeable {
47
46
public MappedQueryImpl (PreparedStatement statement , RowMapper <T > mapper ) throws SQLException {
48
47
this .statement = statement ;
49
48
this .mapper = mapper ;
50
- this .resultSet = statement .executeQuery ();
51
49
}
52
50
53
51
/**
@@ -56,18 +54,21 @@ public MappedQueryImpl(PreparedStatement statement, RowMapper<T> mapper) throws
56
54
@ Override
57
55
public T one () {
58
56
T rval = null ;
59
- try {
60
- if (this . resultSet .next ()) {
57
+ try ( ResultSet resultSet = statement . executeQuery ()) {
58
+ if (resultSet .next ()) {
61
59
rval = this .mapper .map (resultSet );
62
- if (this . resultSet .next ()) {
60
+ if (resultSet .next ()) {
63
61
throw new RuntimeSqlException ("SQL error: Expected only one result but got multiple." );
64
62
}
65
- } else {
63
+ }
64
+ else {
66
65
throw new RuntimeSqlException ("SQL error: Expected only one result row but got none." );
67
66
}
68
- } catch (SQLException e ) {
67
+ }
68
+ catch (SQLException e ) {
69
69
throw new RuntimeSqlException (e );
70
- } finally {
70
+ }
71
+ finally {
71
72
close ();
72
73
}
73
74
return rval ;
@@ -79,15 +80,18 @@ public T one() {
79
80
@ Override
80
81
public T first () {
81
82
T rval = null ;
82
- try {
83
- if (this . resultSet .next ()) {
83
+ try ( ResultSet resultSet = statement . executeQuery ()) {
84
+ if (resultSet .next ()) {
84
85
rval = this .mapper .map (resultSet );
85
- } else {
86
+ }
87
+ else {
86
88
throw new RuntimeSqlException ("SQL error: Expected AT LEAST one result row but got none." );
87
89
}
88
- } catch (SQLException e ) {
90
+ }
91
+ catch (SQLException e ) {
89
92
throw new RuntimeSqlException (e );
90
- } finally {
93
+ }
94
+ finally {
91
95
close ();
92
96
}
93
97
return rval ;
@@ -99,18 +103,21 @@ public T first() {
99
103
@ Override
100
104
public Optional <T > findOne () {
101
105
Optional <T > rval ;
102
- try {
103
- if (this . resultSet .next ()) {
106
+ try ( ResultSet resultSet = statement . executeQuery ()) {
107
+ if (resultSet .next ()) {
104
108
rval = Optional .of (this .mapper .map (resultSet ));
105
- if (this . resultSet .next ()) {
109
+ if (resultSet .next ()) {
106
110
throw new RuntimeSqlException ("SQL error: Expected only one result but got multiple." );
107
111
}
108
- } else {
112
+ }
113
+ else {
109
114
rval = Optional .empty ();
110
115
}
111
- } catch (SQLException e ) {
116
+ }
117
+ catch (SQLException e ) {
112
118
throw new RuntimeSqlException (e );
113
- } finally {
119
+ }
120
+ finally {
114
121
close ();
115
122
}
116
123
return rval ;
@@ -122,15 +129,18 @@ public Optional<T> findOne() {
122
129
@ Override
123
130
public Optional <T > findFirst () {
124
131
Optional <T > rval = null ;
125
- try {
126
- if (this . resultSet .next ()) {
132
+ try ( ResultSet resultSet = statement . executeQuery ()) {
133
+ if (resultSet .next ()) {
127
134
rval = Optional .of (this .mapper .map (resultSet ));
128
- } else {
135
+ }
136
+ else {
129
137
rval = Optional .empty ();
130
138
}
131
- } catch (SQLException e ) {
139
+ }
140
+ catch (SQLException e ) {
132
141
throw new RuntimeSqlException (e );
133
- } finally {
142
+ }
143
+ finally {
134
144
close ();
135
145
}
136
146
return rval ;
@@ -142,16 +152,18 @@ public Optional<T> findFirst() {
142
152
@ Override
143
153
public Optional <T > findLast () {
144
154
Optional <T > rval = null ;
145
- try {
146
- while (this . resultSet .next ()) {
155
+ try ( ResultSet resultSet = statement . executeQuery ()) {
156
+ while (resultSet .next ()) {
147
157
rval = Optional .of (this .mapper .map (resultSet ));
148
158
}
149
159
if (rval == null ) {
150
160
rval = Optional .empty ();
151
161
}
152
- } catch (SQLException e ) {
162
+ }
163
+ catch (SQLException e ) {
153
164
throw new RuntimeSqlException (e );
154
- } finally {
165
+ }
166
+ finally {
155
167
close ();
156
168
}
157
169
return rval ;
@@ -163,14 +175,16 @@ public Optional<T> findLast() {
163
175
@ Override
164
176
public List <T > list () {
165
177
List <T > rval = new LinkedList <>();
166
- try {
167
- while (this . resultSet .next ()) {
178
+ try ( ResultSet resultSet = statement . executeQuery ()) {
179
+ while (resultSet .next ()) {
168
180
T t = this .mapper .map (resultSet );
169
181
rval .add (t );
170
182
}
171
- } catch (SQLException e ) {
183
+ }
184
+ catch (SQLException e ) {
172
185
throw new RuntimeSqlException (e );
173
- } finally {
186
+ }
187
+ finally {
174
188
close ();
175
189
}
176
190
return rval ;
@@ -181,22 +195,38 @@ public List<T> list() {
181
195
*/
182
196
@ Override
183
197
public Stream <T > stream () {
184
- return StreamSupport .stream (new Spliterators .AbstractSpliterator <T >(Long .MAX_VALUE , Spliterator .IMMUTABLE | Spliterator .ORDERED | Spliterator .DISTINCT | Spliterator .NONNULL ) {
185
- @ Override
186
- public boolean tryAdvance (Consumer <? super T > action ) {
198
+ try {
199
+ ResultSet resultSet = statement .executeQuery ();
200
+ return StreamSupport .stream (
201
+ new Spliterators .AbstractSpliterator <T >(Long .MAX_VALUE , Spliterator .IMMUTABLE | Spliterator .ORDERED | Spliterator .DISTINCT | Spliterator .NONNULL ) {
202
+ @ Override
203
+ public boolean tryAdvance (Consumer <? super T > action ) {
204
+ try {
205
+ if (!resultSet .next ()) {
206
+ return false ;
207
+ }
208
+ T t = mapper .map (resultSet );
209
+ action .accept (t );
210
+ return true ;
211
+ }
212
+ catch (SQLException e ) {
213
+ throw new RuntimeSqlException (e );
214
+ }
215
+ }
216
+
217
+ }, false ).onClose (() -> {
187
218
try {
188
- if (!resultSet .next ()) {
189
- return false ;
190
- }
191
- T t = mapper .map (resultSet );
192
- action .accept (t );
193
- return true ;
194
- } catch (SQLException e ) {
195
- throw new RuntimeSqlException (e );
219
+ resultSet .close ();
220
+ close ();
196
221
}
197
- }
198
-
199
- }, false ).onClose (this ::close );
222
+ catch (SQLException e ) {
223
+ throw new RuntimeException (e );
224
+ }
225
+ });
226
+ }
227
+ catch (SQLException e ) {
228
+ throw new RuntimeException (e );
229
+ }
200
230
}
201
231
202
232
/**
@@ -206,7 +236,8 @@ public boolean tryAdvance(Consumer<? super T> action) {
206
236
public void close () {
207
237
try {
208
238
this .statement .close ();
209
- } catch (SQLException e ) {
239
+ }
240
+ catch (SQLException e ) {
210
241
throw new RuntimeSqlException (e );
211
242
}
212
243
}
0 commit comments