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

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