sql: delete is not deleting wtf

This commit is contained in:
ineanto 2023-07-14 22:54:01 +02:00
parent 8464909b6d
commit 3a7da4b3d6
4 changed files with 18 additions and 19 deletions

View file

@ -5,10 +5,13 @@ version: '3.1'
services: services:
db: db:
image: mariadb image: mariadb:latest
restart: no restart: no
environment: environment:
MARIADB_DB: db
MARIADB_ROOT_PASSWORD: 12345 MARIADB_ROOT_PASSWORD: 12345
volumes:
- mysql:/var/lib/mysql
ports: ports:
- "3306:3306" - "3306:3306"
@ -25,3 +28,6 @@ services:
restart: no restart: no
ports: ports:
- "6379:6379" - "6379:6379"
volumes:
mysql:

View file

@ -9,10 +9,7 @@ import xyz.atnrch.nicko.storage.Storage;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.sql.Connection; import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -44,7 +41,6 @@ public class SQLStorage extends Storage {
final PreparedStatement statement = isStored(uuid) ? final PreparedStatement statement = isStored(uuid) ?
getUpdateStatement(connection, uuid, profile) : getInsertStatement(connection, uuid, profile); getUpdateStatement(connection, uuid, profile) : getInsertStatement(connection, uuid, profile);
statement.executeUpdate(); statement.executeUpdate();
statement.close();
return ActionResult.ok(); return ActionResult.ok();
} catch (SQLException e) { } catch (SQLException e) {
logger.warning("Couldn't send SQL Request: " + e.getMessage()); logger.warning("Couldn't send SQL Request: " + e.getMessage());
@ -64,7 +60,6 @@ public class SQLStorage extends Storage {
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
final ResultSet resultSet = statement.executeQuery(); final ResultSet resultSet = statement.executeQuery();
statement.close();
return resultSet.next(); return resultSet.next();
} catch (SQLException e) { } catch (SQLException e) {
logger.warning("Couldn't check if data is present: " + e.getMessage()); logger.warning("Couldn't check if data is present: " + e.getMessage());
@ -94,7 +89,6 @@ public class SQLStorage extends Storage {
locale = resultSet.getString("locale"); locale = resultSet.getString("locale");
bungeecord = resultSet.getBoolean("bungeecord"); bungeecord = resultSet.getBoolean("bungeecord");
} }
statement.close();
final NickoProfile profile = new NickoProfile(name, skin, Locale.fromCode(locale), bungeecord); final NickoProfile profile = new NickoProfile(name, skin, Locale.fromCode(locale), bungeecord);
return Optional.of(profile); return Optional.of(profile);
@ -110,11 +104,10 @@ public class SQLStorage extends Storage {
if (connection == null) return ActionResult.error(I18NDict.Error.SQL_ERROR); if (connection == null) return ActionResult.error(I18NDict.Error.SQL_ERROR);
try { 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); final PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
int rows = statement.executeUpdate(); int rows = statement.executeUpdate();
statement.close();
return (rows == 1 ? ActionResult.ok() : ActionResult.error(I18NDict.Error.SQL_ERROR)); return (rows == 1 ? ActionResult.ok() : ActionResult.error(I18NDict.Error.SQL_ERROR));
} catch (SQLException e) { } catch (SQLException e) {
logger.warning("Couldn't delete profile: " + e.getMessage()); logger.warning("Couldn't delete profile: " + e.getMessage());

View file

@ -1,9 +1,9 @@
package xyz.atnrch.nicko.storage.sql; package xyz.atnrch.nicko.storage.sql;
import org.mariadb.jdbc.MariaDbDataSource;
import xyz.atnrch.nicko.config.Configuration; import xyz.atnrch.nicko.config.Configuration;
import xyz.atnrch.nicko.config.DataSourceConfiguration; import xyz.atnrch.nicko.config.DataSourceConfiguration;
import xyz.atnrch.nicko.storage.StorageProvider; import xyz.atnrch.nicko.storage.StorageProvider;
import org.mariadb.jdbc.MariaDbDataSource;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -31,6 +31,7 @@ public class SQLStorageProvider implements StorageProvider {
dataSource.setUser(sqlConfiguration.getUsername()); dataSource.setUser(sqlConfiguration.getUsername());
dataSource.setPassword(sqlConfiguration.getPassword()); dataSource.setPassword(sqlConfiguration.getPassword());
connection = dataSource.getConnection(); connection = dataSource.getConnection();
connection.setAutoCommit(true);
final boolean initialized = connection != null && !connection.isClosed(); final boolean initialized = connection != null && !connection.isClosed();
if (!initialized) return false; if (!initialized) return false;
@ -69,7 +70,6 @@ public class SQLStorageProvider implements StorageProvider {
final PreparedStatement statement = connection.prepareStatement(query); final PreparedStatement statement = connection.prepareStatement(query);
statement.executeUpdate(); statement.executeUpdate();
statement.close();
} }
private void createDatabase() throws SQLException { private void createDatabase() throws SQLException {
@ -80,7 +80,6 @@ public class SQLStorageProvider implements StorageProvider {
final PreparedStatement statement = connection.prepareStatement(query); final PreparedStatement statement = connection.prepareStatement(query);
statement.executeUpdate(); statement.executeUpdate();
statement.close();
} }
public Connection getConnection() { public Connection getConnection() {

View file

@ -21,6 +21,7 @@ public class SQLStorageTest {
private static ServerMock server; private static ServerMock server;
private static NickoBukkit plugin; private static NickoBukkit plugin;
private static PlayerMock player; private static PlayerMock player;
private static PlayerDataStore dataStore;
@BeforeAll @BeforeAll
public static void setup() { public static void setup() {
@ -31,6 +32,7 @@ public class SQLStorageTest {
false); false);
server = MockBukkit.mock(); server = MockBukkit.mock();
plugin = MockBukkit.load(NickoBukkit.class, config); plugin = MockBukkit.load(NickoBukkit.class, config);
dataStore = plugin.getDataStore();
player = server.addPlayer(); player = server.addPlayer();
} }
@ -38,14 +40,14 @@ public class SQLStorageTest {
@DisplayName("Create tables") @DisplayName("Create tables")
@Order(1) @Order(1)
public void createTables() { public void createTables() {
assertFalse(plugin.getDataStore().getStorage().isError()); assertFalse(dataStore.getStorage().isError());
} }
@Test @Test
@DisplayName("Store empty profile") @DisplayName("Store empty profile")
@Order(2) @Order(2)
public void storeEmptyProfile() { public void storeEmptyProfile() {
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); final Optional<NickoProfile> optionalProfile = dataStore.getData(player.getUniqueId());
assertTrue(optionalProfile.isPresent()); assertTrue(optionalProfile.isPresent());
} }
@ -53,7 +55,7 @@ public class SQLStorageTest {
@DisplayName("Update profile") @DisplayName("Update profile")
@Order(3) @Order(3)
public void updateProfile() { public void updateProfile() {
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); final Optional<NickoProfile> optionalProfile = dataStore.getData(player.getUniqueId());
final NickoProfile profile = optionalProfile.get(); final NickoProfile profile = optionalProfile.get();
assertNull(profile.getName()); assertNull(profile.getName());
assertNull(profile.getSkin()); assertNull(profile.getSkin());
@ -65,7 +67,7 @@ public class SQLStorageTest {
profile.setLocale(Locale.FRENCH); profile.setLocale(Locale.FRENCH);
profile.setBungeecordTransfer(false); profile.setBungeecordTransfer(false);
final ActionResult result = plugin.getDataStore().saveData(player); final ActionResult result = dataStore.saveData(player);
assertFalse(result.isError()); assertFalse(result.isError());
} }
@ -73,7 +75,7 @@ public class SQLStorageTest {
@DisplayName("Get updated profile") @DisplayName("Get updated profile")
@Order(4) @Order(4)
public void hasProfileBeenUpdated() { public void hasProfileBeenUpdated() {
final Optional<NickoProfile> profile = plugin.getDataStore().getData(player.getUniqueId()); final Optional<NickoProfile> profile = dataStore.getData(player.getUniqueId());
assertTrue(profile.isPresent()); assertTrue(profile.isPresent());
final NickoProfile updatedProfile = profile.get(); final NickoProfile updatedProfile = profile.get();
@ -87,7 +89,6 @@ public class SQLStorageTest {
@DisplayName("Delete profile") @DisplayName("Delete profile")
@Order(5) @Order(5)
public void deleteProfile() { public void deleteProfile() {
final PlayerDataStore dataStore = plugin.getDataStore();
final ActionResult sqlDelete = dataStore.getStorage().delete(player.getUniqueId()); final ActionResult sqlDelete = dataStore.getStorage().delete(player.getUniqueId());
assertFalse(sqlDelete.isError()); assertFalse(sqlDelete.isError());
} }