mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2025-01-29 19:51:40 +01:00
Recreate individual sql tables if they are missing (#3995)
This commit is contained in:
parent
8d65693d9a
commit
a92e04f605
@ -86,6 +86,6 @@ public class SqlMessenger extends AbstractSqlMessenger {
|
||||
|
||||
@Override
|
||||
protected String getTableName() {
|
||||
return this.sqlStorage.getStatementProcessor().apply("{prefix}messenger");
|
||||
return this.sqlStorage.getStatementProcessor().process("{prefix}messenger");
|
||||
}
|
||||
}
|
||||
|
@ -32,10 +32,24 @@ import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class SchemaReader {
|
||||
private SchemaReader() {}
|
||||
|
||||
private static final Pattern CREATE_TABLE_PATTERN = Pattern.compile("^CREATE TABLE [`\"']([^`\"']+)[`\"'].*");
|
||||
private static final Pattern CREATE_INDEX_PATTERN = Pattern.compile("^CREATE INDEX.* ON [`\"']([^`\"']+)[`\"'].*");
|
||||
|
||||
/**
|
||||
* Parses a schema file to a list of SQL statements
|
||||
*
|
||||
* @param is the input stream to read from
|
||||
* @return a list of statements
|
||||
* @throws IOException if an error occurs whilst reading the file
|
||||
*/
|
||||
public static List<String> getStatements(InputStream is) throws IOException {
|
||||
List<String> queries = new LinkedList<>();
|
||||
|
||||
@ -53,7 +67,7 @@ public final class SchemaReader {
|
||||
if (line.endsWith(";")) {
|
||||
sb.deleteCharAt(sb.length() - 1);
|
||||
|
||||
String result = sb.toString().trim();
|
||||
String result = sb.toString().trim().replaceAll(" +", " ");
|
||||
if (!result.isEmpty()) {
|
||||
queries.add(result);
|
||||
}
|
||||
@ -66,4 +80,25 @@ public final class SchemaReader {
|
||||
|
||||
return queries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters which statements should be executed based on the current list of tables in the database
|
||||
*
|
||||
* @param statements the statements to filter
|
||||
* @param currentTables the current tables in the database
|
||||
* @return the filtered list of statements
|
||||
*/
|
||||
public static List<String> filterStatements(List<String> statements, List<String> currentTables) {
|
||||
return statements.stream().filter(statement -> {
|
||||
Matcher table = CREATE_TABLE_PATTERN.matcher(statement);
|
||||
if (table.matches()) {
|
||||
return !currentTables.contains(table.group(1).toLowerCase(Locale.ROOT));
|
||||
}
|
||||
Matcher index = CREATE_INDEX_PATTERN.matcher(statement);
|
||||
if (index.matches()) {
|
||||
return !currentTables.contains(index.group(1).toLowerCase(Locale.ROOT));
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown statement type: " + statement);
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
@ -80,7 +80,6 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SqlStorage implements StorageImplementation {
|
||||
@ -137,7 +136,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
private final LuckPermsPlugin plugin;
|
||||
|
||||
private final ConnectionFactory connectionFactory;
|
||||
private final Function<String, String> statementProcessor;
|
||||
private final StatementProcessor statementProcessor;
|
||||
|
||||
public SqlStorage(LuckPermsPlugin plugin, ConnectionFactory connectionFactory, String tablePrefix) {
|
||||
this.plugin = plugin;
|
||||
@ -159,7 +158,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
return this.connectionFactory;
|
||||
}
|
||||
|
||||
public Function<String, String> getStatementProcessor() {
|
||||
public StatementProcessor getStatementProcessor() {
|
||||
return this.statementProcessor;
|
||||
}
|
||||
|
||||
@ -167,30 +166,32 @@ public class SqlStorage implements StorageImplementation {
|
||||
public void init() throws Exception {
|
||||
this.connectionFactory.init(this.plugin);
|
||||
|
||||
boolean tableExists;
|
||||
List<String> tables;
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
tableExists = tableExists(c, this.statementProcessor.apply("{prefix}user_permissions"));
|
||||
}
|
||||
|
||||
if (!tableExists) {
|
||||
applySchema();
|
||||
tables = listTables(c);
|
||||
}
|
||||
applySchema(tables);
|
||||
}
|
||||
|
||||
private void applySchema() throws IOException, SQLException {
|
||||
List<String> statements;
|
||||
|
||||
private void applySchema(List<String> existingTables) throws IOException, SQLException {
|
||||
String schemaFileName = "me/lucko/luckperms/schema/" + this.connectionFactory.getImplementationName().toLowerCase(Locale.ROOT) + ".sql";
|
||||
|
||||
List<String> statements;
|
||||
try (InputStream is = this.plugin.getBootstrap().getResourceStream(schemaFileName)) {
|
||||
if (is == null) {
|
||||
throw new IOException("Couldn't locate schema file for " + this.connectionFactory.getImplementationName());
|
||||
}
|
||||
|
||||
statements = SchemaReader.getStatements(is).stream()
|
||||
.map(this.statementProcessor)
|
||||
.map(this.statementProcessor::process)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
statements = SchemaReader.filterStatements(statements, existingTables);
|
||||
if (statements.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try (Connection connection = this.connectionFactory.getConnection()) {
|
||||
boolean utf8mb4Unsupported = false;
|
||||
|
||||
@ -240,7 +241,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
@Override
|
||||
public void logAction(Action entry) throws SQLException {
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(ACTION_INSERT))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(ACTION_INSERT))) {
|
||||
writeAction(entry, ps);
|
||||
ps.execute();
|
||||
}
|
||||
@ -288,7 +289,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
if (bulkUpdate.getDataType().isIncludingUsers()) {
|
||||
Function<String, String> tableReplacement = s -> s.replace("{table}", "{prefix}user_permissions");
|
||||
StatementProcessor tableReplacement = s -> s.replace("{table}", "{prefix}user_permissions");
|
||||
|
||||
BulkUpdateSqlBuilder sqlBuilder = new BulkUpdateSqlBuilder();
|
||||
sqlBuilder.visit(bulkUpdate);
|
||||
@ -319,7 +320,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
}
|
||||
|
||||
if (bulkUpdate.getDataType().isIncludingGroups()) {
|
||||
Function<String, String> tableReplacement = s -> s.replace("{table}", "{prefix}group_permissions");
|
||||
StatementProcessor tableReplacement = s -> s.replace("{table}", "{prefix}group_permissions");
|
||||
|
||||
BulkUpdateSqlBuilder sqlBuilder = new BulkUpdateSqlBuilder();
|
||||
sqlBuilder.visit(bulkUpdate);
|
||||
@ -448,7 +449,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
public Set<UUID> getUniqueUsers() throws SQLException {
|
||||
Set<UUID> uuids = new HashSet<>();
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(USER_PERMISSIONS_SELECT_DISTINCT))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(USER_PERMISSIONS_SELECT_DISTINCT))) {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
UUID uuid = Uuids.fromString(rs.getString("uuid"));
|
||||
@ -496,7 +497,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
public Group createAndLoadGroup(String name) throws SQLException {
|
||||
String query = GROUP_INSERT.getOrDefault(this.connectionFactory.getImplementationName(), GROUP_INSERT_DEFAULT);
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(query))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(query))) {
|
||||
ps.setString(1, name);
|
||||
ps.execute();
|
||||
}
|
||||
@ -559,7 +560,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
deleteGroupPermissions(c, group.getName());
|
||||
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(GROUP_DELETE))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(GROUP_DELETE))) {
|
||||
ps.setString(1, group.getName());
|
||||
ps.execute();
|
||||
}
|
||||
@ -662,7 +663,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
@Override
|
||||
public void deleteTrack(Track track) throws SQLException {
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(TRACK_DELETE))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(TRACK_DELETE))) {
|
||||
ps.setString(1, track.getName());
|
||||
ps.execute();
|
||||
}
|
||||
@ -679,7 +680,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
SqlPlayerData existingPlayerData = selectPlayerData(c, uniqueId);
|
||||
if (existingPlayerData == null) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_INSERT))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_INSERT))) {
|
||||
ps.setString(1, uniqueId.toString());
|
||||
ps.setString(2, username);
|
||||
ps.setString(3, GroupManager.DEFAULT_GROUP_NAME);
|
||||
@ -688,7 +689,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
} else {
|
||||
oldUsername = existingPlayerData.username;
|
||||
if (!username.equals(oldUsername)) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_UPDATE_USERNAME_FOR_UUID))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_UPDATE_USERNAME_FOR_UUID))) {
|
||||
ps.setString(1, username);
|
||||
ps.setString(2, uniqueId.toString());
|
||||
ps.execute();
|
||||
@ -701,7 +702,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
Set<UUID> conflicting = new HashSet<>();
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_SELECT_ALL_UUIDS_BY_USERNAME))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_SELECT_ALL_UUIDS_BY_USERNAME))) {
|
||||
ps.setString(1, username);
|
||||
ps.setString(2, uniqueId.toString());
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
@ -715,7 +716,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
if (!conflicting.isEmpty()) {
|
||||
// remove the mappings for conflicting uuids
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_DELETE_ALL_UUIDS_BY_USERNAME))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_DELETE_ALL_UUIDS_BY_USERNAME))) {
|
||||
ps.setString(1, username);
|
||||
ps.setString(2, uniqueId.toString());
|
||||
ps.execute();
|
||||
@ -730,7 +731,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
@Override
|
||||
public void deletePlayerData(UUID uniqueId) throws SQLException {
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_DELETE))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_DELETE))) {
|
||||
ps.setString(1, uniqueId.toString());
|
||||
ps.execute();
|
||||
}
|
||||
@ -741,7 +742,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
public UUID getPlayerUniqueId(String username) throws SQLException {
|
||||
username = username.toLowerCase(Locale.ROOT);
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_SELECT_UUID_BY_USERNAME))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_SELECT_UUID_BY_USERNAME))) {
|
||||
ps.setString(1, username);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
@ -756,7 +757,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
@Override
|
||||
public String getPlayerName(UUID uniqueId) throws SQLException {
|
||||
try (Connection c = this.connectionFactory.getConnection()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_SELECT_USERNAME_BY_UUID))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_SELECT_USERNAME_BY_UUID))) {
|
||||
ps.setString(1, uniqueId.toString());
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
@ -872,7 +873,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
}
|
||||
|
||||
if (!deleteRows.isEmpty()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(deleteSpecificQuery))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(deleteSpecificQuery))) {
|
||||
for (Long id : deleteRows) {
|
||||
ps.setLong(1, id);
|
||||
ps.addBatch();
|
||||
@ -881,7 +882,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
}
|
||||
}
|
||||
if (!deleteNodes.isEmpty()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(deleteQuery))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(deleteQuery))) {
|
||||
for (Node node : deleteNodes) {
|
||||
ps.setString(1, holder);
|
||||
writeNode(node, ps);
|
||||
@ -893,7 +894,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
}
|
||||
|
||||
if (!add.isEmpty()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(insertQuery))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(insertQuery))) {
|
||||
for (Node node : add) {
|
||||
ps.setString(1, holder);
|
||||
writeNode(node, ps);
|
||||
@ -906,7 +907,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
private List<Node> selectUserPermissions(Connection c, UUID user) throws SQLException {
|
||||
List<Node> nodes = new ArrayList<>();
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(USER_PERMISSIONS_SELECT))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(USER_PERMISSIONS_SELECT))) {
|
||||
ps.setString(1, user.toString());
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
@ -921,7 +922,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
}
|
||||
|
||||
private SqlPlayerData selectPlayerData(Connection c, UUID user) throws SQLException {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_SELECT_BY_UUID))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_SELECT_BY_UUID))) {
|
||||
ps.setString(1, user.toString());
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
@ -982,15 +983,15 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
// we don't want to use preparedstatements because the parameter length is variable
|
||||
// safe to do string concat/replacement because the UUID.toString value isn't injectable
|
||||
return this.statementProcessor.apply(baseQuery) + param;
|
||||
return this.statementProcessor.process(baseQuery) + param;
|
||||
}
|
||||
|
||||
private void deleteUser(Connection c, UUID user) throws SQLException {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(USER_PERMISSIONS_DELETE))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(USER_PERMISSIONS_DELETE))) {
|
||||
ps.setString(1, user.toString());
|
||||
ps.execute();
|
||||
}
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_UPDATE_PRIMARY_GROUP_BY_UUID))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_UPDATE_PRIMARY_GROUP_BY_UUID))) {
|
||||
ps.setString(1, GroupManager.DEFAULT_GROUP_NAME);
|
||||
ps.setString(2, user.toString());
|
||||
ps.execute();
|
||||
@ -999,7 +1000,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
private void insertPlayerData(Connection c, UUID user, SqlPlayerData data) throws SQLException {
|
||||
boolean hasPrimaryGroupSaved;
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_SELECT_PRIMARY_GROUP_BY_UUID))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_SELECT_PRIMARY_GROUP_BY_UUID))) {
|
||||
ps.setString(1, user.toString());
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
hasPrimaryGroupSaved = rs.next();
|
||||
@ -1008,14 +1009,14 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
if (hasPrimaryGroupSaved) {
|
||||
// update
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_UPDATE_PRIMARY_GROUP_BY_UUID))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_UPDATE_PRIMARY_GROUP_BY_UUID))) {
|
||||
ps.setString(1, data.primaryGroup);
|
||||
ps.setString(2, user.toString());
|
||||
ps.execute();
|
||||
}
|
||||
} else {
|
||||
// insert
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(PLAYER_INSERT))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(PLAYER_INSERT))) {
|
||||
ps.setString(1, user.toString());
|
||||
ps.setString(2, data.username);
|
||||
ps.setString(3, data.primaryGroup);
|
||||
@ -1026,7 +1027,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
private Set<String> selectGroups(Connection c) throws SQLException {
|
||||
Set<String> groups = new HashSet<>();
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(GROUP_SELECT_ALL))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(GROUP_SELECT_ALL))) {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
groups.add(rs.getString("name").toLowerCase(Locale.ROOT));
|
||||
@ -1038,7 +1039,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
private List<Node> selectGroupPermissions(Connection c, String group) throws SQLException {
|
||||
List<Node> nodes = new ArrayList<>();
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(GROUP_PERMISSIONS_SELECT))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(GROUP_PERMISSIONS_SELECT))) {
|
||||
ps.setString(1, group);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
@ -1053,7 +1054,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
}
|
||||
|
||||
private void selectAllGroupPermissions(Map<String, Collection<Node>> nodes, Connection c) throws SQLException {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(GROUP_PERMISSIONS_SELECT_ALL))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(GROUP_PERMISSIONS_SELECT_ALL))) {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
String holder = rs.getString("name");
|
||||
@ -1070,7 +1071,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
}
|
||||
|
||||
private void deleteGroupPermissions(Connection c, String group) throws SQLException {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(GROUP_PERMISSIONS_DELETE))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(GROUP_PERMISSIONS_DELETE))) {
|
||||
ps.setString(1, group);
|
||||
ps.execute();
|
||||
}
|
||||
@ -1078,7 +1079,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
private List<String> selectTrack(Connection c, String name) throws SQLException {
|
||||
String groups;
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(TRACK_SELECT))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(TRACK_SELECT))) {
|
||||
ps.setString(1, name);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
@ -1093,7 +1094,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
private void insertTrack(Connection c, String name, List<String> groups) throws SQLException {
|
||||
String json = GsonProvider.normal().toJson(groups);
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(TRACK_INSERT))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(TRACK_INSERT))) {
|
||||
ps.setString(1, name);
|
||||
ps.setString(2, json);
|
||||
ps.execute();
|
||||
@ -1102,7 +1103,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
private void updateTrack(Connection c, String name, List<String> groups) throws SQLException {
|
||||
String json = GsonProvider.normal().toJson(groups);
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(TRACK_UPDATE))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(TRACK_UPDATE))) {
|
||||
ps.setString(1, json);
|
||||
ps.setString(2, name);
|
||||
ps.execute();
|
||||
@ -1111,7 +1112,7 @@ public class SqlStorage implements StorageImplementation {
|
||||
|
||||
private Set<String> selectTracks(Connection c) throws SQLException {
|
||||
Set<String> tracks = new HashSet<>();
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(TRACK_SELECT_ALL))) {
|
||||
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.process(TRACK_SELECT_ALL))) {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
tracks.add(rs.getString("name").toLowerCase(Locale.ROOT));
|
||||
@ -1121,15 +1122,14 @@ public class SqlStorage implements StorageImplementation {
|
||||
return tracks;
|
||||
}
|
||||
|
||||
private static boolean tableExists(Connection connection, String table) throws SQLException {
|
||||
private static List<String> listTables(Connection connection) throws SQLException {
|
||||
List<String> tables = new ArrayList<>();
|
||||
try (ResultSet rs = connection.getMetaData().getTables(connection.getCatalog(), null, "%", null)) {
|
||||
while (rs.next()) {
|
||||
if (rs.getString(3).equalsIgnoreCase(table)) {
|
||||
return true;
|
||||
}
|
||||
tables.add(rs.getString(3).toLowerCase(Locale.ROOT));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return tables;
|
||||
}
|
||||
|
||||
private static final class SqlPlayerData {
|
||||
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* This file is part of LuckPerms, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.common.storage.implementation.sql;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public interface StatementProcessor {
|
||||
|
||||
StatementProcessor USE_BACKTICKS = s -> s.replace('\'', '`');
|
||||
|
||||
StatementProcessor USE_DOUBLE_QUOTES = s -> s.replace('\'', '"');
|
||||
|
||||
String process(String statement);
|
||||
|
||||
default StatementProcessor compose(StatementProcessor before) {
|
||||
Objects.requireNonNull(before);
|
||||
return s -> process(before.process(s));
|
||||
}
|
||||
|
||||
}
|
@ -25,12 +25,13 @@
|
||||
|
||||
package me.lucko.luckperms.common.storage.implementation.sql.builder;
|
||||
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.StatementProcessor;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PreparedStatementBuilder {
|
||||
private final StringBuilder sb = new StringBuilder();
|
||||
@ -56,10 +57,10 @@ public class PreparedStatementBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public PreparedStatement build(Connection connection, Function<String, String> mapping) throws SQLException {
|
||||
public PreparedStatement build(Connection connection, StatementProcessor processor) throws SQLException {
|
||||
PreparedStatement statement = null;
|
||||
try {
|
||||
statement = connection.prepareStatement(mapping.apply(this.sb.toString()));
|
||||
statement = connection.prepareStatement(processor.process(this.sb.toString()));
|
||||
for (int i = 0; i < this.variables.size(); i++) {
|
||||
String var = this.variables.get(i);
|
||||
statement.setString(i + 1, var);
|
||||
|
@ -27,10 +27,10 @@ package me.lucko.luckperms.common.storage.implementation.sql.connection;
|
||||
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.common.storage.StorageMetadata;
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.StatementProcessor;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface ConnectionFactory {
|
||||
|
||||
@ -42,7 +42,7 @@ public interface ConnectionFactory {
|
||||
|
||||
StorageMetadata getMeta();
|
||||
|
||||
Function<String, String> getStatementProcessor();
|
||||
StatementProcessor getStatementProcessor();
|
||||
|
||||
Connection getConnection() throws SQLException;
|
||||
|
||||
|
@ -27,6 +27,7 @@ package me.lucko.luckperms.common.storage.implementation.sql.connection.file;
|
||||
|
||||
import me.lucko.luckperms.common.dependencies.Dependency;
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.StatementProcessor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
@ -38,9 +39,14 @@ import java.sql.Statement;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Properties;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class H2ConnectionFactory extends FlatfileConnectionFactory {
|
||||
public static final StatementProcessor STATEMENT_PROCESSOR = s -> s
|
||||
.replace('\'', '`')
|
||||
.replace("LIKE", "ILIKE")
|
||||
.replace("value", "`value`")
|
||||
.replace("``value``", "`value`");
|
||||
|
||||
private Constructor<?> connectionConstructor;
|
||||
|
||||
public H2ConnectionFactory(Path file) {
|
||||
@ -91,11 +97,8 @@ public class H2ConnectionFactory extends FlatfileConnectionFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<String, String> getStatementProcessor() {
|
||||
return s -> s.replace('\'', '`')
|
||||
.replace("LIKE", "ILIKE")
|
||||
.replace("value", "`value`")
|
||||
.replace("``value``", "`value`");
|
||||
public StatementProcessor getStatementProcessor() {
|
||||
return STATEMENT_PROCESSOR;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -27,6 +27,7 @@ package me.lucko.luckperms.common.storage.implementation.sql.connection.file;
|
||||
|
||||
import me.lucko.luckperms.common.dependencies.Dependency;
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.StatementProcessor;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.nio.file.Path;
|
||||
@ -34,7 +35,6 @@ import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Properties;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class SqliteConnectionFactory extends FlatfileConnectionFactory {
|
||||
private Constructor<?> connectionConstructor;
|
||||
@ -74,7 +74,7 @@ public class SqliteConnectionFactory extends FlatfileConnectionFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<String, String> getStatementProcessor() {
|
||||
return s -> s.replace('\'', '`');
|
||||
public StatementProcessor getStatementProcessor() {
|
||||
return StatementProcessor.USE_BACKTICKS;
|
||||
}
|
||||
}
|
||||
|
@ -25,10 +25,9 @@
|
||||
|
||||
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
|
||||
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.StatementProcessor;
|
||||
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class MariaDbConnectionFactory extends DriverBasedHikariConnectionFactory {
|
||||
public MariaDbConnectionFactory(StorageCredentials configuration) {
|
||||
super(configuration);
|
||||
@ -55,7 +54,7 @@ public class MariaDbConnectionFactory extends DriverBasedHikariConnectionFactory
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<String, String> getStatementProcessor() {
|
||||
return s -> s.replace('\'', '`'); // use backticks for quotes
|
||||
public StatementProcessor getStatementProcessor() {
|
||||
return StatementProcessor.USE_BACKTICKS;
|
||||
}
|
||||
}
|
||||
|
@ -25,10 +25,10 @@
|
||||
|
||||
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
|
||||
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.StatementProcessor;
|
||||
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class MySqlConnectionFactory extends DriverBasedHikariConnectionFactory {
|
||||
public MySqlConnectionFactory(StorageCredentials configuration) {
|
||||
@ -80,7 +80,7 @@ public class MySqlConnectionFactory extends DriverBasedHikariConnectionFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<String, String> getStatementProcessor() {
|
||||
return s -> s.replace('\'', '`'); // use backticks for quotes
|
||||
public StatementProcessor getStatementProcessor() {
|
||||
return StatementProcessor.USE_BACKTICKS;
|
||||
}
|
||||
}
|
||||
|
@ -25,10 +25,10 @@
|
||||
|
||||
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
|
||||
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.StatementProcessor;
|
||||
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PostgresConnectionFactory extends DriverBasedHikariConnectionFactory {
|
||||
public PostgresConnectionFactory(StorageCredentials configuration) {
|
||||
@ -65,7 +65,7 @@ public class PostgresConnectionFactory extends DriverBasedHikariConnectionFactor
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<String, String> getStatementProcessor() {
|
||||
return s -> s.replace('\'', '"');
|
||||
public StatementProcessor getStatementProcessor() {
|
||||
return StatementProcessor.USE_DOUBLE_QUOTES;
|
||||
}
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ public abstract class AbstractStorageTest {
|
||||
@Mock protected LuckPermsBootstrap bootstrap;
|
||||
@Mock protected LuckPermsConfiguration configuration;
|
||||
|
||||
private StorageImplementation storage;
|
||||
protected StorageImplementation storage;
|
||||
|
||||
@BeforeEach
|
||||
public final void setupMocksAndStorage() throws Exception {
|
||||
|
@ -25,16 +25,24 @@
|
||||
|
||||
package me.lucko.luckperms.common.storage;
|
||||
|
||||
import me.lucko.luckperms.common.actionlog.LoggedAction;
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.common.storage.implementation.StorageImplementation;
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.SqlStorage;
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.StatementProcessor;
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.connection.ConnectionFactory;
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.connection.file.H2ConnectionFactory;
|
||||
import me.lucko.luckperms.common.storage.implementation.sql.connection.file.NonClosableConnection;
|
||||
import net.luckperms.api.actionlog.Action;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.function.Function;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class SqlStorageTest extends AbstractStorageTest {
|
||||
|
||||
@ -43,6 +51,35 @@ public class SqlStorageTest extends AbstractStorageTest {
|
||||
return new SqlStorage(plugin, new TestH2ConnectionFactory(), "luckperms_");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecreateTables() throws Exception {
|
||||
SqlStorage sql = (SqlStorage) this.storage;
|
||||
|
||||
LoggedAction testAction = LoggedAction.build()
|
||||
.source(UUID.randomUUID())
|
||||
.sourceName("Test")
|
||||
.targetType(Action.Target.Type.TRACK)
|
||||
.targetName("test")
|
||||
.description("test ")
|
||||
.timestamp(Instant.now())
|
||||
.build();
|
||||
|
||||
// perform an action - ensure it works
|
||||
this.storage.logAction(testAction);
|
||||
|
||||
// delete the table
|
||||
try (Connection c = sql.getConnectionFactory().getConnection()) {
|
||||
c.createStatement().execute("DROP TABLE `luckperms_actions`");
|
||||
}
|
||||
|
||||
// perform the action again - expect an exception
|
||||
assertThrows(SQLException.class, () -> this.storage.logAction(testAction));
|
||||
|
||||
// recreate the table & repeat the action, ensure it works
|
||||
this.storage.init();
|
||||
this.storage.logAction(testAction);
|
||||
}
|
||||
|
||||
private static class TestH2ConnectionFactory implements ConnectionFactory {
|
||||
private final NonClosableConnection connection;
|
||||
|
||||
@ -73,11 +110,8 @@ public class SqlStorageTest extends AbstractStorageTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<String, String> getStatementProcessor() {
|
||||
return s -> s.replace('\'', '`')
|
||||
.replace("LIKE", "ILIKE")
|
||||
.replace("value", "`value`")
|
||||
.replace("``value``", "`value`");
|
||||
public StatementProcessor getStatementProcessor() {
|
||||
return H2ConnectionFactory.STATEMENT_PROCESSOR;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,148 @@
|
||||
/*
|
||||
* This file is part of LuckPerms, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.common.storage.implementation.sql;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class SchemaReaderTest {
|
||||
|
||||
private static List<String> readStatements(String type) throws IOException {
|
||||
List<String> statements;
|
||||
try (InputStream is = SchemaReaderTest.class.getResourceAsStream("/me/lucko/luckperms/schema/" + type + ".sql")) {
|
||||
if (is == null) {
|
||||
throw new IOException("Couldn't locate schema file");
|
||||
}
|
||||
|
||||
statements = SchemaReader.getStatements(is);
|
||||
}
|
||||
return statements;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadH2() throws IOException {
|
||||
assertEquals(ImmutableList.of(
|
||||
"CREATE TABLE `{prefix}user_permissions` ( `id` INT AUTO_INCREMENT NOT NULL, `uuid` VARCHAR(36) NOT NULL, `permission` VARCHAR(200) NOT NULL, `value` BOOL NOT NULL, `server` VARCHAR(36) NOT NULL, `world` VARCHAR(64) NOT NULL, `expiry` BIGINT NOT NULL, `contexts` VARCHAR(200) NOT NULL, PRIMARY KEY (`id`))",
|
||||
"CREATE INDEX ON `{prefix}user_permissions` (`uuid`)",
|
||||
"CREATE TABLE `{prefix}group_permissions` ( `id` INT AUTO_INCREMENT NOT NULL, `name` VARCHAR(36) NOT NULL, `permission` VARCHAR(200) NOT NULL, `value` BOOL NOT NULL, `server` VARCHAR(36) NOT NULL, `world` VARCHAR(64) NOT NULL, `expiry` BIGINT NOT NULL, `contexts` VARCHAR(200) NOT NULL, PRIMARY KEY (`id`))",
|
||||
"CREATE INDEX ON `{prefix}group_permissions` (`name`)",
|
||||
"CREATE TABLE `{prefix}players` ( `uuid` VARCHAR(36) NOT NULL, `username` VARCHAR(16) NOT NULL, `primary_group` VARCHAR(36) NOT NULL, PRIMARY KEY (`uuid`))",
|
||||
"CREATE INDEX ON `{prefix}players` (`username`)",
|
||||
"CREATE TABLE `{prefix}groups` ( `name` VARCHAR(36) NOT NULL, PRIMARY KEY (`name`))",
|
||||
"CREATE TABLE `{prefix}actions` ( `id` INT AUTO_INCREMENT NOT NULL, `time` BIGINT NOT NULL, `actor_uuid` VARCHAR(36) NOT NULL, `actor_name` VARCHAR(100) NOT NULL, `type` CHAR(1) NOT NULL, `acted_uuid` VARCHAR(36) NOT NULL, `acted_name` VARCHAR(36) NOT NULL, `action` VARCHAR(300) NOT NULL, PRIMARY KEY (`id`))",
|
||||
"CREATE TABLE `{prefix}tracks` ( `name` VARCHAR(36) NOT NULL, `groups` TEXT NOT NULL, PRIMARY KEY (`name`))"
|
||||
), readStatements("h2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadSqlite() throws IOException {
|
||||
assertEquals(ImmutableList.of(
|
||||
"CREATE TABLE `{prefix}user_permissions` ( `id` INTEGER PRIMARY KEY NOT NULL, `uuid` VARCHAR(36) NOT NULL, `permission` VARCHAR(200) NOT NULL, `value` BOOL NOT NULL, `server` VARCHAR(36) NOT NULL, `world` VARCHAR(64) NOT NULL, `expiry` BIGINT NOT NULL, `contexts` VARCHAR(200) NOT NULL)",
|
||||
"CREATE INDEX `{prefix}user_permissions_uuid` ON `{prefix}user_permissions` (`uuid`)",
|
||||
"CREATE TABLE `{prefix}group_permissions` ( `id` INTEGER PRIMARY KEY NOT NULL, `name` VARCHAR(36) NOT NULL, `permission` VARCHAR(200) NOT NULL, `value` BOOL NOT NULL, `server` VARCHAR(36) NOT NULL, `world` VARCHAR(64) NOT NULL, `expiry` BIGINT NOT NULL, `contexts` VARCHAR(200) NOT NULL)",
|
||||
"CREATE INDEX `{prefix}group_permissions_name` ON `{prefix}group_permissions` (`name`)",
|
||||
"CREATE TABLE `{prefix}players` ( `uuid` VARCHAR(36) NOT NULL, `username` VARCHAR(16) NOT NULL, `primary_group` VARCHAR(36) NOT NULL, PRIMARY KEY (`uuid`))",
|
||||
"CREATE INDEX `{prefix}players_username` ON `{prefix}players` (`username`)",
|
||||
"CREATE TABLE `{prefix}groups` ( `name` VARCHAR(36) NOT NULL, PRIMARY KEY (`name`))",
|
||||
"CREATE TABLE `{prefix}actions` ( `id` INTEGER PRIMARY KEY NOT NULL, `time` BIGINT NOT NULL, `actor_uuid` VARCHAR(36) NOT NULL, `actor_name` VARCHAR(100) NOT NULL, `type` CHAR(1) NOT NULL, `acted_uuid` VARCHAR(36) NOT NULL, `acted_name` VARCHAR(36) NOT NULL, `action` VARCHAR(300) NOT NULL)",
|
||||
"CREATE TABLE `{prefix}tracks` ( `name` VARCHAR(36) NOT NULL, `groups` TEXT NOT NULL, PRIMARY KEY (`name`))"
|
||||
), readStatements("sqlite"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadMysql() throws IOException {
|
||||
ImmutableList<String> expected = ImmutableList.of(
|
||||
"CREATE TABLE `{prefix}user_permissions` ( `id` INT AUTO_INCREMENT NOT NULL, `uuid` VARCHAR(36) NOT NULL, `permission` VARCHAR(200) NOT NULL, `value` BOOL NOT NULL, `server` VARCHAR(36) NOT NULL, `world` VARCHAR(64) NOT NULL, `expiry` BIGINT NOT NULL, `contexts` VARCHAR(200) NOT NULL, PRIMARY KEY (`id`)) DEFAULT CHARSET = utf8mb4",
|
||||
"CREATE INDEX `{prefix}user_permissions_uuid` ON `{prefix}user_permissions` (`uuid`)",
|
||||
"CREATE TABLE `{prefix}group_permissions` ( `id` INT AUTO_INCREMENT NOT NULL, `name` VARCHAR(36) NOT NULL, `permission` VARCHAR(200) NOT NULL, `value` BOOL NOT NULL, `server` VARCHAR(36) NOT NULL, `world` VARCHAR(64) NOT NULL, `expiry` BIGINT NOT NULL, `contexts` VARCHAR(200) NOT NULL, PRIMARY KEY (`id`)) DEFAULT CHARSET = utf8mb4",
|
||||
"CREATE INDEX `{prefix}group_permissions_name` ON `{prefix}group_permissions` (`name`)",
|
||||
"CREATE TABLE `{prefix}players` ( `uuid` VARCHAR(36) NOT NULL, `username` VARCHAR(16) NOT NULL, `primary_group` VARCHAR(36) NOT NULL, PRIMARY KEY (`uuid`)) DEFAULT CHARSET = utf8mb4",
|
||||
"CREATE INDEX `{prefix}players_username` ON `{prefix}players` (`username`)",
|
||||
"CREATE TABLE `{prefix}groups` ( `name` VARCHAR(36) NOT NULL, PRIMARY KEY (`name`)) DEFAULT CHARSET = utf8mb4",
|
||||
"CREATE TABLE `{prefix}actions` ( `id` INT AUTO_INCREMENT NOT NULL, `time` BIGINT NOT NULL, `actor_uuid` VARCHAR(36) NOT NULL, `actor_name` VARCHAR(100) NOT NULL, `type` CHAR(1) NOT NULL, `acted_uuid` VARCHAR(36) NOT NULL, `acted_name` VARCHAR(36) NOT NULL, `action` VARCHAR(300) NOT NULL, PRIMARY KEY (`id`)) DEFAULT CHARSET = utf8mb4",
|
||||
"CREATE TABLE `{prefix}tracks` ( `name` VARCHAR(36) NOT NULL, `groups` TEXT NOT NULL, PRIMARY KEY (`name`)) DEFAULT CHARSET = utf8mb4"
|
||||
);
|
||||
assertEquals(expected, readStatements("mysql"));
|
||||
assertEquals(expected, readStatements("mariadb"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadPostgres() throws IOException {
|
||||
assertEquals(ImmutableList.of(
|
||||
"CREATE TABLE \"{prefix}user_permissions\" ( \"id\" SERIAL PRIMARY KEY NOT NULL, \"uuid\" VARCHAR(36) NOT NULL, \"permission\" VARCHAR(200) NOT NULL, \"value\" BOOL NOT NULL, \"server\" VARCHAR(36) NOT NULL, \"world\" VARCHAR(64) NOT NULL, \"expiry\" BIGINT NOT NULL, \"contexts\" VARCHAR(200) NOT NULL)",
|
||||
"CREATE INDEX \"{prefix}user_permissions_uuid\" ON \"{prefix}user_permissions\" (\"uuid\")",
|
||||
"CREATE TABLE \"{prefix}group_permissions\" ( \"id\" SERIAL PRIMARY KEY NOT NULL, \"name\" VARCHAR(36) NOT NULL, \"permission\" VARCHAR(200) NOT NULL, \"value\" BOOL NOT NULL, \"server\" VARCHAR(36) NOT NULL, \"world\" VARCHAR(64) NOT NULL, \"expiry\" BIGINT NOT NULL, \"contexts\" VARCHAR(200) NOT NULL)",
|
||||
"CREATE INDEX \"{prefix}group_permissions_name\" ON \"{prefix}group_permissions\" (\"name\")",
|
||||
"CREATE TABLE \"{prefix}players\" ( \"uuid\" VARCHAR(36) PRIMARY KEY NOT NULL, \"username\" VARCHAR(16) NOT NULL, \"primary_group\" VARCHAR(36) NOT NULL)",
|
||||
"CREATE INDEX \"{prefix}players_username\" ON \"{prefix}players\" (\"username\")",
|
||||
"CREATE TABLE \"{prefix}groups\" ( \"name\" VARCHAR(36) PRIMARY KEY NOT NULL)",
|
||||
"CREATE TABLE \"{prefix}actions\" ( \"id\" SERIAL PRIMARY KEY NOT NULL, \"time\" BIGINT NOT NULL, \"actor_uuid\" VARCHAR(36) NOT NULL, \"actor_name\" VARCHAR(100) NOT NULL, \"type\" CHAR(1) NOT NULL, \"acted_uuid\" VARCHAR(36) NOT NULL, \"acted_name\" VARCHAR(36) NOT NULL, \"action\" VARCHAR(300) NOT NULL)",
|
||||
"CREATE TABLE \"{prefix}tracks\" ( \"name\" VARCHAR(36) PRIMARY KEY NOT NULL, \"groups\" TEXT NOT NULL)"
|
||||
), readStatements("postgresql"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilter() throws IOException {
|
||||
StatementProcessor processor = s -> s.replace("{prefix}", "luckperms_");
|
||||
List<String> statements = readStatements("mysql").stream().map(processor::process).collect(Collectors.toList());
|
||||
|
||||
// no tables exist, all should be created
|
||||
List<String> filtered = SchemaReader.filterStatements(statements, ImmutableList.of());
|
||||
assertEquals(statements, filtered);
|
||||
|
||||
// all tables exist, none should be created
|
||||
filtered = SchemaReader.filterStatements(statements, ImmutableList.of(
|
||||
"luckperms_user_permissions",
|
||||
"luckperms_group_permissions",
|
||||
"luckperms_players",
|
||||
"luckperms_groups",
|
||||
"luckperms_actions",
|
||||
"luckperms_tracks"
|
||||
));
|
||||
assertEquals(ImmutableList.of(), filtered);
|
||||
|
||||
// some tables exist, some should be created
|
||||
filtered = SchemaReader.filterStatements(statements, ImmutableList.of(
|
||||
"luckperms_user_permissions",
|
||||
"luckperms_players",
|
||||
"luckperms_groups",
|
||||
"luckperms_actions",
|
||||
"luckperms_tracks"
|
||||
));
|
||||
assertEquals(ImmutableList.of(
|
||||
"CREATE TABLE `luckperms_group_permissions` ( `id` INT AUTO_INCREMENT NOT NULL, `name` VARCHAR(36) NOT NULL, `permission` VARCHAR(200) NOT NULL, `value` BOOL NOT NULL, `server` VARCHAR(36) NOT NULL, `world` VARCHAR(64) NOT NULL, `expiry` BIGINT NOT NULL, `contexts` VARCHAR(200) NOT NULL, PRIMARY KEY (`id`)) DEFAULT CHARSET = utf8mb4",
|
||||
"CREATE INDEX `luckperms_group_permissions_name` ON `luckperms_group_permissions` (`name`)"
|
||||
), filtered);
|
||||
}
|
||||
|
||||
}
|
@ -49,7 +49,6 @@ import net.minecraftforge.eventbus.api.EventPriority;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class ForgeConnectionListener extends AbstractConnectionListener {
|
||||
private static final ConfigurationTask.Type USER_LOGIN_TASK_TYPE = new ConfigurationTask.Type("luckperms:user_login");
|
||||
|
@ -32,8 +32,6 @@ import net.minecraftforge.network.config.ConfigurationTaskContext;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class AsyncConfigurationTask implements ConfigurationTask {
|
||||
private final LPForgePlugin plugin;
|
||||
|
@ -1,6 +1,7 @@
|
||||
import net.neoforged.moddevgradle.internal.RunGameTask
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.StandardCopyOption
|
||||
import net.neoforged.moddevgradle.internal.RunGameTask
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.shadow)
|
||||
|
@ -36,11 +36,11 @@ import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.commands.arguments.EntityArgument;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.neoforge.event.RegisterCommandsEvent;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.neoforge.event.RegisterCommandsEvent;
|
||||
|
||||
public class NeoForgeCommandExecutor extends BrigadierCommandExecutor<CommandSourceStack> {
|
||||
|
||||
|
@ -25,7 +25,6 @@
|
||||
|
||||
package me.lucko.luckperms.neoforge.capabilities;
|
||||
|
||||
import java.util.Optional;
|
||||
import me.lucko.luckperms.common.cacheddata.type.PermissionCache;
|
||||
import me.lucko.luckperms.common.context.manager.QueryOptionsCache;
|
||||
import me.lucko.luckperms.common.locale.TranslationManager;
|
||||
@ -40,6 +39,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
|
||||
public class UserCapabilityImpl implements UserCapability {
|
||||
|
||||
|
@ -31,8 +31,8 @@ import me.lucko.luckperms.common.locale.Message;
|
||||
import me.lucko.luckperms.common.locale.TranslationManager;
|
||||
import me.lucko.luckperms.common.model.User;
|
||||
import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener;
|
||||
import me.lucko.luckperms.neoforge.NeoForgeSenderFactory;
|
||||
import me.lucko.luckperms.neoforge.LPNeoForgePlugin;
|
||||
import me.lucko.luckperms.neoforge.NeoForgeSenderFactory;
|
||||
import me.lucko.luckperms.neoforge.capabilities.UserCapabilityImpl;
|
||||
import me.lucko.luckperms.neoforge.util.AsyncConfigurationTask;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@ -42,14 +42,13 @@ import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.network.ConfigurationTask;
|
||||
import net.minecraft.server.network.ServerConfigurationPacketListenerImpl;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import net.neoforged.bus.api.EventPriority;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
||||
import net.neoforged.neoforge.network.event.RegisterConfigurationTasksEvent;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class NeoForgeConnectionListener extends AbstractConnectionListener {
|
||||
private static final ConfigurationTask.Type USER_LOGIN_TASK_TYPE = new ConfigurationTask.Type("luckperms:user_login");
|
||||
|
||||
|
@ -35,14 +35,14 @@ import me.lucko.luckperms.neoforge.util.BrigadierInjector;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.server.players.ServerOpList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.neoforge.event.AddReloadListenerEvent;
|
||||
import net.neoforged.neoforge.event.CommandEvent;
|
||||
import net.neoforged.neoforge.event.server.ServerStartedEvent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
public class NeoForgePlatformListener {
|
||||
private final LPNeoForgePlugin plugin;
|
||||
|
||||
|
@ -37,14 +37,14 @@ import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.players.PlayerList;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.neoforge.network.PacketDistributor;
|
||||
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
|
||||
import net.neoforged.neoforge.network.registration.HandlerThread;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class PluginMessageMessenger extends AbstractPluginMessageMessenger implements Messenger {
|
||||
private static final ResourceLocation CHANNEL_ID = ResourceLocation.parse(AbstractPluginMessageMessenger.CHANNEL);
|
||||
private static final CustomPacketPayload.Type<MessageWrapper> PAYLOAD_TYPE = new CustomPacketPayload.Type<>(CHANNEL_ID);
|
||||
|
@ -39,17 +39,17 @@ import net.luckperms.api.query.QueryOptions;
|
||||
import net.luckperms.api.util.Tristate;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.neoforged.neoforge.server.permission.handler.IPermissionHandler;
|
||||
import net.neoforged.neoforge.server.permission.nodes.PermissionDynamicContext;
|
||||
import net.neoforged.neoforge.server.permission.nodes.PermissionNode;
|
||||
import net.neoforged.neoforge.server.permission.nodes.PermissionType;
|
||||
import net.neoforged.neoforge.server.permission.nodes.PermissionTypes;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.neoforged.neoforge.server.permission.handler.IPermissionHandler;
|
||||
import net.neoforged.neoforge.server.permission.nodes.PermissionDynamicContext;
|
||||
import net.neoforged.neoforge.server.permission.nodes.PermissionNode;
|
||||
import net.neoforged.neoforge.server.permission.nodes.PermissionType;
|
||||
import net.neoforged.neoforge.server.permission.nodes.PermissionTypes;
|
||||
|
||||
public class NeoForgePermissionHandler implements IPermissionHandler {
|
||||
public static final ResourceLocation IDENTIFIER = ResourceLocation.fromNamespaceAndPath(LPNeoForgeBootstrap.ID, "permission_handler");
|
||||
|
@ -32,7 +32,6 @@ import net.minecraft.server.network.ConfigurationTask;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class AsyncConfigurationTask implements ConfigurationTask {
|
||||
private final LPNeoForgePlugin plugin;
|
||||
|
@ -31,6 +31,7 @@ import net.neoforged.bus.api.IEventBus;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.fml.ModLoadingContext;
|
||||
import net.neoforged.fml.event.IModBusEvent;
|
||||
import net.neoforged.neoforge.common.NeoForge;
|
||||
|
||||
import java.lang.invoke.CallSite;
|
||||
import java.lang.invoke.LambdaMetafactory;
|
||||
@ -44,7 +45,6 @@ import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import net.neoforged.neoforge.common.NeoForge;
|
||||
|
||||
/**
|
||||
* A utility for registering Forge listeners for methods in a jar-in-jar.
|
||||
|
Loading…
Reference in New Issue
Block a user