Skip to content

Commit 3ebb9d5

Browse files
committed
Support for SQLite
1 parent bc7a7ee commit 3ebb9d5

File tree

7 files changed

+89
-34
lines changed

7 files changed

+89
-34
lines changed

API/src/main/java/me/innectic/permissify/api/database/ConnectionInformation.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import lombok.AllArgsConstructor;
2828
import lombok.Getter;
2929

30+
import java.util.Map;
31+
3032
/**
3133
* @author Innectic
3234
* @since 6/8/2017
@@ -40,4 +42,5 @@ public class ConnectionInformation {
4042
@Getter private int port;
4143
@Getter private String username;
4244
@Getter private String password;
45+
@Getter private Map<String, Object> meta;
4346
}

API/src/main/java/me/innectic/permissify/api/database/handlers/HandlerType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
@AllArgsConstructor
3939
public enum HandlerType {
4040

41-
MYSQL(MySQLHandler.class, "MySQL");
41+
MYSQL(SQLHandler.class, "MySQL"), SQLITE(SQLHandler.class, "SQLite");
4242

4343
@Getter private Class<? extends DatabaseHandler> handler;
4444
@Getter private String displayName;

API/src/main/java/me/innectic/permissify/api/database/handlers/MySQLHandler.java renamed to API/src/main/java/me/innectic/permissify/api/database/handlers/SQLHandler.java

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,24 @@
4040
* @author Innectic
4141
* @since 6/8/2017
4242
*/
43-
public class MySQLHandler extends DatabaseHandler {
43+
public class SQLHandler extends DatabaseHandler {
4444

45-
public MySQLHandler(ConnectionInformation connectionInformation) {
45+
private final String baseConnectionURL;
46+
private boolean isUsingSqlite = false;
47+
48+
public SQLHandler(ConnectionInformation connectionInformation) {
4649
super(connectionInformation);
50+
String type = "mysql";
51+
String databaseUrl = "//" + connectionInformation.getUrl() + ":" + connectionInformation.getPort();
52+
53+
if(connectionInformation.getMeta().containsKey("sqlite")) {
54+
type = "sqlite";
55+
// TODO: It would be nice to give this types...
56+
Map sqliteData = (Map) connectionInformation.getMeta().get("sqlite");
57+
databaseUrl = (String) sqliteData.get("file");
58+
isUsingSqlite = true;
59+
}
60+
baseConnectionURL = "jdbc:" + type + ":" + databaseUrl;
4761
}
4862

4963
/**
@@ -54,7 +68,8 @@ public MySQLHandler(ConnectionInformation connectionInformation) {
5468
private Optional<Connection> getConnection() {
5569
if (!connectionInformation.isPresent()) return Optional.empty();
5670
try {
57-
String connectionURL = "jdbc:mysql://" + connectionInformation.get().getUrl() + ":" + connectionInformation.get().getPort() + "/" + connectionInformation.get().getDatabase();
71+
if (isUsingSqlite) return Optional.ofNullable(DriverManager.getConnection(baseConnectionURL));
72+
String connectionURL = baseConnectionURL + "/" + connectionInformation.get().getDatabase();
5873
return Optional.ofNullable(DriverManager.getConnection(connectionURL, connectionInformation.get().getUsername(), connectionInformation.get().getPassword()));
5974
} catch (SQLException e) {
6075
PermissifyAPI.get().ifPresent(api -> api.getDisplayUtil().displayError(ConnectionError.REJECTED, Optional.of(e)));
@@ -70,48 +85,52 @@ public void initialize() {
7085
if (!connectionInformation.isPresent()) return;
7186

7287
try {
73-
String connectionURL = "jdbc:mysql://" + connectionInformation.get().getUrl() + ":" + connectionInformation.get().getPort();
74-
Optional<Connection> connection = Optional.ofNullable(DriverManager.getConnection(connectionURL, connectionInformation.get().getUsername(), connectionInformation.get().getPassword()));
88+
Optional<Connection> connection;
89+
if (isUsingSqlite) connection = Optional.ofNullable(DriverManager.getConnection(baseConnectionURL));
90+
else connection = Optional.ofNullable(DriverManager.getConnection(baseConnectionURL, connectionInformation.get().getUsername(), connectionInformation.get().getPassword()));
7591
if (!connection.isPresent()) return;
7692
String database = connectionInformation.get().getDatabase();
93+
if (!database.equals("")) database += ".";
7794
// TODO: This should all be prepared statements, but that breaks for some reason
78-
PreparedStatement databaseStatement = connection.get().prepareStatement("CREATE DATABASE IF NOT EXISTS " + database);
79-
databaseStatement.execute();
80-
databaseStatement.close();
95+
if (!isUsingSqlite) {
96+
PreparedStatement databaseStatement = connection.get().prepareStatement("CREATE DATABASE IF NOT EXISTS " + database);
97+
databaseStatement.execute();
98+
databaseStatement.close();
99+
}
81100

82-
PreparedStatement groupMembersStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + ".groupMembers (uuid VARCHAR(767) NOT NULL, `group` VARCHAR(700) NOT NULL, `primary` TINYINT NOT NULL)");
101+
PreparedStatement groupMembersStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + "groupMembers (uuid VARCHAR(767) NOT NULL, `group` VARCHAR(700) NOT NULL, `primary` TINYINT NOT NULL)");
83102
groupMembersStatement.execute();
84103
groupMembersStatement.close();
85104

86-
PreparedStatement groupPermissionsStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database +".groupPermissions (groupName VARCHAR(767) NOT NULL, permission VARCHAR(767) NOT NULL)");
105+
PreparedStatement groupPermissionsStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database +"groupPermissions (groupName VARCHAR(767) NOT NULL, permission VARCHAR(767) NOT NULL)");
87106
groupPermissionsStatement.execute();
88107
groupPermissionsStatement.close();
89108

90-
PreparedStatement groupsStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + ".groups (name VARCHAR(100) NOT NULL UNIQUE, prefix VARCHAR(100) NOT NULL, suffix VARCHAR(100) NOT NULL, chatcolor VARCHAR(4) NOT NULL, defaultGroup TINYINT NOT NULL)");
109+
PreparedStatement groupsStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + "groups (name VARCHAR(100) NOT NULL UNIQUE, prefix VARCHAR(100) NOT NULL, suffix VARCHAR(100) NOT NULL, chatcolor VARCHAR(4) NOT NULL, defaultGroup TINYINT NOT NULL)");
91110
groupsStatement.execute();
92111
groupsStatement.close();
93112

94-
PreparedStatement playerPermissionsStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + ".playerPermissions (uuid VARCHAR(767) NOT NULL, permission VARCHAR(767) NOT NULL, granted TINYINT NOT NULL)");
113+
PreparedStatement playerPermissionsStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + "playerPermissions (uuid VARCHAR(767) NOT NULL, permission VARCHAR(767) NOT NULL, granted TINYINT NOT NULL)");
95114
playerPermissionsStatement.execute();
96115
playerPermissionsStatement.close();
97116

98-
PreparedStatement superAdminStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + ".superAdmin (uuid VARCHAR(767) NOT NULL)");
117+
PreparedStatement superAdminStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + "superAdmin (uuid VARCHAR(767) NOT NULL)");
99118
superAdminStatement.execute();
100119
superAdminStatement.close();
101120

102-
ResultSet results = connection.get().getMetaData().getTables(database, null, "formatting", new String[]{"TABLE"});
103-
if (results.next()) {
104-
PreparedStatement formattingStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + ".formatting (`format` VARCHAR(400) NOT NULL, formatter VARCHAR(200) NOT NULL)");
121+
if (!hasFormattingTable(connection.get(), database)) {
122+
System.out.println("Creating");
123+
PreparedStatement formattingStatement = connection.get().prepareStatement("CREATE TABLE IF NOT EXISTS " + database + "formatting (`format` VARCHAR(400) NOT NULL, formatter VARCHAR(200) NOT NULL)");
105124
formattingStatement.execute();
106125
formattingStatement.close();
107126

108-
PreparedStatement defaultChatFormat = connection.get().prepareStatement("INSERT INTO " + database + ".formatting (`format`, formatter) VALUES (?,?)");
127+
PreparedStatement defaultChatFormat = connection.get().prepareStatement("INSERT INTO " + database + "formatting (`format`, formatter) VALUES (?,?)");
109128
defaultChatFormat.setString(1, "{group} {username}: {message}");
110129
defaultChatFormat.setString(2, "chat");
111130
defaultChatFormat.execute();
112131
defaultChatFormat.close();
113132

114-
PreparedStatement defaultWhisperFormat = connection.get().prepareStatement("INSERT INTO " + database + ".formatting (`format`, formatter) VALUES (?,?)");
133+
PreparedStatement defaultWhisperFormat = connection.get().prepareStatement("INSERT INTO " + database + "formatting (`format`, formatter) VALUES (?,?)");
115134
defaultWhisperFormat.setString(1, "{senderGroup} {username} > {receiverGroup} {to}: {message}");
116135
defaultWhisperFormat.setString(2, "whisper");
117136
defaultWhisperFormat.execute();
@@ -733,4 +752,18 @@ public void setDefaultGroup(PermissionGroup group) {
733752
e.printStackTrace();
734753
}
735754
}
755+
756+
private boolean hasFormattingTable(Connection connection, String database) {
757+
try {
758+
if (isUsingSqlite) {
759+
PreparedStatement statement = connection.prepareStatement("SELECT name FROM sqlite_master WHERE type='table' AND name=?");
760+
statement.setString(1, "formatting");
761+
return statement.executeQuery().next();
762+
}
763+
return connection.getMetaData().getTables(database, null, "formatting", new String[]{"TABLE"}).next();
764+
} catch (SQLException e) {
765+
e.printStackTrace();
766+
}
767+
return false;
768+
}
736769
}

Spigot/src/main/java/me/innectic/permissify/spigot/PermissifyMain.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ private void registerCommands() {
121121
playerCommand = new PlayerCommand();
122122
formatCommand = new FormatCommand();
123123
cacheCommand = new CacheCommand();
124+
124125
getCommand("permissify").setExecutor(new PermissifyCommand());
125126
}
126127

127128
private void registerListeners() {
128129
PluginManager pluginManager = Bukkit.getPluginManager();
130+
129131
pluginManager.registerEvents(new PlayerJoin(), this);
130132
pluginManager.registerEvents(new PlayerChat(), this);
131133
pluginManager.registerEvents(new PreProcess(), this);

Spigot/src/main/java/me/innectic/permissify/spigot/utils/ConfigVerifier.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import me.innectic.permissify.api.database.ConnectionInformation;
2929
import me.innectic.permissify.api.database.handlers.FullHandler;
3030
import me.innectic.permissify.api.database.handlers.HandlerType;
31-
import me.innectic.permissify.api.database.handlers.MySQLHandler;
31+
import me.innectic.permissify.api.database.handlers.SQLHandler;
3232
import me.innectic.permissify.api.util.VerifyConfig;
3333

34+
import java.util.HashMap;
35+
import java.util.Map;
3436
import java.util.Optional;
3537

3638
/**
@@ -57,19 +59,32 @@ public Optional<FullHandler> verifyConnectionInformation() {
5759
if (plugin.getConfig().get("handleChat") == null) return Optional.empty();
5860

5961
Optional<ConnectionInformation> connectionInformation = Optional.empty();
60-
if (type.get().getHandler() == MySQLHandler.class) {
61-
if (plugin.getConfig().getString("connection.host") == null) return Optional.empty();
62-
if (plugin.getConfig().getString("connection.database") == null) return Optional.empty();
63-
if (plugin.getConfig().getString("connection.port") == null) return Optional.empty();
64-
if (plugin.getConfig().getString("connection.username") == null) return Optional.empty();
65-
if (plugin.getConfig().getString("connection.password") == null) return Optional.empty();
62+
if (type.get().getHandler() == SQLHandler.class) {
63+
if (type.get().getDisplayName().equalsIgnoreCase("mysql")) {
64+
if (plugin.getConfig().getString("connection.host") == null) return Optional.empty();
65+
if (plugin.getConfig().getString("connection.database") == null) return Optional.empty();
66+
if (plugin.getConfig().getString("connection.port") == null) return Optional.empty();
67+
if (plugin.getConfig().getString("connection.username") == null) return Optional.empty();
68+
if (plugin.getConfig().getString("connection.password") == null) return Optional.empty();
6669

67-
connectionInformation = Optional.of(new ConnectionInformation(
68-
plugin.getConfig().getString("connection.host"),
69-
plugin.getConfig().getString("connection.database"),
70-
plugin.getConfig().getInt("connection.port"),
71-
plugin.getConfig().getString("connection.username"),
72-
plugin.getConfig().getString("connection.password")));
70+
connectionInformation = Optional.of(new ConnectionInformation(
71+
plugin.getConfig().getString("connection.host"),
72+
plugin.getConfig().getString("connection.database"),
73+
plugin.getConfig().getInt("connection.port"),
74+
plugin.getConfig().getString("connection.username"),
75+
plugin.getConfig().getString("connection.password"),
76+
new HashMap<>())
77+
);
78+
} else if (type.get().getDisplayName().equalsIgnoreCase("sqlite")) {
79+
if (plugin.getConfig().getString("connection.file") == null) return Optional.empty();
80+
81+
Map<String, Object> sqliteMeta = new HashMap<>();
82+
sqliteMeta.put("file", plugin.getDataFolder() + "/" + plugin.getConfig().getString("connection.file") + ".db");
83+
Map<String, Object> meta = new HashMap<>();
84+
meta.put("sqlite", sqliteMeta);
85+
86+
connectionInformation = Optional.of(new ConnectionInformation("", "", 0, "", "", meta));
87+
}
7388
}
7489
return Optional.of(new FullHandler(type, connectionInformation));
7590
}

Spigot/src/main/resources/config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ connection:
55
port: 3306
66
database: permissify
77
username: permissify
8-
password: magicalpassword
8+
password: magicalpassword
9+
file: test.db

Sponge/src/main/java/me/innectic/permissify/sponge/config/PermissifyConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import ninja.leaping.configurate.loader.ConfigurationLoader;
99

1010
import java.io.IOException;
11+
import java.util.HashMap;
1112

1213
/**
1314
* @author Innectic
@@ -52,7 +53,7 @@ private void verifyConfiguration() {
5253
String password = checkNode(configuration.getNode("connection.password", ""), "", "The password for the database").getString();
5354
String database = checkNode(configuration.getNode("connection.database", ""), "", "The database to connect to").getString();
5455
int port = checkNode(configuration.getNode("connection.port", ""), "", "The port to connect to").getInt();
55-
connectionInformation = new ConnectionInformation(host, database, port, username, password);
56+
connectionInformation = new ConnectionInformation(host, database, port, username, password, new HashMap<>());
5657
});
5758
}
5859

0 commit comments

Comments
 (0)