From a8a871d931acc805a8cd62cf033e902c4339acf2 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Thu, 24 Jan 2019 15:00:40 +0200 Subject: [PATCH] Removed NicknamesTable.Col --- .../db/patches/NicknameLastSeenPatch.java | 16 ++-- .../patches/NicknamesOptimizationPatch.java | 17 ++-- .../plan/db/sql/tables/NicknamesTable.java | 94 ++++++------------- .../plan/db/sql/tables/UsersTable.java | 4 +- 4 files changed, 45 insertions(+), 86 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/NicknameLastSeenPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/NicknameLastSeenPatch.java index d362a3c54..90a4d9a30 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/NicknameLastSeenPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/NicknameLastSeenPatch.java @@ -37,16 +37,16 @@ public class NicknameLastSeenPatch extends Patch { @Override public boolean hasBeenApplied() { - return hasColumn(NicknamesTable.TABLE_NAME, NicknamesTable.Col.LAST_USED.get()); + return hasColumn(NicknamesTable.TABLE_NAME, NicknamesTable.LAST_USED); } @Override protected void applyPatch() { addColumn(NicknamesTable.TABLE_NAME, - NicknamesTable.Col.LAST_USED + " bigint NOT NULL DEFAULT '0'" + NicknamesTable.LAST_USED + " bigint NOT NULL DEFAULT '0'" ); - if (hasColumn(NicknamesTable.TABLE_NAME, NicknamesTable.Col.UUID.get())) { + if (hasColumn(NicknamesTable.TABLE_NAME, NicknamesTable.USER_UUID)) { // NicknamesOptimizationPatch makes this patch incompatible with newer patch versions. return; } @@ -69,7 +69,7 @@ public class NicknameLastSeenPatch extends Patch { private Map getServerUUIDsByID() { String sql = Select.from(ServerTable.TABLE_NAME, - ServerTable.Col.SERVER_ID, ServerTable.Col.SERVER_UUID) + ServerTable.SERVER_ID, ServerTable.SERVER_UUID) .toString(); return query(new QueryAllStatement>(sql) { @@ -77,8 +77,8 @@ public class NicknameLastSeenPatch extends Patch { public Map processResults(ResultSet set) throws SQLException { Map uuids = new HashMap<>(); while (set.next()) { - int id = set.getInt(ServerTable.Col.SERVER_ID.get()); - uuids.put(id, UUID.fromString(set.getString(ServerTable.Col.SERVER_UUID.get()))); + int id = set.getInt(ServerTable.SERVER_ID); + uuids.put(id, UUID.fromString(set.getString(ServerTable.SERVER_UUID))); } return uuids; } @@ -112,8 +112,8 @@ public class NicknameLastSeenPatch extends Patch { } private void updateLastUsed(Map serverIDsByUUID, Map> nicknames) { - String updateSQL = "UPDATE " + NicknamesTable.TABLE_NAME + " SET " + NicknamesTable.Col.LAST_USED + "=?" + - " WHERE " + NicknamesTable.Col.NICKNAME + "=?" + + String updateSQL = "UPDATE " + NicknamesTable.TABLE_NAME + " SET " + NicknamesTable.LAST_USED + "=?" + + " WHERE " + NicknamesTable.NICKNAME + "=?" + " AND user_id=?" + " AND server_id=?"; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/NicknamesOptimizationPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/NicknamesOptimizationPatch.java index fea9d6a47..882f12786 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/patches/NicknamesOptimizationPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/patches/NicknamesOptimizationPatch.java @@ -19,7 +19,6 @@ package com.djrapitops.plan.db.patches; import com.djrapitops.plan.api.exceptions.database.DBOpException; import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.sql.tables.NicknamesTable; -import com.djrapitops.plan.db.sql.tables.NicknamesTable.Col; public class NicknamesOptimizationPatch extends Patch { @@ -34,8 +33,8 @@ public class NicknamesOptimizationPatch extends Patch { @Override public boolean hasBeenApplied() { - return hasColumn(tableName, Col.UUID.get()) - && hasColumn(tableName, Col.SERVER_UUID.get()) + return hasColumn(tableName, NicknamesTable.USER_UUID) + && hasColumn(tableName, NicknamesTable.SERVER_UUID) && !hasColumn(tableName, "user_id") && !hasColumn(tableName, "server_id") && !hasTable(tempTableName); // If this table exists the patch has failed to finish. @@ -48,15 +47,15 @@ public class NicknamesOptimizationPatch extends Patch { db.getNicknamesTable().createTable(); execute("INSERT INTO " + tableName + " (" + - Col.UUID + ", " + - Col.SERVER_UUID + ", " + - Col.NICKNAME + ", " + - Col.LAST_USED + + NicknamesTable.USER_UUID + ", " + + NicknamesTable.SERVER_UUID + ", " + + NicknamesTable.NICKNAME + ", " + + NicknamesTable.LAST_USED + ") SELECT " + "(SELECT plan_users.uuid FROM plan_users WHERE plan_users.id = " + tempTableName + ".user_id LIMIT 1), " + "(SELECT plan_servers.uuid FROM plan_servers WHERE plan_servers.id = " + tempTableName + ".server_id LIMIT 1), " + - Col.NICKNAME + ", " + - Col.LAST_USED + + NicknamesTable.NICKNAME + ", " + + NicknamesTable.LAST_USED + " FROM " + tempTableName ); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java index 9617235b6..266429da6 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/NicknamesTable.java @@ -26,10 +26,8 @@ import com.djrapitops.plan.db.access.QueryStatement; import com.djrapitops.plan.db.patches.NicknameLastSeenPatch; import com.djrapitops.plan.db.patches.NicknamesOptimizationPatch; import com.djrapitops.plan.db.patches.Version10Patch; -import com.djrapitops.plan.db.sql.parsing.Column; import com.djrapitops.plan.db.sql.parsing.CreateTableParser; import com.djrapitops.plan.db.sql.parsing.Sql; -import com.djrapitops.plan.db.sql.parsing.TableSqlParser; import com.djrapitops.plugin.utilities.Verify; import java.sql.PreparedStatement; @@ -65,15 +63,15 @@ public class NicknamesTable extends UserUUIDTable { public NicknamesTable(SQLDB db) { super(TABLE_NAME, db); insertStatement = "INSERT INTO " + tableName + " (" + - Col.UUID + ", " + - Col.SERVER_UUID + ", " + - Col.NICKNAME + ", " + - Col.LAST_USED + + USER_UUID + ", " + + SERVER_UUID + ", " + + NICKNAME + ", " + + LAST_USED + ") VALUES (?, ?, ?, ?)"; - updateStatement = "UPDATE " + tableName + " SET " + Col.LAST_USED + "=?" + - " WHERE " + Col.NICKNAME + "=?" + - " AND " + Col.UUID + "=?" + - " AND " + Col.SERVER_UUID + "=?"; + updateStatement = "UPDATE " + tableName + " SET " + LAST_USED + "=?" + + " WHERE " + NICKNAME + "=?" + + " AND " + USER_UUID + "=?" + + " AND " + SERVER_UUID + "=?"; } public static String createTableSQL(DBType dbType) { @@ -88,15 +86,7 @@ public class NicknamesTable extends UserUUIDTable { @Override public void createTable() throws DBInitException { - createTable(TableSqlParser.createTable(tableName) - .primaryKeyIDColumn(supportsMySQLQueries, Col.ID) - .column(Col.UUID, Sql.varchar(36)).notNull() - .column(Col.NICKNAME, Sql.varchar(75)).notNull() - .column(Col.SERVER_UUID, Sql.varchar(36)).notNull() - .column(Col.LAST_USED, Sql.LONG).notNull() - .primaryKey(supportsMySQLQueries, Col.ID) - .toString() - ); + createTable(createTableSQL(db.getType())); } /** @@ -107,9 +97,9 @@ public class NicknamesTable extends UserUUIDTable { * @return The nicknames of the User */ public List getNicknames(UUID uuid, UUID serverUUID) { - String sql = "SELECT " + Col.NICKNAME + " FROM " + tableName + - " WHERE (" + Col.UUID + "=?)" + - " AND " + Col.SERVER_UUID + "=?"; + String sql = "SELECT " + NICKNAME + " FROM " + tableName + + " WHERE (" + USER_UUID + "=?)" + + " AND " + SERVER_UUID + "=?"; return query(new QueryStatement>(sql, 1000) { @Override @@ -122,7 +112,7 @@ public class NicknamesTable extends UserUUIDTable { public List processResults(ResultSet set) throws SQLException { List nicknames = new ArrayList<>(); while (set.next()) { - String nickname = set.getString(Col.NICKNAME.get()); + String nickname = set.getString(NICKNAME); if (nickname.isEmpty()) { continue; } @@ -147,10 +137,10 @@ public class NicknamesTable extends UserUUIDTable { public Map>> getAllNicknames() { String sql = "SELECT " + - Col.NICKNAME + ", " + - Col.LAST_USED + ", " + - Col.UUID + ", " + - Col.SERVER_UUID + + NICKNAME + ", " + + LAST_USED + ", " + + USER_UUID + ", " + + SERVER_UUID + " FROM " + tableName; return query(new QueryAllStatement>>>(sql, 5000) { @@ -158,14 +148,14 @@ public class NicknamesTable extends UserUUIDTable { public Map>> processResults(ResultSet set) throws SQLException { Map>> map = new HashMap<>(); while (set.next()) { - UUID serverUUID = UUID.fromString(set.getString(Col.SERVER_UUID.get())); - UUID uuid = UUID.fromString(set.getString(Col.UUID.get())); + UUID serverUUID = UUID.fromString(set.getString(SERVER_UUID)); + UUID uuid = UUID.fromString(set.getString(USER_UUID)); Map> serverMap = map.getOrDefault(serverUUID, new HashMap<>()); List nicknames = serverMap.getOrDefault(uuid, new ArrayList<>()); nicknames.add(new Nickname( - set.getString(Col.NICKNAME.get()), set.getLong(Col.LAST_USED.get()), serverUUID + set.getString(NICKNAME), set.getLong(LAST_USED), serverUUID )); serverMap.put(uuid, nicknames); @@ -211,11 +201,11 @@ public class NicknamesTable extends UserUUIDTable { public List getNicknameInformation(UUID uuid) { String sql = "SELECT " + - Col.NICKNAME + ", " + - Col.LAST_USED + ", " + - Col.SERVER_UUID + + NICKNAME + ", " + + LAST_USED + ", " + + SERVER_UUID + " FROM " + tableName + - " WHERE (" + Col.UUID + "=?)"; + " WHERE (" + USER_UUID + "=?)"; return query(new QueryStatement>(sql, 5000) { @@ -228,9 +218,9 @@ public class NicknamesTable extends UserUUIDTable { public List processResults(ResultSet set) throws SQLException { List nicknames = new ArrayList<>(); while (set.next()) { - UUID serverUUID = UUID.fromString(set.getString(Col.SERVER_UUID.get())); - String nickname = set.getString(Col.NICKNAME.get()); - nicknames.add(new Nickname(nickname, set.getLong(Col.LAST_USED.get()), serverUUID)); + UUID serverUUID = UUID.fromString(set.getString(SERVER_UUID)); + String nickname = set.getString(NICKNAME); + nicknames.add(new Nickname(nickname, set.getLong(LAST_USED), serverUUID)); } return nicknames; } @@ -264,34 +254,4 @@ public class NicknamesTable extends UserUUIDTable { } }); } - - @Deprecated - public enum Col implements Column { - @Deprecated - ID("id"), - @Deprecated - UUID(UserUUIDTable.Col.UUID.get()), - @Deprecated - SERVER_UUID("server_uuid"), - @Deprecated - NICKNAME("nickname"), - @Deprecated - LAST_USED("last_used"); - - private final String column; - - Col(String column) { - this.column = column; - } - - @Override - public String get() { - return toString(); - } - - @Override - public String toString() { - return column; - } - } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/UsersTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/UsersTable.java index 5ae717505..6d5b00ea0 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/UsersTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/UsersTable.java @@ -268,8 +268,8 @@ public class UsersTable extends UserUUIDTable { String sql = "SELECT DISTINCT " + Col.USER_NAME + " FROM " + tableName + " WHERE LOWER(" + Col.USER_NAME + ") LIKE LOWER(?)" + " UNION SELECT DISTINCT " + Col.USER_NAME + " FROM " + tableName + - " INNER JOIN " + nicknamesTable + " on " + tableName + "." + Col.UUID + "=" + nicknamesTable + "." + NicknamesTable.Col.UUID + - " WHERE LOWER(" + NicknamesTable.Col.NICKNAME + ") LIKE LOWER(?)"; + " INNER JOIN " + nicknamesTable + " on " + tableName + "." + USER_UUID + "=" + nicknamesTable + "." + NicknamesTable.USER_UUID + + " WHERE LOWER(" + NicknamesTable.NICKNAME + ") LIKE LOWER(?)"; return query(new QueryStatement>(sql, 5000) { @Override