Reformat database class and queries using text blocks
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2023-01-16 04:57:25 +01:00
parent 546637c188
commit 009fec3be3

View File

@ -12,29 +12,25 @@ import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class DatabaseSource
{
public class DatabaseSource {
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DatabaseSource.class);
private static final String JDBC_URL = "jdbc:sqlite:%path%";
private Connection dbConnection = null;
private final String dbPath;
public DatabaseSource(String dbPath)
{
public DatabaseSource(String dbPath) {
this.dbPath = dbPath;
}
private void logException(SQLException e)
{
private void logException(SQLException e) {
LOGGER.error("Database Exception", e);
}
public boolean connect()
{
public boolean connect() {
String url = JDBC_URL.replace("%path%", dbPath);
if(!close()) return false;
if (!close()) return false;
try {
dbConnection = DriverManager.getConnection(url);
@ -48,13 +44,10 @@ public class DatabaseSource
}
public boolean close()
{
if (dbConnection != null)
{
public boolean close() {
if (dbConnection != null) {
try {
if(!dbConnection.isClosed())
{
if (!dbConnection.isClosed()) {
dbConnection.close();
}
} catch (SQLException e) {
@ -94,38 +87,39 @@ public class DatabaseSource
*/
//todo: javadocs
public boolean initDb()
{
public boolean initDb() {
List<String> newTables = new ArrayList<>();
newTables.add("CREATE TABLE IF NOT EXISTS pending_disabled_messages (" +
"guild_id TEXT NOT NULL, " +
"channel_id TEXT NOT NULL," +
"message_id TEXT NOT NULL," +
"expiry_timestamp TEXT NOT NULL " +
");");
newTables.add("""
CREATE TABLE IF NOT EXISTS pending_disabled_messages (
guild_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
message_id TEXT NOT NULL,
expiry_timestamp TEXT NOT NULL);
""");
newTables.add("CREATE TABLE IF NOT EXISTS command_runners (" +
"guild_id TEXT NOT NULL, " +
"channel_id TEXT NOT NULL," + // channel the command was run in
"message_id TEXT NOT NULL," + // message id of the bot's response
"user_id TEXT NOT NULL, " + // user who ran the command
"channel_type TEXT NOT NULL" + // channel type (PRIVATE, FORUM, ...)
");");
newTables.add("""
CREATE TABLE IF NOT EXISTS command_runners (
guild_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
message_id TEXT NOT NULL,
user_id TEXT NOT NULL,
channel_type TEXT NOT NULL);
""");
newTables.add("CREATE TABLE IF NOT EXISTS urban_dictionary (" +
"message_id TEXT NOT NULL, " + // message id of the bot's response
"page INTEGER NOT NULL," + // page that we are currently on
"meanings TEXT NOT NULL," + // list of all meanings, serialized and encoded
"examples TEXT NOT NULL, " + // list of all examples, serialized and encoded
"contributors TEXT NOT NULL, " + // list of all contributors, serialized and encoded
"dates TEXT NOT NULL, " + // list of all submission dates, serialized and encoded
"term TEXT NOT NULL" + // the term that was searched
");");
newTables.add("""
CREATE TABLE IF NOT EXISTS urban_dictionary (
message_id TEXT NOT NULL,
page INTEGER NOT NULL,
meanings TEXT NOT NULL,
examples TEXT NOT NULL,
contributors TEXT NOT NULL,
dates TEXT NOT NULL,
term TEXT NOT NULL
);
""");
for(String sql : newTables)
{
for (String sql : newTables) {
try (Statement stmt = dbConnection.createStatement()) {
// execute the statement
stmt.execute(sql);
@ -138,14 +132,12 @@ public class DatabaseSource
return true;
}
public boolean trackRanCommandReply(Message message, User user)
{
public boolean trackRanCommandReply(Message message, User user) {
String userId = user.getId();
String guildId;
ChannelType channelType = message.getChannelType();
if(!(channelType.isGuild()))
{
if (!(channelType.isGuild())) {
guildId = userId;
} else {
guildId = message.getGuild().getId();
@ -155,12 +147,13 @@ public class DatabaseSource
String messageId = message.getId();
String query = "INSERT INTO command_runners " +
"(guild_id, channel_id, message_id, user_id, channel_type) VALUES " +
" (?, ?, ?, ?, ?);";
String query = """
INSERT INTO command_runners
(guild_id, channel_id, message_id, user_id, channel_type) VALUES
(?, ?, ?, ?, ?);
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, guildId);
preparedStatement.setString(2, channelId);
preparedStatement.setString(3, messageId);
@ -170,34 +163,31 @@ public class DatabaseSource
preparedStatement.executeUpdate();
return true;
} catch (SQLException e)
{
} catch (SQLException e) {
logException(e);
}
return false;
}
public boolean isUserTrackedFor(String userId, String messageId)
{
public boolean isUserTrackedFor(String userId, String messageId) {
String trackedUserId = getTrackedReplyUserId(messageId);
if(trackedUserId == null) return false;
if (trackedUserId == null) return false;
return userId.equals(trackedUserId);
}
public ChannelType getTrackedMessageChannelType(String messageId)
{
String query = "SELECT channel_type " +
"FROM command_runners " +
"WHERE message_id = ?;";
public ChannelType getTrackedMessageChannelType(String messageId) {
String query = """
""SELECT channel_type
FROM command_runners
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return null;
while(resultSet.next())
{
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
String channelTypeName = resultSet.getString("channel_type");
return ChannelType.valueOf(channelTypeName);
}
@ -210,19 +200,18 @@ public class DatabaseSource
}
public String getTrackedReplyUserId(String messageId)
{
String query = "SELECT user_id " +
"FROM command_runners " +
"WHERE message_id = ?;";
public String getTrackedReplyUserId(String messageId) {
String query = """
""SELECT user_id
FROM command_runners
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return null;
while(resultSet.next())
{
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
return resultSet.getString("user_id");
}
@ -233,15 +222,13 @@ public class DatabaseSource
return null;
}
public boolean queueDisabling(Message message)
{
public boolean queueDisabling(Message message) {
String messageId = message.getId();
String channelId = message.getChannel().getId();
String guildId;
ChannelType channelType = message.getChannelType();
if(!(channelType.isGuild()))
{
if (!(channelType.isGuild())) {
guildId = "PRIVATE";
} else {
guildId = message.getGuild().getId();
@ -252,12 +239,13 @@ public class DatabaseSource
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(Cache.getExpiryTimestampFormat());
String expiryTimeFormatted = dateTimeFormatter.format(expiryTime);
String query = "INSERT INTO pending_disabled_messages " +
"(guild_id, channel_id, message_id, expiry_timestamp) VALUES " +
" (?, ?, ?, ?);";
String query = """
INSERT INTO pending_disabled_messages
(guild_id, channel_id, message_id, expiry_timestamp) VALUES
(?, ?, ?, ?);
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, guildId);
preparedStatement.setString(2, channelId);
preparedStatement.setString(3, messageId);
@ -266,27 +254,25 @@ public class DatabaseSource
preparedStatement.executeUpdate();
return true;
} catch (SQLException e)
{
} catch (SQLException e) {
logException(e);
}
return false;
}
public List<String> getQueuedExpiringMessages()
{
public List<String> getQueuedExpiringMessages() {
List<String> messages = new ArrayList<>();
String query = "SELECT message_id " +
"FROM pending_disabled_messages ";
String query = """
SELECT message_id
FROM pending_disabled_messages;
""";
try (Statement statement = dbConnection.createStatement())
{
try (Statement statement = dbConnection.createStatement()) {
ResultSet resultSet = statement.executeQuery(query);
if(resultSet.isClosed()) return messages;
while(resultSet.next())
{
if (resultSet.isClosed()) return messages;
while (resultSet.next()) {
messages.add(resultSet.getString("message_id"));
}
} catch (SQLException e) {
@ -296,38 +282,31 @@ public class DatabaseSource
return messages;
}
public boolean untrackExpiredMessage(String messageId)
{
public boolean untrackExpiredMessage(String messageId) {
String query = "DELETE FROM pending_disabled_messages WHERE message_id = ?;";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
preparedStatement.execute();
} catch (SQLException e)
{
} catch (SQLException e) {
logException(e);
return false;
}
query = "DELETE FROM command_runners WHERE message_id = ?;";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
preparedStatement.execute();
} catch (SQLException e)
{
} catch (SQLException e) {
logException(e);
return false;
}
query = "DELETE FROM urban_dictionary WHERE message_id = ?;";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
preparedStatement.execute();
} catch (SQLException e)
{
} catch (SQLException e) {
logException(e);
return false;
}
@ -335,19 +314,18 @@ public class DatabaseSource
return true;
}
public String getQueuedExpiringMessageExpiryDate(String messageId)
{
String query = "SELECT expiry_timestamp " +
"FROM pending_disabled_messages " +
"WHERE message_id = ?;";
public String getQueuedExpiringMessageExpiryDate(String messageId) {
String query = """
SELECT expiry_timestamp
FROM pending_disabled_messages
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return null;
while(resultSet.next())
{
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
return resultSet.getString("expiry_timestamp");
}
@ -358,19 +336,18 @@ public class DatabaseSource
return null;
}
public String getQueuedExpiringMessageChannel(String messageId)
{
String query = "SELECT channel_id " +
"FROM pending_disabled_messages " +
"WHERE message_id = ?;";
public String getQueuedExpiringMessageChannel(String messageId) {
String query = """
SELECT channel_id
FROM pending_disabled_messages
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return null;
while(resultSet.next())
{
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
return resultSet.getString("channel_id");
}
@ -381,19 +358,18 @@ public class DatabaseSource
return null;
}
public String getQueuedExpiringMessageGuild(String messageId)
{
String query = "SELECT guild_id " +
"FROM pending_disabled_messages " +
"WHERE message_id = ?;";
public String getQueuedExpiringMessageGuild(String messageId) {
String query = """
SELECT guild_id
FROM pending_disabled_messages
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return null;
while(resultSet.next())
{
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
return resultSet.getString("guild_id");
}
@ -406,15 +382,15 @@ public class DatabaseSource
public boolean trackUrban(String meanings, String examples,
String contributors, String dates,
Message message, String term)
{
Message message, String term) {
String query = "INSERT INTO urban_dictionary " +
"(message_id, page, meanings, examples, contributors, dates, term) VALUES " +
" (?, ?, ?, ?, ?, ?, ?);";
String query = """
INSERT INTO urban_dictionary
(message_id, page, meanings, examples, contributors, dates, term) VALUES
(?, ?, ?, ?, ?, ?, ?);
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, message.getId());
preparedStatement.setInt(2, 0);
preparedStatement.setString(3, meanings);
@ -426,27 +402,25 @@ public class DatabaseSource
preparedStatement.executeUpdate();
return true;
} catch (SQLException e)
{
} catch (SQLException e) {
logException(e);
}
return false;
}
public int getUrbanPage(String messageId)
{
String query = "SELECT page " +
"FROM urban_dictionary " +
"WHERE message_id = ?;";
public int getUrbanPage(String messageId) {
String query = """
SELECT page
FROM urban_dictionary
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return 0;
while(resultSet.next())
{
if (resultSet.isClosed()) return 0;
while (resultSet.next()) {
return resultSet.getInt("page");
}
@ -457,19 +431,18 @@ public class DatabaseSource
return 0;
}
public String getUrbanMeanings(String messageId)
{
String query = "SELECT meanings " +
"FROM urban_dictionary " +
"WHERE message_id = ?;";
public String getUrbanMeanings(String messageId) {
String query = """
SELECT meanings
FROM urban_dictionary
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return null;
while(resultSet.next())
{
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
return resultSet.getString("meanings");
}
@ -480,19 +453,18 @@ public class DatabaseSource
return null;
}
public String getUrbanExamples(String messageId)
{
String query = "SELECT examples " +
"FROM urban_dictionary " +
"WHERE message_id = ?;";
public String getUrbanExamples(String messageId) {
String query = """
SELECT examples
FROM urban_dictionary
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return null;
while(resultSet.next())
{
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
return resultSet.getString("examples");
}
@ -503,19 +475,18 @@ public class DatabaseSource
return null;
}
public String getUrbanContributors(String messageId)
{
String query = "SELECT contributors " +
"FROM urban_dictionary " +
"WHERE message_id = ?;";
public String getUrbanContributors(String messageId) {
String query = """
SELECT contributors
FROM urban_dictionary
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return null;
while(resultSet.next())
{
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
return resultSet.getString("contributors");
}
@ -526,19 +497,18 @@ public class DatabaseSource
return null;
}
public String getUrbanDates(String messageId)
{
String query = "SELECT dates " +
"FROM urban_dictionary " +
"WHERE message_id = ?;";
public String getUrbanDates(String messageId) {
String query = """
SELECT dates
FROM urban_dictionary
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return null;
while(resultSet.next())
{
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
return resultSet.getString("dates");
}
@ -549,19 +519,18 @@ public class DatabaseSource
return null;
}
public String getUrbanTerm(String messageId)
{
String query = "SELECT term " +
"FROM urban_dictionary " +
"WHERE message_id = ?;";
public String getUrbanTerm(String messageId) {
String query = """
SELECT term
FROM urban_dictionary
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, messageId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.isClosed()) return null;
while(resultSet.next())
{
if (resultSet.isClosed()) return null;
while (resultSet.next()) {
return resultSet.getString("term");
}
@ -572,14 +541,14 @@ public class DatabaseSource
return null;
}
public boolean setUrbanPage(String messageId, int page)
{
String query = "UPDATE urban_dictionary " +
"SET page = ? " +
"WHERE message_id = ?;";
public boolean setUrbanPage(String messageId, int page) {
String query = """
UPDATE urban_dictionary
SET page = ?
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setInt(1, page);
preparedStatement.setString(2, messageId);
preparedStatement.executeUpdate();
@ -593,19 +562,19 @@ public class DatabaseSource
return false;
}
public boolean resetExpiryTimestamp(String messageId)
{
public boolean resetExpiryTimestamp(String messageId) {
LocalDateTime expiryTime = LocalDateTime.now().plusSeconds(Cache.getExpiryTimeSeconds());
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(Cache.getExpiryTimestampFormat());
String expiryTimeFormatted = dateTimeFormatter.format(expiryTime);
String query = "UPDATE pending_disabled_messages " +
"SET expiry_timestamp = ? " +
"WHERE message_id = ?;";
String query = """
UPDATE pending_disabled_messages
SET expiry_timestamp = ?
WHERE message_id = ?;
""";
try(PreparedStatement preparedStatement = dbConnection.prepareStatement(query))
{
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, expiryTimeFormatted);
preparedStatement.setString(2, messageId);
preparedStatement.executeUpdate();