Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import java.sql.SQLException;

/**
* Interface for connecting to the H2 SQL Database.
* Functional interface for a function which produces some object using a
* connection.
*
* @param <T> The generic type that is returned.
*/
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/net/javadiscord/javabot/data/h2db/DbActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ public static <T> CompletableFuture<T> mapQueryAsync(String query, StatementModi
}

/**
* Counts the amount of rows that fit the given query.
* Gets a count, using a query which <strong>must</strong> return a long
* integer value as the first column of the result set.
*
* @param query The query.
* @param modifier The {@link StatementModifier}.
* @param modifier A modifier to use to set parameters for the query.
* @return The column value.
*/
public static long count(String query, StatementModifier modifier) {
Expand All @@ -103,13 +104,18 @@ public static long count(String query, StatementModifier modifier) {
}

/**
* Counts the amount of rows that fit the given query.
* Gets a count, using a query which <strong>must</strong> return a long
* integer value as the first column of the result set.
*
* @param query The query.
* @return The column value.
*/
public static long count(String query) {
try (var rs = Bot.dataSource.getConnection().createStatement().executeQuery(query)) {
try (
var conn = Bot.dataSource.getConnection();
var stmt = conn.createStatement()
) {
var rs = stmt.executeQuery(query);
if (!rs.next()) return 0;
return rs.getLong(1);
} catch (SQLException e) {
Expand All @@ -118,6 +124,23 @@ public static long count(String query) {
}
}

/**
* Convenience method similar to {@link DbActions#count(String, StatementModifier)}
* which allows for getting the count from a query using simple string
* formatting instead of having to define a statement modifier.
* <p>
* <strong>WARNING</strong>: This method should NEVER be called with
* user-provided data.
* </p>
*
* @param queryFormat The format string.
* @param args The set of arguments to pass to the formatter.
* @return The count.
*/
public static long countf(String queryFormat, Object... args) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of that method? Why not use PreparedStatement for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes count operations more compact and removes them from the specificity of jdbc.

countf("SELECT COUNT(id) FROM entries WHERE id = %d", 4);

versus

count("SELECT COUNT(id) FROM entries WHERE id = ?", s -> s.setLong(1, 4));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think such a method should be created since it may be used accidentally for user input without reading the documentation.

return count(String.format(queryFormat, args));
}

/**
* Updates a database table.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.sql.Connection;

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