Skip to content

Commit e79b7c8

Browse files
committed
Add DDL script (db init)
1 parent b1c297c commit e79b7c8

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/main/java/pro/cloudnode/smp/cloudnodemsg/CloudnodeMSG.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.bukkit.metadata.MetadataValue;
77
import org.bukkit.plugin.java.JavaPlugin;
88
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
910
import pro.cloudnode.smp.cloudnodemsg.command.IgnoreCommand;
1011
import pro.cloudnode.smp.cloudnodemsg.command.MainCommand;
1112
import pro.cloudnode.smp.cloudnodemsg.command.MessageCommand;
@@ -14,8 +15,13 @@
1415
import pro.cloudnode.smp.cloudnodemsg.command.UnIgnoreCommand;
1516
import pro.cloudnode.smp.cloudnodemsg.listener.AsyncChatListener;
1617

18+
import java.io.InputStream;
19+
import java.sql.PreparedStatement;
20+
import java.sql.SQLException;
21+
import java.util.Arrays;
1722
import java.util.Map;
1823
import java.util.Objects;
24+
import java.util.logging.Level;
1925

2026
public final class CloudnodeMSG extends JavaPlugin {
2127
public static @NotNull CloudnodeMSG getInstance() {
@@ -26,6 +32,7 @@ public void reload() {
2632
getInstance().reloadConfig();
2733
getInstance().config.config = getInstance().getConfig();
2834
setupDbSource();
35+
runDDL();
2936
}
3037

3138
@Override
@@ -77,4 +84,34 @@ private void setupDbSource() {
7784

7885
dbSource = new HikariDataSource(hikariConfig);
7986
}
87+
88+
/**
89+
* Run DDL script
90+
*/
91+
public void runDDL() {
92+
final @NotNull String file = "ddl/sqlite.sql";
93+
final @NotNull String @NotNull [] queries;
94+
try (final @Nullable InputStream inputStream = getClassLoader().getResourceAsStream(file)) {
95+
queries = Arrays.stream(
96+
new String(Objects.requireNonNull(inputStream).readAllBytes()).split(";")
97+
).map(q -> q.stripTrailing().stripIndent().replaceAll("^\\s+(?:--.+)*", "")).toArray(String[]::new);
98+
}
99+
catch (final @NotNull Exception exception) {
100+
getLogger().log(Level.SEVERE, "Could not read DDL script: " + file, exception);
101+
getServer().getPluginManager().disablePlugin(this);
102+
return;
103+
}
104+
for (final @NotNull String query : queries) {
105+
if (query.isBlank()) continue;
106+
try (final @NotNull PreparedStatement stmt = db().getConnection().prepareStatement(query)) {
107+
stmt.execute();
108+
}
109+
catch (final @NotNull SQLException exception) {
110+
getLogger().log(Level.SEVERE, "Could not execute DDL query: " + query, exception);
111+
getServer().getPluginManager().disablePlugin(this);
112+
return;
113+
}
114+
}
115+
getLogger().info("Database successfully initialised with DDL");
116+
}
80117
}

src/main/resources/ddl/sqlite.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE IF NOT EXISTS `cloudnodemsg_mail`
2+
(
3+
`id` CHAR(32) COLLATE NOCASE PRIMARY KEY,
4+
`sender` CHAR(32) COLLATE NOCASE NOT NULL,
5+
`recipient` CHAR(32) COLLATE NOCASE NOT NULL,
6+
`message` TEXT NOT NULL,
7+
`seen` BOOLEAN DEFAULT 0,
8+
`starred` BOOLEAN DEFAULT 0
9+
);

0 commit comments

Comments
 (0)