Skip to content

Commit

Permalink
Finish SQL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
dig committed Jul 29, 2020
1 parent 7c7f8a3 commit 3fa8145
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

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_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 , PRIMARY KEY (`id`, `owner_uuid`) )";
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 , PRIMARY KEY (`id`, `owner_uuid`, `name`) )";

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 = ?";
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` = ?";
public static final String SQL_INSERT_VAULT = "INSERT INTO `%s`(`id`, `owner_uuid`, `size`, `contents`) VALUES (?, ?, ?, ?)";
public static final String SQL_UPDATE_VAULT_BY_ID_AND_OWNER = "UPDATE `%s` SET `size` = ?, `contents` = ? WHERE `id` = ? AND `owner_uuid` = ?";

public static final String SQL_SELECT_VAULT_METADATA_BY_ID_AND_OWNER = "SELECT * FROM `%s` WHERE `id` = ? AND `owner_uuid` = ?";
public static final String SQL_SELECT_VAULT_METADATA_BY_ID_AND_OWNER_AND_KEY = "SELECT * FROM `%s` WHERE `id` = ? AND `owner_uuid` = ? AND `name` = ?";
public static final String SQL_INSERT_VAULT_METADATA = "INSERT INTO `%s`(`id`, `owner_uuid`, `name`, `value`) VALUES (?, ?, ?, ?)";
public static final String SQL_UPDATE_VAULT_METADATA_BY_ID_AND_OWNER_AND_KEY = "UPDATE `%s` SET `value` = ? WHERE `id` = ? AND `owner_uuid` = ? AND `name` = ?";

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.github.dig.endervaults.bukkit.storage;

import com.github.dig.endervaults.api.PluginProvider;
import com.github.dig.endervaults.api.lang.Lang;
import com.github.dig.endervaults.api.storage.DataStorage;
import com.github.dig.endervaults.api.util.VaultSerializable;
import com.github.dig.endervaults.api.vault.Vault;
import com.github.dig.endervaults.api.vault.metadata.MetadataConverter;
import com.github.dig.endervaults.api.vault.metadata.VaultMetadataRegistry;
import com.github.dig.endervaults.bukkit.EVBukkitPlugin;
import com.github.dig.endervaults.bukkit.vault.BukkitVault;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariPool;
Expand All @@ -15,9 +20,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.logging.Level;

@Log
Expand Down Expand Up @@ -75,7 +78,6 @@ public void close() {
@Override
public boolean exists(UUID ownerUUID, UUID id) {
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());
Expand All @@ -87,23 +89,44 @@ public boolean exists(UUID ownerUUID, UUID id) {
log.log(Level.SEVERE, "[EnderVaults] Error while executing query.", ex);
return false;
}

return has;
}

@Override
public List<Vault> load(UUID ownerUUID) {
return null;
return get(ownerUUID);
}

@Override
public Optional<Vault> load(UUID ownerUUID, UUID id) {
return Optional.empty();
return get(id, ownerUUID);
}

@Override
public void save(Vault vault) {

VaultMetadataRegistry metadataRegistry = plugin.getMetadataRegistry();
if (exists(vault.getOwner(), vault.getId())) {
update(vault.getId(), vault.getOwner(), vault.getSize(), ((VaultSerializable) vault).encode());
for (String key : vault.getMetadata().keySet()) {
Object value = vault.getMetadata().get(key);
metadataRegistry.get(key)
.ifPresent(converter -> {
if (exists(vault.getId(), vault.getOwner(), key)) {
update(vault.getId(), vault.getOwner(), key, converter.from(value));
} else {
insert(vault.getId(), vault.getOwner(), key, converter.from(value));
}
});
}
} else {
String contents = ((VaultSerializable) vault).encode();
insert(vault.getId(), vault.getOwner(), vault.getSize(), contents);
for (String key : vault.getMetadata().keySet()) {
Object value = vault.getMetadata().get(key);
metadataRegistry.get(key)
.ifPresent(converter -> insert(vault.getId(), vault.getOwner(), key, converter.from(value)));
}
}
}

private void createTableIfNotExist(String table, String TABLE_SQL) {
Expand All @@ -114,4 +137,149 @@ private void createTableIfNotExist(String table, String TABLE_SQL) {
log.log(Level.SEVERE, "[EnderVaults] Unable to create table " + table + ".", ex);
}
}

private Vault create(UUID id, UUID ownerUUID, int size, String contents) {
Map<String, Object> metadata = getVaultMetadata(ownerUUID, id);
String title = plugin.getLanguage().get(Lang.VAULT_TITLE, metadata);
BukkitVault vault = new BukkitVault(id, title, size, ownerUUID, metadata);

VaultSerializable serializable = vault;
serializable.decode(contents);
return vault;
}

private Map<String, Object> getVaultMetadata(UUID ownerUUID, UUID id) {
VaultMetadataRegistry metadataRegistry = plugin.getMetadataRegistry();

Map<String, Object> metadata = new HashMap<>();
String sql = String.format(DatabaseConstants.SQL_SELECT_VAULT_METADATA_BY_ID_AND_OWNER, metadataTable);
try (Connection conn = hikariDataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, id.toString());
stmt.setString(2, ownerUUID.toString());

ResultSet rs = stmt.executeQuery();
if (rs.next()) {
String key = rs.getString("name");
Optional<MetadataConverter> converterOptional = metadataRegistry.get(key);
if (converterOptional.isPresent()) {
MetadataConverter converter = converterOptional.get();
metadata.put(key, converter.to(rs.getString("value")));
}
}
} catch (SQLException ex) {
log.log(Level.SEVERE, "[EnderVaults] Error while executing query.", ex);
}

return metadata;
}

private void insert(UUID id, UUID ownerUUID, int size, String contents) {
String sql = String.format(DatabaseConstants.SQL_INSERT_VAULT, vaultTable);
try (Connection conn = hikariDataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, id.toString());
stmt.setString(2, ownerUUID.toString());
stmt.setInt(3, size);
stmt.setString(4, contents);
stmt.executeUpdate();
} catch (SQLException ex) {
log.log(Level.SEVERE, "[EnderVaults] Error while executing query.", ex);
}
}

private void insert(UUID id, UUID ownerUUID, String key, String value) {
String sql = String.format(DatabaseConstants.SQL_INSERT_VAULT_METADATA, metadataTable);
try (Connection conn = hikariDataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, id.toString());
stmt.setString(2, ownerUUID.toString());
stmt.setString(3, key);
stmt.setString(4, value);
stmt.executeUpdate();
} catch (SQLException ex) {
log.log(Level.SEVERE, "[EnderVaults] Error while executing query.", ex);
}
}

private void update(UUID id, UUID ownerUUID, int size, String contents) {
String sql = String.format(DatabaseConstants.SQL_UPDATE_VAULT_BY_ID_AND_OWNER, vaultTable);
try (Connection conn = hikariDataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, size);
stmt.setString(2, contents);
stmt.setString(3, id.toString());
stmt.setString(4, ownerUUID.toString());
stmt.executeUpdate();
} catch (SQLException ex) {
log.log(Level.SEVERE, "[EnderVaults] Error while executing query.", ex);
}
}

private void update(UUID id, UUID ownerUUID, String key, String value) {
String sql = String.format(DatabaseConstants.SQL_UPDATE_VAULT_METADATA_BY_ID_AND_OWNER_AND_KEY, metadataTable);
try (Connection conn = hikariDataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, value);
stmt.setString(2, id.toString());
stmt.setString(3, ownerUUID.toString());
stmt.setString(4, key);
stmt.executeUpdate();
} catch (SQLException ex) {
log.log(Level.SEVERE, "[EnderVaults] Error while executing query.", ex);
}
}

private Optional<Vault> get(UUID id, UUID ownerUUID) {
int size;
String contents;
String sql = String.format(DatabaseConstants.SQL_SELECT_VAULT_BY_ID_AND_OWNER, vaultTable);
try (Connection conn = hikariDataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, id.toString());
stmt.setString(2, ownerUUID.toString());

ResultSet rs = stmt.executeQuery();
if (rs.next()) {
size = rs.getInt("size");
contents = rs.getString("contents");
} else {
return Optional.empty();
}
} catch (SQLException ex) {
log.log(Level.SEVERE, "[EnderVaults] Error while executing query.", ex);
return Optional.empty();
}
return Optional.ofNullable(create(id, ownerUUID, size, contents));
}

private List<Vault> get(UUID ownerUUID) {
List<Vault> vaults = new ArrayList<>();
String sql = String.format(DatabaseConstants.SQL_SELECT_VAULT_BY_OWNER, vaultTable);
try (Connection conn = hikariDataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, ownerUUID.toString());

ResultSet rs = stmt.executeQuery();
while (rs.next()) {
UUID id = UUID.fromString(rs.getString("id"));
int size = rs.getInt("size");
String contents = rs.getString("contents");
vaults.add(create(id, ownerUUID, size, contents));
}
} catch (SQLException ex) {
log.log(Level.SEVERE, "[EnderVaults] Error while executing query.", ex);
}
return vaults;
}

private boolean exists(UUID id, UUID ownerUUID, String key) {
String sql = String.format(DatabaseConstants.SQL_SELECT_VAULT_METADATA_BY_ID_AND_OWNER_AND_KEY, metadataTable);
boolean has;
try (Connection conn = hikariDataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, id.toString());
stmt.setString(2, ownerUUID.toString());
stmt.setString(3, key);

ResultSet rs = stmt.executeQuery();
has = rs.next();
} catch (SQLException ex) {
log.log(Level.SEVERE, "[EnderVaults] Error while executing query.", ex);
return false;
}
return has;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public List<Vault> load(UUID ownerUUID) {
@Override
public Optional<Vault> load(UUID ownerUUID, UUID id) {
if (!exists(ownerUUID, id)) return Optional.empty();

VaultMetadataRegistry metadataRegistry = plugin.getMetadataRegistry();
FileConfiguration configuration = YamlConfiguration.loadConfiguration(getVaultFile(ownerUUID, id));

Expand Down

0 comments on commit 3fa8145

Please sign in to comment.