mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-03-13 07:10:03 +01:00
NicknamesTable
This commit is contained in:
parent
75d1a3e88c
commit
4516ab81bf
@ -1,16 +1,17 @@
|
|||||||
package main.java.com.djrapitops.plan.database.tables;
|
package main.java.com.djrapitops.plan.database.tables;
|
||||||
|
|
||||||
import com.djrapitops.plugin.utilities.Verify;
|
|
||||||
import main.java.com.djrapitops.plan.Log;
|
import main.java.com.djrapitops.plan.Log;
|
||||||
|
import main.java.com.djrapitops.plan.Plan;
|
||||||
import main.java.com.djrapitops.plan.database.databases.SQLDB;
|
import main.java.com.djrapitops.plan.database.databases.SQLDB;
|
||||||
import main.java.com.djrapitops.plan.database.sql.Sql;
|
import main.java.com.djrapitops.plan.database.sql.Sql;
|
||||||
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
|
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
|
||||||
import main.java.com.djrapitops.plan.utilities.Benchmark;
|
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rsl1122
|
* @author Rsl1122
|
||||||
@ -18,8 +19,9 @@ import java.util.*;
|
|||||||
public class NicknamesTable extends UserIDTable {
|
public class NicknamesTable extends UserIDTable {
|
||||||
|
|
||||||
private final String columnNick = "nickname";
|
private final String columnNick = "nickname";
|
||||||
private final String columnCurrent = "current_nick";
|
private final String columnServerID = "server_id";
|
||||||
private final String columnServerID = "server_id"; //TODO
|
|
||||||
|
private ServerTable serverTable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param db The database
|
* @param db The database
|
||||||
@ -27,6 +29,7 @@ public class NicknamesTable extends UserIDTable {
|
|||||||
*/
|
*/
|
||||||
public NicknamesTable(SQLDB db, boolean usingMySQL) {
|
public NicknamesTable(SQLDB db, boolean usingMySQL) {
|
||||||
super("plan_nicknames", db, usingMySQL);
|
super("plan_nicknames", db, usingMySQL);
|
||||||
|
serverTable = db.getServerTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,14 +42,11 @@ public class NicknamesTable extends UserIDTable {
|
|||||||
execute(TableSqlParser.createTable(tableName)
|
execute(TableSqlParser.createTable(tableName)
|
||||||
.column(columnUserID, Sql.INT).notNull()
|
.column(columnUserID, Sql.INT).notNull()
|
||||||
.column(columnNick, Sql.varchar(75)).notNull()
|
.column(columnNick, Sql.varchar(75)).notNull()
|
||||||
.column(columnCurrent, Sql.BOOL).notNull().defaultValue(false)
|
.column(columnServerID, Sql.INT).notNull()
|
||||||
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
|
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
|
||||||
|
.foreignKey(columnServerID, serverTable.getTableName(), serverTable.getColumnID())
|
||||||
.toString()
|
.toString()
|
||||||
);
|
);
|
||||||
|
|
||||||
if (getVersion() < 3) {
|
|
||||||
alterTablesV3();
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
Log.toLog(this.getClass().getName(), ex);
|
Log.toLog(this.getClass().getName(), ex);
|
||||||
@ -54,12 +54,8 @@ public class NicknamesTable extends UserIDTable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void alterTablesV3() {
|
|
||||||
addColumns(columnCurrent + " boolean NOT NULL DEFAULT 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param userId The User ID from which the nicknames should be removed from
|
* @param userId The User ID whose the nicknames should be removed
|
||||||
* @return if the removal was successful
|
* @return if the removal was successful
|
||||||
*/
|
*/
|
||||||
public boolean removeUserNicknames(int userId) {
|
public boolean removeUserNicknames(int userId) {
|
||||||
@ -67,36 +63,33 @@ public class NicknamesTable extends UserIDTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param userId The User ID from which the nicknames should be retrieved from
|
* Get ALL nicknames of the user.
|
||||||
|
* <p>
|
||||||
|
* Get's nicknames from other servers as well.
|
||||||
|
*
|
||||||
|
* @param uuid UUID of the Player
|
||||||
* @return The nicknames of the User
|
* @return The nicknames of the User
|
||||||
* @throws SQLException when an error at retrieval happens
|
* @throws SQLException when an error at retrieval happens
|
||||||
*/
|
*/
|
||||||
public List<String> getNicknames(int userId) throws SQLException {
|
public List<String> getAllNicknames(UUID uuid) throws SQLException {
|
||||||
PreparedStatement statement = null;
|
PreparedStatement statement = null;
|
||||||
ResultSet set = null;
|
ResultSet set = null;
|
||||||
try {
|
try {
|
||||||
statement = prepareStatement("SELECT * FROM " + tableName + " WHERE (" + columnUserID + "=?)");
|
statement = prepareStatement("SELECT " + columnNick + " FROM " + tableName +
|
||||||
statement.setInt(1, userId);
|
" WHERE (" + columnUserID + "=" + usersTable.statementSelectID + ")");
|
||||||
|
statement.setString(1, uuid.toString());
|
||||||
set = statement.executeQuery();
|
set = statement.executeQuery();
|
||||||
|
|
||||||
List<String> nicknames = new ArrayList<>();
|
List<String> nicknames = new ArrayList<>();
|
||||||
String lastNick = "";
|
|
||||||
while (set.next()) {
|
while (set.next()) {
|
||||||
String nickname = set.getString(columnNick);
|
String nickname = set.getString(columnNick);
|
||||||
if (nickname.isEmpty()) {
|
if (nickname.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (!nicknames.contains(nickname)) {
|
||||||
nicknames.add(nickname);
|
nicknames.add(nickname);
|
||||||
if (set.getBoolean(columnCurrent)) {
|
|
||||||
lastNick = nickname;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lastNick.isEmpty()) {
|
|
||||||
nicknames.set(nicknames.size() - 1, lastNick);
|
|
||||||
}
|
|
||||||
|
|
||||||
return nicknames;
|
return nicknames;
|
||||||
} finally {
|
} finally {
|
||||||
close(set, statement);
|
close(set, statement);
|
||||||
@ -104,151 +97,78 @@ public class NicknamesTable extends UserIDTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param userId The User ID for which the nicknames should be saved for
|
* Get nicknames of the user on THIS server.
|
||||||
* @param names The nicknames
|
* <p>
|
||||||
* @param lastNick The latest nickname
|
* Get's nicknames from other servers as well.
|
||||||
* @throws SQLException when an error at saving happens
|
*
|
||||||
|
* @param uuid UUID of the Player
|
||||||
|
* @return The nicknames of the User
|
||||||
|
* @throws SQLException when an error at retrieval happens
|
||||||
*/
|
*/
|
||||||
public void saveNickList(int userId, Set<String> names, String lastNick) throws SQLException {
|
public List<String> getNicknames(UUID uuid) throws SQLException {
|
||||||
if (Verify.isEmpty(names)) {
|
return getNicknames(uuid, Plan.getServerUUID());
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
names.removeAll(getNicknames(userId));
|
|
||||||
if (names.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PreparedStatement statement = null;
|
|
||||||
try {
|
|
||||||
statement = prepareStatement("INSERT INTO " + tableName + " ("
|
|
||||||
+ columnUserID + ", "
|
|
||||||
+ columnCurrent + ", "
|
|
||||||
+ columnNick
|
|
||||||
+ ") VALUES (?, ?, ?)");
|
|
||||||
|
|
||||||
for (String name : names) {
|
|
||||||
statement.setInt(1, userId);
|
|
||||||
statement.setInt(2, name.equals(lastNick) ? 1 : 0);
|
|
||||||
statement.setString(3, name);
|
|
||||||
statement.addBatch();
|
|
||||||
}
|
|
||||||
|
|
||||||
statement.executeBatch();
|
|
||||||
} finally {
|
|
||||||
close(statement);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ids The User IDs for which the nicknames should be retrieved for
|
* Get nicknames of the user on a server.
|
||||||
* @return The User ID corresponding with the nicknames
|
* <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
|
* @throws SQLException when an error at retrieval happens
|
||||||
*/
|
*/
|
||||||
public Map<Integer, List<String>> getNicknames(Collection<Integer> ids) throws SQLException {
|
public List<String> getNicknames(UUID uuid, UUID serverUUID) throws SQLException {
|
||||||
if (Verify.isEmpty(ids)) {
|
|
||||||
return new HashMap<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
Benchmark.start("Get Nicknames Multiple");
|
|
||||||
|
|
||||||
PreparedStatement statement = null;
|
PreparedStatement statement = null;
|
||||||
ResultSet set = null;
|
ResultSet set = null;
|
||||||
try {
|
try {
|
||||||
Map<Integer, List<String>> nicks = new HashMap<>();
|
statement = prepareStatement("SELECT " + columnNick + " FROM " + tableName +
|
||||||
Map<Integer, String> lastNicks = new HashMap<>();
|
" WHERE (" + columnUserID + "=" + usersTable.statementSelectID + ")" +
|
||||||
|
" AND " + columnServerID + "=" + serverTable.statementSelectServerID
|
||||||
for (Integer id : ids) {
|
);
|
||||||
nicks.put(id, new ArrayList<>());
|
statement.setString(1, uuid.toString());
|
||||||
}
|
statement.setString(2, serverUUID.toString());
|
||||||
|
|
||||||
statement = prepareStatement("SELECT * FROM " + tableName);
|
|
||||||
set = statement.executeQuery();
|
set = statement.executeQuery();
|
||||||
while (set.next()) {
|
|
||||||
Integer id = set.getInt(columnUserID);
|
|
||||||
String nickname = set.getString(columnNick);
|
|
||||||
|
|
||||||
if (!ids.contains(id) || nickname.isEmpty()) {
|
List<String> nicknames = new ArrayList<>();
|
||||||
|
while (set.next()) {
|
||||||
|
String nickname = set.getString(columnNick);
|
||||||
|
if (nickname.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (!nicknames.contains(nickname)) {
|
||||||
nicks.get(id).add(nickname);
|
nicknames.add(nickname);
|
||||||
if (set.getBoolean(columnCurrent)) {
|
|
||||||
lastNicks.put(id, nickname);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nicknames;
|
||||||
for (Map.Entry<Integer, String> entry : lastNicks.entrySet()) {
|
|
||||||
Integer id = entry.getKey();
|
|
||||||
String lastNick = entry.getValue();
|
|
||||||
|
|
||||||
List<String> list = nicks.get(id);
|
|
||||||
|
|
||||||
// Moves the last known nickname to the end of the List.
|
|
||||||
// This is due to the way nicknames are added to UserData,
|
|
||||||
// Nicknames are stored as a Set and last Nickname is a separate String.
|
|
||||||
list.set(list.size() - 1, lastNick);
|
|
||||||
}
|
|
||||||
|
|
||||||
return nicks;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(set, statement);
|
close(set, statement);
|
||||||
Benchmark.stop("Database", "Get Nicknames Multiple");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void saveUserName(UUID uuid, String displayName) throws SQLException {
|
||||||
* @param nicknames The User ID corresponding to the nicknames
|
List<String> saved = getNicknames(uuid);
|
||||||
* @param lastNicks The User ID corresponding with the last nick they inherited
|
if (saved.contains(displayName)) {
|
||||||
* @throws SQLException when an error at saving happens
|
|
||||||
*/
|
|
||||||
public void saveNickLists(Map<Integer, Set<String>> nicknames, Map<Integer, String> lastNicks) throws SQLException {
|
|
||||||
if (Verify.isEmpty(nicknames)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Benchmark.start("Save Nicknames Multiple");
|
|
||||||
|
|
||||||
Map<Integer, List<String>> saved = getNicknames(nicknames.keySet());
|
|
||||||
PreparedStatement statement = null;
|
PreparedStatement statement = null;
|
||||||
try {
|
try {
|
||||||
boolean commitRequired = false;
|
statement = prepareStatement("INSERT INTO " + tableName + " (" +
|
||||||
statement = prepareStatement("INSERT INTO " + tableName + " ("
|
columnUserID + ", " +
|
||||||
+ columnUserID + ", "
|
columnServerID + ", " +
|
||||||
+ columnCurrent + ", "
|
columnNick +
|
||||||
+ columnNick
|
") VALUES (" +
|
||||||
+ ") VALUES (?, ?, ?)");
|
usersTable.statementSelectID + ", " +
|
||||||
|
serverTable.statementSelectServerID + ", " +
|
||||||
for (Map.Entry<Integer, Set<String>> entrySet : nicknames.entrySet()) {
|
"?)");
|
||||||
Integer id = entrySet.getKey();
|
statement.setString(1, uuid.toString());
|
||||||
Set<String> newNicks = entrySet.getValue();
|
statement.setString(2, Plan.getServerUUID().toString());
|
||||||
|
statement.setString(3, displayName);
|
||||||
String lastNick = lastNicks.get(id);
|
statement.execute();
|
||||||
List<String> s = saved.get(id);
|
|
||||||
|
|
||||||
if (s != null) {
|
|
||||||
newNicks.removeAll(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newNicks.isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (String name : newNicks) {
|
|
||||||
statement.setInt(1, id);
|
|
||||||
statement.setInt(2, (name.equals(lastNick)) ? 1 : 0);
|
|
||||||
statement.setString(3, name);
|
|
||||||
statement.addBatch();
|
|
||||||
commitRequired = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (commitRequired) {
|
|
||||||
statement.executeBatch();
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
close(statement);
|
close(statement);
|
||||||
Benchmark.stop("Database", "Save Nicknames Multiple");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,13 +62,6 @@ public class TPSTable extends Table {
|
|||||||
.column(columnChunksLoaded, Sql.INT).notNull()
|
.column(columnChunksLoaded, Sql.INT).notNull()
|
||||||
.toString()
|
.toString()
|
||||||
);
|
);
|
||||||
int version = getVersion();
|
|
||||||
if (version < 6) {
|
|
||||||
alterTablesV6();
|
|
||||||
}
|
|
||||||
if (version < 7) {
|
|
||||||
alterTablesV7();
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
Log.toLog(this.getClass().getName(), ex);
|
Log.toLog(this.getClass().getName(), ex);
|
||||||
@ -76,18 +69,6 @@ public class TPSTable extends Table {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void alterTablesV6() {
|
|
||||||
addColumns(columnCPUUsage + " double NOT NULL DEFAULT 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void alterTablesV7() {
|
|
||||||
addColumns(
|
|
||||||
columnRAMUsage + " bigint NOT NULL DEFAULT 0",
|
|
||||||
columnEntities + " integer NOT NULL DEFAULT 0",
|
|
||||||
columnChunksLoaded + " integer NOT NULL DEFAULT 0"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return @throws SQLException
|
* @return @throws SQLException
|
||||||
*/
|
*/
|
||||||
|
@ -5,6 +5,7 @@ import main.java.com.djrapitops.plan.database.databases.SQLDB;
|
|||||||
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a Table that uses UsersTable IDs to get their data.
|
* Represents a Table that uses UsersTable IDs to get their data.
|
||||||
@ -22,6 +23,7 @@ public abstract class UserIDTable extends Table {
|
|||||||
usersTable = db.getUsersTable();
|
usersTable = db.getUsersTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
protected boolean removeDataOf(int userID) {
|
protected boolean removeDataOf(int userID) {
|
||||||
PreparedStatement statement = null;
|
PreparedStatement statement = null;
|
||||||
try {
|
try {
|
||||||
@ -36,4 +38,20 @@ public abstract class UserIDTable extends Table {
|
|||||||
close(statement);
|
close(statement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean removeDataOf(UUID uuid) {
|
||||||
|
PreparedStatement statement = null;
|
||||||
|
try {
|
||||||
|
statement = prepareStatement("DELETE FROM " + tableName +
|
||||||
|
" WHERE (" + columnUserID + "=" + usersTable.statementSelectID + ")");
|
||||||
|
statement.setString(1, uuid.toString());
|
||||||
|
statement.execute();
|
||||||
|
return true;
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
Log.toLog(this.getClass().getName(), ex);
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
close(statement);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user