mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-10-31 07:50:09 +01:00
Refactored CommandUseTable#insertCommandUsage to an executable
This commit is contained in:
parent
f65ccbf3ff
commit
2245e9bd00
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<UUID, Map<String, Integer>> 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<String, Integer> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -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<UUID, Map<String, Integer>> 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<String, Integer> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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)";
|
||||
|
@ -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<UUID, Map<String, Integer>> ofServers) {
|
||||
commandUseTable.insertCommandUsage(ofServers);
|
||||
db.executeTransaction(new Transaction() {
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
execute(LargeStoreQueries.storeAllCommandUsageData(ofServers));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user