From f65fd5ec8ca307aefdc45cb07f08a6415e24d64a Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 13 Nov 2017 18:54:34 +0200 Subject: [PATCH] Fixed /plan search resulting every player #414 --- .../plan/database/tables/NicknamesTable.java | 4 ++++ .../plan/database/tables/UsersTable.java | 18 ++++++++++-------- .../plan/utilities/MiscUtilsTest.java | 12 +++++++++++- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java index 047ee8512..8719965e7 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java @@ -223,4 +223,8 @@ public class NicknamesTable extends UserIDTable { public String getColumnNick() { return columnNick; } + + public String getColumnUserID() { + return columnUserID; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java index 528ffc627..ba1c2ef8c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java @@ -260,14 +260,13 @@ public class UsersTable extends UserIDTable { * @throws SQLException when an error at fetching the names happens. */ public List getMatchingNames(String name) throws SQLException { - String searchString = "%" + name + "%"; + String searchString = "%" + name.toLowerCase() + "%"; NicknamesTable nicknamesTable = db.getNicknamesTable(); - String sql = "SELECT " + columnName + " FROM " + tableName + - " WHERE " + columnName + " LIKE LOWER(?)" + - " UNION SELECT " + columnName + " FROM " + tableName + - " WHERE " + columnID + " =" + - " (SELECT " + columnID + " FROM " + nicknamesTable + - " WHERE " + nicknamesTable.getColumnNick() + " LIKE LOWER(?))"; + String sql = "SELECT DISTINCT " + columnName + " FROM " + tableName + + " WHERE " + columnName + " LIKE ?" + + " UNION SELECT DISTINCT " + columnName + " FROM " + tableName + + " JOIN " + nicknamesTable + " on " + columnID + "=" + nicknamesTable + "." + nicknamesTable.getColumnUserID() + + " WHERE " + nicknamesTable.getColumnNick() + " LIKE ?"; return query(new QueryStatement>(sql, 5000) { @Override @@ -280,7 +279,10 @@ public class UsersTable extends UserIDTable { public List processResults(ResultSet set) throws SQLException { List matchingNames = new ArrayList<>(); while (set.next()) { - matchingNames.add(set.getString("name")); + String match = set.getString("name"); + if (!matchingNames.contains(match)) { + matchingNames.add(match); + } } return matchingNames; } diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java index 5b77939ad..ab825285b 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java @@ -5,6 +5,7 @@ */ package main.java.com.djrapitops.plan.utilities; +import com.djrapitops.plugin.StaticHolder; import com.djrapitops.plugin.command.ISender; import com.djrapitops.plugin.command.bukkit.BukkitCMDSender; import main.java.com.djrapitops.plan.Plan; @@ -14,6 +15,7 @@ import main.java.com.djrapitops.plan.database.tables.UsersTable; import main.java.com.djrapitops.plan.systems.info.server.ServerInfo; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -38,6 +40,11 @@ public class MiscUtilsTest { private SQLDB db; + @Before + public void setUp() throws Exception { + StaticHolder.saveInstance(MiscUtils.class, Plan.class); + } + @Test public void testGetPlayerDisplayNameArgsPerm() { String[] args = new String[]{"Rsl1122", "Test"}; @@ -114,7 +121,8 @@ public class MiscUtilsTest { String exp2 = "TestName2"; UsersTable usersTable = db.getUsersTable(); - usersTable.registerUser(UUID.randomUUID(), 0L, exp1); + UUID uuid1 = UUID.randomUUID(); + usersTable.registerUser(uuid1, 0L, exp1); usersTable.registerUser(UUID.randomUUID(), 0L, exp2); String search = "testname"; @@ -134,9 +142,11 @@ public class MiscUtilsTest { UUID uuid = UUID.randomUUID(); String userName = RandomData.randomString(10); db.getUsersTable().registerUser(uuid, 0L, userName); + db.getUsersTable().registerUser(MockUtils.getPlayerUUID(), 1L, "Not random"); String nickname = "2" + RandomData.randomString(10); db.getNicknamesTable().saveUserName(uuid, nickname); + db.getNicknamesTable().saveUserName(MockUtils.getPlayerUUID(), "No nick"); String search = "2";