diff --git a/src/main/java/de/butzlabben/WorldSystem/data/AbstractSqlLiteDatabase.java b/src/main/java/de/butzlabben/WorldSystem/data/AbstractSqlLiteDatabase.java deleted file mode 100644 index 93b33ac..0000000 --- a/src/main/java/de/butzlabben/WorldSystem/data/AbstractSqlLiteDatabase.java +++ /dev/null @@ -1,36 +0,0 @@ -package de.butzlabben.WorldSystem.data; - -import de.butzlabben.world.config.PluginConfig; - -import java.sql.*; - -public abstract class AbstractSqlLiteDatabase { - - private Connection connection; - private Statement state; - - public AbstractSqlLiteDatabase(String db_path) throws SQLException { - this.connection = DriverManager.getConnection("jdbc:sqlite:" + db_path); - this.state = this.connection.createStatement(); - this.construct_database(); - } - - protected void void_query(String sql) throws SQLException { - this.state.executeUpdate(sql); - } - - protected ResultSet query(String sql) throws SQLException { - return this.state.executeQuery(sql); - } - - protected abstract void construct_database(); - - public void close() { - try { - this.state.close(); - this.connection.close(); - } catch (SQLException e) { - - } - } -} diff --git a/src/main/java/de/butzlabben/WorldSystem/data/SqlLiteDatabase.java b/src/main/java/de/butzlabben/WorldSystem/data/SqlLiteDatabase.java new file mode 100644 index 0000000..81e25ea --- /dev/null +++ b/src/main/java/de/butzlabben/WorldSystem/data/SqlLiteDatabase.java @@ -0,0 +1,68 @@ +package de.butzlabben.WorldSystem.data; + +import de.butzlabben.world.WorldSystem; +import lombok.SneakyThrows; + +import java.sql.*; +import java.util.logging.Level; + +public class SqlLiteDatabase { + + private static final String DATABASE_FILE = "plugins/WorldSystem/data.db"; + private static final String WS_WORLDS = "ws_worlds"; + + private Connection connection; + private Statement state; + + private static SqlLiteDatabase database; + + SqlLiteDatabase(String db_path) throws SQLException { + this.connection = DriverManager.getConnection("jdbc:sqlite:" + db_path); + this.state = this.connection.createStatement(); + this.construct_database(); + } + + @SneakyThrows + public static SqlLiteDatabase connect() { + try { + if (database == null) { + database = new SqlLiteDatabase(DATABASE_FILE); + } + return database; + } catch (SQLException e) { + + WorldSystem.disable_plugin(); + return new SqlLiteDatabase(DATABASE_FILE); //doesn't create + } + } + + + + protected void void_query(String sql) throws SQLException { + this.state.executeUpdate(sql); + } + + protected ResultSet query(String sql) throws SQLException { + return this.state.executeQuery(sql); + } + + protected void construct_database() { + try { + //create the worlds table + this.void_query(String.format("CREATE TABLE IF NOT EXISTS %s (world_id INTEGER PRIMARY KEY, player_uuid varchar(40), player_name varchar(20), last_loaded long);", WS_WORLDS)); + } catch (SQLException e) { + WorldSystem.logger().log(Level.SEVERE, "[WorldSystem] Unable To create the SQLite Database"); + WorldSystem.logger().log(Level.SEVERE, e.getMessage()); + WorldSystem.disable_plugin(); + } + } + + public void close() { + try { + this.state.close(); + this.connection.close(); + } catch (SQLException e) { + + } + } +} diff --git a/src/main/java/de/butzlabben/WorldSystem/data/WorldSystemData.java b/src/main/java/de/butzlabben/WorldSystem/data/WorldSystemData.java index 41a33c9..66d63b2 100644 --- a/src/main/java/de/butzlabben/WorldSystem/data/WorldSystemData.java +++ b/src/main/java/de/butzlabben/WorldSystem/data/WorldSystemData.java @@ -1,57 +1,38 @@ package de.butzlabben.WorldSystem.data; import de.butzlabben.world.WorldSystem; -import de.butzlabben.world.config.PluginConfig; import lombok.SneakyThrows; -import org.bukkit.Bukkit; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.UUID; import java.util.logging.Level; -public class WorldSystemData extends AbstractSqlLiteDatabase { +public class WorldSystemData { private static final String WS_WORLDS = "ws_worlds"; - private static final String DATABASE_FILE = "plugins/WorldSystem/data.db"; private static WorldSystemData con; + private static SqlLiteDatabase core; - private WorldSystemData() throws SQLException { - super(DATABASE_FILE); //PluginConfig.getSqliteFile() - } + private WorldSystemData() { + core = SqlLiteDatabase.connect(); + } @SneakyThrows public static WorldSystemData connect() { - try { if (con == null) { con = new WorldSystemData(); } return con; - } catch (SQLException e) { - WorldSystem.disable_plugin(); - return new WorldSystemData(); //doesn't create - } } - @Override - protected void construct_database() { - try { - //create the worlds table - this.void_query(String.format("CREATE TABLE IF NOT EXISTS %s (world_id INTEGER PRIMARY KEY, player_uuid varchar(40), player_name varchar(20), last_loaded long);", WS_WORLDS)); - } catch (SQLException e) { - WorldSystem.logger().log(Level.SEVERE, "[WorldSystem] Unable To create the SQLite Database"); - WorldSystem.logger().log(Level.SEVERE, e.getMessage()); - WorldSystem.disable_plugin(); - } - } public int getHighestID() { try { - ResultSet res = this.query(String.format("SELECT count(*) FROM %s", WS_WORLDS)); + ResultSet res = core.query(String.format("SELECT count(*) FROM %s", WS_WORLDS)); res.next(); int count = res.getInt("count(*)"); @@ -66,7 +47,7 @@ public class WorldSystemData extends AbstractSqlLiteDatabase { try { List worlds = new ArrayList(); - ResultSet res = this.query(String.format("SELECT * FROM WS_WORLDS;", WS_WORLDS)); + ResultSet res = core.query(String.format("SELECT * FROM WS_WORLDS;", WS_WORLDS)); while (res.next()) { int world_id = res.getInt("world_id"); @@ -88,7 +69,7 @@ public class WorldSystemData extends AbstractSqlLiteDatabase { public void update_name(String uuid, String new_name) { try { - this.void_query(String.format("UPDATE %s SET player_name = '%s' WHERE player_uuid = '%s;'", WS_WORLDS, new_name, uuid)); + core.void_query(String.format("UPDATE %s SET player_name = '%s' WHERE player_uuid = '%s;'", WS_WORLDS, new_name, uuid)); } catch (SQLException e) { WorldSystem.logger().log(Level.SEVERE, "Tried to update player name but failed skipping"); WorldSystem.logger().log(Level.SEVERE, e.getMessage()); @@ -97,7 +78,7 @@ public class WorldSystemData extends AbstractSqlLiteDatabase { public void create_new_world_record(PlayerWorld world) { try { - this.void_query(String.format("INSERT INTO %s (world_id, player_uuid, player_name, last_loaded) VALUES (%d, '%s', '%s', %d);", + core.void_query(String.format("INSERT INTO %s (world_id, player_uuid, player_name, last_loaded) VALUES (%d, '%s', '%s', %d);", WS_WORLDS, world.getWorld_id(), world.getPlayer_uuid(), world.getPlayer_name(), world.getLast_loaded())); } catch (SQLException e) { WorldSystem.logger().log(Level.SEVERE, "File to Insert to world for player name but failed Disabling"); @@ -108,7 +89,7 @@ public class WorldSystemData extends AbstractSqlLiteDatabase { public int getWorldCountForPlayer(String uuid) { try { - ResultSet res = this.query(String.format("SELECT count(*) FROM %s WHERE player_uuid = '%s';", WS_WORLDS, uuid)); + ResultSet res = core.query(String.format("SELECT count(*) FROM %s WHERE player_uuid = '%s';", WS_WORLDS, uuid)); res.next(); int count = res.getInt("count(*)"); return count; @@ -123,7 +104,7 @@ public class WorldSystemData extends AbstractSqlLiteDatabase { try { List worlds = new ArrayList(); - ResultSet res = this.query(String.format("SELECT * FROM %s WHERE player_uuid = '%s';", WS_WORLDS, uuid)); + ResultSet res = core.query(String.format("SELECT * FROM %s WHERE player_uuid = '%s';", WS_WORLDS, uuid)); while (res.next()) { int world_id = res.getInt("world_id"); @@ -144,11 +125,15 @@ public class WorldSystemData extends AbstractSqlLiteDatabase { public void update_last_loaded(String uuid, long new_time) { try { - this.void_query(String.format("UPDATE %s SET last_loaded = %d WHERE player_uuid = '%s';", WS_WORLDS, new_time, uuid)); + core.void_query(String.format("UPDATE %s SET last_loaded = %d WHERE player_uuid = '%s';", WS_WORLDS, new_time, uuid)); } catch (SQLException e) { WorldSystem.logger().log(Level.SEVERE, "[WorldSystem] Tried to update player name but failed skipping"); } } + public void close() { + core.close(); + } + } diff --git a/src/test/java/de/butzlabben/WorldSystem/data/MockDatabase.java b/src/test/java/de/butzlabben/WorldSystem/data/MockDatabase.java deleted file mode 100644 index 16ebd8c..0000000 --- a/src/test/java/de/butzlabben/WorldSystem/data/MockDatabase.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.butzlabben.WorldSystem.data; - -import java.sql.ResultSet; -import java.sql.SQLException; - -public class MockDatabase extends AbstractSqlLiteDatabase { - - public MockDatabase() throws SQLException { - super("./mock.db"); - } - - @Override - protected void construct_database() { - try { - this.query("CREATE TABLE IF NOT EXISTS RealTable (id INTEGER PRIMARY KEY, quantity INTEGER);").close(); - } catch (SQLException e) { - // - } - } - - public boolean does_table_exist(String Table_Name) throws SQLException { - ResultSet res = this.query(String.format("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '%s';", Table_Name)); - res.next(); - int count = res.getInt("count(*)"); - System.out.println(count); - return (count == 1); - } -} diff --git a/src/test/java/de/butzlabben/WorldSystem/data/TestAbstractSqlLiteDatabase.java b/src/test/java/de/butzlabben/WorldSystem/data/TestAbstractSqlLiteDatabase.java deleted file mode 100644 index 07d289a..0000000 --- a/src/test/java/de/butzlabben/WorldSystem/data/TestAbstractSqlLiteDatabase.java +++ /dev/null @@ -1,23 +0,0 @@ -package de.butzlabben.WorldSystem.data; - -import org.junit.jupiter.api.Test; - -import java.sql.SQLException; - -import static org.junit.jupiter.api.Assertions.*; - -public class TestAbstractSqlLiteDatabase { - - @Test - public void testContructDatabase() throws SQLException { - MockDatabase db = new MockDatabase(); - assertTrue(db.does_table_exist("RealTable")); - db.close(); - } - @Test - public void testNonExistantTable() throws SQLException { - MockDatabase db = new MockDatabase(); - assertFalse(db.does_table_exist("NotRealTable")); - db.close(); - } -}