99
1010import java .sql .*;
1111import java .util .Optional ;
12- import java .util .concurrent .CompletableFuture ;
13- import java .util .concurrent .ExecutorService ;
14- import java .util .function .Function ;
1512
1613import javax .sql .DataSource ;
1714
2219@ Service
2320@ RequiredArgsConstructor
2421public class DbActions {
25- private final ExecutorService asyncPool ;
2622 @ Getter
2723 private final DataSource dataSource ;
2824
29- /**
30- * Consumes an action based on the given {@link ConnectionConsumer}.
31- *
32- * @param consumer The {@link ConnectionConsumer}.
33- * @throws SQLException If an error occurs.
34- */
35- public void doAction (@ NotNull ConnectionConsumer consumer ) throws SQLException {
36- try (Connection c = dataSource .getConnection ()) {
37- consumer .consume (c );
38- }
39- }
40-
41- /**
42- * Maps an action based on the given {@link ConnectionFunction}.
43- *
44- * @param function The {@link ConnectionFunction}.
45- * @param <T> The generic type.
46- * @return A generic type.
47- * @throws SQLException If an error occurs.
48- */
49- public <T > T map (@ NotNull ConnectionFunction <T > function ) throws SQLException {
50- try (Connection c = dataSource .getConnection ()) {
51- return function .apply (c );
52- }
53- }
54-
5525 /**
5626 * Maps a query.
5727 *
@@ -70,28 +40,6 @@ public <T> T mapQuery(@NotNull String query, @NotNull StatementModifier modifier
7040 }
7141 }
7242
73- /**
74- * Maps a query asynchronous.
75- *
76- * @param query The query.
77- * @param modifier The {@link StatementModifier}.
78- * @param mapper The {@link ResultSetMapper}.
79- * @param <T> The generic type.
80- * @return A generic type.
81- */
82- public <T > @ NotNull CompletableFuture <T > mapQueryAsync (@ NotNull String query , @ NotNull StatementModifier modifier , @ NotNull ResultSetMapper <T > mapper ) {
83- CompletableFuture <T > cf = new CompletableFuture <>();
84- asyncPool .submit (() -> {
85- try {
86- cf .complete (mapQuery (query , modifier , mapper ));
87- } catch (SQLException e ) {
88- ExceptionLogger .capture (e , DbActions .class .getSimpleName ());
89- cf .completeExceptionally (e );
90- }
91- });
92- return cf ;
93- }
94-
9543 /**
9644 * Gets a count, using a query which <strong>must</strong> return a long
9745 * integer value as the first column of the result set.
@@ -133,23 +81,6 @@ public long count(@NotNull String query) {
13381 }
13482 }
13583
136- /**
137- * Convenience method similar to {@link DbActions#count(String, StatementModifier)}
138- * which allows for getting the count from a query using simple string
139- * formatting instead of having to define a statement modifier.
140- * <p>
141- * <strong>WARNING</strong>: This method should NEVER be called with
142- * user-provided data.
143- * </p>
144- *
145- * @param queryFormat The format string.
146- * @param args The set of arguments to pass to the formatter.
147- * @return The count.
148- */
149- public long countf (@ NotNull String queryFormat , @ NotNull Object ... args ) {
150- return count (String .format (queryFormat , args ));
151- }
152-
15384 /**
15485 * Updates a database table.
15586 *
@@ -168,71 +99,6 @@ public int update(@NotNull String query, Object @NotNull ... params) throws SQLE
16899 }
169100 }
170101
171- /**
172- * Does an asynchronous database action using the bot's async pool.
173- *
174- * @param consumer The consumer that will use a connection.
175- * @return A future that completes when the action is complete.
176- */
177- public @ NotNull CompletableFuture <Void > doAsyncAction (ConnectionConsumer consumer ) {
178- CompletableFuture <Void > future = new CompletableFuture <>();
179- asyncPool .submit (() -> {
180- try (Connection c = dataSource .getConnection ()) {
181- consumer .consume (c );
182- future .complete (null );
183- } catch (SQLException e ) {
184- ExceptionLogger .capture (e , DbActions .class .getSimpleName ());
185- future .completeExceptionally (e );
186- }
187- });
188- return future ;
189- }
190-
191- /**
192- * Does an asynchronous database action using the bot's async pool, and
193- * wraps access to the connection behind a data access object that can be
194- * built using the provided dao constructor.
195- *
196- * @param daoConstructor A function to build a DAO using a connection.
197- * @param consumer The consumer that does something with the DAO.
198- * @param <T> The type of data access object. Usually some kind of repository.
199- * @return A future that completes when the action is complete.
200- */
201- public <T > @ NotNull CompletableFuture <Void > doAsyncDaoAction (Function <Connection , T > daoConstructor , DaoConsumer <T > consumer ) {
202- CompletableFuture <Void > future = new CompletableFuture <>();
203- asyncPool .submit (() -> {
204- try (Connection c = dataSource .getConnection ()) {
205- T dao = daoConstructor .apply (c );
206- consumer .consume (dao );
207- future .complete (null );
208- } catch (SQLException e ) {
209- ExceptionLogger .capture (e , DbActions .class .getSimpleName ());
210- future .completeExceptionally (e );
211- }
212- });
213- return future ;
214- }
215-
216- /**
217- * Maps a {@link ConnectionFunction} asynchronous.
218- *
219- * @param function The {@link ConnectionFunction}.
220- * @param <T> The generic type.
221- * @return A generic type.
222- */
223- public <T > @ NotNull CompletableFuture <T > mapAsync (ConnectionFunction <T > function ) {
224- CompletableFuture <T > future = new CompletableFuture <>();
225- asyncPool .submit (() -> {
226- try (Connection c = dataSource .getConnection ()) {
227- future .complete (function .apply (c ));
228- } catch (SQLException e ) {
229- ExceptionLogger .capture (e , DbActions .class .getSimpleName ());
230- future .completeExceptionally (e );
231- }
232- });
233- return future ;
234- }
235-
236102 /**
237103 * Fetches a single result from the database.
238104 *
0 commit comments