66import org .bukkit .metadata .MetadataValue ;
77import org .bukkit .plugin .java .JavaPlugin ;
88import org .jetbrains .annotations .NotNull ;
9+ import org .jetbrains .annotations .Nullable ;
910import pro .cloudnode .smp .cloudnodemsg .command .IgnoreCommand ;
1011import pro .cloudnode .smp .cloudnodemsg .command .MainCommand ;
1112import pro .cloudnode .smp .cloudnodemsg .command .MessageCommand ;
1415import pro .cloudnode .smp .cloudnodemsg .command .UnIgnoreCommand ;
1516import 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 ;
1722import java .util .Map ;
1823import java .util .Objects ;
24+ import java .util .logging .Level ;
1925
2026public 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}
0 commit comments