mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-10-31 07:50:09 +01:00
Refactored UsersTable#getMatchingNames to a query:
- Removed SearchOperations
This commit is contained in:
parent
de7a1b3286
commit
4c235b95e2
@ -17,6 +17,7 @@
|
||||
package com.djrapitops.plan.command.commands;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBOpException;
|
||||
import com.djrapitops.plan.db.access.queries.objects.UserIdentifierQueries;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
import com.djrapitops.plan.system.locale.lang.CmdHelpLang;
|
||||
@ -82,12 +83,12 @@ public class SearchCommand extends CommandNode {
|
||||
private void runSearchTask(String[] args, Sender sender) {
|
||||
processing.submitNonCritical(() -> {
|
||||
try {
|
||||
String searchTerm = args[0];
|
||||
List<String> names = dbSystem.getDatabase().search().matchingPlayers(searchTerm);
|
||||
String searchFor = args[0];
|
||||
List<String> names = dbSystem.getDatabase().query(UserIdentifierQueries.fetchMatchingPlayerNames(searchFor));
|
||||
Collections.sort(names);
|
||||
boolean empty = Verify.isEmpty(names);
|
||||
|
||||
sender.sendMessage(locale.getString(CommandLang.HEADER_SEARCH, empty ? 0 : names.size(), searchTerm));
|
||||
sender.sendMessage(locale.getString(CommandLang.HEADER_SEARCH, empty ? 0 : names.size(), searchFor));
|
||||
// Results
|
||||
if (!empty) {
|
||||
String message = names.toString();
|
||||
|
@ -22,7 +22,6 @@ import com.djrapitops.plan.db.access.Query;
|
||||
import com.djrapitops.plan.db.access.transactions.Transaction;
|
||||
import com.djrapitops.plan.system.database.databases.operation.FetchOperations;
|
||||
import com.djrapitops.plan.system.database.databases.operation.SaveOperations;
|
||||
import com.djrapitops.plan.system.database.databases.operation.SearchOperations;
|
||||
|
||||
/**
|
||||
* Interface for interacting with a Plan SQL database.
|
||||
@ -58,9 +57,6 @@ public interface Database {
|
||||
@Deprecated
|
||||
FetchOperations fetch();
|
||||
|
||||
@Deprecated
|
||||
SearchOperations search();
|
||||
|
||||
@Deprecated
|
||||
SaveOperations save();
|
||||
|
||||
|
@ -33,10 +33,8 @@ import com.djrapitops.plan.db.sql.tables.UsersTable;
|
||||
import com.djrapitops.plan.db.tasks.PatchTask;
|
||||
import com.djrapitops.plan.system.database.databases.operation.FetchOperations;
|
||||
import com.djrapitops.plan.system.database.databases.operation.SaveOperations;
|
||||
import com.djrapitops.plan.system.database.databases.operation.SearchOperations;
|
||||
import com.djrapitops.plan.system.database.databases.sql.operation.SQLFetchOps;
|
||||
import com.djrapitops.plan.system.database.databases.sql.operation.SQLSaveOps;
|
||||
import com.djrapitops.plan.system.database.databases.sql.operation.SQLSearchOps;
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.system.settings.paths.PluginSettings;
|
||||
@ -81,7 +79,6 @@ public abstract class SQLDB extends AbstractDatabase {
|
||||
private final TPSTable tpsTable;
|
||||
|
||||
private final SQLFetchOps fetchOps;
|
||||
private final SQLSearchOps searchOps;
|
||||
private final SQLSaveOps saveOps;
|
||||
|
||||
private PluginTask dbCleanTask;
|
||||
@ -110,7 +107,6 @@ public abstract class SQLDB extends AbstractDatabase {
|
||||
userInfoTable = new UserInfoTable(this);
|
||||
|
||||
fetchOps = new SQLFetchOps(this);
|
||||
searchOps = new SQLSearchOps(this);
|
||||
saveOps = new SQLSaveOps(this);
|
||||
}
|
||||
|
||||
@ -352,12 +348,6 @@ public abstract class SQLDB extends AbstractDatabase {
|
||||
return fetchOps;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public SearchOperations search() {
|
||||
return searchOps;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public SaveOperations save() {
|
||||
|
@ -20,6 +20,7 @@ import com.djrapitops.plan.db.access.Query;
|
||||
import com.djrapitops.plan.db.access.QueryAllStatement;
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.sql.parsing.Select;
|
||||
import com.djrapitops.plan.db.sql.tables.NicknamesTable;
|
||||
import com.djrapitops.plan.db.sql.tables.UserInfoTable;
|
||||
import com.djrapitops.plan.db.sql.tables.UsersTable;
|
||||
|
||||
@ -28,6 +29,8 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
|
||||
|
||||
/**
|
||||
* Queries for fetching different user identifiers in the database.
|
||||
*
|
||||
@ -67,12 +70,12 @@ public class UserIdentifierQueries {
|
||||
* @return Set of UUIDs.
|
||||
*/
|
||||
public static Query<Set<UUID>> fetchPlayerUUIDsOfServer(UUID serverUUID) {
|
||||
String sql = "SELECT " +
|
||||
String sql = SELECT +
|
||||
UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + ", " +
|
||||
" FROM " + UsersTable.TABLE_NAME +
|
||||
" INNER JOIN " + UserInfoTable.TABLE_NAME + " on " +
|
||||
FROM + UsersTable.TABLE_NAME +
|
||||
INNER_JOIN + UserInfoTable.TABLE_NAME + " on " +
|
||||
UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + "=" + UserInfoTable.TABLE_NAME + "." + UserInfoTable.USER_UUID +
|
||||
" WHERE " + UserInfoTable.SERVER_UUID + "=?";
|
||||
WHERE + UserInfoTable.SERVER_UUID + "=?";
|
||||
return new QueryStatement<Set<UUID>>(sql, 1000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
@ -166,4 +169,36 @@ public class UserIdentifierQueries {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Query<List<String>> fetchMatchingPlayerNames(String searchFor) {
|
||||
String sql = SELECT + DISTINCT + UsersTable.USER_NAME +
|
||||
FROM + UsersTable.TABLE_NAME +
|
||||
WHERE + "LOWER(" + UsersTable.USER_NAME + ") LIKE LOWER(%?%)" +
|
||||
" UNION " +
|
||||
SELECT + DISTINCT + UsersTable.USER_NAME +
|
||||
FROM + UsersTable.TABLE_NAME +
|
||||
INNER_JOIN + NicknamesTable.TABLE_NAME + " on " +
|
||||
UsersTable.TABLE_NAME + "." + UsersTable.USER_UUID + "=" + NicknamesTable.TABLE_NAME + "." + NicknamesTable.USER_UUID +
|
||||
WHERE + "LOWER(" + NicknamesTable.NICKNAME + ") LIKE LOWER(%?%)";
|
||||
|
||||
return new QueryStatement<List<String>>(sql, 5000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, searchFor);
|
||||
statement.setString(2, searchFor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> processResults(ResultSet set) throws SQLException {
|
||||
List<String> matchingNames = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
String match = set.getString(UsersTable.USER_NAME);
|
||||
if (!matchingNames.contains(match)) {
|
||||
matchingNames.add(match);
|
||||
}
|
||||
}
|
||||
return matchingNames;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -23,9 +23,11 @@ public class Sql {
|
||||
public static final String BOOL = "boolean";
|
||||
|
||||
public static final String SELECT = "SELECT ";
|
||||
public static final String DISTINCT = "DISTINCT ";
|
||||
public static final String FROM = " FROM ";
|
||||
public static final String WHERE = " WHERE ";
|
||||
public static final String GROUP_BY = " GROUP BY ";
|
||||
public static final String INNER_JOIN = " INNER JOIN ";
|
||||
public static final String AND = " AND ";
|
||||
|
||||
private Sql() {
|
||||
|
@ -19,16 +19,12 @@ package com.djrapitops.plan.db.sql.tables;
|
||||
import com.djrapitops.plan.db.DBType;
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.access.ExecStatement;
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
|
||||
import com.djrapitops.plan.db.sql.parsing.Insert;
|
||||
import com.djrapitops.plan.db.sql.parsing.Sql;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@ -76,39 +72,4 @@ public class UsersTable extends Table {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the names of the players which names or nicknames match {@code name}.
|
||||
*
|
||||
* @param name the name / nickname.
|
||||
* @return a list of distinct names.
|
||||
*/
|
||||
public List<String> getMatchingNames(String name) {
|
||||
String searchString = "%" + name + "%";
|
||||
String sql = "SELECT DISTINCT " + USER_NAME + " FROM " + tableName +
|
||||
" WHERE LOWER(" + USER_NAME + ") LIKE LOWER(?)" +
|
||||
" UNION SELECT DISTINCT " + USER_NAME + " FROM " + tableName +
|
||||
" INNER JOIN " + NicknamesTable.TABLE_NAME + " on " + tableName + "." + USER_UUID + "=" + NicknamesTable.TABLE_NAME + "." + NicknamesTable.USER_UUID +
|
||||
" WHERE LOWER(" + NicknamesTable.NICKNAME + ") LIKE LOWER(?)";
|
||||
|
||||
return query(new QueryStatement<List<String>>(sql, 5000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, searchString);
|
||||
statement.setString(2, searchString);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> processResults(ResultSet set) throws SQLException {
|
||||
List<String> matchingNames = new ArrayList<>();
|
||||
while (set.next()) {
|
||||
String match = set.getString("name");
|
||||
if (!matchingNames.contains(match)) {
|
||||
matchingNames.add(match);
|
||||
}
|
||||
}
|
||||
return matchingNames;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +0,0 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.system.database.databases.operation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Deprecated
|
||||
public interface SearchOperations {
|
||||
@Deprecated
|
||||
List<String> matchingPlayers(String search);
|
||||
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.system.database.databases.sql.operation;
|
||||
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.operation.SearchOperations;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class SQLSearchOps extends SQLOps implements SearchOperations {
|
||||
|
||||
public SQLSearchOps(SQLDB db) {
|
||||
super(db);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> matchingPlayers(String search) {
|
||||
List<String> matchingNames = usersTable.getMatchingNames(search);
|
||||
Collections.sort(matchingNames);
|
||||
return matchingNames;
|
||||
}
|
||||
}
|
@ -943,9 +943,9 @@ public abstract class CommonDBTest {
|
||||
db.executeTransaction(new PlayerRegisterTransaction(uuid1, () -> 0L, exp1));
|
||||
db.executeTransaction(new PlayerRegisterTransaction(UUID.randomUUID(), () -> 0L, exp2));
|
||||
|
||||
String search = "testname";
|
||||
String searchFor = "testname";
|
||||
|
||||
List<String> result = db.search().matchingPlayers(search);
|
||||
List<String> result = db.query(UserIdentifierQueries.fetchMatchingPlayerNames(searchFor));
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(2, result.size());
|
||||
@ -965,9 +965,9 @@ public abstract class CommonDBTest {
|
||||
db.executeTransaction(new NicknameStoreTransaction(uuid, new Nickname(nickname, System.currentTimeMillis(), serverUUID)));
|
||||
db.executeTransaction(new NicknameStoreTransaction(playerUUID, new Nickname("No nick", System.currentTimeMillis(), serverUUID)));
|
||||
|
||||
String search = "2";
|
||||
String searchFor = "2";
|
||||
|
||||
List<String> result = db.search().matchingPlayers(search);
|
||||
List<String> result = db.query(UserIdentifierQueries.fetchMatchingPlayerNames(searchFor));
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.size());
|
||||
|
Loading…
Reference in New Issue
Block a user