refactor(sql): delete

This commit is contained in:
ineanto 2023-07-14 16:53:24 +02:00
parent 87a256c9c5
commit 8464909b6d
4 changed files with 46 additions and 57 deletions

View file

@ -12,11 +12,13 @@ services:
ports: ports:
- "3306:3306" - "3306:3306"
adminer: phpmyadmin:
image: adminer image: phpmyadmin
restart: no restart: no
links:
- db
ports: ports:
- "8080:8080" - "8080:80"
redis: redis:
image: redis image: redis

View file

@ -44,6 +44,7 @@ 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());
@ -60,9 +61,10 @@ public class SQLStorage extends Storage {
final String sql = "SELECT * FROM nicko.DATA WHERE uuid = ?"; final String sql = "SELECT * FROM nicko.DATA WHERE uuid = ?";
final PreparedStatement statement = connection.prepareStatement(sql); final PreparedStatement statement = connection.prepareStatement(sql);
statement.setBinaryStream(1, uuidToBin(uuid)); 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());
@ -79,7 +81,7 @@ public class SQLStorage extends Storage {
final String sql = "SELECT * FROM nicko.DATA WHERE uuid = ?"; final String sql = "SELECT * FROM nicko.DATA WHERE uuid = ?";
final PreparedStatement statement = connection.prepareStatement(sql); final PreparedStatement statement = connection.prepareStatement(sql);
statement.setBinaryStream(1, uuidToBin(uuid)); statement.setString(1, uuid.toString());
final ResultSet resultSet = statement.executeQuery(); final ResultSet resultSet = statement.executeQuery();
String name = ""; String name = "";
@ -92,6 +94,7 @@ 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);
@ -109,11 +112,12 @@ public class SQLStorage extends Storage {
try { try {
final String sql = "DELETE FROM nicko.DATA WHERE uuid = ?"; final String sql = "DELETE FROM nicko.DATA WHERE uuid = ?";
final PreparedStatement statement = connection.prepareStatement(sql); final PreparedStatement statement = connection.prepareStatement(sql);
statement.setBinaryStream(1, uuidToBin(uuid)); statement.setString(1, uuid.toString());
statement.executeUpdate(); int rows = statement.executeUpdate();
return ActionResult.ok(); statement.close();
return (rows == 1 ? ActionResult.ok() : ActionResult.error(I18NDict.Error.SQL_ERROR));
} catch (SQLException e) { } 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); 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 { 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 String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `bungeecord`) VALUES (?, ?, ?, ?, ?)";
final PreparedStatement statement = connection.prepareStatement(sql); 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(2, profile.getName() == null ? null : profile.getName());
statement.setString(3, profile.getSkin() == null ? null : profile.getSkin()); statement.setString(3, profile.getSkin() == null ? null : profile.getSkin());
statement.setString(4, profile.getLocale().getCode()); 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(2, profile.getSkin() == null ? null : profile.getSkin());
statement.setString(3, profile.getLocale().getCode()); statement.setString(3, profile.getLocale().getCode());
statement.setBoolean(4, profile.isBungeecordTransfer()); statement.setBoolean(4, profile.isBungeecordTransfer());
statement.setBinaryStream(5, uuidToBin(uuid)); statement.setString(5, uuid.toString());
return statement; return statement;
} }

View file

