From 7c7f8a3e2fec249d50c13314b42e599ab5ded3cc Mon Sep 17 00:00:00 2001 From: Joseph Date: Wed, 29 Jul 2020 00:00:14 +0100 Subject: [PATCH] Create tables on init --- .../api/storage/mysql/MySQLConstants.java | 10 ----- .../bukkit/storage/DatabaseConstants.java | 11 +++++ .../bukkit/storage/HikariMySQLStorage.java | 40 ++++++++++++++++++- 3 files changed, 49 insertions(+), 12 deletions(-) delete mode 100644 api/src/main/java/com/github/dig/endervaults/api/storage/mysql/MySQLConstants.java create mode 100644 bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/DatabaseConstants.java diff --git a/api/src/main/java/com/github/dig/endervaults/api/storage/mysql/MySQLConstants.java b/api/src/main/java/com/github/dig/endervaults/api/storage/mysql/MySQLConstants.java deleted file mode 100644 index 7328856..0000000 --- a/api/src/main/java/com/github/dig/endervaults/api/storage/mysql/MySQLConstants.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.dig.endervaults.api.storage.mysql; - -public class MySQLConstants { - - private static final String CREATE_VAULT_TABLE = ""; - - - private static final String QUERY_SELECT_VAULT = "SELECT "; - -} diff --git a/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/DatabaseConstants.java b/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/DatabaseConstants.java new file mode 100644 index 0000000..2385cab --- /dev/null +++ b/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/DatabaseConstants.java @@ -0,0 +1,11 @@ +package com.github.dig.endervaults.bukkit.storage; + +public class DatabaseConstants { + + public static final String SQL_CREATE_TABLE_VAULT = "CREATE TABLE IF NOT EXISTS `%s` ( `id` VARCHAR(36) NOT NULL , `owner_uuid` VARCHAR(36) NOT NULL , `size` INT(8) NOT NULL , `contents` TEXT NOT NULL )"; + public static final String SQL_CREATE_TABLE_VAULT_METADATA = "CREATE TABLE IF NOT EXISTS `%s` ( `id` VARCHAR(36) NOT NULL , `owner_uuid` VARCHAR(36) NOT NULL , `name` VARCHAR(16) NOT NULL , `value` TEXT NOT NULL )"; + + public static final String SQL_SELECT_VAULT_BY_ID_AND_OWNER = "SELECT * FROM `%s` WHERE id = ? AND owner_uuid = ?"; + public static final String SQL_SELECT_VAULT_BY_OWNER = "SELECT * FROM `%s` WHERE owner_uuid = ?"; + +} diff --git a/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/HikariMySQLStorage.java b/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/HikariMySQLStorage.java index 6787bac..a14d782 100644 --- a/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/HikariMySQLStorage.java +++ b/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/HikariMySQLStorage.java @@ -11,6 +11,10 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -20,7 +24,10 @@ public class HikariMySQLStorage implements DataStorage { private final EVBukkitPlugin plugin = (EVBukkitPlugin) PluginProvider.getPlugin(); + private HikariDataSource hikariDataSource; + private String vaultTable; + private String metadataTable; @Override public boolean init() { @@ -45,11 +52,17 @@ public boolean init() { try { hikariDataSource = new HikariDataSource(hikariConfig); - return hikariDataSource.isRunning(); } catch (HikariPool.PoolInitializationException e) { log.log(Level.SEVERE, "[EnderVaults] Unable to connect to database.", e); return false; } + + vaultTable = settings.getString("tables.vault"); + metadataTable = settings.getString("tables.vault-metadata"); + + createTableIfNotExist(vaultTable, DatabaseConstants.SQL_CREATE_TABLE_VAULT); + createTableIfNotExist(metadataTable, DatabaseConstants.SQL_CREATE_TABLE_VAULT_METADATA); + return hikariDataSource.isRunning(); } @Override @@ -61,7 +74,21 @@ public void close() { @Override public boolean exists(UUID ownerUUID, UUID id) { - return false; + String sql = String.format(DatabaseConstants.SQL_SELECT_VAULT_BY_ID_AND_OWNER, vaultTable); + + boolean has; + try (Connection conn = hikariDataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, id.toString()); + stmt.setString(2, ownerUUID.toString()); + + ResultSet rs = stmt.executeQuery(); + has = rs.next(); + } catch (SQLException ex) { + log.log(Level.SEVERE, "[EnderVaults] Error while executing query.", ex); + return false; + } + + return has; } @Override @@ -78,4 +105,13 @@ public Optional load(UUID ownerUUID, UUID id) { public void save(Vault vault) { } + + private void createTableIfNotExist(String table, String TABLE_SQL) { + TABLE_SQL = String.format(TABLE_SQL, table); + try (Connection conn = hikariDataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(TABLE_SQL)) { + stmt.executeUpdate(); + } catch (SQLException ex) { + log.log(Level.SEVERE, "[EnderVaults] Unable to create table " + table + ".", ex); + } + } }