Refactored CommandUseTable#getCommandUse to a query

This commit is contained in:
Rsl1122 2019-01-26 12:09:14 +02:00
parent e309f83c3d
commit 41275a1f84
4 changed files with 36 additions and 37 deletions

View File

@ -19,6 +19,8 @@ package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.tables.CommandUseTable;
import com.djrapitops.plan.db.sql.tables.ServerTable;
import com.djrapitops.plan.db.sql.tables.UserInfoTable;
import com.djrapitops.plan.db.sql.tables.UsersTable;
@ -102,4 +104,33 @@ public class AggregateQueries {
}
};
}
/**
* Count how many times commands have been used on a server.
*
* @param serverUUID Server UUID of the Plan server.
* @return Map: Lowercase used command - Count of use times.
*/
public static Query<Map<String, Integer>> commandUsageCounts(UUID serverUUID) {
String sql = "SELECT " + CommandUseTable.COMMAND + ", " + CommandUseTable.TIMES_USED + " FROM " + CommandUseTable.TABLE_NAME +
"WHERE " + CommandUseTable.SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID;
return new QueryStatement<Map<String, Integer>>(sql, 5000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
}
@Override
public Map<String, Integer> processResults(ResultSet set) throws SQLException {
Map<String, Integer> commandUse = new HashMap<>();
while (set.next()) {
String cmd = set.getString(CommandUseTable.COMMAND).toLowerCase();
int amountUsed = set.getInt(CommandUseTable.TIMES_USED);
commandUse.put(cmd, amountUsed);
}
return commandUse;
}
};
}
}

View File

@ -23,6 +23,7 @@ import com.djrapitops.plan.data.store.mutators.PlayersMutator;
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.sql.queries.AggregateQueries;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.info.server.Server;
@ -74,7 +75,7 @@ public class ServerContainerQuery implements Query<ServerContainer> {
return db.query(OptionalFetchQueries.fetchPeakPlayerCount(serverUUID, twoDaysAgo)).orElse(null);
});
container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.getCommandUseTable().getCommandUse(serverUUID));
container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.query(AggregateQueries.commandUsageCounts(serverUUID)));
container.putCachingSupplier(ServerKeys.WORLD_TIMES, () -> db.getWorldTimesTable().getWorldTimesOfServer(serverUUID));
// Calculating getters

View File

@ -27,10 +27,7 @@ import com.djrapitops.plan.db.sql.parsing.Sql;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
* Table that is in charge of storing command data.
@ -72,37 +69,6 @@ public class CommandUseTable extends Table {
.toString();
}
/**
* Used to get all commands used in a server.
*
* @param serverUUID UUID of the server.
* @return command - times used Map
*/
public Map<String, Integer> getCommandUse(UUID serverUUID) {
String sql = Select.from(tableName,
COMMAND, TIMES_USED)
.where(SERVER_ID + "=" + serverTable.statementSelectServerID)
.toString();
return query(new QueryStatement<Map<String, Integer>>(sql, 5000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
}
@Override
public Map<String, Integer> processResults(ResultSet set) throws SQLException {
Map<String, Integer> commandUse = new HashMap<>();
while (set.next()) {
String cmd = set.getString(COMMAND).toLowerCase();
int amountUsed = set.getInt(TIMES_USED);
commandUse.put(cmd, amountUsed);
}
return commandUse;
}
});
}
public void commandUsed(String command) {
if (command.length() > 20) {
return;

View File

@ -31,6 +31,7 @@ import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.transactions.*;
import com.djrapitops.plan.db.patches.Patch;
import com.djrapitops.plan.db.sql.queries.AggregateQueries;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.db.sql.queries.LargeStoreQueries;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
@ -172,7 +173,7 @@ public abstract class CommonDBTest {
commitTest();
Map<String, Integer> commandUse = db.getCommandUseTable().getCommandUse(serverUUID);
Map<String, Integer> commandUse = db.query(AggregateQueries.commandUsageCounts(serverUUID));
assertEquals(expected, commandUse);
for (int i = 0; i < 3; i++) {
@ -186,7 +187,7 @@ public abstract class CommonDBTest {
expected.put("test", 3);
expected.put("tp", 6);
commandUse = db.getCommandUseTable().getCommandUse(serverUUID);
commandUse = db.query(AggregateQueries.commandUsageCounts(serverUUID));
assertEquals(expected, commandUse);
}