From 2245e9bd002e337350cdef86e2658487dff05099 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Thu, 24 Jan 2019 19:11:21 +0200 Subject: [PATCH] Refactored CommandUseTable#insertCommandUsage to an executable --- .../db/sql/queries/LargeStoreQueries.java | 63 +++++++++++++++++++ .../plan/db/sql/tables/CommandUseTable.java | 40 +++--------- .../plan/db/sql/tables/ServerTable.java | 5 ++ .../databases/sql/operation/SQLSaveOps.java | 9 ++- 4 files changed, 84 insertions(+), 33 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/LargeStoreQueries.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/LargeStoreQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/LargeStoreQueries.java new file mode 100644 index 000000000..4df2f2bdf --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/LargeStoreQueries.java @@ -0,0 +1,63 @@ +/* + * 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.ExecBatchStatement; +import com.djrapitops.plan.db.access.Executable; +import com.djrapitops.plan.db.sql.tables.CommandUseTable; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Map; +import java.util.UUID; + +/** + * Static method class for large storage queries. + * + * @author Rsl1122 + */ +public class LargeStoreQueries { + + private LargeStoreQueries() { + /* Static method class */ + } + + public static Executable storeAllCommandUsageData(Map> allCommandUsages) { + if (allCommandUsages.isEmpty()) { + return Executable.empty(); + } + + return new ExecBatchStatement(CommandUseTable.INSERT_STATEMENT) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + // Every Server + for (UUID serverUUID : allCommandUsages.keySet()) { + // Every Command + for (Map.Entry entry : allCommandUsages.get(serverUUID).entrySet()) { + String command = entry.getKey(); + int timesUsed = entry.getValue(); + + statement.setString(1, command); + statement.setInt(2, timesUsed); + statement.setString(3, serverUUID.toString()); + statement.addBatch(); + } + } + } + }; + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/CommandUseTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/CommandUseTable.java index 68ef83b0e..0ed9b3478 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/CommandUseTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/CommandUseTable.java @@ -48,18 +48,19 @@ public class CommandUseTable extends Table { public static final String COMMAND = "command"; public static final String TIMES_USED = "times_used"; + public static final String INSERT_STATEMENT = "INSERT INTO " + TABLE_NAME + " (" + + COMMAND + ", " + + TIMES_USED + ", " + + SERVER_ID + + ") VALUES (?, ?, " + ServerTable.STATEMENT_SELECT_SERVER_ID + ")"; + public CommandUseTable(SQLDB db) { super(TABLE_NAME, db); serverTable = db.getServerTable(); - insertStatement = "INSERT INTO " + tableName + " (" - + COMMAND + ", " - + TIMES_USED + ", " - + SERVER_ID - + ") VALUES (?, ?, " + serverTable.statementSelectServerID + ")"; + } private final ServerTable serverTable; - private String insertStatement; public static String createTableSQL(DBType dbType) { return CreateTableParser.create(TABLE_NAME, dbType) @@ -153,7 +154,7 @@ public class CommandUseTable extends Table { } private void insertCommand(String command) { - execute(new ExecStatement(insertStatement) { + execute(new ExecStatement(INSERT_STATEMENT) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, command); @@ -181,29 +182,4 @@ public class CommandUseTable extends Table { } }); } - - public void insertCommandUsage(Map> allCommandUsages) { - if (allCommandUsages.isEmpty()) { - return; - } - - executeBatch(new ExecStatement(insertStatement) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - // Every Server - for (UUID serverUUID : allCommandUsages.keySet()) { - // Every Command - for (Map.Entry entry : allCommandUsages.get(serverUUID).entrySet()) { - String command = entry.getKey(); - int timesUsed = entry.getValue(); - - statement.setString(1, command); - statement.setInt(2, timesUsed); - statement.setString(3, serverUUID.toString()); - statement.addBatch(); - } - } - } - }); - } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ServerTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ServerTable.java index 32cc9e170..e3b4c7907 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ServerTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ServerTable.java @@ -49,6 +49,11 @@ public class ServerTable extends Table { public static final String INSTALLED = "is_installed"; public static final String MAX_PLAYERS = "max_players"; + public static final String STATEMENT_SELECT_SERVER_ID = + "(SELECT " + TABLE_NAME + "." + SERVER_ID + " FROM " + TABLE_NAME + + " WHERE " + TABLE_NAME + "." + SERVER_UUID + "=?" + + " LIMIT 1)"; + public ServerTable(SQLDB db) { super(TABLE_NAME, db); statementSelectServerID = "(" + Select.from(tableName, tableName + "." + SERVER_ID).where(tableName + "." + SERVER_UUID + "=?").toString() + " LIMIT 1)"; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java index 624d2f2f7..7596737a8 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java @@ -20,6 +20,8 @@ import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.data.container.*; import com.djrapitops.plan.data.store.objects.Nickname; import com.djrapitops.plan.db.SQLDB; +import com.djrapitops.plan.db.access.transactions.Transaction; +import com.djrapitops.plan.db.sql.queries.LargeStoreQueries; import com.djrapitops.plan.system.database.databases.operation.SaveOperations; import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.settings.config.Config; @@ -46,7 +48,12 @@ public class SQLSaveOps extends SQLOps implements SaveOperations { @Override public void insertCommandUsage(Map> ofServers) { - commandUseTable.insertCommandUsage(ofServers); + db.executeTransaction(new Transaction() { + @Override + protected void performOperations() { + execute(LargeStoreQueries.storeAllCommandUsageData(ofServers)); + } + }); } @Override