mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-28 05:35:26 +01:00
Refactor storage
This commit is contained in:
parent
5ee6db02ff
commit
6540ffa4dd
@ -56,7 +56,7 @@ public class H2Datastore extends SQLDatastore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
boolean runQuery(QueryPS queryPS) {
|
boolean runQuery(String query, QueryPS queryPS) {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
try {
|
try {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
@ -64,7 +64,7 @@ public class H2Datastore extends SQLDatastore {
|
|||||||
throw new IllegalStateException("SQL connection is null");
|
throw new IllegalStateException("SQL connection is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(queryPS.getQuery());
|
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||||
queryPS.onRun(preparedStatement);
|
queryPS.onRun(preparedStatement);
|
||||||
preparedStatement.execute();
|
preparedStatement.execute();
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ public class H2Datastore extends SQLDatastore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
boolean runQuery(QueryRS queryRS) {
|
boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
try {
|
try {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
@ -84,8 +84,8 @@ public class H2Datastore extends SQLDatastore {
|
|||||||
throw new IllegalStateException("SQL connection is null");
|
throw new IllegalStateException("SQL connection is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(queryRS.getQuery());
|
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||||
queryRS.onRun(preparedStatement);
|
queryPS.onRun(preparedStatement);
|
||||||
|
|
||||||
@Cleanup ResultSet resultSet = preparedStatement.executeQuery();
|
@Cleanup ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
success = queryRS.onResult(resultSet);
|
success = queryRS.onResult(resultSet);
|
||||||
|
@ -37,7 +37,7 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import static me.lucko.luckperms.core.PermissionHolder.exportToLegacy;
|
import static me.lucko.luckperms.core.PermissionHolder.exportToLegacy;
|
||||||
|
|
||||||
@SuppressWarnings({"ResultOfMethodCallIgnored", "UnnecessaryLocalVariable"})
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
public class JSONDatastore extends FlatfileDatastore {
|
public class JSONDatastore extends FlatfileDatastore {
|
||||||
public JSONDatastore(LuckPermsPlugin plugin, File pluginDir) {
|
public JSONDatastore(LuckPermsPlugin plugin, File pluginDir) {
|
||||||
super(plugin, "Flatfile - JSON", pluginDir);
|
super(plugin, "Flatfile - JSON", pluginDir);
|
||||||
@ -148,7 +148,7 @@ public class JSONDatastore extends FlatfileDatastore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean success = doWrite(userFile, writer -> {
|
return doWrite(userFile, writer -> {
|
||||||
writer.beginObject();
|
writer.beginObject();
|
||||||
writer.name("uuid").value(user.getUuid().toString());
|
writer.name("uuid").value(user.getUuid().toString());
|
||||||
writer.name("name").value(user.getName());
|
writer.name("name").value(user.getName());
|
||||||
@ -162,7 +162,6 @@ public class JSONDatastore extends FlatfileDatastore {
|
|||||||
writer.endObject();
|
writer.endObject();
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -323,7 +322,7 @@ public class JSONDatastore extends FlatfileDatastore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean success = doWrite(groupFile, writer -> {
|
return doWrite(groupFile, writer -> {
|
||||||
writer.beginObject();
|
writer.beginObject();
|
||||||
writer.name("name").value(group.getName());
|
writer.name("name").value(group.getName());
|
||||||
writer.name("perms");
|
writer.name("perms");
|
||||||
@ -335,8 +334,6 @@ public class JSONDatastore extends FlatfileDatastore {
|
|||||||
writer.endObject();
|
writer.endObject();
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -451,7 +448,7 @@ public class JSONDatastore extends FlatfileDatastore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean success = doWrite(trackFile, writer -> {
|
return doWrite(trackFile, writer -> {
|
||||||
writer.beginObject();
|
writer.beginObject();
|
||||||
writer.name("name").value(track.getName());
|
writer.name("name").value(track.getName());
|
||||||
writer.name("groups");
|
writer.name("groups");
|
||||||
@ -463,8 +460,6 @@ public class JSONDatastore extends FlatfileDatastore {
|
|||||||
writer.endObject();
|
writer.endObject();
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -169,14 +169,13 @@ public class MongoDBDatastore extends Datastore {
|
|||||||
@Override
|
@Override
|
||||||
public boolean saveUser(User user) {
|
public boolean saveUser(User user) {
|
||||||
if (!plugin.getUserManager().shouldSave(user)) {
|
if (!plugin.getUserManager().shouldSave(user)) {
|
||||||
boolean success = call(() -> {
|
return call(() -> {
|
||||||
MongoCollection<Document> c = database.getCollection("users");
|
MongoCollection<Document> c = database.getCollection("users");
|
||||||
return c.deleteOne(new Document("_id", user.getUuid())).wasAcknowledged();
|
return c.deleteOne(new Document("_id", user.getUuid())).wasAcknowledged();
|
||||||
}, false);
|
}, false);
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean success = call(() -> {
|
return call(() -> {
|
||||||
MongoCollection<Document> c = database.getCollection("users");
|
MongoCollection<Document> c = database.getCollection("users");
|
||||||
try (MongoCursor<Document> cursor = c.find(new Document("_id", user.getUuid())).iterator()) {
|
try (MongoCursor<Document> cursor = c.find(new Document("_id", user.getUuid())).iterator()) {
|
||||||
if (!cursor.hasNext()) {
|
if (!cursor.hasNext()) {
|
||||||
@ -187,7 +186,6 @@ public class MongoDBDatastore extends Datastore {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}, false);
|
}, false);
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -87,7 +87,7 @@ public class MySQLDatastore extends SQLDatastore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
boolean runQuery(QueryPS queryPS) {
|
boolean runQuery(String query, QueryPS queryPS) {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
try {
|
try {
|
||||||
@Cleanup Connection connection = getConnection();
|
@Cleanup Connection connection = getConnection();
|
||||||
@ -95,7 +95,7 @@ public class MySQLDatastore extends SQLDatastore {
|
|||||||
throw new IllegalStateException("SQL connection is null");
|
throw new IllegalStateException("SQL connection is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(queryPS.getQuery());
|
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||||
queryPS.onRun(preparedStatement);
|
queryPS.onRun(preparedStatement);
|
||||||
preparedStatement.execute();
|
preparedStatement.execute();
|
||||||
|
|
||||||
@ -107,7 +107,7 @@ public class MySQLDatastore extends SQLDatastore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
boolean runQuery(QueryRS queryRS) {
|
boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
try {
|
try {
|
||||||
@Cleanup Connection connection = getConnection();
|
@Cleanup Connection connection = getConnection();
|
||||||
@ -115,8 +115,8 @@ public class MySQLDatastore extends SQLDatastore {
|
|||||||
throw new IllegalStateException("SQL connection is null");
|
throw new IllegalStateException("SQL connection is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(queryRS.getQuery());
|
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||||
queryRS.onRun(preparedStatement);
|
queryPS.onRun(preparedStatement);
|
||||||
|
|
||||||
@Cleanup ResultSet resultSet = preparedStatement.executeQuery();
|
@Cleanup ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
success = queryRS.onResult(resultSet);
|
success = queryRS.onResult(resultSet);
|
||||||
|
@ -24,8 +24,6 @@ package me.lucko.luckperms.storage.methods;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import me.lucko.luckperms.LuckPermsPlugin;
|
import me.lucko.luckperms.LuckPermsPlugin;
|
||||||
import me.lucko.luckperms.api.LogEntry;
|
import me.lucko.luckperms.api.LogEntry;
|
||||||
import me.lucko.luckperms.data.Log;
|
import me.lucko.luckperms.data.Log;
|
||||||
@ -45,8 +43,8 @@ import java.util.*;
|
|||||||
|
|
||||||
import static me.lucko.luckperms.core.PermissionHolder.exportToLegacy;
|
import static me.lucko.luckperms.core.PermissionHolder.exportToLegacy;
|
||||||
|
|
||||||
@SuppressWarnings("UnnecessaryLocalVariable")
|
|
||||||
abstract class SQLDatastore extends Datastore {
|
abstract class SQLDatastore extends Datastore {
|
||||||
|
private static final QueryPS EMPTY_PS = preparedStatement -> {};
|
||||||
|
|
||||||
private static final Type NM_TYPE = new TypeToken<Map<String, Boolean>>(){}.getType();
|
private static final Type NM_TYPE = new TypeToken<Map<String, Boolean>>(){}.getType();
|
||||||
private static final Type T_TYPE = new TypeToken<List<String>>(){}.getType();
|
private static final Type T_TYPE = new TypeToken<List<String>>(){}.getType();
|
||||||
@ -87,13 +85,21 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
|
|
||||||
abstract Connection getConnection() throws SQLException;
|
abstract Connection getConnection() throws SQLException;
|
||||||
|
|
||||||
abstract boolean runQuery(QueryPS queryPS);
|
abstract boolean runQuery(String query, QueryPS queryPS);
|
||||||
abstract boolean runQuery(QueryRS queryRS);
|
abstract boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS);
|
||||||
|
|
||||||
|
boolean runQuery(String query) {
|
||||||
|
return runQuery(query, EMPTY_PS);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean runQuery(String query, QueryRS queryRS) {
|
||||||
|
return runQuery(query, EMPTY_PS, queryRS);
|
||||||
|
}
|
||||||
|
|
||||||
boolean setupTables(String... tableQueries) {
|
boolean setupTables(String... tableQueries) {
|
||||||
boolean success = true;
|
boolean success = true;
|
||||||
for (String q : tableQueries) {
|
for (String q : tableQueries) {
|
||||||
if (!runQuery(new Query(q))) success = false;
|
if (!runQuery(q)) success = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return success && cleanupUsers();
|
return success && cleanupUsers();
|
||||||
@ -101,47 +107,35 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean logAction(LogEntry entry) {
|
public boolean logAction(LogEntry entry) {
|
||||||
boolean success = runQuery(new QueryPS(ACTION_INSERT) {
|
return runQuery(ACTION_INSERT, preparedStatement -> {
|
||||||
@Override
|
preparedStatement.setLong(1, entry.getTimestamp());
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
preparedStatement.setString(2, entry.getActor().toString());
|
||||||
preparedStatement.setLong(1, entry.getTimestamp());
|
preparedStatement.setString(3, entry.getActorName());
|
||||||
preparedStatement.setString(2, entry.getActor().toString());
|
preparedStatement.setString(4, Character.toString(entry.getType()));
|
||||||
preparedStatement.setString(3, entry.getActorName());
|
preparedStatement.setString(5, entry.getActed() == null ? "null" : entry.getActed().toString());
|
||||||
preparedStatement.setString(4, Character.toString(entry.getType()));
|
preparedStatement.setString(6, entry.getActedName());
|
||||||
preparedStatement.setString(5, entry.getActed() == null ? "null" : entry.getActed().toString());
|
preparedStatement.setString(7, entry.getAction());
|
||||||
preparedStatement.setString(6, entry.getActedName());
|
|
||||||
preparedStatement.setString(7, entry.getAction());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Log getLog() {
|
public Log getLog() {
|
||||||
final Log.Builder log = Log.builder();
|
final Log.Builder log = Log.builder();
|
||||||
boolean success = runQuery(new QueryRS(ACTION_SELECT_ALL) {
|
boolean success = runQuery(ACTION_SELECT_ALL, resultSet -> {
|
||||||
@Override
|
while (resultSet.next()) {
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
final String actedUuid = resultSet.getString("acted_uuid");
|
||||||
|
LogEntry e = new LogEntry(
|
||||||
}
|
resultSet.getLong("time"),
|
||||||
|
UUID.fromString(resultSet.getString("actor_uuid")),
|
||||||
@Override
|
resultSet.getString("actor_name"),
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
resultSet.getString("type").toCharArray()[0],
|
||||||
while (resultSet.next()) {
|
actedUuid.equals("null") ? null : UUID.fromString(actedUuid),
|
||||||
final String actedUuid = resultSet.getString("acted_uuid");
|
resultSet.getString("acted_name"),
|
||||||
LogEntry e = new LogEntry(
|
resultSet.getString("action")
|
||||||
resultSet.getLong("time"),
|
);
|
||||||
UUID.fromString(resultSet.getString("actor_uuid")),
|
log.add(e);
|
||||||
resultSet.getString("actor_name"),
|
|
||||||
resultSet.getString("type").toCharArray()[0],
|
|
||||||
actedUuid.equals("null") ? null : UUID.fromString(actedUuid),
|
|
||||||
resultSet.getString("acted_name"),
|
|
||||||
resultSet.getString("action")
|
|
||||||
);
|
|
||||||
log.add(e);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
return success ? log.build() : null;
|
return success ? log.build() : null;
|
||||||
}
|
}
|
||||||
@ -149,39 +143,31 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
@Override
|
@Override
|
||||||
public boolean loadUser(UUID uuid, String username) {
|
public boolean loadUser(UUID uuid, String username) {
|
||||||
User user = plugin.getUserManager().make(uuid, username);
|
User user = plugin.getUserManager().make(uuid, username);
|
||||||
boolean success = runQuery(new QueryRS(USER_SELECT) {
|
boolean success = runQuery(USER_SELECT,
|
||||||
@Override
|
preparedStatement -> preparedStatement.setString(1, user.getUuid().toString()),
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
resultSet -> {
|
||||||
preparedStatement.setString(1, user.getUuid().toString());
|
if (resultSet.next()) {
|
||||||
}
|
// User exists, let's load.
|
||||||
|
Map<String, Boolean> nodes = gson.fromJson(resultSet.getString("perms"), NM_TYPE);
|
||||||
|
user.setNodes(nodes);
|
||||||
|
user.setPrimaryGroup(resultSet.getString("primary_group"));
|
||||||
|
|
||||||
@Override
|
if (user.getName().equalsIgnoreCase("null")) {
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
user.setName(resultSet.getString("name"));
|
||||||
if (resultSet.next()) {
|
} else {
|
||||||
// User exists, let's load.
|
if (!resultSet.getString("name").equals(user.getName())) {
|
||||||
Map<String, Boolean> nodes = gson.fromJson(resultSet.getString("perms"), NM_TYPE);
|
runQuery(USER_UPDATE, preparedStatement -> {
|
||||||
user.setNodes(nodes);
|
|
||||||
user.setPrimaryGroup(resultSet.getString("primary_group"));
|
|
||||||
|
|
||||||
if (user.getName().equalsIgnoreCase("null")) {
|
|
||||||
user.setName(resultSet.getString("name"));
|
|
||||||
} else {
|
|
||||||
if (!resultSet.getString("name").equals(user.getName())) {
|
|
||||||
runQuery(new QueryPS(USER_UPDATE) {
|
|
||||||
@Override
|
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, user.getName());
|
preparedStatement.setString(1, user.getName());
|
||||||
preparedStatement.setString(2, user.getPrimaryGroup());
|
preparedStatement.setString(2, user.getPrimaryGroup());
|
||||||
preparedStatement.setString(3, gson.toJson(exportToLegacy(user.getNodes())));
|
preparedStatement.setString(3, gson.toJson(exportToLegacy(user.getNodes())));
|
||||||
preparedStatement.setString(4, user.getUuid().toString());
|
preparedStatement.setString(4, user.getUuid().toString());
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return true;
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (success) plugin.getUserManager().updateOrSet(user);
|
if (success) plugin.getUserManager().updateOrSet(user);
|
||||||
return success;
|
return success;
|
||||||
@ -190,83 +176,53 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
@Override
|
@Override
|
||||||
public boolean saveUser(User user) {
|
public boolean saveUser(User user) {
|
||||||
if (!plugin.getUserManager().shouldSave(user)) {
|
if (!plugin.getUserManager().shouldSave(user)) {
|
||||||
boolean success = runQuery(new QueryPS(USER_DELETE) {
|
return runQuery(USER_DELETE, preparedStatement -> {
|
||||||
@Override
|
preparedStatement.setString(1, user.getUuid().toString());
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, user.getUuid().toString());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean success = runQuery(new QueryRS(USER_SELECT) {
|
return runQuery(USER_SELECT,
|
||||||
@Override
|
preparedStatement -> preparedStatement.setString(1, user.getUuid().toString()),
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
resultSet -> {
|
||||||
preparedStatement.setString(1, user.getUuid().toString());
|
if (!resultSet.next()) {
|
||||||
}
|
// Doesn't already exist, let's insert.
|
||||||
|
return runQuery(USER_INSERT, preparedStatement -> {
|
||||||
@Override
|
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
|
||||||
boolean b;
|
|
||||||
if (!resultSet.next()) {
|
|
||||||
// Doesn't already exist, let's insert.
|
|
||||||
b = runQuery(new QueryPS(USER_INSERT) {
|
|
||||||
@Override
|
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, user.getUuid().toString());
|
preparedStatement.setString(1, user.getUuid().toString());
|
||||||
preparedStatement.setString(2, user.getName());
|
preparedStatement.setString(2, user.getName());
|
||||||
preparedStatement.setString(3, user.getPrimaryGroup());
|
preparedStatement.setString(3, user.getPrimaryGroup());
|
||||||
preparedStatement.setString(4, gson.toJson(exportToLegacy(user.getNodes())));
|
preparedStatement.setString(4, gson.toJson(exportToLegacy(user.getNodes())));
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// User exists, let's update.
|
// User exists, let's update.
|
||||||
b = runQuery(new QueryPS(USER_UPDATE) {
|
return runQuery(USER_UPDATE, preparedStatement -> {
|
||||||
@Override
|
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, user.getName());
|
preparedStatement.setString(1, user.getName());
|
||||||
preparedStatement.setString(2, user.getPrimaryGroup());
|
preparedStatement.setString(2, user.getPrimaryGroup());
|
||||||
preparedStatement.setString(3, gson.toJson(exportToLegacy(user.getNodes())));
|
preparedStatement.setString(3, gson.toJson(exportToLegacy(user.getNodes())));
|
||||||
preparedStatement.setString(4, user.getUuid().toString());
|
preparedStatement.setString(4, user.getUuid().toString());
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
return b;
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean cleanupUsers() {
|
public boolean cleanupUsers() {
|
||||||
boolean success = runQuery(new QueryPS(USER_DELETE_ALL) {
|
return runQuery(USER_DELETE_ALL, preparedStatement -> {
|
||||||
@Override
|
preparedStatement.setString(1, "{\"group.default\":true}");
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, "{\"group.default\":true}");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<UUID> getUniqueUsers() {
|
public Set<UUID> getUniqueUsers() {
|
||||||
Set<UUID> uuids = new HashSet<>();
|
Set<UUID> uuids = new HashSet<>();
|
||||||
|
|
||||||
boolean success = runQuery(new QueryRS(USER_SELECT_ALL) {
|
boolean success = runQuery(USER_SELECT_ALL, resultSet -> {
|
||||||
@Override
|
while (resultSet.next()) {
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
String uuid = resultSet.getString("uuid");
|
||||||
}
|
uuids.add(UUID.fromString(uuid));
|
||||||
|
|
||||||
@Override
|
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
|
||||||
while (resultSet.next()) {
|
|
||||||
String uuid = resultSet.getString("uuid");
|
|
||||||
uuids.add(UUID.fromString(uuid));
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
return success ? uuids : null;
|
return success ? uuids : null;
|
||||||
@ -275,30 +231,21 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
@Override
|
@Override
|
||||||
public boolean createAndLoadGroup(String name) {
|
public boolean createAndLoadGroup(String name) {
|
||||||
Group group = plugin.getGroupManager().make(name);
|
Group group = plugin.getGroupManager().make(name);
|
||||||
boolean success = runQuery(new QueryRS(GROUP_SELECT) {
|
boolean success = runQuery(GROUP_SELECT,
|
||||||
@Override
|
preparedStatement -> preparedStatement.setString(1, group.getName()),
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
resultSet -> {
|
||||||
preparedStatement.setString(1, group.getName());
|
if (!resultSet.next()) {
|
||||||
}
|
return runQuery(GROUP_INSERT, preparedStatement -> {
|
||||||
|
|
||||||
@Override
|
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
|
||||||
boolean success = true;
|
|
||||||
if (!resultSet.next()) {
|
|
||||||
success = runQuery(new QueryPS(GROUP_INSERT) {
|
|
||||||
@Override
|
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, group.getName());
|
preparedStatement.setString(1, group.getName());
|
||||||
preparedStatement.setString(2, gson.toJson(exportToLegacy(group.getNodes())));
|
preparedStatement.setString(2, gson.toJson(exportToLegacy(group.getNodes())));
|
||||||
}
|
});
|
||||||
});
|
} else {
|
||||||
} else {
|
Map<String, Boolean> nodes = gson.fromJson(resultSet.getString("perms"), NM_TYPE);
|
||||||
Map<String, Boolean> nodes = gson.fromJson(resultSet.getString("perms"), NM_TYPE);
|
group.setNodes(nodes);
|
||||||
group.setNodes(nodes);
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return success;
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (success) plugin.getGroupManager().updateOrSet(group);
|
if (success) plugin.getGroupManager().updateOrSet(group);
|
||||||
return success;
|
return success;
|
||||||
@ -307,22 +254,17 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
@Override
|
@Override
|
||||||
public boolean loadGroup(String name) {
|
public boolean loadGroup(String name) {
|
||||||
Group group = plugin.getGroupManager().make(name);
|
Group group = plugin.getGroupManager().make(name);
|
||||||
boolean success = runQuery(new QueryRS(GROUP_SELECT) {
|
boolean success = runQuery(GROUP_SELECT,
|
||||||
@Override
|
preparedStatement -> preparedStatement.setString(1, name),
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
resultSet -> {
|
||||||
preparedStatement.setString(1, name);
|
if (resultSet.next()) {
|
||||||
}
|
Map<String, Boolean> nodes = gson.fromJson(resultSet.getString("perms"), NM_TYPE);
|
||||||
|
group.setNodes(nodes);
|
||||||
@Override
|
return true;
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
}
|
||||||
if (resultSet.next()) {
|
return false;
|
||||||
Map<String, Boolean> nodes = gson.fromJson(resultSet.getString("perms"), NM_TYPE);
|
|
||||||
group.setNodes(nodes);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (success) plugin.getGroupManager().updateOrSet(group);
|
if (success) plugin.getGroupManager().updateOrSet(group);
|
||||||
return success;
|
return success;
|
||||||
@ -331,22 +273,14 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
@Override
|
@Override
|
||||||
public boolean loadAllGroups() {
|
public boolean loadAllGroups() {
|
||||||
List<Group> groups = new ArrayList<>();
|
List<Group> groups = new ArrayList<>();
|
||||||
boolean success = runQuery(new QueryRS(GROUP_SELECT_ALL) {
|
boolean success = runQuery(GROUP_SELECT_ALL, resultSet -> {
|
||||||
@Override
|
while (resultSet.next()) {
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
Group group = plugin.getGroupManager().make(resultSet.getString("name"));
|
||||||
|
Map<String, Boolean> nodes = gson.fromJson(resultSet.getString("perms"), NM_TYPE);
|
||||||
}
|
group.setNodes(nodes);
|
||||||
|
groups.add(group);
|
||||||
@Override
|
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
|
||||||
while (resultSet.next()) {
|
|
||||||
Group group = plugin.getGroupManager().make(resultSet.getString("name"));
|
|
||||||
Map<String, Boolean> nodes = gson.fromJson(resultSet.getString("perms"), NM_TYPE);
|
|
||||||
group.setNodes(nodes);
|
|
||||||
groups.add(group);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
@ -359,23 +293,16 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean saveGroup(Group group) {
|
public boolean saveGroup(Group group) {
|
||||||
boolean success = runQuery(new QueryPS(GROUP_UPDATE) {
|
return runQuery(GROUP_UPDATE, preparedStatement -> {
|
||||||
@Override
|
preparedStatement.setString(1, gson.toJson(exportToLegacy(group.getNodes())));
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
preparedStatement.setString(2, group.getName());
|
||||||
preparedStatement.setString(1, gson.toJson(exportToLegacy(group.getNodes())));
|
|
||||||
preparedStatement.setString(2, group.getName());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteGroup(Group group) {
|
public boolean deleteGroup(Group group) {
|
||||||
boolean success = runQuery(new QueryPS(GROUP_DELETE) {
|
boolean success = runQuery(GROUP_DELETE, preparedStatement -> {
|
||||||
@Override
|
preparedStatement.setString(1, group.getName());
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, group.getName());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (success) plugin.getGroupManager().unload(group);
|
if (success) plugin.getGroupManager().unload(group);
|
||||||
@ -385,29 +312,20 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
@Override
|
@Override
|
||||||
public boolean createAndLoadTrack(String name) {
|
public boolean createAndLoadTrack(String name) {
|
||||||
Track track = plugin.getTrackManager().make(name);
|
Track track = plugin.getTrackManager().make(name);
|
||||||
boolean success = runQuery(new QueryRS(TRACK_SELECT) {
|
boolean success = runQuery(TRACK_SELECT,
|
||||||
@Override
|
preparedStatement -> preparedStatement.setString(1, track.getName()),
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
resultSet -> {
|
||||||
preparedStatement.setString(1, track.getName());
|
if (!resultSet.next()) {
|
||||||
}
|
return runQuery(TRACK_INSERT, preparedStatement -> {
|
||||||
|
|
||||||
@Override
|
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
|
||||||
boolean success = true;
|
|
||||||
if (!resultSet.next()) {
|
|
||||||
success = runQuery(new QueryPS(TRACK_INSERT) {
|
|
||||||
@Override
|
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, track.getName());
|
preparedStatement.setString(1, track.getName());
|
||||||
preparedStatement.setString(2, gson.toJson(track.getGroups()));
|
preparedStatement.setString(2, gson.toJson(track.getGroups()));
|
||||||
}
|
});
|
||||||
});
|
} else {
|
||||||
} else {
|
track.setGroups(gson.fromJson(resultSet.getString("groups"), T_TYPE));
|
||||||
track.setGroups(gson.fromJson(resultSet.getString("groups"), T_TYPE));
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return success;
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (success) plugin.getTrackManager().updateOrSet(track);
|
if (success) plugin.getTrackManager().updateOrSet(track);
|
||||||
return success;
|
return success;
|
||||||
@ -416,21 +334,16 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
@Override
|
@Override
|
||||||
public boolean loadTrack(String name) {
|
public boolean loadTrack(String name) {
|
||||||
Track track = plugin.getTrackManager().make(name);
|
Track track = plugin.getTrackManager().make(name);
|
||||||
boolean success = runQuery(new QueryRS(TRACK_SELECT) {
|
boolean success = runQuery(TRACK_SELECT,
|
||||||
@Override
|
preparedStatement -> preparedStatement.setString(1, name),
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
resultSet -> {
|
||||||
preparedStatement.setString(1, name);
|
if (resultSet.next()) {
|
||||||
}
|
track.setGroups(gson.fromJson(resultSet.getString("groups"), T_TYPE));
|
||||||
|
return true;
|
||||||
@Override
|
}
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
return false;
|
||||||
if (resultSet.next()) {
|
|
||||||
track.setGroups(gson.fromJson(resultSet.getString("groups"), T_TYPE));
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (success) plugin.getTrackManager().updateOrSet(track);
|
if (success) plugin.getTrackManager().updateOrSet(track);
|
||||||
return success;
|
return success;
|
||||||
@ -439,21 +352,13 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
@Override
|
@Override
|
||||||
public boolean loadAllTracks() {
|
public boolean loadAllTracks() {
|
||||||
List<Track> tracks = new ArrayList<>();
|
List<Track> tracks = new ArrayList<>();
|
||||||
boolean success = runQuery(new QueryRS(TRACK_SELECT_ALL) {
|
boolean success = runQuery(TRACK_SELECT_ALL, resultSet -> {
|
||||||
@Override
|
while (resultSet.next()) {
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
Track track = plugin.getTrackManager().make(resultSet.getString("name"));
|
||||||
|
track.setGroups(gson.fromJson(resultSet.getString("groups"), T_TYPE));
|
||||||
}
|
tracks.add(track);
|
||||||
|
|
||||||
@Override
|
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
|
||||||
while (resultSet.next()) {
|
|
||||||
Track track = plugin.getTrackManager().make(resultSet.getString("name"));
|
|
||||||
track.setGroups(gson.fromJson(resultSet.getString("groups"), T_TYPE));
|
|
||||||
tracks.add(track);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
@ -466,23 +371,16 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean saveTrack(Track track) {
|
public boolean saveTrack(Track track) {
|
||||||
boolean success = runQuery(new QueryPS(TRACK_UPDATE) {
|
return runQuery(TRACK_UPDATE, preparedStatement -> {
|
||||||
@Override
|
preparedStatement.setString(1, gson.toJson(track.getGroups()));
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
preparedStatement.setString(2, track.getName());
|
||||||
preparedStatement.setString(1, gson.toJson(track.getGroups()));
|
|
||||||
preparedStatement.setString(2, track.getName());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteTrack(Track track) {
|
public boolean deleteTrack(Track track) {
|
||||||
boolean success = runQuery(new QueryPS(TRACK_DELETE) {
|
boolean success = runQuery(TRACK_DELETE, preparedStatement -> {
|
||||||
@Override
|
preparedStatement.setString(1, track.getName());
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, track.getName());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (success) plugin.getTrackManager().unload(track);
|
if (success) plugin.getTrackManager().unload(track);
|
||||||
@ -492,37 +390,24 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
@Override
|
@Override
|
||||||
public boolean saveUUIDData(String username, UUID uuid) {
|
public boolean saveUUIDData(String username, UUID uuid) {
|
||||||
final String u = username.toLowerCase();
|
final String u = username.toLowerCase();
|
||||||
boolean success = runQuery(new QueryRS(UUIDCACHE_SELECT) {
|
return runQuery(UUIDCACHE_SELECT,
|
||||||
@Override
|
preparedStatement -> preparedStatement.setString(1, u),
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
resultSet -> {
|
||||||
preparedStatement.setString(1, u);
|
boolean success;
|
||||||
}
|
if (resultSet.next()) {
|
||||||
|
success = runQuery(UUIDCACHE_UPDATE, preparedStatement -> {
|
||||||
@Override
|
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
|
||||||
boolean success;
|
|
||||||
if (resultSet.next()) {
|
|
||||||
success = runQuery(new QueryPS(UUIDCACHE_UPDATE) {
|
|
||||||
@Override
|
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, uuid.toString());
|
preparedStatement.setString(1, uuid.toString());
|
||||||
preparedStatement.setString(2, u);
|
preparedStatement.setString(2, u);
|
||||||
}
|
});
|
||||||
});
|
} else {
|
||||||
} else {
|
success = runQuery(UUIDCACHE_INSERT, preparedStatement -> {
|
||||||
success = runQuery(new QueryPS(UUIDCACHE_INSERT) {
|
|
||||||
@Override
|
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
preparedStatement.setString(1, u);
|
preparedStatement.setString(1, u);
|
||||||
preparedStatement.setString(2, uuid.toString());
|
preparedStatement.setString(2, uuid.toString());
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
return success;
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -530,21 +415,16 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
final String u = username.toLowerCase();
|
final String u = username.toLowerCase();
|
||||||
final UUID[] uuid = {null};
|
final UUID[] uuid = {null};
|
||||||
|
|
||||||
boolean success = runQuery(new QueryRS(UUIDCACHE_SELECT) {
|
boolean success = runQuery(UUIDCACHE_SELECT,
|
||||||
@Override
|
preparedStatement -> preparedStatement.setString(1, u),
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
resultSet -> {
|
||||||
preparedStatement.setString(1, u);
|
if (resultSet.next()) {
|
||||||
}
|
uuid[0] = UUID.fromString(resultSet.getString("uuid"));
|
||||||
|
return true;
|
||||||
@Override
|
}
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
return false;
|
||||||
if (resultSet.next()) {
|
|
||||||
uuid[0] = UUID.fromString(resultSet.getString("uuid"));
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return success ? uuid[0] : null;
|
return success ? uuid[0] : null;
|
||||||
}
|
}
|
||||||
@ -554,48 +434,25 @@ abstract class SQLDatastore extends Datastore {
|
|||||||
final String u = uuid.toString();
|
final String u = uuid.toString();
|
||||||
final String[] name = {null};
|
final String[] name = {null};
|
||||||
|
|
||||||
boolean success = runQuery(new QueryRS(UUIDCACHE_SELECT_NAME) {
|
boolean success = runQuery(UUIDCACHE_SELECT_NAME,
|
||||||
@Override
|
preparedStatement -> preparedStatement.setString(1, u),
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
resultSet -> {
|
||||||
preparedStatement.setString(1, u);
|
if (resultSet.next()) {
|
||||||
}
|
name[0] = resultSet.getString("name");
|
||||||
|
return true;
|
||||||
@Override
|
}
|
||||||
boolean onResult(ResultSet resultSet) throws SQLException {
|
return false;
|
||||||
if (resultSet.next()) {
|
|
||||||
name[0] = resultSet.getString("name");
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return success ? name[0] : null;
|
return success ? name[0] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Query extends QueryPS {
|
interface QueryPS {
|
||||||
Query(String query) {
|
void onRun(PreparedStatement preparedStatement) throws SQLException;
|
||||||
super(query);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
void onRun(PreparedStatement preparedStatement) throws SQLException {
|
|
||||||
// Do nothing
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
interface QueryRS {
|
||||||
@AllArgsConstructor
|
boolean onResult(ResultSet resultSet) throws SQLException;
|
||||||
abstract class QueryPS {
|
|
||||||
private final String query;
|
|
||||||
abstract void onRun(PreparedStatement preparedStatement) throws SQLException;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@AllArgsConstructor
|
|
||||||
abstract class QueryRS {
|
|
||||||
private final String query;
|
|
||||||
abstract void onRun(PreparedStatement preparedStatement) throws SQLException;
|
|
||||||
abstract boolean onResult(ResultSet resultSet) throws SQLException;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ public class SQLiteDatastore extends SQLDatastore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
boolean runQuery(QueryPS queryPS) {
|
boolean runQuery(String query, QueryPS queryPS) {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
try {
|
try {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
@ -64,7 +64,7 @@ public class SQLiteDatastore extends SQLDatastore {
|
|||||||
throw new IllegalStateException("SQL connection is null");
|
throw new IllegalStateException("SQL connection is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(queryPS.getQuery());
|
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||||
queryPS.onRun(preparedStatement);
|
queryPS.onRun(preparedStatement);
|
||||||
preparedStatement.execute();
|
preparedStatement.execute();
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ public class SQLiteDatastore extends SQLDatastore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
boolean runQuery(QueryRS queryRS) {
|
boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
try {
|
try {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
@ -84,8 +84,8 @@ public class SQLiteDatastore extends SQLDatastore {
|
|||||||
throw new IllegalStateException("SQL connection is null");
|
throw new IllegalStateException("SQL connection is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(queryRS.getQuery());
|
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||||
queryRS.onRun(preparedStatement);
|
queryPS.onRun(preparedStatement);
|
||||||
|
|
||||||
@Cleanup ResultSet resultSet = preparedStatement.executeQuery();
|
@Cleanup ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
success = queryRS.onResult(resultSet);
|
success = queryRS.onResult(resultSet);
|
||||||
|
Loading…
Reference in New Issue
Block a user