Add sqlite support
This commit is contained in:
parent
f6d8fb5286
commit
60f6bac903
@ -14,7 +14,7 @@ Yeah, I don't know. There are other more advanced, optimized, better programmed
|
||||
- **BungeeCord compatible** - my main motive for making this was that all other Bungee/Bukkit compatible perms plugins are utter aids. (At least, I couldn't find any decent ones)
|
||||
|
||||
### Caveats
|
||||
- Only supports MySQL
|
||||
- Only supports MySQL & SQLite (support for other stuff might happen in the future)
|
||||
- Not at all tested and could be super unreliable
|
||||
- It's quite possibly shit™
|
||||
|
||||
|
@ -68,4 +68,9 @@ public class BukkitConfig implements LPConfiguration {
|
||||
public String getDatabaseValue(String value) {
|
||||
return configuration.getString("sql." + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStorageMethod() {
|
||||
return configuration.getString("storage-method", "sqlite");
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package me.lucko.luckperms;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import lombok.Getter;
|
||||
import me.lucko.luckperms.data.Datastore;
|
||||
import me.lucko.luckperms.data.DatastoreConfiguration;
|
||||
import me.lucko.luckperms.data.HikariDatastore;
|
||||
import me.lucko.luckperms.data.MySQLConfiguration;
|
||||
import me.lucko.luckperms.data.methods.MySQLDatastore;
|
||||
import me.lucko.luckperms.data.methods.SQLiteDatastore;
|
||||
import me.lucko.luckperms.groups.GroupManager;
|
||||
import me.lucko.luckperms.listeners.PlayerListener;
|
||||
import me.lucko.luckperms.runnables.UpdateTask;
|
||||
@ -18,6 +18,7 @@ import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.ServicePriority;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -29,11 +30,9 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
|
||||
private UserManager userManager;
|
||||
private GroupManager groupManager;
|
||||
private Datastore datastore;
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
gson = new Gson();
|
||||
configuration = new BukkitConfig(this);
|
||||
|
||||
// register events
|
||||
@ -46,13 +45,25 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
|
||||
main.setExecutor(commandManager);
|
||||
main.setAliases(Arrays.asList("perms", "lp", "permissions", "p", "perm"));
|
||||
|
||||
datastore = new HikariDatastore(this);
|
||||
datastore.init(new DatastoreConfiguration(
|
||||
configuration.getDatabaseValue("address"),
|
||||
configuration.getDatabaseValue("database"),
|
||||
configuration.getDatabaseValue("username"),
|
||||
configuration.getDatabaseValue("password")
|
||||
));
|
||||
final String storageMethod = configuration.getStorageMethod();
|
||||
|
||||
if (storageMethod.equalsIgnoreCase("mysql")) {
|
||||
getLogger().info("Using MySQL as storage method.");
|
||||
datastore = new MySQLDatastore(this, new MySQLConfiguration(
|
||||
configuration.getDatabaseValue("address"),
|
||||
configuration.getDatabaseValue("database"),
|
||||
configuration.getDatabaseValue("username"),
|
||||
configuration.getDatabaseValue("password")
|
||||
));
|
||||
} else if (storageMethod.equalsIgnoreCase("sqlite")) {
|
||||
getLogger().info("Using SQLite as storage method.");
|
||||
datastore = new SQLiteDatastore(this, new File(getDataFolder(), "luckperms.sqlite"));
|
||||
} else {
|
||||
getLogger().warning("Storage method '" + storageMethod + "' was not recognised. Using SQLite as fallback.");
|
||||
datastore = new SQLiteDatastore(this, new File(getDataFolder(), "luckperms.sqlite"));
|
||||
}
|
||||
|
||||
datastore.init();
|
||||
|
||||
userManager = new BukkitUserManager(this);
|
||||
groupManager = new GroupManager(this);
|
||||
@ -96,11 +107,6 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
|
||||
Bukkit.getScheduler().runTask(this, r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Gson getGson() {
|
||||
return gson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return VERSION;
|
||||
|
@ -11,6 +11,11 @@ include-global: true
|
||||
|
||||
prefix: '&7&l[&b&lL&a&lP&7&l] &c'
|
||||
|
||||
# Which storage method the plugin should use.
|
||||
# Currently supported: mysql, sqlite
|
||||
# Fill out connection info below if you're using MySQL
|
||||
storage-method: sqlite
|
||||
|
||||
sql:
|
||||
address: localhost:3306
|
||||
database: minecraft
|
||||
|
@ -75,4 +75,9 @@ public class BungeeConfig implements LPConfiguration {
|
||||
public String getDatabaseValue(String value) {
|
||||
return configuration.getString("sql." + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStorageMethod() {
|
||||
return configuration.getString("storage-method", "sqlite");
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
package me.lucko.luckperms;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import lombok.Getter;
|
||||
import me.lucko.luckperms.commands.CommandManager;
|
||||
import me.lucko.luckperms.data.Datastore;
|
||||
import me.lucko.luckperms.data.DatastoreConfiguration;
|
||||
import me.lucko.luckperms.data.HikariDatastore;
|
||||
import me.lucko.luckperms.data.MySQLConfiguration;
|
||||
import me.lucko.luckperms.data.methods.MySQLDatastore;
|
||||
import me.lucko.luckperms.data.methods.SQLiteDatastore;
|
||||
import me.lucko.luckperms.groups.GroupManager;
|
||||
import me.lucko.luckperms.listeners.PlayerListener;
|
||||
import me.lucko.luckperms.runnables.UpdateTask;
|
||||
@ -14,6 +14,7 @@ import me.lucko.luckperms.users.UserManager;
|
||||
import me.lucko.luckperms.utils.LPConfiguration;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ -25,11 +26,9 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
|
||||
private UserManager userManager;
|
||||
private GroupManager groupManager;
|
||||
private Datastore datastore;
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
gson = new Gson();
|
||||
configuration = new BungeeConfig(this);
|
||||
|
||||
// register events
|
||||
@ -38,13 +37,25 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
|
||||
// register commands
|
||||
getProxy().getPluginManager().registerCommand(this, new MainCommand(new CommandManager(this)));
|
||||
|
||||
datastore = new HikariDatastore(this);
|
||||
datastore.init(new DatastoreConfiguration(
|
||||
configuration.getDatabaseValue("address"),
|
||||
configuration.getDatabaseValue("database"),
|
||||
configuration.getDatabaseValue("username"),
|
||||
configuration.getDatabaseValue("password")
|
||||
));
|
||||
final String storageMethod = configuration.getStorageMethod();
|
||||
|
||||
if (storageMethod.equalsIgnoreCase("mysql")) {
|
||||
getLogger().info("Using MySQL as storage method.");
|
||||
datastore = new MySQLDatastore(this, new MySQLConfiguration(
|
||||
configuration.getDatabaseValue("address"),
|
||||
configuration.getDatabaseValue("database"),
|
||||
configuration.getDatabaseValue("username"),
|
||||
configuration.getDatabaseValue("password")
|
||||
));
|
||||
} else if (storageMethod.equalsIgnoreCase("sqlite")) {
|
||||
getLogger().info("Using SQLite as storage method.");
|
||||
datastore = new SQLiteDatastore(this, new File(getDataFolder(), "luckperms.sqlite"));
|
||||
} else {
|
||||
getLogger().warning("Storage method '" + storageMethod + "' was not recognised. Using SQLite as fallback.");
|
||||
datastore = new SQLiteDatastore(this, new File(getDataFolder(), "luckperms.sqlite"));
|
||||
}
|
||||
|
||||
datastore.init();
|
||||
|
||||
userManager = new BungeeUserManager(this);
|
||||
groupManager = new GroupManager(this);
|
||||
@ -90,9 +101,4 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
|
||||
public void doSync(Runnable r) {
|
||||
r.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Gson getGson() {
|
||||
return gson;
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,11 @@ include-global: false
|
||||
|
||||
prefix: '&7&l[&b&lL&a&lP&7&l] &c'
|
||||
|
||||
# Which storage method the plugin should use.
|
||||
# Currently supported: mysql, sqlite
|
||||
# Fill out connection info below if you're using MySQL
|
||||
storage-method: sqlite
|
||||
|
||||
sql:
|
||||
address: localhost:3306
|
||||
database: minecraft
|
||||
|
@ -1,6 +1,5 @@
|
||||
package me.lucko.luckperms;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import me.lucko.luckperms.data.Datastore;
|
||||
import me.lucko.luckperms.groups.GroupManager;
|
||||
import me.lucko.luckperms.users.UserManager;
|
||||
@ -75,6 +74,4 @@ public interface LuckPermsPlugin {
|
||||
* @param r the task to run
|
||||
*/
|
||||
void doSync(Runnable r);
|
||||
|
||||
Gson getGson();
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ public abstract class Datastore {
|
||||
/*
|
||||
These methods will block the thread that they're ran on.
|
||||
*/
|
||||
public abstract void init(DatastoreConfiguration configuration);
|
||||
public abstract void init();
|
||||
public abstract void shutdown();
|
||||
public abstract boolean loadOrCreateUser(UUID uuid, String username);
|
||||
public abstract boolean loadUser(UUID uuid);
|
||||
|
@ -5,7 +5,7 @@ import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class DatastoreConfiguration {
|
||||
public class MySQLConfiguration {
|
||||
|
||||
private final String address;
|
||||
private final String database;
|
@ -0,0 +1,55 @@
|
||||
package me.lucko.luckperms.data.methods;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import me.lucko.luckperms.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.data.MySQLConfiguration;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class MySQLDatastore extends SQLDatastore {
|
||||
|
||||
private static final String CREATETABLE_UUID = "CREATE TABLE IF NOT EXISTS `lp_uuid` (`name` VARCHAR(16) NOT NULL, `uuid` VARCHAR(36) NOT NULL, PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
|
||||
private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
|
||||
private static final String CREATETABLE_GROUPS = "CREATE TABLE IF NOT EXISTS `lp_groups` (`name` VARCHAR(36) NOT NULL, `perms` TEXT NULL, PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
|
||||
|
||||
private MySQLConfiguration configuration;
|
||||
private HikariDataSource hikari;
|
||||
|
||||
public MySQLDatastore(LuckPermsPlugin plugin, MySQLConfiguration configuration) {
|
||||
super(plugin, "MySQL");
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
hikari = new HikariDataSource();
|
||||
|
||||
final String address = configuration.getAddress();
|
||||
final String database = configuration.getDatabase();
|
||||
final String username = configuration.getUsername();
|
||||
final String password = configuration.getPassword();
|
||||
|
||||
hikari.setMaximumPoolSize(10);
|
||||
hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
|
||||
hikari.addDataSourceProperty("serverName", address.split(":")[0]);
|
||||
hikari.addDataSourceProperty("port", address.split(":")[1]);
|
||||
hikari.addDataSourceProperty("databaseName", database);
|
||||
hikari.addDataSourceProperty("user", username);
|
||||
hikari.addDataSourceProperty("password", password);
|
||||
|
||||
setupTables(CREATETABLE_UUID, CREATETABLE_USERS, CREATETABLE_GROUPS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
if (hikari != null) {
|
||||
hikari.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
Connection getConnection() throws SQLException {
|
||||
return hikari.getConnection();
|
||||
}
|
||||
}
|
@ -1,48 +1,56 @@
|
||||
package me.lucko.luckperms.data;
|
||||
package me.lucko.luckperms.data.methods;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import me.lucko.luckperms.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.data.Datastore;
|
||||
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
|
||||
import me.lucko.luckperms.groups.Group;
|
||||
import me.lucko.luckperms.groups.GroupManager;
|
||||
import me.lucko.luckperms.users.User;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class HikariDatastore extends Datastore {
|
||||
@SuppressWarnings("UnnecessaryLocalVariable")
|
||||
public abstract class SQLDatastore extends Datastore {
|
||||
|
||||
private static final String CREATETABLE_UUID = "CREATE TABLE IF NOT EXISTS `lp_uuid` (`name` VARCHAR(16) NOT NULL, `uuid` VARCHAR(36) NOT NULL, PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
|
||||
private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
|
||||
private static final String CREATETABLE_GROUPS = "CREATE TABLE IF NOT EXISTS `lp_groups` (`name` VARCHAR(36) NOT NULL, `perms` TEXT NULL, PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
|
||||
private static final Type NM_TYPE = new TypeToken<Map<String, Boolean>>(){}.getType();
|
||||
|
||||
private static final String USER_INSERT = "INSERT INTO lp_users VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE name=?";
|
||||
private static final String USER_INSERT = "INSERT INTO lp_users VALUES(?, ?, ?)";
|
||||
private static final String USER_SELECT = "SELECT * FROM lp_users WHERE uuid=?";
|
||||
private static final String USER_SAVE = "UPDATE lp_users SET name=?, perms=? WHERE uuid=?";
|
||||
private static final String USER_UPDATE = "UPDATE lp_users SET name=?, perms=? WHERE uuid=?";
|
||||
|
||||
private static final String GROUP_INSERT = "INSERT INTO lp_groups VALUES(?, ?) ON DUPLICATE KEY UPDATE perms=?";
|
||||
private static final String GROUP_INSERT = "INSERT INTO lp_groups VALUES(?, ?)";
|
||||
private static final String GROUP_SELECT = "SELECT perms FROM lp_groups WHERE name=?";
|
||||
private static final String GROUP_SELECT_ALL = "SELECT * FROM lp_groups";
|
||||
private static final String GROUP_SAVE = "UPDATE lp_groups SET perms=? WHERE name=?";
|
||||
private static final String GROUP_UPDATE = "UPDATE lp_groups SET perms=? WHERE name=?";
|
||||
private static final String GROUP_DELETE = "DELETE FROM lp_groups WHERE name=?";
|
||||
|
||||
private static final String UUIDCACHE_INSERT = "INSERT INTO lp_uuid VALUES(?, ?) ON DUPLICATE KEY UPDATE uuid=?";
|
||||
private static final String UUIDCACHE_INSERT = "INSERT INTO lp_uuid VALUES(?, ?)";
|
||||
private static final String UUIDCACHE_SELECT = "SELECT uuid FROM lp_uuid WHERE name=?";
|
||||
private static final String UUIDCACHE_UPDATE = "UPDATE lp_uuid SET uuid=? WHERE name=?";
|
||||
|
||||
private HikariDataSource hikari;
|
||||
private Gson gson;
|
||||
|
||||
public HikariDatastore(LuckPermsPlugin plugin) {
|
||||
super(plugin, "MySQL");
|
||||
public SQLDatastore(LuckPermsPlugin plugin, String name) {
|
||||
super(plugin, name);
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
private static void executeQuery(Connection connection, String query) throws SQLException {
|
||||
abstract Connection getConnection() throws SQLException;
|
||||
|
||||
private static void executeQuery(Connection connection, String query, Closer closer) throws SQLException {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
closer.add(preparedStatement);
|
||||
preparedStatement.execute();
|
||||
preparedStatement.close();
|
||||
}
|
||||
@ -51,13 +59,18 @@ public class HikariDatastore extends Datastore {
|
||||
boolean success = false;
|
||||
|
||||
Connection connection = null;
|
||||
Closer c = new Closer();
|
||||
try {
|
||||
connection = hikari.getConnection();
|
||||
success = query.onRun(connection);
|
||||
connection = getConnection();
|
||||
if (connection == null) {
|
||||
throw new IllegalStateException("SQL connection is null");
|
||||
}
|
||||
success = query.onRun(connection, c);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
success = false;
|
||||
} finally {
|
||||
c.closeAll();
|
||||
if (connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
@ -69,67 +82,36 @@ public class HikariDatastore extends Datastore {
|
||||
return success;
|
||||
}
|
||||
|
||||
private void setupTables() {
|
||||
boolean success = runQuery(connection -> {
|
||||
executeQuery(connection, CREATETABLE_UUID);
|
||||
executeQuery(connection, CREATETABLE_USERS);
|
||||
executeQuery(connection, CREATETABLE_GROUPS);
|
||||
void setupTables(String... tableQueries) {
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
for (String s : tableQueries) {
|
||||
executeQuery(connection, s, closer);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
if (!success) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Error occurred whilst connecting to the database. All connections are disallowed.");
|
||||
hikari.shutdown();
|
||||
plugin.getLogger().log(Level.SEVERE, "Error occurred whilst initialising the database. All connections are disallowed.");
|
||||
shutdown();
|
||||
setAcceptingLogins(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(DatastoreConfiguration configuration) {
|
||||
hikari = new HikariDataSource();
|
||||
|
||||
final String address = configuration.getAddress();
|
||||
final String database = configuration.getDatabase();
|
||||
final String username = configuration.getUsername();
|
||||
final String password = configuration.getPassword();
|
||||
|
||||
hikari.setMaximumPoolSize(10);
|
||||
hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
|
||||
hikari.addDataSourceProperty("serverName", address.split(":")[0]);
|
||||
hikari.addDataSourceProperty("port", address.split(":")[1]);
|
||||
hikari.addDataSourceProperty("databaseName", database);
|
||||
hikari.addDataSourceProperty("user", username);
|
||||
hikari.addDataSourceProperty("password", password);
|
||||
|
||||
setupTables();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
if (hikari != null) {
|
||||
hikari.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean loadUser(UUID uuid) {
|
||||
User user = plugin.getUserManager().makeUser(uuid);
|
||||
boolean success = runQuery(connection -> {
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(USER_SELECT);
|
||||
closer.add(preparedStatement);
|
||||
preparedStatement.setString(1, uuid.toString());
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
closer.add(resultSet);
|
||||
if (resultSet.next()) {
|
||||
user.loadNodes(resultSet.getString("perms"));
|
||||
user.getNodes().putAll(gson.fromJson(resultSet.getString("perms"), NM_TYPE));
|
||||
user.setName(resultSet.getString("name"));
|
||||
|
||||
preparedStatement.close();
|
||||
resultSet.close();
|
||||
return true;
|
||||
}
|
||||
preparedStatement.close();
|
||||
resultSet.close();
|
||||
return false;
|
||||
});
|
||||
|
||||
@ -144,30 +126,23 @@ public class HikariDatastore extends Datastore {
|
||||
try {
|
||||
user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true);
|
||||
} catch (ObjectAlreadyHasException ignored) {}
|
||||
boolean success = runQuery(connection -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(USER_INSERT);
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(USER_SELECT);
|
||||
closer.add(preparedStatement);
|
||||
preparedStatement.setString(1, user.getUuid().toString());
|
||||
preparedStatement.setString(2, user.getName());
|
||||
preparedStatement.setString(3, user.serializeNodes());
|
||||
preparedStatement.setString(4, username);
|
||||
preparedStatement.execute();
|
||||
preparedStatement.close();
|
||||
|
||||
preparedStatement = connection.prepareStatement(USER_SELECT);
|
||||
preparedStatement.setString(1, uuid.toString());
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
if (resultSet.next()) {
|
||||
user.loadNodes(resultSet.getString("perms"));
|
||||
|
||||
preparedStatement.close();
|
||||
resultSet.close();
|
||||
return true;
|
||||
closer.add(resultSet);
|
||||
if (!resultSet.next()) {
|
||||
PreparedStatement preparedStatement1 = connection.prepareStatement(USER_INSERT);
|
||||
closer.add(preparedStatement1);
|
||||
preparedStatement1.setString(1, user.getUuid().toString());
|
||||
preparedStatement1.setString(2, user.getName());
|
||||
preparedStatement1.setString(3, gson.toJson(user.getNodes()));
|
||||
preparedStatement1.execute();
|
||||
} else {
|
||||
user.getNodes().putAll(gson.fromJson(resultSet.getString("perms"), NM_TYPE));
|
||||
}
|
||||
|
||||
preparedStatement.close();
|
||||
resultSet.close();
|
||||
return true;
|
||||
});
|
||||
|
||||
@ -178,13 +153,13 @@ public class HikariDatastore extends Datastore {
|
||||
|
||||
@Override
|
||||
public boolean saveUser(User user) {
|
||||
boolean success = runQuery(connection -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(USER_SAVE);
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(USER_UPDATE);
|
||||
closer.add(preparedStatement);
|
||||
preparedStatement.setString(1, user.getName());
|
||||
preparedStatement.setString(2, user.serializeNodes());
|
||||
preparedStatement.setString(2, gson.toJson(user.getNodes()));
|
||||
preparedStatement.setString(3, user.getUuid().toString());
|
||||
preparedStatement.execute();
|
||||
preparedStatement.close();
|
||||
return true;
|
||||
});
|
||||
return success;
|
||||
@ -193,27 +168,27 @@ public class HikariDatastore extends Datastore {
|
||||
@Override
|
||||
public boolean createAndLoadGroup(String name) {
|
||||
Group group = plugin.getGroupManager().makeGroup(name);
|
||||
boolean success = runQuery(connection -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(GROUP_INSERT);
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(GROUP_SELECT);
|
||||
closer.add(preparedStatement);
|
||||
preparedStatement.setString(1, group.getName());
|
||||
preparedStatement.setString(2, group.serializeNodes());
|
||||
preparedStatement.setString(3, group.serializeNodes());
|
||||
preparedStatement.execute();
|
||||
preparedStatement.close();
|
||||
|
||||
preparedStatement = connection.prepareStatement(GROUP_SELECT);
|
||||
preparedStatement.setString(1, name);
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
closer.add(resultSet);
|
||||
|
||||
if (resultSet.next()) {
|
||||
group.loadNodes(resultSet.getString("perms"));
|
||||
if (!resultSet.next()) {
|
||||
PreparedStatement preparedStatement1 = connection.prepareStatement(GROUP_INSERT);
|
||||
closer.add(preparedStatement1);
|
||||
preparedStatement1.setString(1, group.getName());
|
||||
preparedStatement1.setString(2, gson.toJson(group.getNodes()));
|
||||
preparedStatement1.execute();
|
||||
|
||||
} else {
|
||||
group.getNodes().putAll(gson.fromJson(resultSet.getString("perms"), NM_TYPE));
|
||||
}
|
||||
|
||||
preparedStatement.close();
|
||||
resultSet.close();
|
||||
return true;
|
||||
});
|
||||
|
||||
if (success) plugin.getGroupManager().updateOrSetGroup(group);
|
||||
return success;
|
||||
}
|
||||
@ -221,24 +196,20 @@ public class HikariDatastore extends Datastore {
|
||||
@Override
|
||||
public boolean loadGroup(String name) {
|
||||
Group group = plugin.getGroupManager().makeGroup(name);
|
||||
boolean success = runQuery(connection -> {
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(GROUP_SELECT);
|
||||
closer.add(preparedStatement);
|
||||
preparedStatement.setString(1, name);
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
closer.add(resultSet);
|
||||
if (resultSet.next()) {
|
||||
group.loadNodes(resultSet.getString("perms"));
|
||||
|
||||
preparedStatement.close();
|
||||
resultSet.close();
|
||||
group.getNodes().putAll(gson.fromJson(resultSet.getString("perms"), NM_TYPE));
|
||||
return true;
|
||||
}
|
||||
|
||||
preparedStatement.close();
|
||||
resultSet.close();
|
||||
return false;
|
||||
});
|
||||
|
||||
if (success) plugin.getGroupManager().updateOrSetGroup(group);
|
||||
return success;
|
||||
}
|
||||
@ -246,18 +217,17 @@ public class HikariDatastore extends Datastore {
|
||||
@Override
|
||||
public boolean loadAllGroups() {
|
||||
List<Group> groups = new ArrayList<>();
|
||||
boolean success = runQuery(connection -> {
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(GROUP_SELECT_ALL);
|
||||
closer.add(preparedStatement);
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
closer.add(resultSet);
|
||||
while (resultSet.next()) {
|
||||
Group group = plugin.getGroupManager().makeGroup(resultSet.getString("name"));
|
||||
group.loadNodes(resultSet.getString("perms"));
|
||||
group.getNodes().putAll(gson.fromJson(resultSet.getString("perms"), NM_TYPE));
|
||||
groups.add(group);
|
||||
}
|
||||
preparedStatement.close();
|
||||
resultSet.close();
|
||||
return true;
|
||||
});
|
||||
|
||||
@ -268,12 +238,12 @@ public class HikariDatastore extends Datastore {
|
||||
|
||||
@Override
|
||||
public boolean saveGroup(Group group) {
|
||||
boolean success = runQuery(connection -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(GROUP_SAVE);
|
||||
preparedStatement.setString(1, group.serializeNodes());
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(GROUP_UPDATE);
|
||||
closer.add(preparedStatement);
|
||||
preparedStatement.setString(1, gson.toJson(group.getNodes()));
|
||||
preparedStatement.setString(2, group.getName());
|
||||
preparedStatement.execute();
|
||||
preparedStatement.close();
|
||||
return true;
|
||||
});
|
||||
return success;
|
||||
@ -281,11 +251,11 @@ public class HikariDatastore extends Datastore {
|
||||
|
||||
@Override
|
||||
public boolean deleteGroup(Group group) {
|
||||
boolean success = runQuery(connection -> {
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(GROUP_DELETE);
|
||||
closer.add(preparedStatement);
|
||||
preparedStatement.setString(1, group.getName());
|
||||
preparedStatement.execute();
|
||||
preparedStatement.close();
|
||||
return true;
|
||||
});
|
||||
if (success) plugin.getGroupManager().unloadGroup(group);
|
||||
@ -294,13 +264,29 @@ public class HikariDatastore extends Datastore {
|
||||
|
||||
@Override
|
||||
public boolean saveUUIDData(String username, UUID uuid) {
|
||||
boolean success = runQuery(connection -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(UUIDCACHE_INSERT);
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(UUIDCACHE_SELECT);
|
||||
closer.add(preparedStatement);
|
||||
preparedStatement.setString(1, username);
|
||||
preparedStatement.setString(2, uuid.toString());
|
||||
preparedStatement.setString(3, uuid.toString());
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
closer.add(resultSet);
|
||||
|
||||
PreparedStatement preparedStatement1;
|
||||
if (resultSet.next()) {
|
||||
preparedStatement1 = connection.prepareStatement(UUIDCACHE_UPDATE);
|
||||
closer.add(preparedStatement1);
|
||||
preparedStatement1.setString(1, uuid.toString());
|
||||
preparedStatement1.setString(2, username);
|
||||
|
||||
} else {
|
||||
preparedStatement1 = connection.prepareStatement(UUIDCACHE_INSERT);
|
||||
closer.add(preparedStatement1);
|
||||
preparedStatement1.setString(1, username);
|
||||
preparedStatement1.setString(2, uuid.toString());
|
||||
}
|
||||
|
||||
preparedStatement.execute();
|
||||
preparedStatement.close();
|
||||
return true;
|
||||
});
|
||||
return success;
|
||||
@ -309,27 +295,40 @@ public class HikariDatastore extends Datastore {
|
||||
@Override
|
||||
public UUID getUUID(String username) {
|
||||
final UUID[] uuid = {null};
|
||||
boolean success = runQuery(connection -> {
|
||||
boolean success = runQuery((connection, closer) -> {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(UUIDCACHE_SELECT);
|
||||
closer.add(preparedStatement);
|
||||
preparedStatement.setString(1, username);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
closer.add(resultSet);
|
||||
if (resultSet.next()) {
|
||||
uuid[0] = UUID.fromString(resultSet.getString("uuid"));
|
||||
preparedStatement.close();
|
||||
resultSet.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
preparedStatement.close();
|
||||
resultSet.close();
|
||||
return false;
|
||||
});
|
||||
|
||||
return success ? uuid[0] : null;
|
||||
}
|
||||
|
||||
private interface Query {
|
||||
boolean onRun(Connection connection) throws SQLException;
|
||||
interface Query {
|
||||
boolean onRun(Connection connection, Closer closer) throws SQLException;
|
||||
}
|
||||
|
||||
private class Closer {
|
||||
private List<AutoCloseable> objects = new ArrayList<>();
|
||||
|
||||
public void add(AutoCloseable a) {
|
||||
objects.add(a);
|
||||
}
|
||||
|
||||
void closeAll() {
|
||||
objects.stream().filter(a -> a != null).forEach(a -> {
|
||||
try {
|
||||
a.close();
|
||||
} catch (Exception ignored) {}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package me.lucko.luckperms.data.methods;
|
||||
|
||||
import me.lucko.luckperms.LuckPermsPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SQLiteDatastore extends SQLDatastore {
|
||||
|
||||
private static final String CREATETABLE_UUID = "CREATE TABLE IF NOT EXISTS `lp_uuid` (`name` VARCHAR(16) NOT NULL, `uuid` VARCHAR(36) NOT NULL, PRIMARY KEY (`name`));";
|
||||
private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`));";
|
||||
private static final String CREATETABLE_GROUPS = "CREATE TABLE IF NOT EXISTS `lp_groups` (`name` VARCHAR(36) NOT NULL, `perms` TEXT NULL, PRIMARY KEY (`name`));";
|
||||
|
||||
private File file;
|
||||
private Connection connection = null;
|
||||
|
||||
public SQLiteDatastore(LuckPermsPlugin plugin, File file) {
|
||||
super(plugin, "SQLite");
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
setupTables(CREATETABLE_UUID, CREATETABLE_USERS, CREATETABLE_GROUPS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
Connection getConnection() throws SQLException {
|
||||
if (connection == null || connection.isClosed()) {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
} catch (ClassNotFoundException ignored) {}
|
||||
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
}
|
@ -9,5 +9,6 @@ public interface LPConfiguration {
|
||||
String getDefaultGroupName();
|
||||
boolean getIncludeGlobalPerms();
|
||||
String getDatabaseValue(String value);
|
||||
String getStorageMethod();
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package me.lucko.luckperms.utils;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.lucko.luckperms.LuckPermsPlugin;
|
||||
@ -260,20 +259,4 @@ public abstract class PermissionObject {
|
||||
|
||||
return perms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads serialised nodes into the object
|
||||
* @param json The json data to be loaded
|
||||
*/
|
||||
public void loadNodes(String json) {
|
||||
nodes.putAll(plugin.getGson().fromJson(json, new TypeToken<Map<String, Boolean>>(){}.getType()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize the nodes in the object to be saved in the datastore
|
||||
* @return A json string
|
||||
*/
|
||||
public String serializeNodes() {
|
||||
return plugin.getGson().toJson(nodes);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user