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:
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:

View file

@ -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());

View file

@ -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() {

View file

@ -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<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
final Optional<NickoProfile> 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<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
final Optional<NickoProfile> 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<NickoProfile> profile = plugin.getDataStore().getData(player.getUniqueId());
final Optional<NickoProfile> 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());
}