Skip to content

Commit a1f5112

Browse files
committed
Improved javadoc and added countf function.
1 parent 4ec0691 commit a1f5112

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/main/java/net/javadiscord/javabot/data/h2db/ConnectionFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import java.sql.SQLException;
55

66
/**
7-
* Interface for connecting to the H2 SQL Database.
7+
* Functional interface for a function which produces some object using a
8+
* connection.
89
*
910
* @param <T> The generic type that is returned.
1011
*/

src/main/java/net/javadiscord/javabot/data/h2db/DbActions.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ public static <T> CompletableFuture<T> mapQueryAsync(String query, StatementModi
8484
}
8585

8686
/**
87-
* Counts the amount of rows that fit the given query.
87+
* Gets a count, using a query which <strong>must</strong> return a long
88+
* integer value as the first column of the result set.
8889
*
8990
* @param query The query.
90-
* @param modifier The {@link StatementModifier}.
91+
* @param modifier A modifier to use to set parameters for the query.
9192
* @return The column value.
9293
*/
9394
public static long count(String query, StatementModifier modifier) {
@@ -103,13 +104,18 @@ public static long count(String query, StatementModifier modifier) {
103104
}
104105

105106
/**
106-
* Counts the amount of rows that fit the given query.
107+
* Gets a count, using a query which <strong>must</strong> return a long
108+
* integer value as the first column of the result set.
107109
*
108110
* @param query The query.
109111
* @return The column value.
110112
*/
111113
public static long count(String query) {
112-
try (var rs = Bot.dataSource.getConnection().createStatement().executeQuery(query)) {
114+
try (
115+
var conn = Bot.dataSource.getConnection();
116+
var stmt = conn.createStatement()
117+
) {
118+
var rs = stmt.executeQuery(query);
113119
if (!rs.next()) return 0;
114120
return rs.getLong(1);
115121
} catch (SQLException e) {
@@ -118,6 +124,23 @@ public static long count(String query) {
118124
}
119125
}
120126

127+
/**
128+
* Convenience method similar to {@link DbActions#count(String, StatementModifier)}
129+
* which allows for getting the count from a query using simple string
130+
* formatting instead of
131+
* <p>
132+
* <strong>WARNING</strong>: This method should NEVER be called with
133+
* user-provided data.
134+
* </p>
135+
*
136+
* @param queryFormat The format string.
137+
* @param args The set of arguments to pass to the formatter.
138+
* @return The count.
139+
*/
140+
public static long countf(String queryFormat, Object... args) {
141+
return count(String.format(queryFormat, args));
142+
}
143+
121144
/**
122145
* Updates a database table.
123146
*

src/main/java/net/javadiscord/javabot/data/h2db/TransactionFunction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.sql.Connection;
44

55
/**
6-
* Simple Interface that handles transactions.
6+
* Interface that defines a function that executes using a transaction.
7+
* It is possible for an exception to be thrown, in which case the transaction
8+
* will attempt to roll back.
79
*/
810
public interface TransactionFunction {
911
void execute(Connection c) throws Exception;

0 commit comments

Comments
 (0)