mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-02-14 19:32:30 +01:00
CommandUseTable, Plan.getServerUUID method.
This commit is contained in:
parent
fe9c32c324
commit
44c9ee3925
@ -53,6 +53,7 @@ import org.bukkit.ChatColor;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
@ -484,4 +485,8 @@ public class Plan extends BukkitPlugin<Plan> {
|
||||
processingQueue.addToQueue(processor);
|
||||
}
|
||||
}
|
||||
|
||||
public static UUID getServerUUID() {
|
||||
return getInstance().serverInfoManager.getServerUUID();
|
||||
}
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ public class ActionsTable extends UserIDTable {
|
||||
+ "?, ?, ?)"
|
||||
);
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, Plan.getInstance().getServerInfoManager().getServerUUID().toString());
|
||||
statement.setString(2, Plan.getServerUUID().toString());
|
||||
statement.setInt(3, action.getDoneAction().getId());
|
||||
statement.setLong(4, action.getDate());
|
||||
statement.setString(5, action.getAdditionalInfo());
|
||||
|
@ -2,7 +2,9 @@ package main.java.com.djrapitops.plan.database.tables;
|
||||
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
import main.java.com.djrapitops.plan.Log;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.database.databases.SQLDB;
|
||||
import main.java.com.djrapitops.plan.database.sql.Select;
|
||||
import main.java.com.djrapitops.plan.database.sql.Sql;
|
||||
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
|
||||
import main.java.com.djrapitops.plan.utilities.Benchmark;
|
||||
@ -12,15 +14,19 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class CommandUseTable extends Table {
|
||||
|
||||
private final String columnCommand;
|
||||
private final String columnTimesUsed;
|
||||
private final String columnServerID; //TODO
|
||||
private final String columnCommandId = "id";
|
||||
private final String columnCommand = "command";
|
||||
private final String columnTimesUsed = "times_used";
|
||||
private final String columnServerID = "server_id";
|
||||
private ServerTable serverTable;
|
||||
|
||||
/**
|
||||
* @param db
|
||||
@ -28,9 +34,7 @@ public class CommandUseTable extends Table {
|
||||
*/
|
||||
public CommandUseTable(SQLDB db, boolean usingMySQL) {
|
||||
super("plan_commandusages", db, usingMySQL);
|
||||
columnCommand = "command";
|
||||
columnTimesUsed = "times_used";
|
||||
columnServerID = "server_id";
|
||||
serverTable = db.getServerTable();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -38,10 +42,15 @@ public class CommandUseTable extends Table {
|
||||
*/
|
||||
@Override
|
||||
public boolean createTable() {
|
||||
ServerTable serverTable = db.getServerTable();
|
||||
try {
|
||||
execute(TableSqlParser.createTable(tableName)
|
||||
.primaryKeyIDColumn(usingMySQL, columnCommandId, Sql.INT)
|
||||
.column(columnCommand, Sql.varchar(20)).notNull()
|
||||
.column(columnTimesUsed, Sql.INT).notNull()
|
||||
.column(columnServerID, Sql.INT).notNull()
|
||||
.primaryKey(usingMySQL, columnCommandId)
|
||||
.foreignKey(columnServerID, serverTable.toString(), serverTable.getColumnID())
|
||||
.toString()
|
||||
);
|
||||
return true;
|
||||
@ -52,15 +61,34 @@ public class CommandUseTable extends Table {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return @throws SQLException
|
||||
* Used to get all commands used in this server.
|
||||
*
|
||||
* @return command - times used Map
|
||||
* @throws SQLException
|
||||
*/
|
||||
public Map<String, Integer> getCommandUse() throws SQLException {
|
||||
return getCommandUse(Plan.getServerUUID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get all commands used in a server.
|
||||
*
|
||||
* @param serverUUID UUID of the server.
|
||||
* @return command - times used Map
|
||||
* @throws SQLException
|
||||
*/
|
||||
public Map<String, Integer> getCommandUse(UUID serverUUID) throws SQLException {
|
||||
ServerTable serverTable = db.getServerTable();
|
||||
Benchmark.start("Get CommandUse");
|
||||
Map<String, Integer> commandUse = new HashMap<>();
|
||||
PreparedStatement statement = null;
|
||||
ResultSet set = null;
|
||||
try {
|
||||
statement = prepareStatement("SELECT * FROM " + tableName);
|
||||
statement = prepareStatement(Select.from(tableName,
|
||||
columnCommand, columnTimesUsed)
|
||||
.where(columnServerID + "=" + serverTable.statementSelectServerID)
|
||||
.toString());
|
||||
statement.setString(1, serverUUID.toString());
|
||||
set = statement.executeQuery();
|
||||
while (set.next()) {
|
||||
String cmd = set.getString(columnCommand).toLowerCase();
|
||||
@ -102,7 +130,6 @@ public class CommandUseTable extends Table {
|
||||
String cmd = savedEntry.getKey();
|
||||
// IMPORTANT - not using saved as value
|
||||
Integer toSave = updateData.get(cmd);
|
||||
|
||||
if (toSave != null && toSave <= savedEntry.getValue()) {
|
||||
updateData.remove(cmd);
|
||||
}
|
||||
@ -117,7 +144,10 @@ public class CommandUseTable extends Table {
|
||||
private void updateCommands(Map<String, Integer> data) throws SQLException {
|
||||
PreparedStatement statement = null;
|
||||
try {
|
||||
String updateStatement = "UPDATE " + tableName + " SET " + columnTimesUsed + "=? WHERE (" + columnCommand + "=?)";
|
||||
String updateStatement = "UPDATE " + tableName + " SET " +
|
||||
columnTimesUsed + "=? " +
|
||||
"WHERE (" + columnCommand + "=?) AND (" +
|
||||
columnServerID + "=" + serverTable.statementSelectServerID + ")";
|
||||
statement = prepareStatement(updateStatement);
|
||||
boolean commitRequired = false;
|
||||
for (Map.Entry<String, Integer> entrySet : data.entrySet()) {
|
||||
@ -130,6 +160,7 @@ public class CommandUseTable extends Table {
|
||||
|
||||
statement.setInt(1, amount);
|
||||
statement.setString(2, key);
|
||||
statement.setString(3, Plan.getServerUUID().toString());
|
||||
statement.addBatch();
|
||||
commitRequired = true;
|
||||
}
|
||||
@ -147,10 +178,11 @@ public class CommandUseTable extends Table {
|
||||
try {
|
||||
String insertStatement = "INSERT INTO " + tableName + " ("
|
||||
+ columnCommand + ", "
|
||||
+ columnTimesUsed
|
||||
+ ") VALUES (?, ?)";
|
||||
+ columnTimesUsed + ", "
|
||||
+ columnServerID
|
||||
+ ") VALUES (?, ?, " + serverTable.statementSelectServerID + ")";
|
||||
statement = prepareStatement(insertStatement);
|
||||
boolean commitRequired = false;
|
||||
boolean addedRows = false;
|
||||
for (Map.Entry<String, Integer> entrySet : data.entrySet()) {
|
||||
String key = entrySet.getKey();
|
||||
Integer amount = entrySet.getValue();
|
||||
@ -161,15 +193,48 @@ public class CommandUseTable extends Table {
|
||||
|
||||
statement.setString(1, key);
|
||||
statement.setInt(2, amount);
|
||||
statement.setString(3, Plan.getServerUUID().toString());
|
||||
statement.addBatch();
|
||||
commitRequired = true;
|
||||
addedRows = true;
|
||||
}
|
||||
|
||||
if (commitRequired) {
|
||||
if (addedRows) {
|
||||
statement.executeBatch();
|
||||
}
|
||||
} finally {
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<String> getCommandByID(int id) throws SQLException {
|
||||
PreparedStatement statement = null;
|
||||
ResultSet set = null;
|
||||
try {
|
||||
statement = prepareStatement(Select.from(tableName, columnCommand).where(columnCommandId + "=?").toString());
|
||||
statement.setInt(1, id);
|
||||
set = statement.executeQuery();
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getString(columnCommand));
|
||||
}
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Integer> getCommandID(String command) throws SQLException {
|
||||
PreparedStatement statement = null;
|
||||
ResultSet set = null;
|
||||
try {
|
||||
statement = prepareStatement(Select.from(tableName, columnCommandId).where(columnCommand + "=?").toString());
|
||||
statement.setString(1, command);
|
||||
set = statement.executeQuery();
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getInt(columnCommandId));
|
||||
}
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
close(statement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,9 +19,17 @@ import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Table representing plan_servers in the database.
|
||||
* Table for managing multiple server's data in the database.
|
||||
* <p>
|
||||
* Used for managing multiple server's data in the database.
|
||||
* plan_servers contains columns:
|
||||
* <ul>
|
||||
* <li>id</li>
|
||||
* <li>uuid</li>
|
||||
* <li>name</li>
|
||||
* <li>web_address</li>
|
||||
* <li>is_installed</li>
|
||||
* </ul>
|
||||
* Columns refer to Server Information.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@ -226,6 +234,6 @@ public class ServerTable extends Table {
|
||||
}
|
||||
|
||||
public String getColumnID() {
|
||||
return columnServerID;
|
||||
return tableName + "." + columnServerID;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user