|
10 | 10 |
|
11 | 11 | import java.sql.Connection; |
12 | 12 | import java.sql.ResultSet; |
| 13 | +import java.sql.SQLException; |
13 | 14 | import java.sql.SQLWarning; |
14 | 15 | import java.sql.Statement; |
15 | 16 | import java.util.Map; |
| 17 | +import java.util.function.Function; |
16 | 18 |
|
17 | 19 | public class UcanaccessStatement implements Statement { |
18 | 20 |
|
@@ -110,93 +112,76 @@ protected void checkLastModified() throws UcanaccessSQLException { |
110 | 112 | } |
111 | 113 | } |
112 | 114 |
|
113 | | - @Override |
114 | | - public boolean execute(String _sql) throws UcanaccessSQLException { |
| 115 | + /** |
| 116 | + * Executes a generic SQL operation, handling common setup and error mapping. |
| 117 | + * This method centralizes the logic for setting the current statement, |
| 118 | + * performing modification checks, and wrapping exceptions. |
| 119 | + * |
| 120 | + * @param <R> The return type of the SQL operation. |
| 121 | + * @param _sql The SQL string associated with the operation (used for error messages). |
| 122 | + * @param _supplier A supplier that provides the result of the actual SQL operation. |
| 123 | + * @return The result of the SQL execution, of type {@code <R>}. |
| 124 | + * @throws UcanaccessSQLException if an SQL error occurs during execution or wrapping. |
| 125 | + */ |
| 126 | + private <R> R executeImpl(String _sql, IThrowingSupplier<R, SQLException> _supplier) throws UcanaccessSQLException { |
115 | 127 | return tryCatch(() -> { |
116 | 128 | connection.setCurrentStatement(this); |
117 | 129 | checkLastModified(); |
118 | | - return new Execute(this, convertSql(_sql, connection)).execute(); |
119 | | - }); |
| 130 | + return _supplier.get(); |
| 131 | + }, ex -> UcanaccessSQLException.wrap("Failed to execute [" + _sql + "]", ex)); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public boolean execute(String _sql) throws UcanaccessSQLException { |
| 136 | + return executeImpl(_sql, () -> new Execute(this, convertSql(_sql, connection)).execute()); |
120 | 137 | } |
121 | 138 |
|
122 | 139 | @Override |
123 | 140 | public boolean execute(String _sql, int _autoGeneratedKeys) throws UcanaccessSQLException { |
124 | | - return tryCatch(() -> { |
125 | | - connection.setCurrentStatement(this); |
126 | | - checkLastModified(); |
127 | | - return new Execute(this, convertSql(_sql, connection), _autoGeneratedKeys).execute(); |
128 | | - }); |
| 141 | + return executeImpl(_sql, () -> new Execute(this, convertSql(_sql, connection), _autoGeneratedKeys).execute()); |
129 | 142 | } |
130 | 143 |
|
131 | 144 | @Override |
132 | 145 | public boolean execute(String _sql, int[] _indexes) throws UcanaccessSQLException { |
133 | | - return tryCatch(() -> { |
134 | | - connection.setCurrentStatement(this); |
135 | | - checkLastModified(); |
136 | | - return new Execute(this, convertSql(_sql, connection), _indexes).execute(); |
137 | | - }); |
| 146 | + return executeImpl(_sql, () -> new Execute(this, convertSql(_sql, connection), _indexes).execute()); |
138 | 147 | } |
139 | 148 |
|
140 | 149 | @Override |
141 | 150 | public boolean execute(String _sql, String[] _columnNames) throws UcanaccessSQLException { |
142 | | - return tryCatch(() -> { |
143 | | - connection.setCurrentStatement(this); |
144 | | - checkLastModified(); |
145 | | - return new Execute(this, _sql, _columnNames).execute(); |
146 | | - }); |
| 151 | + return executeImpl(_sql, () -> new Execute(this, _sql, _columnNames).execute()); |
147 | 152 | } |
148 | 153 |
|
149 | 154 | @Override |
150 | 155 | public int[] executeBatch() throws UcanaccessSQLException { |
151 | 156 | return tryCatch(() -> { |
152 | 157 | connection.setCurrentStatement(this); |
153 | 158 | return new ExecuteUpdate(this).executeBatch(); |
154 | | - }); |
| 159 | + }, ex -> UcanaccessSQLException.wrap("Failed to execute batch", ex)); |
155 | 160 | } |
156 | 161 |
|
157 | 162 | @Override |
158 | 163 | public ResultSet executeQuery(String _sql) throws UcanaccessSQLException { |
159 | | - return tryCatch(() -> { |
160 | | - connection.setCurrentStatement(this); |
161 | | - checkLastModified(); |
162 | | - return new UcanaccessResultSet(wrapped.executeQuery(convertSql(_sql, connection)), this); |
163 | | - }); |
| 164 | + return executeImpl(_sql, () -> new UcanaccessResultSet(wrapped.executeQuery(convertSql(_sql, connection)), this)); |
164 | 165 | } |
165 | 166 |
|
166 | 167 | @Override |
167 | 168 | public int executeUpdate(String _sql) throws UcanaccessSQLException { |
168 | | - return tryCatch(() -> { |
169 | | - connection.setCurrentStatement(this); |
170 | | - checkLastModified(); |
171 | | - return new ExecuteUpdate(this, convertSql(_sql)).execute(); |
172 | | - }); |
| 169 | + return executeImpl(_sql, () -> new ExecuteUpdate(this, convertSql(_sql)).execute()); |
173 | 170 | } |
174 | 171 |
|
175 | 172 | @Override |
176 | 173 | public int executeUpdate(String _sql, int _autoGeneratedKeys) throws UcanaccessSQLException { |
177 | | - return tryCatch(() -> { |
178 | | - connection.setCurrentStatement(this); |
179 | | - checkLastModified(); |
180 | | - return new ExecuteUpdate(this, convertSql(_sql), _autoGeneratedKeys).execute(); |
181 | | - }); |
| 174 | + return executeImpl(_sql, () -> new ExecuteUpdate(this, convertSql(_sql), _autoGeneratedKeys).execute()); |
182 | 175 | } |
183 | 176 |
|
184 | 177 | @Override |
185 | 178 | public int executeUpdate(String _sql, int[] _indexes) throws UcanaccessSQLException { |
186 | | - return tryCatch(() -> { |
187 | | - connection.setCurrentStatement(this); |
188 | | - checkLastModified(); |
189 | | - return new ExecuteUpdate(this, convertSql(_sql), _indexes).execute(); |
190 | | - }); |
| 179 | + return executeImpl(_sql, () -> new ExecuteUpdate(this, convertSql(_sql), _indexes).execute()); |
191 | 180 | } |
192 | 181 |
|
193 | 182 | @Override |
194 | 183 | public int executeUpdate(String _sql, String[] _columnNames) throws UcanaccessSQLException { |
195 | | - return tryCatch(() -> { |
196 | | - connection.setCurrentStatement(this); |
197 | | - checkLastModified(); |
198 | | - return new ExecuteUpdate(this, convertSql(_sql), _columnNames).execute(); |
199 | | - }); |
| 184 | + return executeImpl(_sql, () -> new ExecuteUpdate(this, convertSql(_sql), _columnNames).execute()); |
200 | 185 | } |
201 | 186 |
|
202 | 187 | @Override |
@@ -406,7 +391,11 @@ protected static final <T extends Throwable> void tryCatch(IThrowingRunnable<T> |
406 | 391 | } |
407 | 392 |
|
408 | 393 | protected static final <R, T extends Throwable> R tryCatch(IThrowingSupplier<R, T> _catchable) throws UcanaccessSQLException { |
409 | | - return Try.catching(_catchable).orThrow(UcanaccessSQLException::wrap); |
| 394 | + return tryCatch(_catchable, UcanaccessSQLException::wrap); |
| 395 | + } |
| 396 | + |
| 397 | + protected static final <R, T extends Throwable> R tryCatch(IThrowingSupplier<R, T> _catchable, Function<T, UcanaccessSQLException> _exSupplier) throws UcanaccessSQLException { |
| 398 | + return Try.catching(_catchable).orThrow(_exSupplier); |
410 | 399 | } |
411 | 400 |
|
412 | 401 | } |
0 commit comments