Skip to content

Commit

Permalink
Create tables on init
Browse files Browse the repository at this point in the history
  • Loading branch information
dig committed Jul 28, 2020
1 parent 3ec7391 commit 7c7f8a3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 = ?";

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -78,4 +105,13 @@ public Optional<Vault> 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);
}
}
}

0 comments on commit 7c7f8a3

Please sign in to comment.