From 3a7da4b3d695835c4e9e5042bf158e6d07bccab1 Mon Sep 17 00:00:00 2001 From: ineanto Date: Fri, 14 Jul 2023 22:54:01 +0200 Subject: [PATCH] sql: delete is not deleting wtf --- docker-compose.yml | 8 +++++++- .../xyz/atnrch/nicko/storage/sql/SQLStorage.java | 11 ++--------- .../nicko/storage/sql/SQLStorageProvider.java | 5 ++--- .../atnrch/nicko/test/storage/SQLStorageTest.java | 13 +++++++------ 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index cc20182..28db8fc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,10 +5,13 @@ version: '3.1' services: db: - image: mariadb + image: mariadb:latest restart: no environment: + MARIADB_DB: db MARIADB_ROOT_PASSWORD: 12345 + volumes: + - mysql:/var/lib/mysql ports: - "3306:3306" @@ -25,3 +28,6 @@ services: restart: no ports: - "6379:6379" + +volumes: + mysql: \ No newline at end of file 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 0a30a51..b26fd76 100644 --- a/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorage.java +++ b/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorage.java @@ -9,10 +9,7 @@ import xyz.atnrch.nicko.storage.Storage; import java.io.ByteArrayInputStream; import java.nio.ByteBuffer; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.Optional; import java.util.UUID; import java.util.logging.Logger; @@ -44,7 +41,6 @@ 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()); @@ -64,7 +60,6 @@ public class SQLStorage extends Storage { 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()); @@ -94,7 +89,6 @@ 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); @@ -110,11 +104,10 @@ public class SQLStorage extends Storage { if (connection == null) return ActionResult.error(I18NDict.Error.SQL_ERROR); try { - final String sql = "DELETE FROM nicko.DATA WHERE uuid = ?"; + final String sql = "DELETE FROM nicko.DATA WHERE uuid = ? LIMIT 1"; final PreparedStatement statement = connection.prepareStatement(sql); 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 delete profile: " + e.getMessage()); 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 b899526..b01a3ec 100644 --- a/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorageProvider.java +++ b/src/main/java/xyz/atnrch/nicko/storage/sql/SQLStorageProvider.java @@ -1,9 +1,9 @@ package xyz.atnrch.nicko.storage.sql; +import org.mariadb.jdbc.MariaDbDataSource; import xyz.atnrch.nicko.config.Configuration; import xyz.atnrch.nicko.config.DataSourceConfiguration; import xyz.atnrch.nicko.storage.StorageProvider; -import org.mariadb.jdbc.MariaDbDataSource; import java.sql.Connection; import java.sql.PreparedStatement; @@ -31,6 +31,7 @@ public class SQLStorageProvider implements StorageProvider { dataSource.setUser(sqlConfiguration.getUsername()); dataSource.setPassword(sqlConfiguration.getPassword()); connection = dataSource.getConnection(); + connection.setAutoCommit(true); final boolean initialized = connection != null && !connection.isClosed(); if (!initialized) return false; @@ -69,7 +70,6 @@ public class SQLStorageProvider implements StorageProvider { final PreparedStatement statement = connection.prepareStatement(query); statement.executeUpdate(); - statement.close(); } private void createDatabase() throws SQLException { @@ -80,7 +80,6 @@ public class SQLStorageProvider implements StorageProvider { final PreparedStatement statement = connection.prepareStatement(query); statement.executeUpdate(); - statement.close(); } public Connection getConnection() { 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 e92dbe2..1493c60 100644 --- a/src/test/java/xyz/atnrch/nicko/test/storage/SQLStorageTest.java +++ b/src/test/java/xyz/atnrch/nicko/test/storage/SQLStorageTest.java @@ -21,6 +21,7 @@ public class SQLStorageTest { private static ServerMock server; private static NickoBukkit plugin; private static PlayerMock player; + private static PlayerDataStore dataStore; @BeforeAll public static void setup() { @@ -31,6 +32,7 @@ public class SQLStorageTest { false); server = MockBukkit.mock(); plugin = MockBukkit.load(NickoBukkit.class, config); + dataStore = plugin.getDataStore(); player = server.addPlayer(); } @@ -38,14 +40,14 @@ public class SQLStorageTest { @DisplayName("Create tables") @Order(1) public void createTables() { - assertFalse(plugin.getDataStore().getStorage().isError()); + assertFalse(dataStore.getStorage().isError()); } @Test @DisplayName("Store empty profile") @Order(2) public void storeEmptyProfile() { - final Optional optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); + final Optional optionalProfile = dataStore.getData(player.getUniqueId()); assertTrue(optionalProfile.isPresent()); } @@ -53,7 +55,7 @@ public class SQLStorageTest { @DisplayName("Update profile") @Order(3) public void updateProfile() { - final Optional optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); + final Optional optionalProfile = dataStore.getData(player.getUniqueId()); final NickoProfile profile = optionalProfile.get(); assertNull(profile.getName()); assertNull(profile.getSkin()); @@ -65,7 +67,7 @@ public class SQLStorageTest { profile.setLocale(Locale.FRENCH); profile.setBungeecordTransfer(false); - final ActionResult result = plugin.getDataStore().saveData(player); + final ActionResult result = dataStore.saveData(player); assertFalse(result.isError()); } @@ -73,7 +75,7 @@ public class SQLStorageTest { @DisplayName("Get updated profile") @Order(4) public void hasProfileBeenUpdated() { - final Optional profile = plugin.getDataStore().getData(player.getUniqueId()); + final Optional profile = dataStore.getData(player.getUniqueId()); assertTrue(profile.isPresent()); final NickoProfile updatedProfile = profile.get(); @@ -87,7 +89,6 @@ public class SQLStorageTest { @DisplayName("Delete profile") @Order(5) public void deleteProfile() { - final PlayerDataStore dataStore = plugin.getDataStore(); final ActionResult sqlDelete = dataStore.getStorage().delete(player.getUniqueId()); assertFalse(sqlDelete.isError()); }