@ -59,7 +59,7 @@ public class SQLStorageProvider implements StorageProvider {
final Connection connection = getConnection(); final Connection connection = getConnection();
String query = "CREATE TABLE IF NOT EXISTS %s.DATA " + String query = "CREATE TABLE IF NOT EXISTS %s.DATA " +
"(uuid binary(16) NOT NULL," + "(uuid varchar(36) NOT NULL," +
"name varchar(16)," + "name varchar(16)," +
"skin varchar(16)," + "skin varchar(16)," +
"locale char(2) NOT NULL," + "locale char(2) NOT NULL," +

View file

@ -10,11 +10,11 @@ import xyz.atnrch.nicko.config.DataSourceConfiguration;
import xyz.atnrch.nicko.appearance.ActionResult; import xyz.atnrch.nicko.appearance.ActionResult;
import xyz.atnrch.nicko.profile.NickoProfile; import xyz.atnrch.nicko.profile.NickoProfile;
import xyz.atnrch.nicko.i18n.Locale; import xyz.atnrch.nicko.i18n.Locale;
import xyz.atnrch.nicko.storage.PlayerDataStore;
import java.util.Optional; import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class SQLStorageTest { public class SQLStorageTest {
@ -35,39 +35,29 @@ public class SQLStorageTest {
} }
@Test @Test
@DisplayName("Create SQL Tables") @DisplayName("Create tables")
@Order(1) @Order(1)
public void createSQLTables() { public void createTables() {
assertFalse(plugin.getDataStore().getStorage().isError()); assertFalse(plugin.getDataStore().getStorage().isError());
} }
@Test @Test
@DisplayName("Store Player Via SQL") @DisplayName("Store empty profile")
@Order(2) @Order(2)
public void storePlayer() { public void storeEmptyProfile() {
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(optionalProfile.isPresent()); assertTrue(optionalProfile.isPresent());
} }
@Test @Test
@DisplayName("Retrieve Player Via SQL") @DisplayName("Update profile")
@Order(3) @Order(3)
public void retrievePlayer() { public void updateProfile() {
final Optional<NickoProfile> storeAction = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(storeAction.isPresent());
}
@Test
@DisplayName("Update Player Via SQL")
@Order(4)
public void updatePlayer() {
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(optionalProfile.isPresent());
final NickoProfile profile = optionalProfile.get(); final NickoProfile profile = optionalProfile.get();
Assertions.assertNull(profile.getName()); assertNull(profile.getName());
Assertions.assertNull(profile.getSkin()); assertNull(profile.getSkin());
Assertions.assertEquals(profile.getLocale(), Locale.ENGLISH); assertEquals(profile.getLocale(), Locale.ENGLISH);
assertTrue(profile.isBungeecordTransfer()); assertTrue(profile.isBungeecordTransfer());
profile.setName("Notch"); profile.setName("Notch");
@ -77,36 +67,29 @@ public class SQLStorageTest {
final ActionResult result = plugin.getDataStore().saveData(player); final ActionResult result = plugin.getDataStore().saveData(player);
assertFalse(result.isError()); assertFalse(result.isError());
}
final Optional<NickoProfile> optionalUpdatedProfile = plugin.getDataStore().getData(player.getUniqueId()); @Test
assertTrue(optionalUpdatedProfile.isPresent()); @DisplayName("Get updated profile")
final NickoProfile updatedProfile = optionalProfile.get(); @Order(4)
Assertions.assertEquals(updatedProfile.getName(), "Notch"); public void hasProfileBeenUpdated() {
Assertions.assertEquals(updatedProfile.getSkin(), "Notch"); final Optional<NickoProfile> profile = plugin.getDataStore().getData(player.getUniqueId());
Assertions.assertEquals(updatedProfile.getLocale(), Locale.FRENCH); assertTrue(profile.isPresent());
final NickoProfile updatedProfile = profile.get();
assertEquals(updatedProfile.getName(), "Notch");
assertEquals(updatedProfile.getSkin(), "Notch");
assertEquals(updatedProfile.getLocale(), Locale.FRENCH);
assertFalse(updatedProfile.isBungeecordTransfer()); assertFalse(updatedProfile.isBungeecordTransfer());
} }
@Test @Test
@DisplayName("Remove Player Disguise Via SQL") @DisplayName("Delete profile")
@Order(5) @Order(5)
public void removePlayerDisguise() { public void deleteProfile() {
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); final PlayerDataStore dataStore = plugin.getDataStore();
assertTrue(optionalProfile.isPresent()); final ActionResult sqlDelete = dataStore.getStorage().delete(player.getUniqueId());
assertFalse(sqlDelete.isError());
final NickoProfile profile = optionalProfile.get();
profile.setName(null);
profile.setSkin(null);
final ActionResult result = plugin.getDataStore().saveData(player);
assertFalse(result.isError());
final Optional<NickoProfile> optionalUpdatedProfile = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(optionalUpdatedProfile.isPresent());
final NickoProfile updatedProfile = optionalProfile.get();
Assertions.assertNull(updatedProfile.getName());
Assertions.assertNull(updatedProfile.getSkin());
} }
@AfterAll @AfterAll