From 8464909b6db4ae186c57279c173085f87d8a4240 Mon Sep 17 00:00:00 2001 From: ineanto Date: Fri, 14 Jul 2023 16:53:24 +0200 Subject: [PATCH] refactor(sql): delete --- docker-compose.yml | 8 +- .../atnrch/nicko/storage/sql/SQLStorage.java | 20 +++-- .../nicko/storage/sql/SQLStorageProvider.java | 2 +- .../nicko/test/storage/SQLStorageTest.java | 73 +++++++------------ 4 files changed, 46 insertions(+), 57 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 6bdf830..cc20182 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,11 +12,13 @@ services: ports: - "3306:3306" - adminer: - image: adminer + phpmyadmin: + image: phpmyadmin restart: no + links: + - db ports: - - "8080:8080" + - "8080:80" redis: image: redis diff --git a/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorage.java b/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorage.java index 27abb9a..0a30a51 100644 --- a/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorage.java +++ b/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorage.java @@ -44,6 +44,7 @@ public class SQLStorage extends Storage { final PreparedStatement statement = isStored(uuid) ? getUpdateStatement(connection, uuid, profile) : getInsertStatement(connection, uuid, profile); statement.executeUpdate(); + statement.close(); return ActionResult.ok(); } catch (SQLException e) { logger.warning("Couldn't send SQL Request: " + e.getMessage()); @@ -60,9 +61,10 @@ public class SQLStorage extends Storage { final String sql = "SELECT * FROM nicko.DATA WHERE uuid = ?"; final PreparedStatement statement = connection.prepareStatement(sql); - statement.setBinaryStream(1, uuidToBin(uuid)); + statement.setString(1, uuid.toString()); final ResultSet resultSet = statement.executeQuery(); + statement.close(); return resultSet.next(); } catch (SQLException e) { logger.warning("Couldn't check if data is present: " + e.getMessage()); @@ -79,7 +81,7 @@ public class SQLStorage extends Storage { final String sql = "SELECT * FROM nicko.DATA WHERE uuid = ?"; final PreparedStatement statement = connection.prepareStatement(sql); - statement.setBinaryStream(1, uuidToBin(uuid)); + statement.setString(1, uuid.toString()); final ResultSet resultSet = statement.executeQuery(); String name = ""; @@ -92,6 +94,7 @@ public class SQLStorage extends Storage { locale = resultSet.getString("locale"); bungeecord = resultSet.getBoolean("bungeecord"); } + statement.close(); final NickoProfile profile = new NickoProfile(name, skin, Locale.fromCode(locale), bungeecord); return Optional.of(profile); @@ -109,11 +112,12 @@ public class SQLStorage extends Storage { try { final String sql = "DELETE FROM nicko.DATA WHERE uuid = ?"; final PreparedStatement statement = connection.prepareStatement(sql); - statement.setBinaryStream(1, uuidToBin(uuid)); - statement.executeUpdate(); - return ActionResult.ok(); + statement.setString(1, uuid.toString()); + int rows = statement.executeUpdate(); + statement.close(); + return (rows == 1 ? ActionResult.ok() : ActionResult.error(I18NDict.Error.SQL_ERROR)); } catch (SQLException e) { - logger.warning("Couldn't fetch profile: " + e.getMessage()); + logger.warning("Couldn't delete profile: " + e.getMessage()); return ActionResult.error(I18NDict.Error.SQL_ERROR); } } @@ -121,7 +125,7 @@ public class SQLStorage extends Storage { private PreparedStatement getInsertStatement(Connection connection, UUID uuid, NickoProfile profile) throws SQLException { final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `bungeecord`) VALUES (?, ?, ?, ?, ?)"; final PreparedStatement statement = connection.prepareStatement(sql); - statement.setBinaryStream(1, uuidToBin(uuid)); + statement.setString(1, uuid.toString()); statement.setString(2, profile.getName() == null ? null : profile.getName()); statement.setString(3, profile.getSkin() == null ? null : profile.getSkin()); statement.setString(4, profile.getLocale().getCode()); @@ -136,7 +140,7 @@ public class SQLStorage extends Storage { statement.setString(2, profile.getSkin() == null ? null : profile.getSkin()); statement.setString(3, profile.getLocale().getCode()); statement.setBoolean(4, profile.isBungeecordTransfer()); - statement.setBinaryStream(5, uuidToBin(uuid)); + statement.setString(5, uuid.toString()); return statement; } diff --git a/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorageProvider.java b/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorageProvider.java index 7f0ed2d..b899526 100644 --- a/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorageProvider.java +++ b/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorageProvider.java @@ -59,7 +59,7 @@ public class SQLStorageProvider implements StorageProvider { final Connection connection = getConnection(); String query = "CREATE TABLE IF NOT EXISTS %s.DATA " + - "(uuid binary(16) NOT NULL," + + "(uuid varchar(36) NOT NULL," + "name varchar(16)," + "skin varchar(16)," + "locale char(2) NOT NULL," + diff --git a/src/test/java/xyz/atnrch/nicko/test/storage/SQLStorageTest.java b/src/test/java/xyz/atnrch/nicko/test/storage/SQLStorageTest.java index 1ddc95d..e92dbe2 100644 --- a/src/test/java/xyz/atnrch/nicko/test/storage/SQLStorageTest.java +++ b/src/test/java/xyz/atnrch/nicko/test/storage/SQLStorageTest.java @@ -10,11 +10,11 @@ import xyz.atnrch.nicko.config.DataSourceConfiguration; import xyz.atnrch.nicko.appearance.ActionResult; import xyz.atnrch.nicko.profile.NickoProfile; import xyz.atnrch.nicko.i18n.Locale; +import xyz.atnrch.nicko.storage.PlayerDataStore; import java.util.Optional; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class SQLStorageTest { @@ -35,39 +35,29 @@ public class SQLStorageTest { } @Test - @DisplayName("Create SQL Tables") + @DisplayName("Create tables") @Order(1) - public void createSQLTables() { + public void createTables() { assertFalse(plugin.getDataStore().getStorage().isError()); } @Test - @DisplayName("Store Player Via SQL") + @DisplayName("Store empty profile") @Order(2) - public void storePlayer() { + public void storeEmptyProfile() { final Optional optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); assertTrue(optionalProfile.isPresent()); } @Test - @DisplayName("Retrieve Player Via SQL") + @DisplayName("Update profile") @Order(3) - public void retrievePlayer() { - final Optional storeAction = plugin.getDataStore().getData(player.getUniqueId()); - assertTrue(storeAction.isPresent()); - } - - @Test - @DisplayName("Update Player Via SQL") - @Order(4) - public void updatePlayer() { + public void updateProfile() { final Optional optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); - assertTrue(optionalProfile.isPresent()); - final NickoProfile profile = optionalProfile.get(); - Assertions.assertNull(profile.getName()); - Assertions.assertNull(profile.getSkin()); - Assertions.assertEquals(profile.getLocale(), Locale.ENGLISH); + assertNull(profile.getName()); + assertNull(profile.getSkin()); + assertEquals(profile.getLocale(), Locale.ENGLISH); assertTrue(profile.isBungeecordTransfer()); profile.setName("Notch"); @@ -77,36 +67,29 @@ public class SQLStorageTest { final ActionResult result = plugin.getDataStore().saveData(player); assertFalse(result.isError()); + } - final Optional optionalUpdatedProfile = plugin.getDataStore().getData(player.getUniqueId()); - assertTrue(optionalUpdatedProfile.isPresent()); - final NickoProfile updatedProfile = optionalProfile.get(); - Assertions.assertEquals(updatedProfile.getName(), "Notch"); - Assertions.assertEquals(updatedProfile.getSkin(), "Notch"); - Assertions.assertEquals(updatedProfile.getLocale(), Locale.FRENCH); + @Test + @DisplayName("Get updated profile") + @Order(4) + public void hasProfileBeenUpdated() { + final Optional profile = plugin.getDataStore().getData(player.getUniqueId()); + assertTrue(profile.isPresent()); + + final NickoProfile updatedProfile = profile.get(); + assertEquals(updatedProfile.getName(), "Notch"); + assertEquals(updatedProfile.getSkin(), "Notch"); + assertEquals(updatedProfile.getLocale(), Locale.FRENCH); assertFalse(updatedProfile.isBungeecordTransfer()); } @Test - @DisplayName("Remove Player Disguise Via SQL") + @DisplayName("Delete profile") @Order(5) - public void removePlayerDisguise() { - final Optional optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); - assertTrue(optionalProfile.isPresent()); - - final NickoProfile profile = optionalProfile.get(); - - profile.setName(null); - profile.setSkin(null); - - final ActionResult result = plugin.getDataStore().saveData(player); - assertFalse(result.isError()); - - final Optional optionalUpdatedProfile = plugin.getDataStore().getData(player.getUniqueId()); - assertTrue(optionalUpdatedProfile.isPresent()); - final NickoProfile updatedProfile = optionalProfile.get(); - Assertions.assertNull(updatedProfile.getName()); - Assertions.assertNull(updatedProfile.getSkin()); + public void deleteProfile() { + final PlayerDataStore dataStore = plugin.getDataStore(); + final ActionResult sqlDelete = dataStore.getStorage().delete(player.getUniqueId()); + assertFalse(sqlDelete.isError()); } @AfterAll