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:
- "3306:3306"
adminer:
image: adminer
phpmyadmin:
image: phpmyadmin
restart: no
links:
- db
ports:
- "8080:8080"
- "8080:80"
redis:
image: redis

View file

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

View file

@ -59,7 +59,7 @@ public class SQLStorageProvider implements StorageProvider {
final Connection connection = getConnection();
String query = "CREATE TABLE IF NOT EXISTS %s.DATA " +
"(uuid binary(16) NOT NULL," +
"(uuid varchar(36) NOT NULL," +
"name varchar(16)," +
"skin varchar(16)," +
"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.profile.NickoProfile;
import xyz.atnrch.nicko.i18n.Locale;
import xyz.atnrch.nicko.storage.PlayerDataStore;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class SQLStorageTest {
@ -35,39 +35,29 @@ public class SQLStorageTest {
}
@Test
@DisplayName("Create SQL Tables")
@DisplayName("Create tables")
@Order(1)
public void createSQLTables() {
public void createTables() {
assertFalse(plugin.getDataStore().getStorage().isError());
}
@Test
@DisplayName("Store Player Via SQL")
@DisplayName("Store empty profile")
@Order(2)
public void storePlayer() {
public void storeEmptyProfile() {
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(optionalProfile.isPresent());
}
@Test
@DisplayName("Retrieve Player Via SQL")
@DisplayName("Update profile")
@Order(3)
public void retrievePlayer() {
final Optional<NickoProfile> storeAction = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(storeAction.isPresent());
}
@Test
@DisplayName("Update Player Via SQL")
@Order(4)
public void updatePlayer() {
public void updateProfile() {
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(optionalProfile.isPresent());
final NickoProfile profile = optionalProfile.get();
Assertions.assertNull(profile.getName());
Assertions.assertNull(profile.getSkin());
Assertions.assertEquals(profile.getLocale(), Locale.ENGLISH);
assertNull(profile.getName());
assertNull(profile.getSkin());
assertEquals(profile.getLocale(), Locale.ENGLISH);
assertTrue(profile.isBungeecordTransfer());
profile.setName("Notch");
@ -77,36 +67,29 @@ public class SQLStorageTest {
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.assertEquals(updatedProfile.getName(), "Notch");
Assertions.assertEquals(updatedProfile.getSkin(), "Notch");
Assertions.assertEquals(updatedProfile.getLocale(), Locale.FRENCH);
@Test
@DisplayName("Get updated profile")
@Order(4)
public void hasProfileBeenUpdated() {
final Optional<NickoProfile> profile = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(profile.isPresent());
final NickoProfile updatedProfile = profile.get();
assertEquals(updatedProfile.getName(), "Notch");
assertEquals(updatedProfile.getSkin(), "Notch");
assertEquals(updatedProfile.getLocale(), Locale.FRENCH);
assertFalse(updatedProfile.isBungeecordTransfer());
}
@Test
@DisplayName("Remove Player Disguise Via SQL")
@DisplayName("Delete profile")
@Order(5)
public void removePlayerDisguise() {
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(optionalProfile.isPresent());
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());
public void deleteProfile() {
final PlayerDataStore dataStore = plugin.getDataStore();
final ActionResult sqlDelete = dataStore.getStorage().delete(player.getUniqueId());
assertFalse(sqlDelete.isError());
}
@AfterAll