diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/events/CommandStoreTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/events/CommandStoreTransaction.java index 6a2d82112..2020b8a19 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/events/CommandStoreTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/events/CommandStoreTransaction.java @@ -16,13 +16,9 @@ */ package com.djrapitops.plan.db.access.transactions.events; -import com.djrapitops.plan.db.access.ExecStatement; import com.djrapitops.plan.db.access.transactions.Transaction; -import com.djrapitops.plan.db.sql.tables.CommandUseTable; -import com.djrapitops.plan.db.sql.tables.ServerTable; +import com.djrapitops.plan.db.sql.queries.DataStoreQueries; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.UUID; /** @@ -50,34 +46,6 @@ public class CommandStoreTransaction extends Transaction { @Override protected void performOperations() { - if (!updateCommandUse()) { - insertCommand(); - } - } - - private boolean updateCommandUse() { - String sql = "UPDATE " + CommandUseTable.TABLE_NAME + " SET " - + CommandUseTable.TIMES_USED + "=" + CommandUseTable.TIMES_USED + "+ 1" + - " WHERE " + CommandUseTable.SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID + - " AND " + CommandUseTable.COMMAND + "=?"; - - return execute(new ExecStatement(sql) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, serverUUID.toString()); - statement.setString(2, commandName); - } - }); - } - - private void insertCommand() { - execute(new ExecStatement(CommandUseTable.INSERT_STATEMENT) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, commandName); - statement.setInt(2, 1); - statement.setString(3, serverUUID.toString()); - } - }); + execute(DataStoreQueries.storeUsedCommandInformation(serverUUID, commandName)); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/DataStoreQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/DataStoreQueries.java new file mode 100644 index 000000000..28205037f --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/DataStoreQueries.java @@ -0,0 +1,74 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.db.sql.queries; + +import com.djrapitops.plan.db.access.ExecStatement; +import com.djrapitops.plan.db.access.Executable; +import com.djrapitops.plan.db.sql.tables.CommandUseTable; +import com.djrapitops.plan.db.sql.tables.ServerTable; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.UUID; + +/** + * Static method class for single item store queries. + * + * @author Rsl1122 + */ +public class DataStoreQueries { + + private DataStoreQueries() { + /* static method class */ + } + + public static Executable storeUsedCommandInformation(UUID serverUUID, String commandName) { + return connection -> { + if (!updateCommandUsage(serverUUID, commandName).execute(connection)) { + insertNewCommandUsage(serverUUID, commandName).execute(connection); + } + return false; + }; + } + + private static Executable updateCommandUsage(UUID serverUUID, String commandName) { + String sql = "UPDATE " + CommandUseTable.TABLE_NAME + " SET " + + CommandUseTable.TIMES_USED + "=" + CommandUseTable.TIMES_USED + "+ 1" + + " WHERE " + CommandUseTable.SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID + + " AND " + CommandUseTable.COMMAND + "=?"; + + return new ExecStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, serverUUID.toString()); + statement.setString(2, commandName); + } + }; + } + + private static Executable insertNewCommandUsage(UUID serverUUID, String commandName) { + return new ExecStatement(CommandUseTable.INSERT_STATEMENT) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, commandName); + statement.setInt(2, 1); + statement.setString(3, serverUUID.toString()); + } + }; + } + +} \ No newline at end of file