mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-12 06:48:50 +02:00
Refactored all SQL tables to use Column enums instead of static variable
This commit is contained in:
parent
9347ee383a
commit
63887c7e29
@ -1,5 +1,7 @@
|
||||
package com.djrapitops.plan.system.database.databases.sql.statements;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Insert extends SqlParser {
|
||||
|
||||
public Insert(String table) {
|
||||
@ -7,6 +9,11 @@ public class Insert extends SqlParser {
|
||||
addSpace();
|
||||
}
|
||||
|
||||
public static String values(String table, Column... columns) {
|
||||
String[] cols = Arrays.stream(columns).map(Column::get).toArray(String[]::new);
|
||||
return values(table, cols);
|
||||
}
|
||||
|
||||
public static String values(String table, String... columns) {
|
||||
Insert parser = new Insert(table);
|
||||
parser.append("(");
|
||||
|
@ -1,11 +1,18 @@
|
||||
package com.djrapitops.plan.system.database.databases.sql.statements;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Select extends WhereParser {
|
||||
|
||||
public Select(String start) {
|
||||
super(start);
|
||||
}
|
||||
|
||||
public static Select from(String table, Column... columns) {
|
||||
String[] cols = Arrays.stream(columns).map(Column::get).toArray(String[]::new);
|
||||
return from(table, cols);
|
||||
}
|
||||
|
||||
public static Select from(String table, String... columns) {
|
||||
Select parser = new Select("SELECT ");
|
||||
int size = columns.length;
|
||||
|
@ -88,6 +88,10 @@ public class TableSqlParser extends SqlParser {
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableSqlParser primaryKeyIDColumn(boolean mySQL, Column column) {
|
||||
return primaryKeyIDColumn(mySQL, column.get());
|
||||
}
|
||||
|
||||
public TableSqlParser primaryKeyIDColumn(boolean mySQL, String column) {
|
||||
if (columns > 0) {
|
||||
append(", ");
|
||||
@ -99,6 +103,10 @@ public class TableSqlParser extends SqlParser {
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableSqlParser primaryKey(boolean mySQL, Column column) {
|
||||
return primaryKey(mySQL, column.get());
|
||||
}
|
||||
|
||||
public TableSqlParser primaryKey(boolean mySQL, String column) {
|
||||
if (mySQL) {
|
||||
if (columns > 0) {
|
||||
|
@ -4,6 +4,8 @@
|
||||
*/
|
||||
package com.djrapitops.plan.system.database.databases.sql.statements;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
@ -14,6 +16,10 @@ public class Update extends WhereParser {
|
||||
addSpace();
|
||||
}
|
||||
|
||||
public static Update values(String table, Column... values) {
|
||||
String[] cols = Arrays.stream(values).map(Column::get).toArray(String[]::new);
|
||||
return values(table, cols);
|
||||
}
|
||||
public static Update values(String table, String... values) {
|
||||
Update parser = new Update(table);
|
||||
|
||||
|
@ -11,6 +11,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Select;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
@ -24,63 +25,87 @@ import java.util.*;
|
||||
|
||||
/**
|
||||
* Table that is in charge of storing actions.
|
||||
* <p>
|
||||
* plan_actions contains columns:
|
||||
* <ul>
|
||||
* <li>user_id (plan_users: id)</li>
|
||||
* <li>server_id (plan_servers: id)</li>
|
||||
* <li>action_id</li>
|
||||
* <li>date</li>
|
||||
* <li>additional_info</li>
|
||||
* </ul>
|
||||
*
|
||||
* Table Name: plan_actions
|
||||
*
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
* @see Action
|
||||
*/
|
||||
public class ActionsTable extends UserIDTable {
|
||||
|
||||
private static final String columnServerID = "server_id";
|
||||
private static final String columnDate = "date";
|
||||
private static final String columnActionID = "action_id";
|
||||
private static final String columnAdditionalInfo = "additional_info";
|
||||
|
||||
private final ServerTable serverTable;
|
||||
private String insertStatement;
|
||||
|
||||
public ActionsTable(SQLDB db) {
|
||||
super("plan_actions", db);
|
||||
serverTable = db.getServerTable();
|
||||
insertStatement = "INSERT INTO " + tableName + " ("
|
||||
+ columnUserID + ", "
|
||||
+ columnServerID + ", "
|
||||
+ columnActionID + ", "
|
||||
+ columnDate + ", "
|
||||
+ columnAdditionalInfo
|
||||
+ Col.USER_ID + ", "
|
||||
+ Col.SERVER_ID + ", "
|
||||
+ Col.ACTION_ID + ", "
|
||||
+ Col.DATE + ", "
|
||||
+ Col.ADDITIONAL_INFO
|
||||
+ ") VALUES ("
|
||||
+ usersTable.statementSelectID + ", "
|
||||
+ serverTable.statementSelectServerID + ", "
|
||||
+ "?, ?, ?)";
|
||||
}
|
||||
|
||||
private final ServerTable serverTable;
|
||||
private String insertStatement;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
ServerTable serverTable = db.getServerTable();
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.column(columnUserID, Sql.INT).notNull()
|
||||
.column(columnServerID, Sql.INT).notNull()
|
||||
.column(columnDate, Sql.LONG).notNull()
|
||||
.column(columnActionID, Sql.INT).notNull()
|
||||
.column(columnAdditionalInfo, Sql.varchar(300))
|
||||
.foreignKey(columnUserID, usersTable.toString(), usersTable.getColumnID())
|
||||
.foreignKey(columnServerID, serverTable.toString(), serverTable.getColumnID())
|
||||
.column(Col.USER_ID, Sql.INT).notNull()
|
||||
.column(Col.SERVER_ID, Sql.INT).notNull()
|
||||
.column(Col.DATE, Sql.LONG).notNull()
|
||||
.column(Col.ACTION_ID, Sql.INT).notNull()
|
||||
.column(Col.ADDITIONAL_INFO, Sql.varchar(300))
|
||||
.foreignKey(Col.USER_ID, usersTable.toString(), UsersTable.Col.ID)
|
||||
.foreignKey(Col.SERVER_ID, serverTable.toString(), ServerTable.Col.SERVER_ID)
|
||||
.toString());
|
||||
}
|
||||
|
||||
public void alterTableV12() {
|
||||
if (usingMySQL) {
|
||||
executeUnsafe("ALTER TABLE " + tableName + " MODIFY " + columnAdditionalInfo + " VARCHAR(300)");
|
||||
executeUnsafe("ALTER TABLE " + tableName + " MODIFY " + Col.ADDITIONAL_INFO + " VARCHAR(300)");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get all Actions done by a user on this server.
|
||||
*
|
||||
* @param uuid UUID of the player
|
||||
* @return List of actions done by the player. Does not include the kills.
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public List<Action> getActions(UUID uuid) throws SQLException {
|
||||
String sql = Select.all(tableName)
|
||||
.where(Col.USER_ID + "=" + usersTable.statementSelectID)
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<List<Action>>(sql, 5000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Action> processResults(ResultSet set) throws SQLException {
|
||||
List<Action> actions = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
int serverID = set.getInt(Col.SERVER_ID.get());
|
||||
long date = set.getLong(Col.DATE.get());
|
||||
Actions doneAction = Actions.getById(set.getInt(Col.ACTION_ID.get()));
|
||||
String additionalInfo = set.getString(Col.ADDITIONAL_INFO.get());
|
||||
actions.add(new Action(date, doneAction, additionalInfo, serverID));
|
||||
}
|
||||
return actions;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void insertAction(UUID uuid, Action action) throws SQLException {
|
||||
execute(new ExecStatement(insertStatement) {
|
||||
@Override
|
||||
@ -94,53 +119,20 @@ public class ActionsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get all Actions done by a user on this server.
|
||||
*
|
||||
* @param uuid UUID of the player
|
||||
* @return List of actions done by the player. Does not include the kills.
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public List<Action> getActions(UUID uuid) throws SQLException {
|
||||
String sql = Select.all(tableName)
|
||||
.where(columnUserID + "=" + usersTable.statementSelectID)
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<List<Action>>(sql, 5000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Action> processResults(ResultSet set) throws SQLException {
|
||||
List<Action> actions = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
int serverID = set.getInt(columnServerID);
|
||||
long date = set.getLong(columnDate);
|
||||
Actions doneAction = Actions.getById(set.getInt(columnActionID));
|
||||
String additionalInfo = set.getString(columnAdditionalInfo);
|
||||
actions.add(new Action(date, doneAction, additionalInfo, serverID));
|
||||
}
|
||||
return actions;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, Map<UUID, List<Action>>> getAllActions() throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as uuid";
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
columnActionID + ", " +
|
||||
columnDate + ", " +
|
||||
columnAdditionalInfo + ", " +
|
||||
Col.ACTION_ID + ", " +
|
||||
Col.DATE + ", " +
|
||||
Col.ADDITIONAL_INFO + ", " +
|
||||
usersUUIDColumn + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID;
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.USER_ID +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + Col.SERVER_ID;
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, Map<UUID, List<Action>>>>(sql, 20000) {
|
||||
@Override
|
||||
@ -153,9 +145,9 @@ public class ActionsTable extends UserIDTable {
|
||||
Map<UUID, List<Action>> serverMap = map.getOrDefault(serverUUID, new HashMap<>());
|
||||
List<Action> actions = serverMap.getOrDefault(uuid, new ArrayList<>());
|
||||
|
||||
long date = set.getLong(columnDate);
|
||||
Actions doneAction = Actions.getById(set.getInt(columnActionID));
|
||||
String additionalInfo = set.getString(columnAdditionalInfo);
|
||||
long date = set.getLong(Col.DATE.get());
|
||||
Actions doneAction = Actions.getById(set.getInt(Col.ACTION_ID.get()));
|
||||
String additionalInfo = set.getString(Col.ADDITIONAL_INFO.get());
|
||||
|
||||
actions.add(new Action(date, doneAction, additionalInfo, -1));
|
||||
|
||||
@ -167,6 +159,45 @@ public class ActionsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, List<Action>> getServerActions(UUID serverUUID) throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as uuid";
|
||||
String sql = "SELECT " +
|
||||
Col.ACTION_ID + ", " +
|
||||
Col.DATE + ", " +
|
||||
Col.ADDITIONAL_INFO + ", " +
|
||||
usersUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.USER_ID +
|
||||
" WHERE " + serverTable.statementSelectServerID + "=" + Col.SERVER_ID;
|
||||
|
||||
return query(new QueryStatement<Map<UUID, List<Action>>>(sql, 20000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<UUID, List<Action>> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, List<Action>> map = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID uuid = UUID.fromString(set.getString("uuid"));
|
||||
|
||||
List<Action> actions = map.getOrDefault(uuid, new ArrayList<>());
|
||||
|
||||
long date = set.getLong(Col.DATE.get());
|
||||
Actions doneAction = Actions.getById(set.getInt(Col.ACTION_ID.get()));
|
||||
String additionalInfo = set.getString(Col.ADDITIONAL_INFO.get());
|
||||
|
||||
actions.add(new Action(date, doneAction, additionalInfo, -1));
|
||||
|
||||
map.put(uuid, actions);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void insertActions(Map<UUID, Map<UUID, List<Action>>> allActions) throws SQLException {
|
||||
if (Verify.isEmpty(allActions)) {
|
||||
return;
|
||||
@ -195,42 +226,26 @@ public class ActionsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, List<Action>> getServerActions(UUID serverUUID) throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||
String sql = "SELECT " +
|
||||
columnActionID + ", " +
|
||||
columnDate + ", " +
|
||||
columnAdditionalInfo + ", " +
|
||||
usersUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||
" WHERE " + serverTable.statementSelectServerID + "=" + columnServerID;
|
||||
public enum Col implements Column {
|
||||
USER_ID(UserIDTable.Col.USER_ID.get()),
|
||||
SERVER_ID("server_id"),
|
||||
DATE("date"),
|
||||
ACTION_ID("action_id"),
|
||||
ADDITIONAL_INFO("additional_info");
|
||||
|
||||
return query(new QueryStatement<Map<UUID, List<Action>>>(sql, 20000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
}
|
||||
private final String column;
|
||||
|
||||
@Override
|
||||
public Map<UUID, List<Action>> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, List<Action>> map = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID uuid = UUID.fromString(set.getString("uuid"));
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
List<Action> actions = map.getOrDefault(uuid, new ArrayList<>());
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
long date = set.getLong(columnDate);
|
||||
Actions doneAction = Actions.getById(set.getInt(columnActionID));
|
||||
String additionalInfo = set.getString(columnAdditionalInfo);
|
||||
|
||||
actions.add(new Action(date, doneAction, additionalInfo, -1));
|
||||
|
||||
map.put(uuid, actions);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Select;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
@ -19,52 +20,43 @@ import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Table that is in charge of storing command data.
|
||||
* <p>
|
||||
* Table Name: plan_commandusages
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class CommandUseTable extends Table {
|
||||
|
||||
private static final String columnCommandId = "id";
|
||||
private static final String columnCommand = "command";
|
||||
private static final String columnTimesUsed = "times_used";
|
||||
private static final String columnServerID = "server_id";
|
||||
|
||||
private final ServerTable serverTable;
|
||||
private String insertStatement;
|
||||
|
||||
public CommandUseTable(SQLDB db) {
|
||||
super("plan_commandusages", db);
|
||||
serverTable = db.getServerTable();
|
||||
insertStatement = "INSERT INTO " + tableName + " ("
|
||||
+ columnCommand + ", "
|
||||
+ columnTimesUsed + ", "
|
||||
+ columnServerID
|
||||
+ Col.COMMAND + ", "
|
||||
+ Col.TIMES_USED + ", "
|
||||
+ Col.SERVER_ID
|
||||
+ ") VALUES (?, ?, " + serverTable.statementSelectServerID + ")";
|
||||
}
|
||||
|
||||
private final ServerTable serverTable;
|
||||
private String insertStatement;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
ServerTable serverTable = db.getServerTable();
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.primaryKeyIDColumn(usingMySQL, columnCommandId)
|
||||
.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())
|
||||
.primaryKeyIDColumn(usingMySQL, Col.COMMAND_ID)
|
||||
.column(Col.COMMAND, Sql.varchar(20)).notNull()
|
||||
.column(Col.TIMES_USED, Sql.INT).notNull()
|
||||
.column(Col.SERVER_ID, Sql.INT).notNull()
|
||||
.primaryKey(usingMySQL, Col.COMMAND_ID)
|
||||
.foreignKey(Col.SERVER_ID, serverTable.toString(), ServerTable.Col.SERVER_ID)
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get all commands used in this server.
|
||||
*
|
||||
* @return command - times used Map
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public Map<String, Integer> getCommandUse() throws SQLException {
|
||||
return getCommandUse(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get all commands used in a server.
|
||||
*
|
||||
@ -74,8 +66,8 @@ public class CommandUseTable extends Table {
|
||||
*/
|
||||
public Map<String, Integer> getCommandUse(UUID serverUUID) throws SQLException {
|
||||
String sql = Select.from(tableName,
|
||||
columnCommand, columnTimesUsed)
|
||||
.where(columnServerID + "=" + serverTable.statementSelectServerID)
|
||||
Col.COMMAND, Col.TIMES_USED)
|
||||
.where(Col.SERVER_ID + "=" + serverTable.statementSelectServerID)
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Map<String, Integer>>(sql, 5000) {
|
||||
@ -88,8 +80,8 @@ public class CommandUseTable extends Table {
|
||||
public Map<String, Integer> processResults(ResultSet set) throws SQLException {
|
||||
Map<String, Integer> commandUse = new HashMap<>();
|
||||
while (set.next()) {
|
||||
String cmd = set.getString(columnCommand).toLowerCase();
|
||||
int amountUsed = set.getInt(columnTimesUsed);
|
||||
String cmd = set.getString(Col.COMMAND.get()).toLowerCase();
|
||||
int amountUsed = set.getInt(Col.TIMES_USED.get());
|
||||
commandUse.put(cmd, amountUsed);
|
||||
}
|
||||
return commandUse;
|
||||
@ -97,15 +89,25 @@ public class CommandUseTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get all commands used in this server.
|
||||
*
|
||||
* @return command - times used Map
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public Map<String, Integer> getCommandUse() throws SQLException {
|
||||
return getCommandUse(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public void commandUsed(String command) throws SQLException {
|
||||
if (command.length() > 20) {
|
||||
return;
|
||||
}
|
||||
|
||||
String sql = "UPDATE " + tableName + " SET "
|
||||
+ columnTimesUsed + "=" + columnTimesUsed + "+ 1" +
|
||||
" WHERE " + columnServerID + "=" + serverTable.statementSelectServerID +
|
||||
" AND " + columnCommand + "=?";
|
||||
+ Col.TIMES_USED + "=" + Col.TIMES_USED + "+ 1" +
|
||||
" WHERE " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID +
|
||||
" AND " + Col.COMMAND + "=?";
|
||||
|
||||
boolean updated = execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
@ -119,6 +121,25 @@ public class CommandUseTable extends Table {
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<String> getCommandByID(int id) throws SQLException {
|
||||
String sql = Select.from(tableName, Col.COMMAND).where(Col.COMMAND_ID + "=?").toString();
|
||||
|
||||
return query(new QueryStatement<Optional<String>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setInt(1, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getString(Col.COMMAND.get()));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void insertCommand(String command) throws SQLException {
|
||||
execute(new ExecStatement(insertStatement) {
|
||||
@Override
|
||||
@ -130,27 +151,8 @@ public class CommandUseTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<String> getCommandByID(int id) throws SQLException {
|
||||
String sql = Select.from(tableName, columnCommand).where(columnCommandId + "=?").toString();
|
||||
|
||||
return query(new QueryStatement<Optional<String>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setInt(1, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getString(columnCommand));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<Integer> getCommandID(String command) throws SQLException {
|
||||
String sql = Select.from(tableName, columnCommandId).where(columnCommand + "=?").toString();
|
||||
String sql = Select.from(tableName, Col.COMMAND_ID).where(Col.COMMAND + "=?").toString();
|
||||
|
||||
return query(new QueryStatement<Optional<Integer>>(sql) {
|
||||
@Override
|
||||
@ -161,7 +163,7 @@ public class CommandUseTable extends Table {
|
||||
@Override
|
||||
public Optional<Integer> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getInt(columnCommandId));
|
||||
return Optional.of(set.getInt(Col.COMMAND_ID.get()));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
@ -169,14 +171,14 @@ public class CommandUseTable extends Table {
|
||||
}
|
||||
|
||||
public Map<UUID, Map<String, Integer>> getAllCommandUsages() throws SQLException {
|
||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
columnCommand + ", " +
|
||||
columnTimesUsed + ", " +
|
||||
Col.COMMAND + ", " +
|
||||
Col.TIMES_USED + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID;
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + Col.SERVER_ID;
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, Map<String, Integer>>>(sql, 10000) {
|
||||
@Override
|
||||
@ -187,8 +189,8 @@ public class CommandUseTable extends Table {
|
||||
|
||||
Map<String, Integer> serverMap = map.getOrDefault(serverUUID, new HashMap<>());
|
||||
|
||||
String command = set.getString(columnCommand);
|
||||
int timesUsed = set.getInt(columnTimesUsed);
|
||||
String command = set.getString(Col.COMMAND.get());
|
||||
int timesUsed = set.getInt(Col.TIMES_USED.get());
|
||||
|
||||
serverMap.put(command, timesUsed);
|
||||
map.put(serverUUID, serverMap);
|
||||
@ -198,6 +200,28 @@ public class CommandUseTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public enum Col implements Column {
|
||||
COMMAND_ID("id"),
|
||||
SERVER_ID("server_id"),
|
||||
COMMAND("command"),
|
||||
TIMES_USED("times_used");
|
||||
|
||||
private final String column;
|
||||
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
|
||||
public void insertCommandUsage(Map<UUID, Map<String, Integer>> allCommandUsages) throws SQLException {
|
||||
if (allCommandUsages.isEmpty()) {
|
||||
return;
|
||||
|
@ -6,6 +6,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Select;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
@ -17,52 +18,55 @@ import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Table that is in charge of storing common IP and Geolocation data for users.
|
||||
* <p>
|
||||
* Table Name: plan_ips
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class GeoInfoTable extends UserIDTable {
|
||||
|
||||
private static final String columnIP = "ip";
|
||||
private static final String columnGeolocation = "geolocation";
|
||||
private static final String columnLastUsed = "last_used";
|
||||
private String insertStatement;
|
||||
|
||||
public GeoInfoTable(SQLDB db) {
|
||||
super("plan_ips", db);
|
||||
insertStatement = "INSERT INTO " + tableName + " ("
|
||||
+ columnUserID + ", "
|
||||
+ columnIP + ", "
|
||||
+ columnGeolocation + ", "
|
||||
+ columnLastUsed
|
||||
+ Col.USER_ID + ", "
|
||||
+ Col.IP + ", "
|
||||
+ Col.GEOLOCATION + ", "
|
||||
+ Col.LAST_USED
|
||||
+ ") VALUES ("
|
||||
+ usersTable.statementSelectID + ", "
|
||||
+ "?, ?, ?)";
|
||||
}
|
||||
|
||||
private String insertStatement;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.column(columnUserID, Sql.INT).notNull()
|
||||
.column(columnIP, Sql.varchar(39)).notNull()
|
||||
.column(columnGeolocation, Sql.varchar(50)).notNull()
|
||||
.column(columnLastUsed, Sql.LONG).notNull().defaultValue("0")
|
||||
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
|
||||
.column(Col.USER_ID, Sql.INT).notNull()
|
||||
.column(Col.IP, Sql.varchar(39)).notNull()
|
||||
.column(Col.GEOLOCATION, Sql.varchar(50)).notNull()
|
||||
.column(Col.LAST_USED, Sql.LONG).notNull().defaultValue("0")
|
||||
.foreignKey(Col.USER_ID, usersTable.getTableName(), UsersTable.Col.ID)
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
|
||||
public void alterTableV12() {
|
||||
if (usingMySQL) {
|
||||
executeUnsafe("ALTER TABLE " + tableName + " MODIFY " + columnIP + " VARCHAR(39) NOT NULL");
|
||||
executeUnsafe("ALTER TABLE " + tableName + " MODIFY " + Col.IP + " VARCHAR(39) NOT NULL");
|
||||
}
|
||||
}
|
||||
|
||||
public void alterTableV13() {
|
||||
addColumns(columnLastUsed + " bigint NOT NULL DEFAULT 0");
|
||||
addColumns(Col.LAST_USED + " bigint NOT NULL DEFAULT 0");
|
||||
}
|
||||
|
||||
public List<GeoInfo> getGeoInfo(UUID uuid) throws SQLException {
|
||||
String sql = "SELECT DISTINCT * FROM " + tableName +
|
||||
" WHERE " + columnUserID + "=" + usersTable.statementSelectID;
|
||||
" WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID;
|
||||
|
||||
return query(new QueryStatement<List<GeoInfo>>(sql, 100) {
|
||||
@Override
|
||||
@ -74,9 +78,9 @@ public class GeoInfoTable extends UserIDTable {
|
||||
public List<GeoInfo> processResults(ResultSet set) throws SQLException {
|
||||
List<GeoInfo> geoInfo = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
String ip = set.getString(columnIP);
|
||||
String geolocation = set.getString(columnGeolocation);
|
||||
long lastUsed = set.getLong(columnLastUsed);
|
||||
String ip = set.getString(Col.IP.get());
|
||||
String geolocation = set.getString(Col.GEOLOCATION.get());
|
||||
long lastUsed = set.getLong(Col.LAST_USED.get());
|
||||
geoInfo.add(new GeoInfo(ip, geolocation, lastUsed));
|
||||
}
|
||||
return geoInfo;
|
||||
@ -84,6 +88,24 @@ public class GeoInfoTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
private void updateGeoInfo(UUID uuid, GeoInfo info) throws SQLException {
|
||||
String sql = "UPDATE " + tableName + " SET "
|
||||
+ Col.LAST_USED + "=?" +
|
||||
" WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID +
|
||||
" AND " + Col.IP + "=?" +
|
||||
" AND " + Col.GEOLOCATION + "=?";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, info.getLastUsed());
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.setString(3, info.getIp());
|
||||
statement.setString(4, info.getGeolocation());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void saveGeoInfo(UUID uuid, GeoInfo info) throws SQLException {
|
||||
List<GeoInfo> geoInfo = getGeoInfo(uuid);
|
||||
if (geoInfo.contains(info)) {
|
||||
@ -104,27 +126,9 @@ public class GeoInfoTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
private void updateGeoInfo(UUID uuid, GeoInfo info) throws SQLException {
|
||||
String sql = "UPDATE " + tableName + " SET "
|
||||
+ columnLastUsed + "=?" +
|
||||
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
|
||||
" AND " + columnIP + "=?" +
|
||||
" AND " + columnGeolocation + "=?";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, info.getLastUsed());
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.setString(3, info.getIp());
|
||||
statement.setString(4, info.getGeolocation());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<String> getGeolocation(String ip) throws SQLException {
|
||||
String sql = Select.from(tableName, columnGeolocation)
|
||||
.where(columnIP + "=?")
|
||||
String sql = Select.from(tableName, Col.GEOLOCATION)
|
||||
.where(Col.IP + "=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Optional<String>>(sql) {
|
||||
@ -136,7 +140,7 @@ public class GeoInfoTable extends UserIDTable {
|
||||
@Override
|
||||
public Optional<String> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getString(columnGeolocation));
|
||||
return Optional.of(set.getString(Col.GEOLOCATION.get()));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
@ -144,15 +148,15 @@ public class GeoInfoTable extends UserIDTable {
|
||||
}
|
||||
|
||||
public Map<UUID, List<GeoInfo>> getAllGeoInfo() throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as uuid";
|
||||
String sql = "SELECT " +
|
||||
columnIP + ", " +
|
||||
columnGeolocation + ", " +
|
||||
columnLastUsed + ", " +
|
||||
Col.IP + ", " +
|
||||
Col.GEOLOCATION + ", " +
|
||||
Col.LAST_USED + ", " +
|
||||
usersUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID;
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.USER_ID;
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, List<GeoInfo>>>(sql, 50000) {
|
||||
@Override
|
||||
@ -163,9 +167,9 @@ public class GeoInfoTable extends UserIDTable {
|
||||
|
||||
List<GeoInfo> userGeoInfo = geoLocations.getOrDefault(uuid, new ArrayList<>());
|
||||
|
||||
String ip = set.getString(columnIP);
|
||||
String geolocation = set.getString(columnGeolocation);
|
||||
long lastUsed = set.getLong(columnLastUsed);
|
||||
String ip = set.getString(Col.IP.get());
|
||||
String geolocation = set.getString(Col.GEOLOCATION.get());
|
||||
long lastUsed = set.getLong(Col.LAST_USED.get());
|
||||
userGeoInfo.add(new GeoInfo(ip, geolocation, lastUsed));
|
||||
|
||||
geoLocations.put(uuid, userGeoInfo);
|
||||
@ -175,6 +179,28 @@ public class GeoInfoTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public enum Col implements Column {
|
||||
USER_ID(UserIDTable.Col.USER_ID.get()),
|
||||
IP("ip"),
|
||||
GEOLOCATION("geolocation"),
|
||||
LAST_USED("last_used");
|
||||
|
||||
private final String column;
|
||||
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
|
||||
public void insertAllGeoInfo(Map<UUID, List<GeoInfo>> allIPsAndGeolocations) throws SQLException {
|
||||
if (Verify.isEmpty(allIPsAndGeolocations)) {
|
||||
return;
|
||||
|
@ -7,6 +7,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
@ -17,45 +18,45 @@ import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Table that is in charge of storing kill data for each session.
|
||||
* <p>
|
||||
* Table Name: plan_kills
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class KillsTable extends UserIDTable {
|
||||
|
||||
private static final String columnKillerUserID = "killer_id";
|
||||
private static final String columnVictimUserID = "victim_id";
|
||||
private static final String columnWeapon = "weapon";
|
||||
private static final String columnDate = "date";
|
||||
private static final String columnSessionID = "session_id";
|
||||
|
||||
private final SessionsTable sessionsTable;
|
||||
private String insertStatement;
|
||||
|
||||
public KillsTable(SQLDB db) {
|
||||
super("plan_kills", db);
|
||||
sessionsTable = db.getSessionsTable();
|
||||
insertStatement = "INSERT INTO " + tableName + " ("
|
||||
+ columnKillerUserID + ", "
|
||||
+ columnVictimUserID + ", "
|
||||
+ columnSessionID + ", "
|
||||
+ columnDate + ", "
|
||||
+ columnWeapon
|
||||
+ Col.KILLER_ID + ", "
|
||||
+ Col.VICTIM_ID + ", "
|
||||
+ Col.SESSION_ID + ", "
|
||||
+ Col.DATE + ", "
|
||||
+ Col.WEAPON
|
||||
+ ") VALUES ("
|
||||
+ usersTable.statementSelectID + ", "
|
||||
+ usersTable.statementSelectID + ", "
|
||||
+ "?, ?, ?)";
|
||||
}
|
||||
|
||||
private final SessionsTable sessionsTable;
|
||||
private String insertStatement;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.column(columnKillerUserID, Sql.INT).notNull()
|
||||
.column(columnVictimUserID, Sql.INT).notNull()
|
||||
.column(columnWeapon, Sql.varchar(30)).notNull()
|
||||
.column(columnDate, Sql.LONG).notNull()
|
||||
.column(columnSessionID, Sql.INT).notNull()
|
||||
.foreignKey(columnKillerUserID, usersTable.getTableName(), usersTable.getColumnID())
|
||||
.foreignKey(columnVictimUserID, usersTable.getTableName(), usersTable.getColumnID())
|
||||
.foreignKey(columnSessionID, sessionsTable.getTableName(), sessionsTable.getColumnID())
|
||||
.column(Col.KILLER_ID, Sql.INT).notNull()
|
||||
.column(Col.VICTIM_ID, Sql.INT).notNull()
|
||||
.column(Col.WEAPON, Sql.varchar(30)).notNull()
|
||||
.column(Col.DATE, Sql.LONG).notNull()
|
||||
.column(Col.SESSION_ID, Sql.INT).notNull()
|
||||
.foreignKey(Col.KILLER_ID, usersTable.getTableName(), UsersTable.Col.ID)
|
||||
.foreignKey(Col.VICTIM_ID, usersTable.getTableName(), UsersTable.Col.ID)
|
||||
.foreignKey(Col.SESSION_ID, sessionsTable.getTableName(), SessionsTable.Col.ID)
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
@ -63,8 +64,8 @@ public class KillsTable extends UserIDTable {
|
||||
@Override
|
||||
public void removeUser(UUID uuid) throws SQLException {
|
||||
String sql = "DELETE FROM " + tableName +
|
||||
" WHERE " + columnKillerUserID + " = " + usersTable.statementSelectID +
|
||||
" OR " + columnVictimUserID + " = " + usersTable.statementSelectID;
|
||||
" WHERE " + Col.KILLER_ID + " = " + usersTable.statementSelectID +
|
||||
" OR " + Col.VICTIM_ID + " = " + usersTable.statementSelectID;
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
@ -75,6 +76,43 @@ public class KillsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public void addKillsToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as victim_uuid";
|
||||
String sql = "SELECT " +
|
||||
Col.SESSION_ID + ", " +
|
||||
Col.DATE + ", " +
|
||||
Col.WEAPON + ", " +
|
||||
usersUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.VICTIM_ID +
|
||||
" WHERE " + Col.KILLER_ID + "=" + usersTable.statementSelectID;
|
||||
|
||||
query(new QueryStatement<Object>(sql, 50000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object processResults(ResultSet set) throws SQLException {
|
||||
while (set.next()) {
|
||||
int sessionID = set.getInt(Col.SESSION_ID.get());
|
||||
Session session = sessions.get(sessionID);
|
||||
if (session == null) {
|
||||
continue;
|
||||
}
|
||||
String uuidS = set.getString("victim_uuid");
|
||||
UUID victim = UUID.fromString(uuidS);
|
||||
long date = set.getLong(Col.DATE.get());
|
||||
String weapon = set.getString(Col.WEAPON.get());
|
||||
session.getPlayerKills().add(new PlayerKill(victim, weapon, date));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void savePlayerKills(UUID uuid, int sessionID, List<PlayerKill> playerKills) throws SQLException {
|
||||
if (Verify.isEmpty(playerKills)) {
|
||||
return;
|
||||
@ -102,56 +140,19 @@ public class KillsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public void addKillsToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as victim_uuid";
|
||||
String sql = "SELECT " +
|
||||
columnSessionID + ", " +
|
||||
columnDate + ", " +
|
||||
columnWeapon + ", " +
|
||||
usersUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnVictimUserID +
|
||||
" WHERE " + columnKillerUserID + "=" + usersTable.statementSelectID;
|
||||
|
||||
query(new QueryStatement<Object>(sql, 50000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object processResults(ResultSet set) throws SQLException {
|
||||
while (set.next()) {
|
||||
int sessionID = set.getInt(columnSessionID);
|
||||
Session session = sessions.get(sessionID);
|
||||
if (session == null) {
|
||||
continue;
|
||||
}
|
||||
String uuidS = set.getString("victim_uuid");
|
||||
UUID victim = UUID.fromString(uuidS);
|
||||
long date = set.getLong(columnDate);
|
||||
String weapon = set.getString(columnWeapon);
|
||||
session.getPlayerKills().add(new PlayerKill(victim, weapon, date));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, List<PlayerKill>> getPlayerKills() throws SQLException {
|
||||
String usersVictimIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersKillerIDColumn = "a." + usersTable.getColumnID();
|
||||
String usersVictimUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as victim_uuid";
|
||||
String usersKillerUUIDColumn = "a." + usersTable.getColumnUUID() + " as killer_uuid";
|
||||
String usersVictimIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersKillerIDColumn = "a." + UsersTable.Col.ID;
|
||||
String usersVictimUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as victim_uuid";
|
||||
String usersKillerUUIDColumn = "a." + UsersTable.Col.UUID + " as killer_uuid";
|
||||
String sql = "SELECT " +
|
||||
columnDate + ", " +
|
||||
columnWeapon + ", " +
|
||||
Col.DATE + ", " +
|
||||
Col.WEAPON + ", " +
|
||||
usersVictimUUIDColumn + ", " +
|
||||
usersKillerUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersVictimIDColumn + "=" + columnVictimUserID +
|
||||
" INNER JOIN " + usersTable + " a on " + usersKillerIDColumn + "=" + columnKillerUserID;
|
||||
" INNER JOIN " + usersTable + " on " + usersVictimIDColumn + "=" + Col.VICTIM_ID +
|
||||
" INNER JOIN " + usersTable + " a on " + usersKillerIDColumn + "=" + Col.KILLER_ID;
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, List<PlayerKill>>>(sql, 50000) {
|
||||
@Override
|
||||
@ -160,8 +161,8 @@ public class KillsTable extends UserIDTable {
|
||||
while (set.next()) {
|
||||
UUID killer = UUID.fromString(set.getString("killer_uuid"));
|
||||
UUID victim = UUID.fromString(set.getString("victim_uuid"));
|
||||
long date = set.getLong(columnDate);
|
||||
String weapon = set.getString(columnWeapon);
|
||||
long date = set.getLong(Col.DATE.get());
|
||||
String weapon = set.getString(Col.WEAPON.get());
|
||||
List<PlayerKill> kills = allKills.getOrDefault(killer, new ArrayList<>());
|
||||
kills.add(new PlayerKill(victim, weapon, date));
|
||||
allKills.put(killer, kills);
|
||||
@ -171,6 +172,39 @@ public class KillsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<Integer, List<PlayerKill>> getAllPlayerKillsBySessionID() throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as victim_uuid";
|
||||
String sql = "SELECT " +
|
||||
Col.SESSION_ID + ", " +
|
||||
Col.DATE + ", " +
|
||||
Col.WEAPON + ", " +
|
||||
usersUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.VICTIM_ID;
|
||||
|
||||
return query(new QueryAllStatement<Map<Integer, List<PlayerKill>>>(sql, 50000) {
|
||||
@Override
|
||||
public Map<Integer, List<PlayerKill>> processResults(ResultSet set) throws SQLException {
|
||||
Map<Integer, List<PlayerKill>> allPlayerKills = new HashMap<>();
|
||||
while (set.next()) {
|
||||
int sessionID = set.getInt(Col.SESSION_ID.get());
|
||||
|
||||
List<PlayerKill> playerKills = allPlayerKills.getOrDefault(sessionID, new ArrayList<>());
|
||||
|
||||
String uuidS = set.getString("victim_uuid");
|
||||
UUID victim = UUID.fromString(uuidS);
|
||||
long date = set.getLong(Col.DATE.get());
|
||||
String weapon = set.getString(Col.WEAPON.get());
|
||||
playerKills.add(new PlayerKill(victim, weapon, date));
|
||||
|
||||
allPlayerKills.put(sessionID, playerKills);
|
||||
}
|
||||
return allPlayerKills;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void addKillsToSessions(Map<UUID, Map<UUID, List<Session>>> map) throws SQLException {
|
||||
Map<Integer, List<PlayerKill>> playerKillsBySessionID = getAllPlayerKillsBySessionID();
|
||||
for (UUID serverUUID : map.keySet()) {
|
||||
@ -221,36 +255,26 @@ public class KillsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<Integer, List<PlayerKill>> getAllPlayerKillsBySessionID() throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as victim_uuid";
|
||||
String sql = "SELECT " +
|
||||
columnSessionID + ", " +
|
||||
columnDate + ", " +
|
||||
columnWeapon + ", " +
|
||||
usersUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnVictimUserID;
|
||||
public enum Col implements Column {
|
||||
KILLER_ID("killer_id"),
|
||||
VICTIM_ID("victim_id"),
|
||||
SESSION_ID("session_id"),
|
||||
WEAPON("weapon"),
|
||||
DATE("date");
|
||||
|
||||
return query(new QueryAllStatement<Map<Integer, List<PlayerKill>>>(sql, 50000) {
|
||||
@Override
|
||||
public Map<Integer, List<PlayerKill>> processResults(ResultSet set) throws SQLException {
|
||||
Map<Integer, List<PlayerKill>> allPlayerKills = new HashMap<>();
|
||||
while (set.next()) {
|
||||
int sessionID = set.getInt(columnSessionID);
|
||||
private final String column;
|
||||
|
||||
List<PlayerKill> playerKills = allPlayerKills.getOrDefault(sessionID, new ArrayList<>());
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
String uuidS = set.getString("victim_uuid");
|
||||
UUID victim = UUID.fromString(uuidS);
|
||||
long date = set.getLong(columnDate);
|
||||
String weapon = set.getString(columnWeapon);
|
||||
playerKills.add(new PlayerKill(victim, weapon, date));
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
allPlayerKills.put(sessionID, playerKills);
|
||||
}
|
||||
return allPlayerKills;
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
@ -16,37 +17,40 @@ import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Table that is in charge of storing nickname data.
|
||||
* <p>
|
||||
* Table Name: plan_nicknames
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class NicknamesTable extends UserIDTable {
|
||||
|
||||
private static final String columnNick = "nickname";
|
||||
private static final String columnServerID = "server_id";
|
||||
|
||||
private final ServerTable serverTable;
|
||||
private String insertStatement;
|
||||
|
||||
public NicknamesTable(SQLDB db) {
|
||||
super("plan_nicknames", db);
|
||||
serverTable = db.getServerTable();
|
||||
insertStatement = "INSERT INTO " + tableName + " (" +
|
||||
columnUserID + ", " +
|
||||
columnServerID + ", " +
|
||||
columnNick +
|
||||
Col.USER_ID + ", " +
|
||||
Col.SERVER_ID + ", " +
|
||||
Col.NICKNAME +
|
||||
") VALUES (" +
|
||||
usersTable.statementSelectID + ", " +
|
||||
serverTable.statementSelectServerID + ", " +
|
||||
"?)";
|
||||
}
|
||||
|
||||
private final ServerTable serverTable;
|
||||
private String insertStatement;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.column(columnUserID, Sql.INT).notNull()
|
||||
.column(columnNick, Sql.varchar(75)).notNull()
|
||||
.column(columnServerID, Sql.INT).notNull()
|
||||
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
|
||||
.foreignKey(columnServerID, serverTable.getTableName(), serverTable.getColumnID())
|
||||
.column(Col.USER_ID, Sql.INT).notNull()
|
||||
.column(Col.NICKNAME, Sql.varchar(75)).notNull()
|
||||
.column(Col.SERVER_ID, Sql.INT).notNull()
|
||||
.foreignKey(Col.USER_ID, usersTable.getTableName(), UsersTable.Col.ID)
|
||||
.foreignKey(Col.SERVER_ID, serverTable.getTableName(), ServerTable.Col.SERVER_ID)
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
@ -61,14 +65,14 @@ public class NicknamesTable extends UserIDTable {
|
||||
* @throws SQLException when an error at retrieval happens
|
||||
*/
|
||||
public Map<UUID, List<String>> getAllNicknames(UUID uuid) throws SQLException {
|
||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
columnNick + ", " +
|
||||
Col.NICKNAME + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID +
|
||||
" WHERE (" + columnUserID + "=" + usersTable.statementSelectID + ")";
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + Col.SERVER_ID +
|
||||
" WHERE (" + Col.USER_ID + "=" + usersTable.statementSelectID + ")";
|
||||
|
||||
return query(new QueryStatement<Map<UUID, List<String>>>(sql, 5000) {
|
||||
|
||||
@ -85,7 +89,7 @@ public class NicknamesTable extends UserIDTable {
|
||||
|
||||
List<String> nicknames = map.getOrDefault(serverUUID, new ArrayList<>());
|
||||
|
||||
nicknames.add(set.getString(columnNick));
|
||||
nicknames.add(set.getString(Col.NICKNAME.get()));
|
||||
|
||||
map.put(serverUUID, nicknames);
|
||||
}
|
||||
@ -94,6 +98,45 @@ public class NicknamesTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get nicknames of the user on a server.
|
||||
* <p>
|
||||
* Get's nicknames from other servers as well.
|
||||
*
|
||||
* @param uuid UUID of the Player
|
||||
* @param serverUUID UUID of the server
|
||||
* @return The nicknames of the User
|
||||
* @throws SQLException when an error at retrieval happens
|
||||
*/
|
||||
public List<String> getNicknames(UUID uuid, UUID serverUUID) throws SQLException {
|
||||
String sql = "SELECT " + Col.NICKNAME + " FROM " + tableName +
|
||||
" WHERE (" + Col.USER_ID + "=" + usersTable.statementSelectID + ")" +
|
||||
" AND " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryStatement<List<String>>(sql, 1000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> processResults(ResultSet set) throws SQLException {
|
||||
List<String> nicknames = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
String nickname = set.getString(Col.NICKNAME.get());
|
||||
if (nickname.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (!nicknames.contains(nickname)) {
|
||||
nicknames.add(nickname);
|
||||
}
|
||||
}
|
||||
return nicknames;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get nicknames of the user on THIS server.
|
||||
* <p>
|
||||
@ -107,41 +150,36 @@ public class NicknamesTable extends UserIDTable {
|
||||
return getNicknames(uuid, ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get nicknames of the user on a server.
|
||||
* <p>
|
||||
* Get's nicknames from other servers as well.
|
||||
*
|
||||
* @param uuid UUID of the Player
|
||||
* @param serverUUID UUID of the server
|
||||
* @return The nicknames of the User
|
||||
* @throws SQLException when an error at retrieval happens
|
||||
*/
|
||||
public List<String> getNicknames(UUID uuid, UUID serverUUID) throws SQLException {
|
||||
String sql = "SELECT " + columnNick + " FROM " + tableName +
|
||||
" WHERE (" + columnUserID + "=" + usersTable.statementSelectID + ")" +
|
||||
" AND " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||
public Map<UUID, Map<UUID, List<String>>> getAllNicknames() throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as uuid";
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
Col.NICKNAME + ", " +
|
||||
usersUUIDColumn + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.USER_ID +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + Col.SERVER_ID;
|
||||
|
||||
return query(new QueryStatement<List<String>>(sql, 1000) {
|
||||
return query(new QueryAllStatement<Map<UUID, Map<UUID, List<String>>>>(sql, 5000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> processResults(ResultSet set) throws SQLException {
|
||||
List<String> nicknames = new ArrayList<>();
|
||||
public Map<UUID, Map<UUID, List<String>>> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, Map<UUID, List<String>>> map = new HashMap<>();
|
||||
while (set.next()) {
|
||||
String nickname = set.getString(columnNick);
|
||||
if (nickname.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (!nicknames.contains(nickname)) {
|
||||
nicknames.add(nickname);
|
||||
}
|
||||
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
|
||||
UUID uuid = UUID.fromString(set.getString("uuid"));
|
||||
|
||||
Map<UUID, List<String>> serverMap = map.getOrDefault(serverUUID, new HashMap<>());
|
||||
List<String> nicknames = serverMap.getOrDefault(uuid, new ArrayList<>());
|
||||
|
||||
nicknames.add(set.getString(Col.NICKNAME.get()));
|
||||
|
||||
serverMap.put(uuid, nicknames);
|
||||
map.put(serverUUID, serverMap);
|
||||
}
|
||||
return nicknames;
|
||||
return map;
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -162,38 +200,25 @@ public class NicknamesTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, Map<UUID, List<String>>> getAllNicknames() throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
columnNick + ", " +
|
||||
usersUUIDColumn + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID;
|
||||
public enum Col implements Column {
|
||||
USER_ID(UserIDTable.Col.USER_ID.get()),
|
||||
SERVER_ID("server_id"),
|
||||
NICKNAME("nickname");
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, Map<UUID, List<String>>>>(sql, 5000) {
|
||||
@Override
|
||||
public Map<UUID, Map<UUID, List<String>>> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, Map<UUID, List<String>>> map = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
|
||||
UUID uuid = UUID.fromString(set.getString("uuid"));
|
||||
private final String column;
|
||||
|
||||
Map<UUID, List<String>> serverMap = map.getOrDefault(serverUUID, new HashMap<>());
|
||||
List<String> nicknames = serverMap.getOrDefault(uuid, new ArrayList<>());
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
nicknames.add(set.getString(columnNick));
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
serverMap.put(uuid, nicknames);
|
||||
map.put(serverUUID, serverMap);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
|
||||
public void insertNicknames(Map<UUID, Map<UUID, List<String>>> allNicknames) throws SQLException {
|
||||
@ -222,13 +247,4 @@ public class NicknamesTable extends UserIDTable {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public String getColumnNick() {
|
||||
return columnNick;
|
||||
}
|
||||
|
||||
public String getColumnUserID() {
|
||||
return columnUserID;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,10 +11,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Insert;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Select;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.*;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
@ -24,35 +21,39 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Table that is in charge of storing WebUser data.
|
||||
* <p>
|
||||
* Table Name: plan_security
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
* @see WebUser
|
||||
*/
|
||||
public class SecurityTable extends Table {
|
||||
|
||||
private static final String columnUser = "username";
|
||||
private static final String columnSaltedHash = "salted_pass_hash";
|
||||
private static final String columnPermLevel = "permission_level";
|
||||
private String insertStatement;
|
||||
|
||||
public SecurityTable(SQLDB db) {
|
||||
super("plan_security", db);
|
||||
insertStatement = Insert.values(tableName,
|
||||
columnUser,
|
||||
columnSaltedHash,
|
||||
columnPermLevel);
|
||||
Col.USERNAME,
|
||||
Col.SALT_PASSWORD_HASH,
|
||||
Col.PERMISSION_LEVEL);
|
||||
}
|
||||
|
||||
private String insertStatement;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.column(columnUser, Sql.varchar(100)).notNull().unique()
|
||||
.column(columnSaltedHash, Sql.varchar(100)).notNull().unique()
|
||||
.column(columnPermLevel, Sql.INT).notNull()
|
||||
.column(Col.USERNAME, Sql.varchar(100)).notNull().unique()
|
||||
.column(Col.SALT_PASSWORD_HASH, Sql.varchar(100)).notNull().unique()
|
||||
.column(Col.PERMISSION_LEVEL, Sql.INT).notNull()
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
|
||||
public void removeUser(String user) throws SQLException {
|
||||
String sql = "DELETE FROM " + tableName + " WHERE (" + columnUser + "=?)";
|
||||
String sql = "DELETE FROM " + tableName + " WHERE (" + Col.USERNAME + "=?)";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
@ -62,6 +63,27 @@ public class SecurityTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public WebUser getWebUser(String user) throws SQLException {
|
||||
String sql = Select.all(tableName).where(Col.USERNAME + "=?").toString();
|
||||
|
||||
return query(new QueryStatement<WebUser>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebUser processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
String saltedPassHash = set.getString(Col.SALT_PASSWORD_HASH.get());
|
||||
int permissionLevel = set.getInt(Col.PERMISSION_LEVEL.get());
|
||||
return new WebUser(user, saltedPassHash, permissionLevel);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void addNewUser(WebUser info) throws SQLException {
|
||||
addNewUser(info.getName(), info.getSaltedPassHash(), info.getPermLevel());
|
||||
}
|
||||
@ -81,27 +103,6 @@ public class SecurityTable extends Table {
|
||||
return getWebUser(user) != null;
|
||||
}
|
||||
|
||||
public WebUser getWebUser(String user) throws SQLException {
|
||||
String sql = Select.all(tableName).where(columnUser + "=?").toString();
|
||||
|
||||
return query(new QueryStatement<WebUser>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebUser processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
String saltedPassHash = set.getString(columnSaltedHash);
|
||||
int permissionLevel = set.getInt(columnPermLevel);
|
||||
return new WebUser(user, saltedPassHash, permissionLevel);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public List<WebUser> getUsers() throws SQLException {
|
||||
String sql = Select.all(tableName).toString();
|
||||
|
||||
@ -110,9 +111,9 @@ public class SecurityTable extends Table {
|
||||
public List<WebUser> processResults(ResultSet set) throws SQLException {
|
||||
List<WebUser> list = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
String user = set.getString(columnUser);
|
||||
String saltedPassHash = set.getString(columnSaltedHash);
|
||||
int permissionLevel = set.getInt(columnPermLevel);
|
||||
String user = set.getString(Col.USERNAME.get());
|
||||
String saltedPassHash = set.getString(Col.SALT_PASSWORD_HASH.get());
|
||||
int permissionLevel = set.getInt(Col.PERMISSION_LEVEL.get());
|
||||
WebUser info = new WebUser(user, saltedPassHash, permissionLevel);
|
||||
list.add(info);
|
||||
}
|
||||
@ -121,6 +122,27 @@ public class SecurityTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public enum Col implements Column {
|
||||
USERNAME("username"),
|
||||
SALT_PASSWORD_HASH("salted_pass_hash"),
|
||||
PERMISSION_LEVEL("permission_level");
|
||||
|
||||
private final String column;
|
||||
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
|
||||
public void addUsers(List<WebUser> users) throws SQLException {
|
||||
if (Verify.isEmpty(users)) {
|
||||
return;
|
||||
|
@ -21,20 +21,27 @@ import java.util.*;
|
||||
/**
|
||||
* Table for managing multiple server's data in the database.
|
||||
* <p>
|
||||
* 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.
|
||||
* Table Name: plan_servers
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
* @see Server
|
||||
*/
|
||||
public class ServerTable extends Table {
|
||||
|
||||
public ServerTable(SQLDB db) {
|
||||
super("plan_servers", db);
|
||||
statementSelectServerID = "(" + Select.from(tableName, tableName + "." + Col.SERVER_ID).where(tableName + "." + Col.SERVER_UUID + "=?").toString() + " LIMIT 1)";
|
||||
statementSelectServerNameID = "(" + Select.from(tableName, tableName + "." + Col.NAME).where(tableName + "." + Col.SERVER_ID + "=?").toString() + " LIMIT 1)";
|
||||
insertStatement = Insert.values(tableName,
|
||||
Col.SERVER_UUID,
|
||||
Col.NAME,
|
||||
Col.WEBSERVER_ADDRESS,
|
||||
Col.INSTALLED,
|
||||
Col.MAX_PLAYERS);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getColumnID() {
|
||||
return Col.SERVER_ID.get();
|
||||
@ -42,62 +49,36 @@ public class ServerTable extends Table {
|
||||
|
||||
public final String statementSelectServerID;
|
||||
public final String statementSelectServerNameID;
|
||||
private static final String columnServerID = "id";
|
||||
private static final String columnServerUUID = "uuid";
|
||||
private static final String columnServerName = "name";
|
||||
private static final String columnWebserverAddress = "web_address";
|
||||
private static final String columnInstalled = "is_installed";
|
||||
private static final String columnMaxPlayers = "max_players";
|
||||
private String insertStatement;
|
||||
|
||||
public ServerTable(SQLDB db) {
|
||||
super("plan_servers", db);
|
||||
statementSelectServerID = "(" + Select.from(tableName, tableName + "." + columnServerID).where(tableName + "." + columnServerUUID + "=?").toString() + " LIMIT 1)";
|
||||
statementSelectServerNameID = "(" + Select.from(tableName, tableName + "." + columnServerName).where(tableName + "." + columnServerID + "=?").toString() + " LIMIT 1)";
|
||||
insertStatement = Insert.values(tableName,
|
||||
columnServerUUID,
|
||||
columnServerName,
|
||||
columnWebserverAddress,
|
||||
columnInstalled,
|
||||
columnMaxPlayers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.primaryKeyIDColumn(usingMySQL, columnServerID)
|
||||
.column(columnServerUUID, Sql.varchar(36)).notNull().unique()
|
||||
.column(columnServerName, Sql.varchar(100))
|
||||
.column(columnWebserverAddress, Sql.varchar(100))
|
||||
.column(columnInstalled, Sql.BOOL).notNull().defaultValue(false)
|
||||
.column(columnMaxPlayers, Sql.INT).notNull().defaultValue("-1")
|
||||
.primaryKey(usingMySQL, columnServerID)
|
||||
.primaryKeyIDColumn(usingMySQL, Col.SERVER_ID)
|
||||
.column(Col.SERVER_UUID, Sql.varchar(36)).notNull().unique()
|
||||
.column(Col.NAME, Sql.varchar(100))
|
||||
.column(Col.WEBSERVER_ADDRESS, Sql.varchar(100))
|
||||
.column(Col.INSTALLED, Sql.BOOL).notNull().defaultValue(false)
|
||||
.column(Col.MAX_PLAYERS, Sql.INT).notNull().defaultValue("-1")
|
||||
.primaryKey(usingMySQL, Col.SERVER_ID)
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
|
||||
public void alterTableV11() {
|
||||
if (usingMySQL) {
|
||||
executeUnsafe("ALTER TABLE " + tableName + " MODIFY " + columnMaxPlayers + " INTEGER NOT NULL DEFAULT -1");
|
||||
}
|
||||
}
|
||||
|
||||
public void saveCurrentServerInfo(Server info) throws SQLException {
|
||||
if (getServerID(info.getUuid()).isPresent()) {
|
||||
updateServerInfo(info);
|
||||
} else {
|
||||
saveNewServerInfo(info);
|
||||
executeUnsafe("ALTER TABLE " + tableName + " MODIFY " + Col.MAX_PLAYERS + " INTEGER NOT NULL DEFAULT -1");
|
||||
}
|
||||
}
|
||||
|
||||
private void updateServerInfo(Server info) throws SQLException {
|
||||
String sql = Update.values(tableName,
|
||||
columnServerUUID,
|
||||
columnServerName,
|
||||
columnWebserverAddress,
|
||||
columnInstalled,
|
||||
columnMaxPlayers)
|
||||
.where(columnServerID + "=?")
|
||||
Col.SERVER_UUID,
|
||||
Col.NAME,
|
||||
Col.WEBSERVER_ADDRESS,
|
||||
Col.INSTALLED,
|
||||
Col.MAX_PLAYERS)
|
||||
.where(Col.SERVER_ID + "=?")
|
||||
.toString();
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@ -113,6 +94,44 @@ public class ServerTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public void saveCurrentServerInfo(Server info) throws SQLException {
|
||||
if (getServerID(info.getUuid()).isPresent()) {
|
||||
updateServerInfo(info);
|
||||
} else {
|
||||
saveNewServerInfo(info);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns server ID for a matching UUID
|
||||
*
|
||||
* @param serverUUID UUID of the server.
|
||||
* @return ID or or empty optional.
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public Optional<Integer> getServerID(UUID serverUUID) throws SQLException {
|
||||
String sql = Select.from(tableName,
|
||||
Col.SERVER_ID)
|
||||
.where(Col.SERVER_UUID + "=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Optional<Integer>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Integer> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getInt(Col.SERVER_ID.get()));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts new row for a server into the table.
|
||||
*
|
||||
@ -138,36 +157,6 @@ public class ServerTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns server ID for a matching UUID
|
||||
*
|
||||
* @param serverUUID UUID of the server.
|
||||
* @return ID or or empty optional.
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public Optional<Integer> getServerID(UUID serverUUID) throws SQLException {
|
||||
String sql = Select.from(tableName,
|
||||
columnServerID)
|
||||
.where(columnServerUUID + "=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Optional<Integer>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Integer> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getInt(columnServerID));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns server Name for a matching UUID
|
||||
*
|
||||
@ -177,8 +166,8 @@ public class ServerTable extends Table {
|
||||
*/
|
||||
public Optional<String> getServerName(UUID serverUUID) throws SQLException {
|
||||
String sql = Select.from(tableName,
|
||||
columnServerName)
|
||||
.where(columnServerUUID + "=?")
|
||||
Col.NAME)
|
||||
.where(Col.SERVER_UUID + "=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Optional<String>>(sql) {
|
||||
@ -190,7 +179,7 @@ public class ServerTable extends Table {
|
||||
@Override
|
||||
public Optional<String> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getString(columnServerName));
|
||||
return Optional.of(set.getString(Col.NAME.get()));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
@ -200,7 +189,7 @@ public class ServerTable extends Table {
|
||||
|
||||
public Map<Integer, String> getServerNamesByID() throws SQLException {
|
||||
String sql = Select.from(tableName,
|
||||
columnServerID, columnServerName)
|
||||
Col.SERVER_ID, Col.NAME)
|
||||
.toString();
|
||||
|
||||
return query(new QueryAllStatement<Map<Integer, String>>(sql) {
|
||||
@ -208,8 +197,8 @@ public class ServerTable extends Table {
|
||||
public Map<Integer, String> processResults(ResultSet set) throws SQLException {
|
||||
Map<Integer, String> names = new HashMap<>();
|
||||
while (set.next()) {
|
||||
int id = set.getInt(columnServerID);
|
||||
names.put(id, set.getString(columnServerName));
|
||||
int id = set.getInt(Col.SERVER_ID.get());
|
||||
names.put(id, set.getString(Col.NAME.get()));
|
||||
}
|
||||
return names;
|
||||
}
|
||||
@ -218,7 +207,7 @@ public class ServerTable extends Table {
|
||||
|
||||
public Map<UUID, String> getServerNames() throws SQLException {
|
||||
String sql = Select.from(tableName,
|
||||
columnServerUUID, columnServerName)
|
||||
Col.SERVER_UUID, Col.NAME)
|
||||
.toString();
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, String>>(sql) {
|
||||
@ -226,8 +215,8 @@ public class ServerTable extends Table {
|
||||
public Map<UUID, String> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, String> names = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID serverUUID = UUID.fromString(set.getString(columnServerUUID));
|
||||
names.put(serverUUID, set.getString(columnServerName));
|
||||
UUID serverUUID = UUID.fromString(set.getString(Col.SERVER_UUID.get()));
|
||||
names.put(serverUUID, set.getString(Col.NAME.get()));
|
||||
}
|
||||
return names;
|
||||
}
|
||||
@ -236,7 +225,7 @@ public class ServerTable extends Table {
|
||||
|
||||
public Map<Integer, UUID> getServerUUIDsByID() throws SQLException {
|
||||
String sql = Select.from(tableName,
|
||||
columnServerID, columnServerUUID)
|
||||
Col.SERVER_ID, Col.SERVER_UUID)
|
||||
.toString();
|
||||
|
||||
return query(new QueryAllStatement<Map<Integer, UUID>>(sql) {
|
||||
@ -244,8 +233,8 @@ public class ServerTable extends Table {
|
||||
public Map<Integer, UUID> processResults(ResultSet set) throws SQLException {
|
||||
Map<Integer, UUID> uuids = new HashMap<>();
|
||||
while (set.next()) {
|
||||
int id = set.getInt(columnServerID);
|
||||
uuids.put(id, UUID.fromString(set.getString(columnServerUUID)));
|
||||
int id = set.getInt(Col.SERVER_ID.get());
|
||||
uuids.put(id, UUID.fromString(set.getString(Col.SERVER_UUID.get())));
|
||||
}
|
||||
return uuids;
|
||||
}
|
||||
@ -260,7 +249,7 @@ public class ServerTable extends Table {
|
||||
*/
|
||||
public Optional<Server> getBungeeInfo() throws SQLException {
|
||||
String sql = Select.from(tableName, "*")
|
||||
.where(columnServerName + "=?")
|
||||
.where(Col.NAME + "=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Optional<Server>>(sql) {
|
||||
@ -273,11 +262,11 @@ public class ServerTable extends Table {
|
||||
public Optional<Server> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(new Server(
|
||||
set.getInt(columnServerID),
|
||||
UUID.fromString(set.getString(columnServerUUID)),
|
||||
set.getString(columnServerName),
|
||||
set.getString(columnWebserverAddress),
|
||||
set.getInt(columnMaxPlayers)));
|
||||
set.getInt(Col.SERVER_ID.get()),
|
||||
UUID.fromString(set.getString(Col.SERVER_UUID.get())),
|
||||
set.getString(Col.NAME.get()),
|
||||
set.getString(Col.WEBSERVER_ADDRESS.get()),
|
||||
set.getInt(Col.MAX_PLAYERS.get())));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
@ -287,7 +276,7 @@ public class ServerTable extends Table {
|
||||
|
||||
public Map<UUID, Server> getBukkitServers() throws SQLException {
|
||||
String sql = Select.from(tableName, "*")
|
||||
.where(columnServerName + "!=?")
|
||||
.where(Col.NAME + "!=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Map<UUID, Server>>(sql, 100) {
|
||||
@ -300,48 +289,40 @@ public class ServerTable extends Table {
|
||||
public Map<UUID, Server> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, Server> servers = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID serverUUID = UUID.fromString(set.getString(columnServerUUID));
|
||||
UUID serverUUID = UUID.fromString(set.getString(Col.SERVER_UUID.get()));
|
||||
servers.put(serverUUID, new Server(
|
||||
set.getInt(columnServerID),
|
||||
set.getInt(Col.SERVER_ID.get()),
|
||||
serverUUID,
|
||||
set.getString(columnServerName),
|
||||
set.getString(columnWebserverAddress),
|
||||
set.getInt(columnMaxPlayers)));
|
||||
set.getString(Col.NAME.get()),
|
||||
set.getString(Col.WEBSERVER_ADDRESS.get()),
|
||||
set.getInt(Col.MAX_PLAYERS.get())));
|
||||
}
|
||||
return servers;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public List<UUID> getServerUUIDs() throws SQLException {
|
||||
String sql = Select.from(tableName, Col.SERVER_UUID)
|
||||
.toString();
|
||||
|
||||
return query(new QueryAllStatement<List<UUID>>(sql) {
|
||||
@Override
|
||||
public List<UUID> processResults(ResultSet set) throws SQLException {
|
||||
List<UUID> uuids = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
uuids.add(UUID.fromString(set.getString(Col.SERVER_UUID.get())));
|
||||
}
|
||||
return uuids;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getColumnUUID() {
|
||||
return Col.SERVER_UUID.get();
|
||||
}
|
||||
|
||||
public enum Col implements Column {
|
||||
SERVER_ID("id"),
|
||||
SERVER_UUID("uuid"),
|
||||
NAME("name"),
|
||||
WEBSERVER_ADDRESS("web_address"),
|
||||
INSTALLED("is_installed"),
|
||||
MAX_PLAYERS("max_players");
|
||||
|
||||
private final String column;
|
||||
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
|
||||
public void insertAllServers(List<Server> allServer) throws SQLException {
|
||||
if (Verify.isEmpty(allServer)) {
|
||||
return;
|
||||
@ -370,26 +351,10 @@ public class ServerTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public List<UUID> getServerUUIDs() throws SQLException {
|
||||
String sql = Select.from(tableName, columnServerUUID)
|
||||
.toString();
|
||||
|
||||
return query(new QueryAllStatement<List<UUID>>(sql) {
|
||||
@Override
|
||||
public List<UUID> processResults(ResultSet set) throws SQLException {
|
||||
List<UUID> uuids = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
uuids.add(UUID.fromString(set.getString(columnServerUUID)));
|
||||
}
|
||||
return uuids;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<UUID> getServerUUID(String serverName) throws SQLException {
|
||||
String sql = Select.from(tableName,
|
||||
columnServerUUID)
|
||||
.where(columnServerName + "=?")
|
||||
Col.SERVER_UUID)
|
||||
.where(Col.NAME + "=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Optional<UUID>>(sql) {
|
||||
@ -401,7 +366,7 @@ public class ServerTable extends Table {
|
||||
@Override
|
||||
public Optional<UUID> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(UUID.fromString(set.getString(columnServerUUID)));
|
||||
return Optional.of(UUID.fromString(set.getString(Col.SERVER_UUID.get())));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
@ -411,7 +376,7 @@ public class ServerTable extends Table {
|
||||
|
||||
public Optional<Server> getServerInfo(UUID serverUUID) throws SQLException {
|
||||
String sql = Select.from(tableName, "*")
|
||||
.where(columnServerUUID + "=?")
|
||||
.where(Col.SERVER_UUID + "=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Optional<Server>>(sql) {
|
||||
@ -424,11 +389,11 @@ public class ServerTable extends Table {
|
||||
public Optional<Server> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(new Server(
|
||||
set.getInt(columnServerID),
|
||||
UUID.fromString(set.getString(columnServerUUID)),
|
||||
set.getString(columnServerName),
|
||||
set.getString(columnWebserverAddress),
|
||||
set.getInt(columnMaxPlayers)));
|
||||
set.getInt(Col.SERVER_ID.get()),
|
||||
UUID.fromString(set.getString(Col.SERVER_UUID.get())),
|
||||
set.getString(Col.NAME.get()),
|
||||
set.getString(Col.WEBSERVER_ADDRESS.get()),
|
||||
set.getInt(Col.MAX_PLAYERS.get())));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
@ -436,7 +401,7 @@ public class ServerTable extends Table {
|
||||
}
|
||||
|
||||
public int getMaxPlayers() throws SQLException {
|
||||
String sql = "SELECT SUM(" + columnMaxPlayers + ") AS max FROM " + tableName;
|
||||
String sql = "SELECT SUM(" + Col.MAX_PLAYERS + ") AS max FROM " + tableName;
|
||||
|
||||
return query(new QueryAllStatement<Integer>(sql) {
|
||||
@Override
|
||||
@ -448,4 +413,28 @@ public class ServerTable extends Table {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public enum Col implements Column {
|
||||
SERVER_ID("id"),
|
||||
SERVER_UUID("uuid"),
|
||||
NAME("name"),
|
||||
WEBSERVER_ADDRESS("web_address"),
|
||||
INSTALLED("is_installed"),
|
||||
MAX_PLAYERS("max_players");
|
||||
|
||||
private final String column;
|
||||
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Select;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
@ -26,49 +27,73 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public class SessionsTable extends UserIDTable {
|
||||
|
||||
private static final String columnID = "id";
|
||||
private static final String columnSessionStart = "session_start";
|
||||
private static final String columnSessionEnd = "session_end";
|
||||
private static final String columnServerID = "server_id";
|
||||
private static final String columnMobKills = "mob_kills";
|
||||
private static final String columnDeaths = "deaths";
|
||||
|
||||
private final ServerTable serverTable;
|
||||
private String insertStatement;
|
||||
|
||||
public SessionsTable(SQLDB db) {
|
||||
super("plan_sessions", db);
|
||||
serverTable = db.getServerTable();
|
||||
insertStatement = "INSERT INTO " + tableName + " ("
|
||||
+ columnUserID + ", "
|
||||
+ columnSessionStart + ", "
|
||||
+ columnSessionEnd + ", "
|
||||
+ columnDeaths + ", "
|
||||
+ columnMobKills + ", "
|
||||
+ columnServerID
|
||||
+ Col.USER_ID + ", "
|
||||
+ Col.SESSION_START + ", "
|
||||
+ Col.SESSION_END + ", "
|
||||
+ Col.DEATHS + ", "
|
||||
+ Col.MOB_KILLS + ", "
|
||||
+ Col.SERVER_ID
|
||||
+ ") VALUES ("
|
||||
+ usersTable.statementSelectID + ", "
|
||||
+ "?, ?, ?, ?, "
|
||||
+ serverTable.statementSelectServerID + ")";
|
||||
}
|
||||
|
||||
private final ServerTable serverTable;
|
||||
private String insertStatement;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(this.tableName)
|
||||
.primaryKeyIDColumn(usingMySQL, columnID)
|
||||
.column(columnUserID, Sql.INT).notNull()
|
||||
.column(columnServerID, Sql.INT).notNull()
|
||||
.column(columnSessionStart, Sql.LONG).notNull()
|
||||
.column(columnSessionEnd, Sql.LONG).notNull()
|
||||
.column(columnMobKills, Sql.INT).notNull()
|
||||
.column(columnDeaths, Sql.INT).notNull()
|
||||
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
|
||||
.foreignKey(columnServerID, serverTable.getTableName(), serverTable.getColumnID())
|
||||
.primaryKey(usingMySQL, columnID)
|
||||
.primaryKeyIDColumn(usingMySQL, Col.ID)
|
||||
.column(Col.USER_ID, Sql.INT).notNull()
|
||||
.column(Col.SERVER_ID, Sql.INT).notNull()
|
||||
.column(Col.SESSION_START, Sql.LONG).notNull()
|
||||
.column(Col.SESSION_END, Sql.LONG).notNull()
|
||||
.column(Col.MOB_KILLS, Sql.INT).notNull()
|
||||
.column(Col.DEATHS, Sql.INT).notNull()
|
||||
.foreignKey(Col.USER_ID, usersTable.getTableName(), UsersTable.Col.ID)
|
||||
.foreignKey(Col.SERVER_ID, serverTable.getTableName(), ServerTable.Col.SERVER_ID)
|
||||
.primaryKey(usingMySQL, Col.ID)
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get the sessionID of a newly inserted row.
|
||||
*
|
||||
* @param uuid UUID of the player
|
||||
* @param session session inserted.
|
||||
* @return ID of the inserted session or -1 if session has not been inserted.
|
||||
*/
|
||||
private int getSessionID(UUID uuid, Session session) throws SQLException {
|
||||
String sql = "SELECT " + Col.ID + " FROM " + tableName +
|
||||
" WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID +
|
||||
" AND " + Col.SESSION_START + "=?" +
|
||||
" AND " + Col.SESSION_END + "=?";
|
||||
|
||||
return query(new QueryStatement<Integer>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setLong(2, session.getSessionStart());
|
||||
statement.setLong(3, session.getSessionEnd());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return set.getInt(Col.ID.get());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to save a session, with all it's information into the database.
|
||||
* <p>
|
||||
@ -112,37 +137,6 @@ public class SessionsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get the sessionID of a newly inserted row.
|
||||
*
|
||||
* @param uuid UUID of the player
|
||||
* @param session session inserted.
|
||||
* @return ID of the inserted session or -1 if session has not been inserted.
|
||||
*/
|
||||
private int getSessionID(UUID uuid, Session session) throws SQLException {
|
||||
String sql = "SELECT " + columnID + " FROM " + tableName +
|
||||
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
|
||||
" AND " + columnSessionStart + "=?" +
|
||||
" AND " + columnSessionEnd + "=?";
|
||||
|
||||
return query(new QueryStatement<Integer>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setLong(2, session.getSessionStart());
|
||||
statement.setLong(3, session.getSessionEnd());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return set.getInt(columnID);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Map containing Lists of sessions, key as ServerName.
|
||||
* <p>
|
||||
@ -156,7 +150,7 @@ public class SessionsTable extends UserIDTable {
|
||||
private Map<UUID, List<Session>> getSessionInformation(UUID uuid) throws SQLException {
|
||||
Map<Integer, UUID> serverUUIDs = serverTable.getServerUUIDsByID();
|
||||
String sql = Select.from(tableName, "*")
|
||||
.where(columnUserID + "=" + usersTable.statementSelectID)
|
||||
.where(Col.USER_ID + "=" + usersTable.statementSelectID)
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Map<UUID, List<Session>>>(sql, 10000) {
|
||||
@ -169,17 +163,17 @@ public class SessionsTable extends UserIDTable {
|
||||
public Map<UUID, List<Session>> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, List<Session>> sessionsByServer = new HashMap<>();
|
||||
while (set.next()) {
|
||||
int id = set.getInt(columnID);
|
||||
long start = set.getLong(columnSessionStart);
|
||||
long end = set.getLong(columnSessionEnd);
|
||||
UUID serverUUID = serverUUIDs.get(set.getInt(columnServerID));
|
||||
int id = set.getInt(Col.ID.get());
|
||||
long start = set.getLong(Col.SESSION_START.get());
|
||||
long end = set.getLong(Col.SESSION_END.get());
|
||||
UUID serverUUID = serverUUIDs.get(set.getInt(Col.SERVER_ID.get()));
|
||||
|
||||
if (serverUUID == null) {
|
||||
throw new IllegalStateException("Server not present");
|
||||
}
|
||||
|
||||
int deaths = set.getInt(columnDeaths);
|
||||
int mobKills = set.getInt(columnMobKills);
|
||||
int deaths = set.getInt(Col.DEATHS.get());
|
||||
int mobKills = set.getInt(Col.MOB_KILLS.get());
|
||||
List<Session> sessions = sessionsByServer.getOrDefault(serverUUID, new ArrayList<>());
|
||||
sessions.add(new Session(id, start, end, mobKills, deaths));
|
||||
sessionsByServer.put(serverUUID, sessions);
|
||||
@ -189,6 +183,41 @@ public class SessionsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get Playtime after Epoch ms on a server.
|
||||
*
|
||||
* @param uuid UUID of the player.
|
||||
* @param serverUUID UUID of the server. @see ServerTable
|
||||
* @param afterDate Epoch ms (Playtime after this date is calculated)
|
||||
* @return Milliseconds played after given epoch ms on the server. 0 if player or server not found.
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public long getPlaytime(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
|
||||
String sql = "SELECT" +
|
||||
" (SUM(" + Col.SESSION_END + ") - SUM(" + Col.SESSION_START + ")) as playtime" +
|
||||
" FROM " + tableName +
|
||||
" WHERE " + Col.SESSION_START + ">?" +
|
||||
" AND " + Col.USER_ID + "=" + usersTable.statementSelectID +
|
||||
" AND " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryStatement<Long>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, afterDate);
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.setString(3, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return set.getLong("playtime");
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, List<Session>> getSessions(UUID uuid) throws SQLException {
|
||||
Map<UUID, List<Session>> sessions = getSessionInformation(uuid);
|
||||
Map<Integer, Session> allSessions = sessions.values().stream().flatMap(Collection::stream).collect(Collectors.toMap(Session::getSessionID, Function.identity()));
|
||||
@ -234,36 +263,38 @@ public class SessionsTable extends UserIDTable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get Playtime after Epoch ms on a server.
|
||||
* Used to get Playtimes after a date in a Map, sorted by ServerNames.
|
||||
*
|
||||
* @param uuid UUID of the player.
|
||||
* @param serverUUID UUID of the server. @see ServerTable
|
||||
* @param afterDate Epoch ms (Playtime after this date is calculated)
|
||||
* @return Milliseconds played after given epoch ms on the server. 0 if player or server not found.
|
||||
* @param uuid UUID of the Player.
|
||||
* @param afterDate Epoch ms (Playtime after this date is calculated)
|
||||
* @return key - ServerName, value ms played
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public long getPlaytime(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
|
||||
String sql = "SELECT" +
|
||||
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
|
||||
public Map<String, Long> getPlaytimeByServer(UUID uuid, long afterDate) throws SQLException {
|
||||
Map<Integer, String> serverNames = serverTable.getServerNamesByID();
|
||||
String sql = "SELECT " +
|
||||
"(SUM(" + Col.SESSION_END + ") - SUM(" + Col.SESSION_START + ")) as playtime, " +
|
||||
Col.SERVER_ID +
|
||||
" FROM " + tableName +
|
||||
" WHERE " + columnSessionStart + ">?" +
|
||||
" AND " + columnUserID + "=" + usersTable.statementSelectID +
|
||||
" AND " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryStatement<Long>(sql) {
|
||||
" WHERE " + Col.SESSION_START + ">?" +
|
||||
" AND " + Col.USER_ID + "=" + usersTable.statementSelectID +
|
||||
" GROUP BY " + Col.SERVER_ID;
|
||||
return query(new QueryStatement<Map<String, Long>>(sql, 100) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, afterDate);
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.setString(3, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return set.getLong("playtime");
|
||||
public Map<String, Long> processResults(ResultSet set) throws SQLException {
|
||||
Map<String, Long> playtimes = new HashMap<>();
|
||||
while (set.next()) {
|
||||
String serverName = serverNames.get(set.getInt(Col.SERVER_ID.get()));
|
||||
long playtime = set.getLong("playtime");
|
||||
playtimes.put(serverName, playtime);
|
||||
}
|
||||
return 0L;
|
||||
return playtimes;
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -280,38 +311,33 @@ public class SessionsTable extends UserIDTable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get Playtimes after a date in a Map, sorted by ServerNames.
|
||||
* Used to get Playtime after a date of a Server.
|
||||
*
|
||||
* @param uuid UUID of the Player.
|
||||
* @param afterDate Epoch ms (Playtime after this date is calculated)
|
||||
* @return key - ServerName, value ms played
|
||||
* @param serverUUID UUID of the server.
|
||||
* @param afterDate Epoch ms (Playtime after this date is calculated)
|
||||
* @return Milliseconds played after given epoch ms on the server. 0 if server not found.
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public Map<String, Long> getPlaytimeByServer(UUID uuid, long afterDate) throws SQLException {
|
||||
Map<Integer, String> serverNames = serverTable.getServerNamesByID();
|
||||
String sql = "SELECT " +
|
||||
"(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime, " +
|
||||
columnServerID +
|
||||
public long getPlaytimeOfServer(UUID serverUUID, long afterDate) throws SQLException {
|
||||
String sql = "SELECT" +
|
||||
" (SUM(" + Col.SESSION_END + ") - SUM(" + Col.SESSION_START + ")) as playtime" +
|
||||
" FROM " + tableName +
|
||||
" WHERE " + columnSessionStart + ">?" +
|
||||
" AND " + columnUserID + "=" + usersTable.statementSelectID +
|
||||
" GROUP BY " + columnServerID;
|
||||
return query(new QueryStatement<Map<String, Long>>(sql, 100) {
|
||||
" WHERE " + Col.SESSION_START + ">?" +
|
||||
" AND " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryStatement<Long>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, afterDate);
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.setString(2, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Long> processResults(ResultSet set) throws SQLException {
|
||||
Map<String, Long> playtimes = new HashMap<>();
|
||||
while (set.next()) {
|
||||
String serverName = serverNames.get(set.getInt(columnServerID));
|
||||
long playtime = set.getLong("playtime");
|
||||
playtimes.put(serverName, playtime);
|
||||
public Long processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return set.getLong("playtime");
|
||||
}
|
||||
return playtimes;
|
||||
return 0L;
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -349,33 +375,36 @@ public class SessionsTable extends UserIDTable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get Playtime after a date of a Server.
|
||||
* Used to get total Session count of a Player on a server after a given epoch ms.
|
||||
*
|
||||
* @param uuid UUID of the player.
|
||||
* @param serverUUID UUID of the server.
|
||||
* @param afterDate Epoch ms (Playtime after this date is calculated)
|
||||
* @return Milliseconds played after given epoch ms on the server. 0 if server not found.
|
||||
* @param afterDate Epoch ms (Session count after this date is calculated)
|
||||
* @return How many sessions player has. 0 if player or server not found.
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public long getPlaytimeOfServer(UUID serverUUID, long afterDate) throws SQLException {
|
||||
public int getSessionCount(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
|
||||
String sql = "SELECT" +
|
||||
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
|
||||
" COUNT(*) as logintimes" +
|
||||
" FROM " + tableName +
|
||||
" WHERE " + columnSessionStart + ">?" +
|
||||
" AND " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||
" WHERE (" + Col.SESSION_START + " >= ?)" +
|
||||
" AND " + Col.USER_ID + "=" + usersTable.statementSelectID +
|
||||
" AND " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryStatement<Long>(sql) {
|
||||
return query(new QueryStatement<Integer>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, afterDate);
|
||||
statement.setString(2, serverUUID.toString());
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.setString(3, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long processResults(ResultSet set) throws SQLException {
|
||||
public Integer processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return set.getLong("playtime");
|
||||
return set.getInt("logintimes");
|
||||
}
|
||||
return 0L;
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -415,62 +444,24 @@ public class SessionsTable extends UserIDTable {
|
||||
return getSessionCount(uuid, serverUUID, 0L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get total Session count of a Player on a server after a given epoch ms.
|
||||
*
|
||||
* @param uuid UUID of the player.
|
||||
* @param serverUUID UUID of the server.
|
||||
* @param afterDate Epoch ms (Session count after this date is calculated)
|
||||
* @return How many sessions player has. 0 if player or server not found.
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public int getSessionCount(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
|
||||
String sql = "SELECT" +
|
||||
" COUNT(*) as logintimes" +
|
||||
" FROM " + tableName +
|
||||
" WHERE (" + columnSessionStart + " >= ?)" +
|
||||
" AND " + columnUserID + "=" + usersTable.statementSelectID +
|
||||
" AND " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryStatement<Integer>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, afterDate);
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.setString(3, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return set.getInt("logintimes");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getColumnID() {
|
||||
return columnID;
|
||||
}
|
||||
|
||||
public Map<UUID, List<Session>> getSessionInfoOfServer() throws SQLException {
|
||||
return getSessionInfoOfServer(ServerInfo.getServerUUID());
|
||||
return Col.ID.get();
|
||||
}
|
||||
|
||||
public Map<UUID, List<Session>> getSessionInfoOfServer(UUID serverUUID) throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as uuid";
|
||||
String sql = "SELECT " +
|
||||
tableName + "." + columnID + ", " +
|
||||
columnSessionStart + ", " +
|
||||
columnSessionEnd + ", " +
|
||||
columnDeaths + ", " +
|
||||
columnMobKills + ", " +
|
||||
tableName + "." + Col.ID + ", " +
|
||||
Col.SESSION_START + ", " +
|
||||
Col.SESSION_END + ", " +
|
||||
Col.DEATHS + ", " +
|
||||
Col.MOB_KILLS + ", " +
|
||||
usersUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||
" WHERE " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.USER_ID +
|
||||
" WHERE " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryStatement<Map<UUID, List<Session>>>(sql, 5000) {
|
||||
@Override
|
||||
@ -483,13 +474,13 @@ public class SessionsTable extends UserIDTable {
|
||||
Map<UUID, List<Session>> sessionsByUser = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID uuid = UUID.fromString(set.getString("uuid"));
|
||||
long start = set.getLong(columnSessionStart);
|
||||
long end = set.getLong(columnSessionEnd);
|
||||
long start = set.getLong(Col.SESSION_START.get());
|
||||
long end = set.getLong(Col.SESSION_END.get());
|
||||
|
||||
int deaths = set.getInt(columnDeaths);
|
||||
int mobKills = set.getInt(columnMobKills);
|
||||
int deaths = set.getInt(Col.DEATHS.get());
|
||||
int mobKills = set.getInt(Col.MOB_KILLS.get());
|
||||
List<Session> sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>());
|
||||
sessions.add(new Session(set.getInt(columnID), start, end, mobKills, deaths));
|
||||
sessions.add(new Session(set.getInt(Col.ID.get()), start, end, mobKills, deaths));
|
||||
sessionsByUser.put(uuid, sessions);
|
||||
}
|
||||
return sessionsByUser;
|
||||
@ -497,12 +488,16 @@ public class SessionsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, List<Session>> getSessionInfoOfServer() throws SQLException {
|
||||
return getSessionInfoOfServer(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
// TODO Write tests for this method
|
||||
public long getLastSeen(UUID uuid) throws SQLException {
|
||||
String sql = "SELECT" +
|
||||
" MAX(" + columnSessionEnd + ") as last_seen" +
|
||||
" MAX(" + Col.SESSION_END + ") as last_seen" +
|
||||
" FROM " + tableName +
|
||||
" WHERE " + columnUserID + "=" + usersTable.statementSelectID;
|
||||
" WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID;
|
||||
|
||||
return query(new QueryStatement<Long>(sql) {
|
||||
@Override
|
||||
@ -522,13 +517,13 @@ public class SessionsTable extends UserIDTable {
|
||||
|
||||
// TODO Write tests for this method
|
||||
public Map<UUID, Long> getLastSeenForAllPlayers() throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as uuid";
|
||||
String sql = "SELECT" +
|
||||
" MAX(" + columnSessionEnd + ") as last_seen, " +
|
||||
" MAX(" + Col.SESSION_END + ") as last_seen, " +
|
||||
usersUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.USER_ID +
|
||||
" GROUP BY uuid";
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, Long>>(sql, 20000) {
|
||||
@ -550,13 +545,13 @@ public class SessionsTable extends UserIDTable {
|
||||
Map<Integer, UUID> serverUUIDsByID = serverTable.getServerUUIDsByID();
|
||||
|
||||
String sql = "SELECT " +
|
||||
columnID + ", " +
|
||||
columnUserID + ", " +
|
||||
columnServerID + ", " +
|
||||
columnSessionStart + ", " +
|
||||
columnSessionEnd + ", " +
|
||||
columnDeaths + ", " +
|
||||
columnMobKills +
|
||||
Col.ID + ", " +
|
||||
Col.USER_ID + ", " +
|
||||
Col.SERVER_ID + ", " +
|
||||
Col.SESSION_START + ", " +
|
||||
Col.SESSION_END + ", " +
|
||||
Col.DEATHS + ", " +
|
||||
Col.MOB_KILLS +
|
||||
" FROM " + tableName;
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, Map<UUID, List<Session>>>>(sql, 20000) {
|
||||
@ -564,18 +559,18 @@ public class SessionsTable extends UserIDTable {
|
||||
public Map<UUID, Map<UUID, List<Session>>> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID serverUUID = serverUUIDsByID.get(set.getInt(columnServerID));
|
||||
UUID uuid = uuidsByID.get(set.getInt(columnUserID));
|
||||
UUID serverUUID = serverUUIDsByID.get(set.getInt(Col.SERVER_ID.get()));
|
||||
UUID uuid = uuidsByID.get(set.getInt(Col.USER_ID.get()));
|
||||
|
||||
Map<UUID, List<Session>> sessionsByUser = map.getOrDefault(serverUUID, new HashMap<>());
|
||||
List<Session> sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>());
|
||||
|
||||
long start = set.getLong(columnSessionStart);
|
||||
long end = set.getLong(columnSessionEnd);
|
||||
long start = set.getLong(Col.SESSION_START.get());
|
||||
long end = set.getLong(Col.SESSION_END.get());
|
||||
|
||||
int deaths = set.getInt(columnDeaths);
|
||||
int mobKills = set.getInt(columnMobKills);
|
||||
int id = set.getInt(columnID);
|
||||
int deaths = set.getInt(Col.DEATHS.get());
|
||||
int mobKills = set.getInt(Col.MOB_KILLS.get());
|
||||
int id = set.getInt(Col.ID.get());
|
||||
|
||||
Session session = new Session(id, start, end, mobKills, deaths);
|
||||
sessions.add(session);
|
||||
@ -592,6 +587,55 @@ public class SessionsTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, Map<UUID, List<Session>>> getSessionInLastMonth() throws SQLException {
|
||||
Map<Integer, UUID> uuidsByID = usersTable.getUUIDsByID();
|
||||
Map<Integer, UUID> serverUUIDsByID = serverTable.getServerUUIDsByID();
|
||||
|
||||
String sql = "SELECT " +
|
||||
Col.ID + ", " +
|
||||
Col.USER_ID + ", " +
|
||||
Col.SERVER_ID + ", " +
|
||||
Col.SESSION_START + ", " +
|
||||
Col.SESSION_END + ", " +
|
||||
Col.DEATHS + ", " +
|
||||
Col.MOB_KILLS +
|
||||
" FROM " + tableName +
|
||||
" WHERE " + Col.SESSION_START + ">?";
|
||||
|
||||
return query(new QueryStatement<Map<UUID, Map<UUID, List<Session>>>>(sql, 20000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, MiscUtils.getTime() - TimeAmount.MONTH.ms());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<UUID, Map<UUID, List<Session>>> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID serverUUID = serverUUIDsByID.get(set.getInt(Col.SERVER_ID.get()));
|
||||
UUID uuid = uuidsByID.get(set.getInt(Col.USER_ID.get()));
|
||||
|
||||
Map<UUID, List<Session>> sessionsByUser = map.getOrDefault(serverUUID, new HashMap<>());
|
||||
List<Session> sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>());
|
||||
|
||||
long start = set.getLong(Col.SESSION_START.get());
|
||||
long end = set.getLong(Col.SESSION_END.get());
|
||||
|
||||
int deaths = set.getInt(Col.DEATHS.get());
|
||||
int mobKills = set.getInt(Col.MOB_KILLS.get());
|
||||
int id = set.getInt(Col.ID.get());
|
||||
|
||||
Session session = new Session(id, start, end, mobKills, deaths);
|
||||
sessions.add(session);
|
||||
|
||||
sessionsByUser.put(uuid, sessions);
|
||||
map.put(serverUUID, sessionsByUser);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void insertSessions(Map<UUID, Map<UUID, List<Session>>> allSessions, boolean saveKillsAndWorldTimes) throws SQLException {
|
||||
if (Verify.isEmpty(allSessions)) {
|
||||
return;
|
||||
@ -674,56 +718,28 @@ public class SessionsTable extends UserIDTable {
|
||||
}
|
||||
}
|
||||
|
||||
String getcolumnServerID() {
|
||||
return columnServerID;
|
||||
}
|
||||
public enum Col implements Column {
|
||||
USER_ID(UserIDTable.Col.USER_ID.get()),
|
||||
ID("id"),
|
||||
SERVER_ID("server_id"),
|
||||
SESSION_START("session_start"),
|
||||
SESSION_END("session_end"),
|
||||
MOB_KILLS("mob_kills"),
|
||||
DEATHS("deaths");
|
||||
|
||||
public Map<UUID, Map<UUID, List<Session>>> getSessionInLastMonth() throws SQLException {
|
||||
Map<Integer, UUID> uuidsByID = usersTable.getUUIDsByID();
|
||||
Map<Integer, UUID> serverUUIDsByID = serverTable.getServerUUIDsByID();
|
||||
private final String column;
|
||||
|
||||
String sql = "SELECT " +
|
||||
columnID + ", " +
|
||||
columnUserID + ", " +
|
||||
columnServerID + ", " +
|
||||
columnSessionStart + ", " +
|
||||
columnSessionEnd + ", " +
|
||||
columnDeaths + ", " +
|
||||
columnMobKills +
|
||||
" FROM " + tableName +
|
||||
" WHERE " + columnSessionStart + ">?";
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
return query(new QueryStatement<Map<UUID, Map<UUID, List<Session>>>>(sql, 20000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, MiscUtils.getTime() - TimeAmount.MONTH.ms());
|
||||
}
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<UUID, Map<UUID, List<Session>>> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID serverUUID = serverUUIDsByID.get(set.getInt(columnServerID));
|
||||
UUID uuid = uuidsByID.get(set.getInt(columnUserID));
|
||||
|
||||
Map<UUID, List<Session>> sessionsByUser = map.getOrDefault(serverUUID, new HashMap<>());
|
||||
List<Session> sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>());
|
||||
|
||||
long start = set.getLong(columnSessionStart);
|
||||
long end = set.getLong(columnSessionEnd);
|
||||
|
||||
int deaths = set.getInt(columnDeaths);
|
||||
int mobKills = set.getInt(columnMobKills);
|
||||
int id = set.getInt(columnID);
|
||||
|
||||
Session session = new Session(id, start, end, mobKills, deaths);
|
||||
sessions.add(session);
|
||||
|
||||
sessionsByUser.put(uuid, sessions);
|
||||
map.put(serverUUID, sessionsByUser);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Select;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
@ -22,68 +23,55 @@ import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Class representing database table plan_tps
|
||||
* Table that is in charge of storing TPS, Players Online & Performance data.
|
||||
* <p>
|
||||
* Table Name: plan_tps
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public class TPSTable extends Table {
|
||||
|
||||
private static final String columnServerID = "server_id";
|
||||
private static final String columnDate = "date";
|
||||
private static final String columnTPS = "tps";
|
||||
private static final String columnPlayers = "players_online";
|
||||
private static final String columnCPUUsage = "cpu_usage";
|
||||
private static final String columnRAMUsage = "ram_usage";
|
||||
private static final String columnEntities = "entities";
|
||||
private static final String columnChunksLoaded = "chunks_loaded";
|
||||
|
||||
private final ServerTable serverTable;
|
||||
private String insertStatement;
|
||||
|
||||
public TPSTable(SQLDB db) {
|
||||
super("plan_tps", db);
|
||||
serverTable = db.getServerTable();
|
||||
insertStatement = "INSERT INTO " + tableName + " ("
|
||||
+ columnServerID + ", "
|
||||
+ columnDate + ", "
|
||||
+ columnTPS + ", "
|
||||
+ columnPlayers + ", "
|
||||
+ columnCPUUsage + ", "
|
||||
+ columnRAMUsage + ", "
|
||||
+ columnEntities + ", "
|
||||
+ columnChunksLoaded
|
||||
+ Col.SERVER_ID + ", "
|
||||
+ Col.DATE + ", "
|
||||
+ Col.TPS + ", "
|
||||
+ Col.PLAYERS_ONLINE + ", "
|
||||
+ Col.CPU_USAGE + ", "
|
||||
+ Col.RAM_USAGE + ", "
|
||||
+ Col.ENTITIES + ", "
|
||||
+ Col.CHUNKS
|
||||
+ ") VALUES ("
|
||||
+ serverTable.statementSelectServerID + ", "
|
||||
+ "?, ?, ?, ?, ?, ?, ?)";
|
||||
}
|
||||
|
||||
private final ServerTable serverTable;
|
||||
private String insertStatement;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.column(columnServerID, Sql.INT).notNull()
|
||||
.column(columnDate, Sql.LONG).notNull()
|
||||
.column(columnTPS, Sql.DOUBLE).notNull()
|
||||
.column(columnPlayers, Sql.INT).notNull()
|
||||
.column(columnCPUUsage, Sql.DOUBLE).notNull()
|
||||
.column(columnRAMUsage, Sql.LONG).notNull()
|
||||
.column(columnEntities, Sql.INT).notNull()
|
||||
.column(columnChunksLoaded, Sql.INT).notNull()
|
||||
.foreignKey(columnServerID, serverTable.getTableName(), serverTable.getColumnID())
|
||||
.column(Col.SERVER_ID, Sql.INT).notNull()
|
||||
.column(Col.DATE, Sql.LONG).notNull()
|
||||
.column(Col.TPS, Sql.DOUBLE).notNull()
|
||||
.column(Col.PLAYERS_ONLINE, Sql.INT).notNull()
|
||||
.column(Col.CPU_USAGE, Sql.DOUBLE).notNull()
|
||||
.column(Col.RAM_USAGE, Sql.LONG).notNull()
|
||||
.column(Col.ENTITIES, Sql.INT).notNull()
|
||||
.column(Col.CHUNKS, Sql.INT).notNull()
|
||||
.foreignKey(Col.SERVER_ID, serverTable.getTableName(), ServerTable.Col.SERVER_ID)
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return @throws SQLException
|
||||
*/
|
||||
public List<TPS> getTPSData() throws SQLException {
|
||||
return getTPSData(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public List<TPS> getTPSData(UUID serverUUID) throws SQLException {
|
||||
String sql = Select.all(tableName)
|
||||
.where(columnServerID + "=" + serverTable.statementSelectServerID)
|
||||
.where(Col.SERVER_ID + "=" + serverTable.statementSelectServerID)
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<List<TPS>>(sql, 50000) {
|
||||
@ -98,13 +86,13 @@ public class TPSTable extends Table {
|
||||
while (set.next()) {
|
||||
|
||||
TPS tps = TPSBuilder.get()
|
||||
.date(set.getLong(columnDate))
|
||||
.tps(set.getDouble(columnTPS))
|
||||
.playersOnline(set.getInt(columnPlayers))
|
||||
.usedCPU(set.getDouble(columnCPUUsage))
|
||||
.usedMemory(set.getLong(columnRAMUsage))
|
||||
.entities(set.getInt(columnEntities))
|
||||
.chunksLoaded(set.getInt(columnChunksLoaded))
|
||||
.date(set.getLong(Col.DATE.get()))
|
||||
.tps(set.getDouble(Col.TPS.get()))
|
||||
.playersOnline(set.getInt(Col.PLAYERS_ONLINE.get()))
|
||||
.usedCPU(set.getDouble(Col.CPU_USAGE.get()))
|
||||
.usedMemory(set.getLong(Col.RAM_USAGE.get()))
|
||||
.entities(set.getInt(Col.ENTITIES.get()))
|
||||
.chunksLoaded(set.getInt(Col.CHUNKS.get()))
|
||||
.toTPS();
|
||||
|
||||
data.add(tps);
|
||||
@ -114,6 +102,42 @@ public class TPSTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return @throws SQLException
|
||||
*/
|
||||
public List<TPS> getTPSData() throws SQLException {
|
||||
return getTPSData(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean the TPS Table of old data.
|
||||
*
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public void clean() throws SQLException {
|
||||
Optional<TPS> allTimePeak = getAllTimePeak();
|
||||
int p = -1;
|
||||
if (allTimePeak.isPresent()) {
|
||||
p = allTimePeak.get().getPlayers();
|
||||
}
|
||||
final int pValue = p;
|
||||
|
||||
String sql = "DELETE FROM " + tableName +
|
||||
" WHERE (" + Col.DATE + "<?)" +
|
||||
" AND (" + Col.PLAYERS_ONLINE + "" +
|
||||
" != ?)";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setInt(1, pValue);
|
||||
// More than 2 Months ago.
|
||||
long fiveWeeks = TimeAmount.MONTH.ms() * 2L;
|
||||
statement.setLong(2, MiscUtils.getTime() - fiveWeeks);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void insertTPS(TPS tps) throws SQLException {
|
||||
execute(new ExecStatement(insertStatement) {
|
||||
@Override
|
||||
@ -130,31 +154,37 @@ public class TPSTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean the TPS Table of old data.
|
||||
*
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public void clean() throws SQLException {
|
||||
Optional<TPS> allTimePeak = getAllTimePeak();
|
||||
int p = -1;
|
||||
if (allTimePeak.isPresent()) {
|
||||
p = allTimePeak.get().getPlayers();
|
||||
}
|
||||
final int pValue = p;
|
||||
public Optional<TPS> getPeakPlayerCount(UUID serverUUID, long afterDate) throws SQLException {
|
||||
String sql = Select.all(tableName)
|
||||
.where(Col.SERVER_ID + "=" + serverTable.statementSelectServerID)
|
||||
.and(Col.PLAYERS_ONLINE + "= (SELECT MAX(" + Col.PLAYERS_ONLINE + ") FROM " + tableName + ")")
|
||||
.and(Col.DATE + ">= ?")
|
||||
.toString();
|
||||
|
||||
String sql = "DELETE FROM " + tableName +
|
||||
" WHERE (" + columnDate + "<?)" +
|
||||
" AND (" + columnPlayers + "" +
|
||||
" != ?)";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
return query(new QueryStatement<Optional<TPS>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setInt(1, pValue);
|
||||
// More than 2 Months ago.
|
||||
long fiveWeeks = TimeAmount.MONTH.ms() * 2L;
|
||||
statement.setLong(2, MiscUtils.getTime() - fiveWeeks);
|
||||
statement.setString(1, serverUUID.toString());
|
||||
statement.setLong(2, afterDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<TPS> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
|
||||
TPS tps = TPSBuilder.get()
|
||||
.date(set.getLong(Col.DATE.get()))
|
||||
.tps(set.getDouble(Col.TPS.get()))
|
||||
.playersOnline(set.getInt(Col.PLAYERS_ONLINE.get()))
|
||||
.usedCPU(set.getDouble(Col.CPU_USAGE.get()))
|
||||
.usedMemory(set.getLong(Col.RAM_USAGE.get()))
|
||||
.entities(set.getInt(Col.ENTITIES.get()))
|
||||
.chunksLoaded(set.getInt(Col.CHUNKS.get()))
|
||||
.toTPS();
|
||||
|
||||
return Optional.of(tps);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -171,55 +201,20 @@ public class TPSTable extends Table {
|
||||
return getPeakPlayerCount(ServerInfo.getServerUUID(), afterDate);
|
||||
}
|
||||
|
||||
public Optional<TPS> getPeakPlayerCount(UUID serverUUID, long afterDate) throws SQLException {
|
||||
String sql = Select.all(tableName)
|
||||
.where(columnServerID + "=" + serverTable.statementSelectServerID)
|
||||
.and(columnPlayers + "= (SELECT MAX(" + columnPlayers + ") FROM " + tableName + ")")
|
||||
.and(columnDate + ">= ?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Optional<TPS>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
statement.setLong(2, afterDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<TPS> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
|
||||
TPS tps = TPSBuilder.get()
|
||||
.date(set.getLong(columnDate))
|
||||
.tps(set.getDouble(columnTPS))
|
||||
.playersOnline(set.getInt(columnPlayers))
|
||||
.usedCPU(set.getDouble(columnCPUUsage))
|
||||
.usedMemory(set.getLong(columnRAMUsage))
|
||||
.entities(set.getInt(columnEntities))
|
||||
.chunksLoaded(set.getInt(columnChunksLoaded))
|
||||
.toTPS();
|
||||
|
||||
return Optional.of(tps);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, List<TPS>> getAllTPS() throws SQLException {
|
||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
columnDate + ", " +
|
||||
columnTPS + ", " +
|
||||
columnPlayers + ", " +
|
||||
columnCPUUsage + ", " +
|
||||
columnRAMUsage + ", " +
|
||||
columnEntities + ", " +
|
||||
columnChunksLoaded + ", " +
|
||||
Col.DATE + ", " +
|
||||
Col.TPS + ", " +
|
||||
Col.PLAYERS_ONLINE + ", " +
|
||||
Col.CPU_USAGE + ", " +
|
||||
Col.RAM_USAGE + ", " +
|
||||
Col.ENTITIES + ", " +
|
||||
Col.CHUNKS + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID;
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + Col.SERVER_ID;
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, List<TPS>>>(sql, 50000) {
|
||||
@Override
|
||||
@ -231,13 +226,13 @@ public class TPSTable extends Table {
|
||||
List<TPS> tpsList = serverMap.getOrDefault(serverUUID, new ArrayList<>());
|
||||
|
||||
TPS tps = TPSBuilder.get()
|
||||
.date(set.getLong(columnDate))
|
||||
.tps(set.getDouble(columnTPS))
|
||||
.playersOnline(set.getInt(columnPlayers))
|
||||
.usedCPU(set.getDouble(columnCPUUsage))
|
||||
.usedMemory(set.getLong(columnRAMUsage))
|
||||
.entities(set.getInt(columnEntities))
|
||||
.chunksLoaded(set.getInt(columnChunksLoaded))
|
||||
.date(set.getLong(Col.DATE.get()))
|
||||
.tps(set.getDouble(Col.TPS.get()))
|
||||
.playersOnline(set.getInt(Col.PLAYERS_ONLINE.get()))
|
||||
.usedCPU(set.getDouble(Col.CPU_USAGE.get()))
|
||||
.usedMemory(set.getLong(Col.RAM_USAGE.get()))
|
||||
.entities(set.getInt(Col.ENTITIES.get()))
|
||||
.chunksLoaded(set.getInt(Col.CHUNKS.get()))
|
||||
.toTPS();
|
||||
|
||||
tpsList.add(tps);
|
||||
@ -248,6 +243,43 @@ public class TPSTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public List<TPS> getNetworkOnlineData() throws SQLException {
|
||||
Optional<Server> bungeeInfo = serverTable.getBungeeInfo();
|
||||
if (!bungeeInfo.isPresent()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
UUID bungeeUUID = bungeeInfo.get().getUuid();
|
||||
|
||||
String sql = "SELECT " +
|
||||
Col.DATE + ", " +
|
||||
Col.PLAYERS_ONLINE +
|
||||
" FROM " + tableName +
|
||||
" WHERE " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryStatement<List<TPS>>(sql, 50000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, bungeeUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TPS> processResults(ResultSet set) throws SQLException {
|
||||
List<TPS> tpsList = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
|
||||
TPS tps = TPSBuilder.get()
|
||||
.date(set.getLong(Col.DATE.get()))
|
||||
.skipTPS()
|
||||
.playersOnline(set.getInt(Col.PLAYERS_ONLINE.get()))
|
||||
.toTPS();
|
||||
|
||||
tpsList.add(tps);
|
||||
}
|
||||
return tpsList;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void insertAllTPS(Map<UUID, List<TPS>> allTPS) throws SQLException {
|
||||
if (Verify.isEmpty(allTPS)) {
|
||||
return;
|
||||
@ -277,40 +309,29 @@ public class TPSTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public List<TPS> getNetworkOnlineData() throws SQLException {
|
||||
Optional<Server> bungeeInfo = serverTable.getBungeeInfo();
|
||||
if (!bungeeInfo.isPresent()) {
|
||||
return new ArrayList<>();
|
||||
public enum Col implements Column {
|
||||
SERVER_ID("server_id"),
|
||||
DATE("date"),
|
||||
TPS("tps"),
|
||||
PLAYERS_ONLINE("players_online"),
|
||||
CPU_USAGE("cpu_usage"),
|
||||
RAM_USAGE("ram_usage"),
|
||||
ENTITIES("entities"),
|
||||
CHUNKS("chunks_loaded");
|
||||
|
||||
private final String column;
|
||||
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
UUID bungeeUUID = bungeeInfo.get().getUuid();
|
||||
|
||||
String sql = "SELECT " +
|
||||
columnDate + ", " +
|
||||
columnPlayers +
|
||||
" FROM " + tableName +
|
||||
" WHERE " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
return query(new QueryStatement<List<TPS>>(sql, 50000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, bungeeUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TPS> processResults(ResultSet set) throws SQLException {
|
||||
List<TPS> tpsList = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
|
||||
TPS tps = TPSBuilder.get()
|
||||
.date(set.getLong(columnDate))
|
||||
.skipTPS()
|
||||
.playersOnline(set.getInt(columnPlayers))
|
||||
.toTPS();
|
||||
|
||||
tpsList.add(tps);
|
||||
}
|
||||
return tpsList;
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,11 @@ import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Table that represents plan_transfer in SQL database.
|
||||
* Table that is in charge of transferring data between network servers.
|
||||
* <p>
|
||||
* Table Name: plan_transfer
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@ -215,8 +219,8 @@ public class TransferTable extends Table {
|
||||
}
|
||||
|
||||
public Map<UUID, String> getPlayerPluginsTabs(UUID playerUUID) throws SQLException {
|
||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
Col.CONTENT + ", " +
|
||||
serverUUIDColumn +
|
||||
@ -250,8 +254,8 @@ public class TransferTable extends Table {
|
||||
|
||||
@Deprecated
|
||||
public Optional<UUID> getServerPlayerIsOnline(UUID playerUUID) throws SQLException {
|
||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
|
@ -2,6 +2,7 @@ package com.djrapitops.plan.system.database.databases.sql.tables;
|
||||
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
@ -15,7 +16,27 @@ import java.util.UUID;
|
||||
*/
|
||||
public abstract class UserIDTable extends Table {
|
||||
|
||||
@Deprecated
|
||||
protected final String columnUserID = "user_id";
|
||||
|
||||
public enum Col implements Column {
|
||||
USER_ID("user_id");
|
||||
|
||||
private final String column;
|
||||
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
protected final UsersTable usersTable;
|
||||
|
||||
public UserIDTable(String name, SQLDB db) {
|
||||
|
@ -10,10 +10,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Select;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Update;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.*;
|
||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
@ -23,18 +20,28 @@ import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Server Specific user information table.
|
||||
* Table that is in charge of storing server specific player data.
|
||||
* <p>
|
||||
* Represents plan_user_info.
|
||||
* Table Name: plan_user_info
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class UserInfoTable extends UserIDTable {
|
||||
|
||||
private static final String columnRegistered = "registered";
|
||||
private static final String columnOP = "opped";
|
||||
private static final String columnBanned = "banned";
|
||||
private static final String columnServerID = "server_id";
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.column(Col.USER_ID, Sql.INT).notNull()
|
||||
.column(Col.REGISTERED, Sql.LONG).notNull()
|
||||
.column(Col.OP, Sql.BOOL).notNull().defaultValue(false)
|
||||
.column(Col.BANNED, Sql.BOOL).notNull().defaultValue(false)
|
||||
.column(Col.SERVER_ID, Sql.INT).notNull()
|
||||
.foreignKey(Col.USER_ID, usersTable.getTableName(), UsersTable.Col.ID)
|
||||
.foreignKey(Col.SERVER_ID, serverTable.getTableName(), ServerTable.Col.SERVER_ID)
|
||||
.toString());
|
||||
}
|
||||
|
||||
private final ServerTable serverTable;
|
||||
|
||||
@ -43,28 +50,15 @@ public class UserInfoTable extends UserIDTable {
|
||||
serverTable = db.getServerTable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.column(columnUserID, Sql.INT).notNull()
|
||||
.column(columnRegistered, Sql.LONG).notNull()
|
||||
.column(columnOP, Sql.BOOL).notNull().defaultValue(false)
|
||||
.column(columnBanned, Sql.BOOL).notNull().defaultValue(false)
|
||||
.column(columnServerID, Sql.INT).notNull()
|
||||
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
|
||||
.foreignKey(columnServerID, serverTable.getTableName(), serverTable.getColumnID())
|
||||
.toString());
|
||||
}
|
||||
|
||||
public void registerUserInfo(UUID uuid, long registered) throws SQLException {
|
||||
if (!usersTable.isRegistered(uuid)) {
|
||||
usersTable.registerUser(uuid, registered, "Waiting for Update..");
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO " + tableName + " (" +
|
||||
columnUserID + ", " +
|
||||
columnRegistered + ", " +
|
||||
columnServerID +
|
||||
Col.USER_ID + ", " +
|
||||
Col.REGISTERED + ", " +
|
||||
Col.SERVER_ID +
|
||||
") VALUES (" +
|
||||
usersTable.statementSelectID + ", " +
|
||||
"?, " +
|
||||
@ -80,14 +74,10 @@ public class UserInfoTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isRegistered(UUID uuid) throws SQLException {
|
||||
return isRegistered(uuid, ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public boolean isRegistered(UUID uuid, UUID serverUUID) throws SQLException {
|
||||
String sql = Select.from(tableName, "COUNT(" + columnUserID + ") as c")
|
||||
.where(columnUserID + "=" + usersTable.statementSelectID)
|
||||
.and(columnServerID + "=" + serverTable.statementSelectServerID)
|
||||
String sql = Select.from(tableName, "COUNT(" + Col.USER_ID + ") as c")
|
||||
.where(Col.USER_ID + "=" + usersTable.statementSelectID)
|
||||
.and(Col.SERVER_ID + "=" + serverTable.statementSelectServerID)
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Boolean>(sql) {
|
||||
@ -104,9 +94,13 @@ public class UserInfoTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isRegistered(UUID uuid) throws SQLException {
|
||||
return isRegistered(uuid, ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public void updateOpStatus(UUID uuid, boolean op) throws SQLException {
|
||||
String sql = Update.values(tableName, columnOP)
|
||||
.where(columnUserID + "=" + usersTable.statementSelectID)
|
||||
String sql = Update.values(tableName, Col.OP)
|
||||
.where(Col.USER_ID + "=" + usersTable.statementSelectID)
|
||||
.toString();
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@ -119,8 +113,8 @@ public class UserInfoTable extends UserIDTable {
|
||||
}
|
||||
|
||||
public void updateBanStatus(UUID uuid, boolean banned) throws SQLException {
|
||||
String sql = Update.values(tableName, columnBanned)
|
||||
.where(columnUserID + "=" + usersTable.statementSelectID)
|
||||
String sql = Update.values(tableName, Col.BANNED)
|
||||
.where(Col.USER_ID + "=" + usersTable.statementSelectID)
|
||||
.toString();
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@ -132,25 +126,21 @@ public class UserInfoTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public UserInfo getUserInfo(UUID uuid) throws SQLException {
|
||||
return getAllUserInfo(uuid).get(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public Map<UUID, UserInfo> getAllUserInfo(UUID uuid) throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||
String usersNameColumn = usersTable + "." + usersTable.getColumnName() + " as name";
|
||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String usersNameColumn = usersTable + "." + UsersTable.Col.USER_NAME + " as name";
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
tableName + "." + columnRegistered + ", " +
|
||||
columnBanned + ", " +
|
||||
columnOP + ", " +
|
||||
tableName + "." + Col.REGISTERED + ", " +
|
||||
Col.BANNED + ", " +
|
||||
Col.OP + ", " +
|
||||
usersNameColumn + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID +
|
||||
" WHERE " + columnUserID + "=" + usersTable.statementSelectID;
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.USER_ID +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + Col.SERVER_ID +
|
||||
" WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID;
|
||||
|
||||
return query(new QueryStatement<Map<UUID, UserInfo>>(sql) {
|
||||
@Override
|
||||
@ -162,19 +152,56 @@ public class UserInfoTable extends UserIDTable {
|
||||
public Map<UUID, UserInfo> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, UserInfo> map = new HashMap<>();
|
||||
while (set.next()) {
|
||||
long registered = set.getLong(columnRegistered);
|
||||
boolean opped = set.getBoolean(columnOP);
|
||||
boolean banned = set.getBoolean(columnBanned);
|
||||
long registered = set.getLong(Col.REGISTERED.get());
|
||||
boolean op = set.getBoolean(Col.OP.get());
|
||||
boolean banned = set.getBoolean(Col.BANNED.get());
|
||||
String name = set.getString("name");
|
||||
|
||||
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
|
||||
map.put(serverUUID, new UserInfo(uuid, name, registered, opped, banned));
|
||||
map.put(serverUUID, new UserInfo(uuid, name, registered, op, banned));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public UserInfo getUserInfo(UUID uuid) throws SQLException {
|
||||
return getAllUserInfo(uuid).get(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public List<UserInfo> getServerUserInfo(UUID serverUUID) throws SQLException {
|
||||
Map<UUID, String> playerNames = usersTable.getPlayerNames();
|
||||
Map<Integer, UUID> uuidsByID = usersTable.getUUIDsByID();
|
||||
|
||||
String sql = "SELECT * FROM " + tableName +
|
||||
" WHERE " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryStatement<List<UserInfo>>(sql, 20000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserInfo> processResults(ResultSet set) throws SQLException {
|
||||
List<UserInfo> userInfo = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
long registered = set.getLong(Col.REGISTERED.get());
|
||||
boolean op = set.getBoolean(Col.OP.get());
|
||||
boolean banned = set.getBoolean(Col.BANNED.get());
|
||||
int userId = set.getInt(Col.USER_ID.get());
|
||||
UUID uuid = uuidsByID.get(userId);
|
||||
String name = playerNames.getOrDefault(uuid, "Unknown");
|
||||
UserInfo info = new UserInfo(uuid, name, registered, op, banned);
|
||||
if (!userInfo.contains(info)) {
|
||||
userInfo.add(info);
|
||||
}
|
||||
}
|
||||
return userInfo;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for getting info of all users on THIS server.
|
||||
*
|
||||
@ -184,53 +211,20 @@ public class UserInfoTable extends UserIDTable {
|
||||
return getServerUserInfo(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public List<UserInfo> getServerUserInfo(UUID serverUUID) throws SQLException {
|
||||
Map<UUID, String> playerNames = usersTable.getPlayerNames();
|
||||
Map<Integer, UUID> uuidsByID = usersTable.getUUIDsByID();
|
||||
|
||||
String sql = "SELECT * FROM " + tableName +
|
||||
" WHERE " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryStatement<List<UserInfo>>(sql, 20000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserInfo> processResults(ResultSet set) throws SQLException {
|
||||
List<UserInfo> userInfo = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
long registered = set.getLong(columnRegistered);
|
||||
boolean opped = set.getBoolean(columnOP);
|
||||
boolean banned = set.getBoolean(columnBanned);
|
||||
int userId = set.getInt(columnUserID);
|
||||
UUID uuid = uuidsByID.get(userId);
|
||||
String name = playerNames.getOrDefault(uuid, "Unknown");
|
||||
UserInfo info = new UserInfo(uuid, name, registered, opped, banned);
|
||||
if (!userInfo.contains(info)) {
|
||||
userInfo.add(info);
|
||||
}
|
||||
}
|
||||
return userInfo;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, List<UserInfo>> getAllUserInfo() throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as uuid";
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
tableName + "." + columnRegistered + ", " +
|
||||
columnBanned + ", " +
|
||||
columnOP + ", " +
|
||||
tableName + "." + Col.REGISTERED + ", " +
|
||||
Col.BANNED + ", " +
|
||||
Col.OP + ", " +
|
||||
usersUUIDColumn + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID;
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.USER_ID +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + Col.SERVER_ID;
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, List<UserInfo>>>(sql, 50000) {
|
||||
@Override
|
||||
@ -242,9 +236,9 @@ public class UserInfoTable extends UserIDTable {
|
||||
|
||||
List<UserInfo> userInfos = serverMap.getOrDefault(serverUUID, new ArrayList<>());
|
||||
|
||||
long registered = set.getLong(columnRegistered);
|
||||
boolean banned = set.getBoolean(columnBanned);
|
||||
boolean op = set.getBoolean(columnOP);
|
||||
long registered = set.getLong(Col.REGISTERED.get());
|
||||
boolean banned = set.getBoolean(Col.BANNED.get());
|
||||
boolean op = set.getBoolean(Col.OP.get());
|
||||
|
||||
userInfos.add(new UserInfo(uuid, "", registered, op, banned));
|
||||
|
||||
@ -261,11 +255,11 @@ public class UserInfoTable extends UserIDTable {
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO " + tableName + " (" +
|
||||
columnUserID + ", " +
|
||||
columnRegistered + ", " +
|
||||
columnServerID + ", " +
|
||||
columnBanned + ", " +
|
||||
columnOP +
|
||||
Col.USER_ID + ", " +
|
||||
Col.REGISTERED + ", " +
|
||||
Col.SERVER_ID + ", " +
|
||||
Col.BANNED + ", " +
|
||||
Col.OP +
|
||||
") VALUES (" +
|
||||
usersTable.statementSelectID + ", " +
|
||||
"?, " +
|
||||
@ -292,16 +286,16 @@ public class UserInfoTable extends UserIDTable {
|
||||
}
|
||||
|
||||
public Map<UUID, Set<UUID>> getSavedUUIDs() throws SQLException {
|
||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||
String usersIDColumn = usersTable + "." + UsersTable.Col.ID;
|
||||
String usersUUIDColumn = usersTable + "." + UsersTable.Col.UUID + " as uuid";
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
usersUUIDColumn + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID;
|
||||
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + Col.USER_ID +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + Col.SERVER_ID;
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, Set<UUID>>>(sql, 50000) {
|
||||
@Override
|
||||
@ -323,9 +317,9 @@ public class UserInfoTable extends UserIDTable {
|
||||
|
||||
public int getServerUserCount(UUID serverUUID) throws SQLException {
|
||||
String sql = "SELECT " +
|
||||
" COUNT(" + columnRegistered + ") as c" +
|
||||
" COUNT(" + Col.REGISTERED + ") as c" +
|
||||
" FROM " + tableName +
|
||||
" WHERE " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||
" WHERE " + Col.SERVER_ID + "=" + serverTable.statementSelectServerID;
|
||||
|
||||
return query(new QueryAllStatement<Integer>(sql, 20000) {
|
||||
@Override
|
||||
@ -343,6 +337,29 @@ public class UserInfoTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public enum Col implements Column {
|
||||
USER_ID(UserIDTable.Col.USER_ID.get()),
|
||||
SERVER_ID("server_id"),
|
||||
REGISTERED("registered"),
|
||||
OP("opped"),
|
||||
BANNED("banned");
|
||||
|
||||
private final String column;
|
||||
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO improve performance of this method.
|
||||
public Set<UUID> getSavedUUIDs(UUID serverUUID) throws SQLException {
|
||||
return getSavedUUIDs().get(serverUUID);
|
||||
|
@ -15,36 +15,37 @@ import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Table that is in charge of storing common player data for all servers.
|
||||
* <p>
|
||||
* Table Name: plan_users
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class UsersTable extends UserIDTable {
|
||||
|
||||
public final String statementSelectID;
|
||||
private static final String columnID = "id";
|
||||
private static final String columnUUID = "uuid";
|
||||
private static final String columnRegistered = "registered";
|
||||
private static final String columnName = "name";
|
||||
private static final String columnTimesKicked = "times_kicked";
|
||||
private String insertStatement;
|
||||
|
||||
public UsersTable(SQLDB db) {
|
||||
super("plan_users", db);
|
||||
statementSelectID = "(" + Select.from(tableName, tableName + "." + columnID).where(columnUUID + "=?").toString() + " LIMIT 1)";
|
||||
statementSelectID = "(" + Select.from(tableName, tableName + "." + Col.ID).where(Col.UUID + "=?").toString() + " LIMIT 1)";
|
||||
insertStatement = Insert.values(tableName,
|
||||
columnUUID,
|
||||
columnRegistered,
|
||||
columnName);
|
||||
Col.UUID,
|
||||
Col.REGISTERED,
|
||||
Col.USER_NAME);
|
||||
}
|
||||
|
||||
public final String statementSelectID;
|
||||
private String insertStatement;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.primaryKeyIDColumn(usingMySQL, columnID)
|
||||
.column(columnUUID, Sql.varchar(36)).notNull().unique()
|
||||
.column(columnRegistered, Sql.LONG).notNull()
|
||||
.column(columnName, Sql.varchar(16)).notNull()
|
||||
.column(columnTimesKicked, Sql.INT).notNull().defaultValue("0")
|
||||
.primaryKey(usingMySQL, columnID)
|
||||
.primaryKeyIDColumn(usingMySQL, Col.ID)
|
||||
.column(Col.UUID, Sql.varchar(36)).notNull().unique()
|
||||
.column(Col.REGISTERED, Sql.LONG).notNull()
|
||||
.column(Col.USER_NAME, Sql.varchar(16)).notNull()
|
||||
.column(Col.TIMES_KICKED, Sql.INT).notNull().defaultValue("0")
|
||||
.primaryKey(usingMySQL, Col.ID)
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
@ -54,14 +55,14 @@ public class UsersTable extends UserIDTable {
|
||||
* @throws SQLException when an error at retrieving the UUIDs happens
|
||||
*/
|
||||
public Set<UUID> getSavedUUIDs() throws SQLException {
|
||||
String sql = Select.from(tableName, columnUUID).toString();
|
||||
String sql = Select.from(tableName, Col.UUID).toString();
|
||||
|
||||
return query(new QueryAllStatement<Set<UUID>>(sql, 50000) {
|
||||
@Override
|
||||
public Set<UUID> processResults(ResultSet set) throws SQLException {
|
||||
Set<UUID> uuids = new HashSet<>();
|
||||
while (set.next()) {
|
||||
UUID uuid = UUID.fromString(set.getString(columnUUID));
|
||||
UUID uuid = UUID.fromString(set.getString(Col.UUID.get()));
|
||||
uuids.add(uuid);
|
||||
}
|
||||
return uuids;
|
||||
@ -77,7 +78,7 @@ public class UsersTable extends UserIDTable {
|
||||
*/
|
||||
@Override
|
||||
public void removeUser(UUID uuid) throws SQLException {
|
||||
String sql = "DELETE FROM " + tableName + " WHERE (" + columnUUID + "=?)";
|
||||
String sql = "DELETE FROM " + tableName + " WHERE (" + Col.UUID + "=?)";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
@ -90,15 +91,17 @@ public class UsersTable extends UserIDTable {
|
||||
/**
|
||||
* @return the name of the column that inherits the ID.
|
||||
*/
|
||||
@Deprecated
|
||||
public String getColumnID() {
|
||||
return columnID;
|
||||
return Col.ID.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name of the column that inherits the UUID.
|
||||
*/
|
||||
@Deprecated
|
||||
public String getColumnUUID() {
|
||||
return columnUUID;
|
||||
return Col.UUID.get();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,8 +112,8 @@ public class UsersTable extends UserIDTable {
|
||||
* @throws SQLException DB Error
|
||||
*/
|
||||
public UUID getUuidOf(String playerName) throws SQLException {
|
||||
String sql = Select.from(tableName, columnUUID)
|
||||
.where("UPPER(" + columnName + ")=UPPER(?)")
|
||||
String sql = Select.from(tableName, Col.UUID)
|
||||
.where("UPPER(" + Col.USER_NAME + ")=UPPER(?)")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<UUID>(sql) {
|
||||
@ -122,7 +125,7 @@ public class UsersTable extends UserIDTable {
|
||||
@Override
|
||||
public UUID processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
String uuidS = set.getString(columnUUID);
|
||||
String uuidS = set.getString(Col.UUID.get());
|
||||
return UUID.fromString(uuidS);
|
||||
}
|
||||
return null;
|
||||
@ -131,20 +134,38 @@ public class UsersTable extends UserIDTable {
|
||||
}
|
||||
|
||||
public List<Long> getRegisterDates() throws SQLException {
|
||||
String sql = Select.from(tableName, columnRegistered).toString();
|
||||
String sql = Select.from(tableName, Col.REGISTERED).toString();
|
||||
|
||||
return query(new QueryAllStatement<List<Long>>(sql, 50000) {
|
||||
@Override
|
||||
public List<Long> processResults(ResultSet set) throws SQLException {
|
||||
List<Long> registerDates = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
registerDates.add(set.getLong(columnRegistered));
|
||||
registerDates.add(set.getLong(Col.REGISTERED.get()));
|
||||
}
|
||||
return registerDates;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isRegistered(UUID uuid) throws SQLException {
|
||||
String sql = Select.from(tableName, "COUNT(" + Col.ID + ") as c")
|
||||
.where(Col.UUID + "=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Boolean>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean processResults(ResultSet set) throws SQLException {
|
||||
return set.next() && set.getInt("c") >= 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new user (UUID) to the database.
|
||||
*
|
||||
@ -167,27 +188,9 @@ public class UsersTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isRegistered(UUID uuid) throws SQLException {
|
||||
String sql = Select.from(tableName, "COUNT(" + columnID + ") as c")
|
||||
.where(columnUUID + "=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Boolean>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean processResults(ResultSet set) throws SQLException {
|
||||
return set.next() && set.getInt("c") >= 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void updateName(UUID uuid, String name) throws SQLException {
|
||||
String sql = Update.values(tableName, columnName)
|
||||
.where(columnUUID + "=?")
|
||||
String sql = Update.values(tableName, Col.USER_NAME.get())
|
||||
.where(Col.UUID + "=?")
|
||||
.toString();
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@ -200,8 +203,8 @@ public class UsersTable extends UserIDTable {
|
||||
}
|
||||
|
||||
public int getTimesKicked(UUID uuid) throws SQLException {
|
||||
String sql = Select.from(tableName, columnTimesKicked)
|
||||
.where(columnUUID + "=?")
|
||||
String sql = Select.from(tableName, Col.TIMES_KICKED)
|
||||
.where(Col.UUID + "=?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Integer>(sql) {
|
||||
@ -213,7 +216,7 @@ public class UsersTable extends UserIDTable {
|
||||
@Override
|
||||
public Integer processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return set.getInt(columnTimesKicked);
|
||||
return set.getInt(Col.TIMES_KICKED.get());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -222,8 +225,8 @@ public class UsersTable extends UserIDTable {
|
||||
|
||||
public void kicked(UUID uuid) throws SQLException {
|
||||
String sql = "UPDATE " + tableName + " SET "
|
||||
+ columnTimesKicked + "=" + columnTimesKicked + "+ 1" +
|
||||
" WHERE " + columnUUID + "=?";
|
||||
+ Col.TIMES_KICKED + "=" + Col.TIMES_KICKED + "+ 1" +
|
||||
" WHERE " + Col.UUID + "=?";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
@ -234,7 +237,7 @@ public class UsersTable extends UserIDTable {
|
||||
}
|
||||
|
||||
public String getPlayerName(UUID uuid) throws SQLException {
|
||||
String sql = Select.from(tableName, columnName).where(columnUUID + "=?").toString();
|
||||
String sql = Select.from(tableName, Col.USER_NAME).where(Col.UUID + "=?").toString();
|
||||
|
||||
return query(new QueryStatement<String>(sql) {
|
||||
@Override
|
||||
@ -245,7 +248,7 @@ public class UsersTable extends UserIDTable {
|
||||
@Override
|
||||
public String processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return set.getString(columnName);
|
||||
return set.getString(Col.USER_NAME.get());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -262,11 +265,11 @@ public class UsersTable extends UserIDTable {
|
||||
public List<String> getMatchingNames(String name) throws SQLException {
|
||||
String searchString = "%" + name.toLowerCase() + "%";
|
||||
NicknamesTable nicknamesTable = db.getNicknamesTable();
|
||||
String sql = "SELECT DISTINCT " + columnName + " FROM " + tableName +
|
||||
" WHERE " + columnName + " LIKE ?" +
|
||||
" UNION SELECT DISTINCT " + columnName + " FROM " + tableName +
|
||||
" INNER JOIN " + nicknamesTable + " on " + columnID + "=" + nicknamesTable + "." + nicknamesTable.getColumnUserID() +
|
||||
" WHERE " + nicknamesTable.getColumnNick() + " LIKE ?";
|
||||
String sql = "SELECT DISTINCT " + Col.USER_NAME + " FROM " + tableName +
|
||||
" WHERE " + Col.USER_NAME + " LIKE ?" +
|
||||
" UNION SELECT DISTINCT " + Col.USER_NAME + " FROM " + tableName +
|
||||
" INNER JOIN " + nicknamesTable + " on " + Col.ID + "=" + nicknamesTable + "." + NicknamesTable.Col.USER_ID +
|
||||
" WHERE " + NicknamesTable.Col.NICKNAME + " LIKE ?";
|
||||
|
||||
return query(new QueryStatement<List<String>>(sql, 5000) {
|
||||
@Override
|
||||
@ -289,8 +292,28 @@ public class UsersTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getColumnName() {
|
||||
return columnName;
|
||||
return Col.USER_NAME.get();
|
||||
}
|
||||
|
||||
public Map<UUID, UserInfo> getUsers() throws SQLException {
|
||||
String sql = Select.all(tableName).toString();
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, UserInfo>>(sql, 20000) {
|
||||
@Override
|
||||
public Map<UUID, UserInfo> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, UserInfo> users = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID uuid = UUID.fromString(set.getString(Col.UUID.get()));
|
||||
String name = set.getString(Col.USER_NAME.get());
|
||||
long registered = set.getLong(Col.REGISTERED.get());
|
||||
|
||||
users.put(uuid, new UserInfo(uuid, name, registered, false, false));
|
||||
}
|
||||
return users;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -325,31 +348,12 @@ public class UsersTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, UserInfo> getUsers() throws SQLException {
|
||||
String sql = Select.all(tableName).toString();
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, UserInfo>>(sql, 20000) {
|
||||
@Override
|
||||
public Map<UUID, UserInfo> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, UserInfo> users = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID uuid = UUID.fromString(set.getString(columnUUID));
|
||||
String name = set.getString(columnName);
|
||||
long registered = set.getLong(columnRegistered);
|
||||
|
||||
users.put(uuid, new UserInfo(uuid, name, registered, false, false));
|
||||
}
|
||||
return users;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void updateKicked(Map<UUID, Integer> timesKicked) throws SQLException {
|
||||
if (Verify.isEmpty(timesKicked)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String sql = "UPDATE " + tableName + " SET " + columnTimesKicked + "=? WHERE " + columnUUID + "=?";
|
||||
String sql = "UPDATE " + tableName + " SET " + Col.TIMES_KICKED + "=? WHERE " + Col.UUID + "=?";
|
||||
|
||||
executeBatch(new ExecStatement(sql) {
|
||||
@Override
|
||||
@ -366,15 +370,15 @@ public class UsersTable extends UserIDTable {
|
||||
}
|
||||
|
||||
public Map<UUID, Integer> getAllTimesKicked() throws SQLException {
|
||||
String sql = Select.from(tableName, columnUUID, columnTimesKicked).toString();
|
||||
String sql = Select.from(tableName, Col.UUID, Col.TIMES_KICKED).toString();
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, Integer>>(sql, 20000) {
|
||||
@Override
|
||||
public Map<UUID, Integer> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, Integer> timesKicked = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID uuid = UUID.fromString(set.getString(columnUUID));
|
||||
int kickCount = set.getInt(columnTimesKicked);
|
||||
UUID uuid = UUID.fromString(set.getString(Col.UUID.get()));
|
||||
int kickCount = set.getInt(Col.TIMES_KICKED.get());
|
||||
|
||||
timesKicked.put(uuid, kickCount);
|
||||
}
|
||||
@ -384,15 +388,15 @@ public class UsersTable extends UserIDTable {
|
||||
}
|
||||
|
||||
public Map<UUID, String> getPlayerNames() throws SQLException {
|
||||
String sql = Select.from(tableName, columnUUID, columnName).toString();
|
||||
String sql = Select.from(tableName, Col.UUID, Col.USER_NAME).toString();
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, String>>(sql, 20000) {
|
||||
@Override
|
||||
public Map<UUID, String> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, String> names = new HashMap<>();
|
||||
while (set.next()) {
|
||||
UUID uuid = UUID.fromString(set.getString(columnUUID));
|
||||
String name = set.getString(columnName);
|
||||
UUID uuid = UUID.fromString(set.getString(Col.UUID.get()));
|
||||
String name = set.getString(Col.USER_NAME.get());
|
||||
|
||||
names.put(uuid, name);
|
||||
}
|
||||
@ -401,6 +405,25 @@ public class UsersTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<Long> getRegisterDate(UUID uuid) throws SQLException {
|
||||
String sql = Select.from(tableName, Col.REGISTERED).where(Col.UUID + "=?").toString();
|
||||
|
||||
return query(new QueryStatement<Optional<Long>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Long> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getLong(Col.REGISTERED.get()));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public int getPlayerCount() throws SQLException {
|
||||
String sql = "SELECT COUNT(*) AS player_count FROM " + tableName;
|
||||
|
||||
@ -415,27 +438,8 @@ public class UsersTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<Long> getRegisterDate(UUID uuid) throws SQLException {
|
||||
String sql = Select.from(tableName, columnRegistered).where(columnUUID + "=?").toString();
|
||||
|
||||
return query(new QueryStatement<Optional<Long>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Long> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(set.getLong(columnRegistered));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Map<Integer, UUID> getUUIDsByID() throws SQLException {
|
||||
String sql = Select.from(tableName, columnID, columnUUID).toString();
|
||||
String sql = Select.from(tableName, Col.ID, Col.UUID).toString();
|
||||
|
||||
return query(new QueryAllStatement<Map<Integer, UUID>>(sql, 20000) {
|
||||
@Override
|
||||
@ -443,8 +447,8 @@ public class UsersTable extends UserIDTable {
|
||||
Map<Integer, UUID> uuidsByID = new TreeMap<>();
|
||||
|
||||
while (set.next()) {
|
||||
int id = set.getInt(columnID);
|
||||
UUID uuid = UUID.fromString(set.getString(columnUUID));
|
||||
int id = set.getInt(Col.ID.get());
|
||||
UUID uuid = UUID.fromString(set.getString(Col.UUID.get()));
|
||||
uuidsByID.put(id, uuid);
|
||||
}
|
||||
|
||||
@ -452,4 +456,27 @@ public class UsersTable extends UserIDTable {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public enum Col implements Column {
|
||||
ID("id"),
|
||||
UUID("uuid"),
|
||||
REGISTERED("registered"),
|
||||
USER_NAME("name"),
|
||||
TIMES_KICKED("times_kicked");
|
||||
|
||||
private final String column;
|
||||
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
@ -25,21 +26,19 @@ import java.util.*;
|
||||
*/
|
||||
public class WorldTable extends Table {
|
||||
|
||||
public final String statementSelectID;
|
||||
private static final String columnWorldId = "id";
|
||||
private static final String columnWorldName = "world_name";
|
||||
|
||||
public WorldTable(SQLDB db) {
|
||||
super("plan_worlds", db);
|
||||
statementSelectID = "(SELECT " + columnWorldId + " FROM " + tableName + " WHERE (" + columnWorldName + "=?) LIMIT 1)";
|
||||
statementSelectID = "(SELECT " + Col.ID + " FROM " + tableName + " WHERE (" + Col.NAME + "=?) LIMIT 1)";
|
||||
}
|
||||
|
||||
public final String statementSelectID;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.primaryKeyIDColumn(usingMySQL, columnWorldId)
|
||||
.column(columnWorldName, Sql.varchar(100)).notNull()
|
||||
.primaryKey(usingMySQL, columnWorldId)
|
||||
.primaryKeyIDColumn(usingMySQL, Col.ID)
|
||||
.column(Col.NAME, Sql.varchar(100)).notNull()
|
||||
.primaryKey(usingMySQL, Col.ID)
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
@ -58,7 +57,7 @@ public class WorldTable extends Table {
|
||||
public List<String> processResults(ResultSet set) throws SQLException {
|
||||
List<String> worldNames = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
String worldName = set.getString(columnWorldName);
|
||||
String worldName = set.getString(Col.NAME.get());
|
||||
worldNames.add(worldName);
|
||||
}
|
||||
return worldNames;
|
||||
@ -85,7 +84,7 @@ public class WorldTable extends Table {
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO " + tableName + " ("
|
||||
+ columnWorldName
|
||||
+ Col.NAME
|
||||
+ ") VALUES (?)";
|
||||
|
||||
executeBatch(new ExecStatement(sql) {
|
||||
@ -99,18 +98,6 @@ public class WorldTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public String getColumnID() {
|
||||
return columnWorldId;
|
||||
}
|
||||
|
||||
public String getColumnWorldName() {
|
||||
return columnWorldName;
|
||||
}
|
||||
|
||||
public Set<String> getWorldNames() throws SQLException {
|
||||
return getWorldNames(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public Set<String> getWorldNames(UUID serverUUID) throws SQLException {
|
||||
WorldTimesTable worldTimesTable = db.getWorldTimesTable();
|
||||
SessionsTable sessionsTable = db.getSessionsTable();
|
||||
@ -118,15 +105,15 @@ public class WorldTable extends Table {
|
||||
|
||||
String statementSelectServerID = serverTable.statementSelectServerID;
|
||||
|
||||
String worldIDColumn = worldTimesTable + "." + worldTimesTable.getColumnWorldId();
|
||||
String worldSessionIDColumn = worldTimesTable + "." + worldTimesTable.getColumnSessionID();
|
||||
String sessionIDColumn = sessionsTable + "." + sessionsTable.getColumnID();
|
||||
String sessionServerIDColumn = sessionsTable + "." + sessionsTable.getcolumnServerID();
|
||||
String worldIDColumn = worldTimesTable + "." + WorldTimesTable.Col.WORLD_ID;
|
||||
String worldSessionIDColumn = worldTimesTable + "." + WorldTimesTable.Col.SESSION_ID;
|
||||
String sessionIDColumn = sessionsTable + "." + SessionsTable.Col.ID;
|
||||
String sessionServerIDColumn = sessionsTable + "." + SessionsTable.Col.SERVER_ID;
|
||||
|
||||
String sql = "SELECT DISTINCT " +
|
||||
columnWorldName + " FROM " +
|
||||
Col.NAME + " FROM " +
|
||||
tableName +
|
||||
" INNER JOIN " + worldTimesTable + " on " + worldIDColumn + "=" + tableName + "." + columnWorldId +
|
||||
" INNER JOIN " + worldTimesTable + " on " + worldIDColumn + "=" + tableName + "." + Col.ID +
|
||||
" INNER JOIN " + sessionsTable + " on " + worldSessionIDColumn + "=" + sessionIDColumn +
|
||||
" WHERE " + statementSelectServerID + "=" + sessionServerIDColumn;
|
||||
|
||||
@ -140,17 +127,21 @@ public class WorldTable extends Table {
|
||||
public Set<String> processResults(ResultSet set) throws SQLException {
|
||||
Set<String> worldNames = new HashSet<>();
|
||||
while (set.next()) {
|
||||
worldNames.add(set.getString(columnWorldName));
|
||||
worldNames.add(set.getString(Col.NAME.get()));
|
||||
}
|
||||
return worldNames;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Set<String> getWorldNames() throws SQLException {
|
||||
return getWorldNames(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public Map<String, Integer> getWorldIds() throws SQLException {
|
||||
String sql = "SELECT DISTINCT " +
|
||||
columnWorldName + ", " +
|
||||
columnWorldId + " FROM " +
|
||||
Col.NAME + ", " +
|
||||
Col.ID + " FROM " +
|
||||
tableName;
|
||||
|
||||
return query(new QueryAllStatement<Map<String, Integer>>(sql, 200) {
|
||||
@ -158,12 +149,32 @@ public class WorldTable extends Table {
|
||||
public Map<String, Integer> processResults(ResultSet set) throws SQLException {
|
||||
Map<String, Integer> worldIds = new HashMap<>();
|
||||
while (set.next()) {
|
||||
String worldName = set.getString(columnWorldName);
|
||||
int worldId = set.getInt(columnWorldId);
|
||||
String worldName = set.getString(Col.NAME.get());
|
||||
int worldId = set.getInt(Col.ID.get());
|
||||
worldIds.put(worldName, worldId);
|
||||
}
|
||||
return worldIds;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public enum Col implements Column {
|
||||
ID("id"),
|
||||
NAME("world_name");
|
||||
|
||||
private final String column;
|
||||
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
|
||||
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
|
||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
@ -20,59 +21,103 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Table class representing database table plan_world_times.
|
||||
* Table that is in charge of storing playtime data for each world in each GameMode.
|
||||
* <p>
|
||||
* Table Name: plan_world_times
|
||||
* <p>
|
||||
* For contained columns {@see Col}
|
||||
*
|
||||
* @author Rsl1122
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public class WorldTimesTable extends UserIDTable {
|
||||
|
||||
private static final String columnSessionID = "session_id";
|
||||
private static final String columnWorldId = "world_id";
|
||||
private static final String columnSurvival = "survival_time";
|
||||
private static final String columnCreative = "creative_time";
|
||||
private static final String columnAdventure = "adventure_time";
|
||||
private static final String columnSpectator = "spectator_time";
|
||||
|
||||
private final WorldTable worldTable;
|
||||
private final SessionsTable sessionsTable;
|
||||
private String insertStatement;
|
||||
|
||||
public WorldTimesTable(SQLDB db) {
|
||||
super("plan_world_times", db);
|
||||
worldTable = db.getWorldTable();
|
||||
sessionsTable = db.getSessionsTable();
|
||||
insertStatement = "INSERT INTO " + tableName + " (" +
|
||||
columnUserID + ", " +
|
||||
columnWorldId + ", " +
|
||||
columnSessionID + ", " +
|
||||
columnSurvival + ", " +
|
||||
columnCreative + ", " +
|
||||
columnAdventure + ", " +
|
||||
columnSpectator +
|
||||
Col.USER_ID + ", " +
|
||||
Col.WORLD_ID + ", " +
|
||||
Col.SESSION_ID + ", " +
|
||||
Col.SURVIVAL + ", " +
|
||||
Col.CREATIVE + ", " +
|
||||
Col.ADVENTURE + ", " +
|
||||
Col.SPECTATOR +
|
||||
") VALUES (" +
|
||||
usersTable.statementSelectID + ", " +
|
||||
worldTable.statementSelectID + ", " +
|
||||
"?, ?, ?, ?, ?)";
|
||||
}
|
||||
|
||||
private final WorldTable worldTable;
|
||||
private final SessionsTable sessionsTable;
|
||||
private String insertStatement;
|
||||
|
||||
@Override
|
||||
public void createTable() throws DBInitException {
|
||||
createTable(TableSqlParser.createTable(tableName)
|
||||
.column(columnUserID, Sql.INT).notNull()
|
||||
.column(columnWorldId, Sql.INT).notNull()
|
||||
.column(columnSessionID, Sql.INT).notNull()
|
||||
.column(columnSurvival, Sql.LONG).notNull().defaultValue("0")
|
||||
.column(columnCreative, Sql.LONG).notNull().defaultValue("0")
|
||||
.column(columnAdventure, Sql.LONG).notNull().defaultValue("0")
|
||||
.column(columnSpectator, Sql.LONG).notNull().defaultValue("0")
|
||||
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
|
||||
.foreignKey(columnWorldId, worldTable.getTableName(), worldTable.getColumnID())
|
||||
.foreignKey(columnSessionID, sessionsTable.getTableName(), sessionsTable.getColumnID())
|
||||
.column(Col.USER_ID, Sql.INT).notNull()
|
||||
.column(Col.WORLD_ID, Sql.INT).notNull()
|
||||
.column(Col.SESSION_ID, Sql.INT).notNull()
|
||||
.column(Col.SURVIVAL, Sql.LONG).notNull().defaultValue("0")
|
||||
.column(Col.CREATIVE, Sql.LONG).notNull().defaultValue("0")
|
||||
.column(Col.ADVENTURE, Sql.LONG).notNull().defaultValue("0")
|
||||
.column(Col.SPECTATOR, Sql.LONG).notNull().defaultValue("0")
|
||||
.foreignKey(Col.USER_ID, usersTable.getTableName(), UsersTable.Col.ID)
|
||||
.foreignKey(Col.WORLD_ID, worldTable.getTableName(), WorldTable.Col.ID)
|
||||
.foreignKey(Col.SESSION_ID, sessionsTable.getTableName(), SessionsTable.Col.ID)
|
||||
.toString()
|
||||
);
|
||||
}
|
||||
|
||||
public void addWorldTimesToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException {
|
||||
String worldIDColumn = worldTable + "." + WorldTable.Col.ID;
|
||||
String worldNameColumn = worldTable + "." + WorldTable.Col.NAME + " as world_name";
|
||||
String sql = "SELECT " +
|
||||
Col.SESSION_ID + ", " +
|
||||
Col.SURVIVAL + ", " +
|
||||
Col.CREATIVE + ", " +
|
||||
Col.ADVENTURE + ", " +
|
||||
Col.SPECTATOR + ", " +
|
||||
worldNameColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + Col.WORLD_ID +
|
||||
" WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID;
|
||||
|
||||
query(new QueryStatement<Object>(sql, 2000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object processResults(ResultSet set) throws SQLException {
|
||||
String[] gms = GMTimes.getGMKeyArray();
|
||||
|
||||
while (set.next()) {
|
||||
int sessionID = set.getInt(Col.SESSION_ID.get());
|
||||
Session session = sessions.get(sessionID);
|
||||
|
||||
if (session == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String worldName = set.getString("world_name");
|
||||
|
||||
Map<String, Long> gmMap = new HashMap<>();
|
||||
gmMap.put(gms[0], set.getLong(Col.SURVIVAL.get()));
|
||||
gmMap.put(gms[1], set.getLong(Col.CREATIVE.get()));
|
||||
gmMap.put(gms[2], set.getLong(Col.ADVENTURE.get()));
|
||||
gmMap.put(gms[3], set.getLong(Col.SPECTATOR.get()));
|
||||
GMTimes gmTimes = new GMTimes(gmMap);
|
||||
|
||||
session.getWorldTimes().setGMTimesForWorld(worldName, gmTimes);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void saveWorldTimes(UUID uuid, int sessionID, WorldTimes worldTimes) throws SQLException {
|
||||
Map<String, GMTimes> worldTimesMap = worldTimes.getWorldTimes();
|
||||
if (Verify.isEmpty(worldTimesMap)) {
|
||||
@ -103,74 +148,22 @@ public class WorldTimesTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public void addWorldTimesToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException {
|
||||
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
|
||||
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
|
||||
String sql = "SELECT " +
|
||||
columnSessionID + ", " +
|
||||
columnSurvival + ", " +
|
||||
columnCreative + ", " +
|
||||
columnAdventure + ", " +
|
||||
columnSpectator + ", " +
|
||||
worldNameColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
|
||||
" WHERE " + columnUserID + "=" + usersTable.statementSelectID;
|
||||
|
||||
query(new QueryStatement<Object>(sql, 2000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, uuid.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object processResults(ResultSet set) throws SQLException {
|
||||
String[] gms = GMTimes.getGMKeyArray();
|
||||
|
||||
while (set.next()) {
|
||||
int sessionID = set.getInt(columnSessionID);
|
||||
Session session = sessions.get(sessionID);
|
||||
|
||||
if (session == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String worldName = set.getString("world_name");
|
||||
|
||||
Map<String, Long> gmMap = new HashMap<>();
|
||||
gmMap.put(gms[0], set.getLong(columnSurvival));
|
||||
gmMap.put(gms[1], set.getLong(columnCreative));
|
||||
gmMap.put(gms[2], set.getLong(columnAdventure));
|
||||
gmMap.put(gms[3], set.getLong(columnSpectator));
|
||||
GMTimes gmTimes = new GMTimes(gmMap);
|
||||
|
||||
session.getWorldTimes().setGMTimesForWorld(worldName, gmTimes);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public WorldTimes getWorldTimesOfServer() throws SQLException {
|
||||
return getWorldTimesOfServer(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public WorldTimes getWorldTimesOfServer(UUID serverUUID) throws SQLException {
|
||||
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
|
||||
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
|
||||
String sessionIDColumn = sessionsTable + "." + sessionsTable.getColumnID();
|
||||
String worldIDColumn = worldTable + "." + WorldTable.Col.ID;
|
||||
String worldNameColumn = worldTable + "." + WorldTable.Col.NAME + " as world_name";
|
||||
String sessionIDColumn = sessionsTable + "." + SessionsTable.Col.ID;
|
||||
String sessionServerIDColumn = sessionsTable + ".server_id";
|
||||
String sql = "SELECT " +
|
||||
"SUM(" + columnSurvival + ") as survival, " +
|
||||
"SUM(" + columnCreative + ") as creative, " +
|
||||
"SUM(" + columnAdventure + ") as adventure, " +
|
||||
"SUM(" + columnSpectator + ") as spectator, " +
|
||||
"SUM(" + Col.SURVIVAL + ") as survival, " +
|
||||
"SUM(" + Col.CREATIVE + ") as creative, " +
|
||||
"SUM(" + Col.ADVENTURE + ") as adventure, " +
|
||||
"SUM(" + Col.SPECTATOR + ") as spectator, " +
|
||||
worldNameColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
|
||||
" INNER JOIN " + sessionsTable + " on " + sessionIDColumn + "=" + columnSessionID +
|
||||
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + Col.WORLD_ID +
|
||||
" INNER JOIN " + sessionsTable + " on " + sessionIDColumn + "=" + Col.SESSION_ID +
|
||||
" WHERE " + sessionServerIDColumn + "=" + db.getServerTable().statementSelectServerID +
|
||||
" GROUP BY " + columnWorldId;
|
||||
" GROUP BY " + Col.WORLD_ID;
|
||||
|
||||
return query(new QueryStatement<WorldTimes>(sql, 1000) {
|
||||
@Override
|
||||
@ -200,19 +193,23 @@ public class WorldTimesTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public WorldTimes getWorldTimesOfServer() throws SQLException {
|
||||
return getWorldTimesOfServer(ServerInfo.getServerUUID());
|
||||
}
|
||||
|
||||
public WorldTimes getWorldTimesOfUser(UUID uuid) throws SQLException {
|
||||
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
|
||||
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
|
||||
String worldIDColumn = worldTable + "." + WorldTable.Col.ID;
|
||||
String worldNameColumn = worldTable + "." + WorldTable.Col.NAME + " as world_name";
|
||||
String sql = "SELECT " +
|
||||
"SUM(" + columnSurvival + ") as survival, " +
|
||||
"SUM(" + columnCreative + ") as creative, " +
|
||||
"SUM(" + columnAdventure + ") as adventure, " +
|
||||
"SUM(" + columnSpectator + ") as spectator, " +
|
||||
"SUM(" + Col.SURVIVAL + ") as survival, " +
|
||||
"SUM(" + Col.CREATIVE + ") as creative, " +
|
||||
"SUM(" + Col.ADVENTURE + ") as adventure, " +
|
||||
"SUM(" + Col.SPECTATOR + ") as spectator, " +
|
||||
worldNameColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId +
|
||||
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
|
||||
" GROUP BY " + columnWorldId;
|
||||
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + Col.WORLD_ID +
|
||||
" WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID +
|
||||
" GROUP BY " + Col.WORLD_ID;
|
||||
|
||||
return query(new QueryStatement<WorldTimes>(sql) {
|
||||
@Override
|
||||
@ -242,6 +239,46 @@ public class WorldTimesTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<Integer, WorldTimes> getAllWorldTimesBySessionID() throws SQLException {
|
||||
String worldIDColumn = worldTable + "." + WorldTable.Col.ID;
|
||||
String worldNameColumn = worldTable + "." + WorldTable.Col.NAME + " as world_name";
|
||||
String sql = "SELECT " +
|
||||
Col.SESSION_ID + ", " +
|
||||
Col.SURVIVAL + ", " +
|
||||
Col.CREATIVE + ", " +
|
||||
Col.ADVENTURE + ", " +
|
||||
Col.SPECTATOR + ", " +
|
||||
worldNameColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + Col.WORLD_ID;
|
||||
|
||||
return query(new QueryAllStatement<Map<Integer, WorldTimes>>(sql, 50000) {
|
||||
@Override
|
||||
public Map<Integer, WorldTimes> processResults(ResultSet set) throws SQLException {
|
||||
String[] gms = GMTimes.getGMKeyArray();
|
||||
|
||||
Map<Integer, WorldTimes> worldTimes = new HashMap<>();
|
||||
while (set.next()) {
|
||||
int sessionID = set.getInt(Col.SESSION_ID.get());
|
||||
|
||||
String worldName = set.getString("world_name");
|
||||
|
||||
Map<String, Long> gmMap = new HashMap<>();
|
||||
gmMap.put(gms[0], set.getLong(Col.SURVIVAL.get()));
|
||||
gmMap.put(gms[1], set.getLong(Col.CREATIVE.get()));
|
||||
gmMap.put(gms[2], set.getLong(Col.ADVENTURE.get()));
|
||||
gmMap.put(gms[3], set.getLong(Col.SPECTATOR.get()));
|
||||
GMTimes gmTimes = new GMTimes(gmMap);
|
||||
|
||||
WorldTimes worldTOfSession = worldTimes.getOrDefault(sessionID, new WorldTimes(new HashMap<>()));
|
||||
worldTOfSession.setGMTimesForWorld(worldName, gmTimes);
|
||||
worldTimes.put(sessionID, worldTOfSession);
|
||||
}
|
||||
return worldTimes;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void addWorldTimesToSessions(Map<UUID, Map<UUID, List<Session>>> map) throws SQLException {
|
||||
Map<Integer, WorldTimes> worldTimesBySessionID = getAllWorldTimesBySessionID();
|
||||
|
||||
@ -306,51 +343,28 @@ public class WorldTimesTable extends UserIDTable {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<Integer, WorldTimes> getAllWorldTimesBySessionID() throws SQLException {
|
||||
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
|
||||
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
|
||||
String sql = "SELECT " +
|
||||
columnSessionID + ", " +
|
||||
columnSurvival + ", " +
|
||||
columnCreative + ", " +
|
||||
columnAdventure + ", " +
|
||||
columnSpectator + ", " +
|
||||
worldNameColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + columnWorldId;
|
||||
public enum Col implements Column {
|
||||
USER_ID(UserIDTable.Col.USER_ID.get()),
|
||||
SESSION_ID("session_id"),
|
||||
WORLD_ID("world_id"),
|
||||
SURVIVAL("survival_time"),
|
||||
CREATIVE("creative_time"),
|
||||
ADVENTURE("adventure_time"),
|
||||
SPECTATOR("spectator_time");
|
||||
|
||||
return query(new QueryAllStatement<Map<Integer, WorldTimes>>(sql, 50000) {
|
||||
@Override
|
||||
public Map<Integer, WorldTimes> processResults(ResultSet set) throws SQLException {
|
||||
String[] gms = GMTimes.getGMKeyArray();
|
||||
private final String column;
|
||||
|
||||
Map<Integer, WorldTimes> worldTimes = new HashMap<>();
|
||||
while (set.next()) {
|
||||
int sessionID = set.getInt(columnSessionID);
|
||||
Col(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
String worldName = set.getString("world_name");
|
||||
public String get() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
Map<String, Long> gmMap = new HashMap<>();
|
||||
gmMap.put(gms[0], set.getLong(columnSurvival));
|
||||
gmMap.put(gms[1], set.getLong(columnCreative));
|
||||
gmMap.put(gms[2], set.getLong(columnAdventure));
|
||||
gmMap.put(gms[3], set.getLong(columnSpectator));
|
||||
GMTimes gmTimes = new GMTimes(gmMap);
|
||||
|
||||
WorldTimes worldTOfSession = worldTimes.getOrDefault(sessionID, new WorldTimes(new HashMap<>()));
|
||||
worldTOfSession.setGMTimesForWorld(worldName, gmTimes);
|
||||
worldTimes.put(sessionID, worldTOfSession);
|
||||
}
|
||||
return worldTimes;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
String getColumnWorldId() {
|
||||
return columnWorldId;
|
||||
}
|
||||
|
||||
String getColumnSessionID() {
|
||||
return columnSessionID;
|
||||
@Override
|
||||
public String toString() {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user