Fixed /plan search resulting every player #414

This commit is contained in:
Rsl1122 2017-11-13 18:54:34 +02:00
parent 7857732b5d
commit f65fd5ec8c
3 changed files with 25 additions and 9 deletions

View File

@ -223,4 +223,8 @@ public class NicknamesTable extends UserIDTable {
public String getColumnNick() {
return columnNick;
}
public String getColumnUserID() {
return columnUserID;
}
}

View File

@ -260,14 +260,13 @@ public class UsersTable extends UserIDTable {
* @throws SQLException when an error at fetching the names happens.
*/
public List<String> 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<List<String>>(sql, 5000) {
@Override
@ -280,7 +279,10 @@ public class UsersTable extends UserIDTable {
public List<String> processResults(ResultSet set) throws SQLException {
List<String> 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;
}

View File

@ -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";