feat(sql): correctly store uuids

This commit is contained in:
aro 2023-01-31 19:59:38 +01:00
parent 219f4426ea
commit b63e60e83d
2 changed files with 26 additions and 34 deletions

View file

@ -1,13 +1,16 @@
package net.artelnatif.nicko.storage.sql; package net.artelnatif.nicko.storage.sql;
import com.google.common.io.ByteStreams;
import net.artelnatif.nicko.Nicko; import net.artelnatif.nicko.Nicko;
import net.artelnatif.nicko.bukkit.i18n.I18NDict;
import net.artelnatif.nicko.disguise.ActionResult; import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile; import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.bukkit.i18n.I18NDict;
import net.artelnatif.nicko.storage.Storage; import net.artelnatif.nicko.storage.Storage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
@ -40,7 +43,7 @@ public class SQLStorage extends Storage {
final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `bungeecord`) VALUES (?, ?, ?, ?)"; final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `bungeecord`) VALUES (?, ?, ?, ?)";
final PreparedStatement statement = connection.prepareStatement(sql); final PreparedStatement statement = connection.prepareStatement(sql);
statement.setObject(1, uuidToBin(uuid)); statement.setBinaryStream(1, uuidToBin(uuid));
statement.setString(2, profile.getName()); statement.setString(2, profile.getName());
statement.setString(3, profile.getSkin()); statement.setString(3, profile.getSkin());
statement.setBoolean(4, profile.isBungeecordTransfer()); statement.setBoolean(4, profile.isBungeecordTransfer());
@ -62,17 +65,20 @@ public class SQLStorage extends Storage {
return Optional.empty(); return Optional.empty();
} }
private byte[] uuidToBin(UUID uuid) { private ByteArrayInputStream uuidToBin(UUID uuid) {
final byte[] uuidBytes = new byte[16]; byte[] bytes = new byte[16];
final ByteBuffer buffer = ByteBuffer.wrap(uuidBytes) ByteBuffer.wrap(bytes)
.order(ByteOrder.BIG_ENDIAN)
.putLong(uuid.getMostSignificantBits()) .putLong(uuid.getMostSignificantBits())
.putLong(uuid.getLeastSignificantBits()); .putLong(uuid.getLeastSignificantBits());
return buffer.array(); return new ByteArrayInputStream(bytes);
} }
private UUID binToUUID(byte[] array) { private UUID binToUUID(InputStream stream) {
final ByteBuffer buffer = ByteBuffer.wrap(array); final ByteBuffer buffer = ByteBuffer.allocate(16);
return new UUID(buffer.getLong(), buffer.getLong()); try {
buffer.put(ByteStreams.toByteArray(stream));
buffer.flip();
return new UUID(buffer.getLong(), buffer.getLong());
} catch (IOException ignored) { return null; }
} }
} }

View file

@ -12,7 +12,6 @@ import java.sql.SQLException;
public class SQLStorageProvider implements StorageProvider { public class SQLStorageProvider implements StorageProvider {
private final Nicko nicko; private final Nicko nicko;
private Connection connection; private Connection connection;
private MariaDbDataSource dataSource;
private final String schemaName = "nicko"; private final String schemaName = "nicko";
@ -24,7 +23,7 @@ public class SQLStorageProvider implements StorageProvider {
public boolean init() { public boolean init() {
try { try {
final Configuration config = nicko.getConfig(); final Configuration config = nicko.getConfig();
dataSource = new MariaDbDataSource(); final MariaDbDataSource dataSource = new MariaDbDataSource();
dataSource.setUrl("jdbc:mariadb://" + config.getAddress()); dataSource.setUrl("jdbc:mariadb://" + config.getAddress());
dataSource.setUser(config.getUsername()); dataSource.setUser(config.getUsername());
dataSource.setPassword(config.getPassword()); dataSource.setPassword(config.getPassword());
@ -33,10 +32,7 @@ public class SQLStorageProvider implements StorageProvider {
if (!initialized) return false; if (!initialized) return false;
nicko.getLogger().info("Creating SQL database...");
createDatabase(); createDatabase();
nicko.getLogger().info("Creating SQL table...");
createTable(); createTable();
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
@ -56,36 +52,26 @@ public class SQLStorageProvider implements StorageProvider {
} }
} }
private void createTable() { private void createTable() throws SQLException {
final Connection connection = getConnection(); final Connection connection = getConnection();
String query = "CREATE TABLE IF NOT EXISTS %s.DATA (uuid binary(16) NOT NULL,name varchar(16) NOT NULL,skin varchar(16) NOT NULL,bungeecord boolean NOT NULL,PRIMARY KEY (UUID))"; String query = "CREATE TABLE IF NOT EXISTS %s.DATA (uuid binary(16) NOT NULL,name varchar(16) NOT NULL,skin varchar(16) NOT NULL,bungeecord boolean NOT NULL,PRIMARY KEY (UUID))";
query = query.replace("%s", schemaName); query = query.replace("%s", schemaName);
try { final PreparedStatement statement = connection.prepareStatement(query);
final PreparedStatement statement = connection.prepareStatement(query); statement.executeUpdate();
statement.executeUpdate(); statement.close();
statement.close();
} catch (SQLException e) {
// TODO: 12/10/22 Handle error
throw new RuntimeException(e);
}
} }
private void createDatabase() { private void createDatabase() throws SQLException {
final Connection connection = getConnection(); final Connection connection = getConnection();
String query = "CREATE DATABASE IF NOT EXISTS %s"; String query = "CREATE DATABASE IF NOT EXISTS %s";
query = query.replace("%s", schemaName); query = query.replace("%s", schemaName);
try { final PreparedStatement statement = connection.prepareStatement(query);
final PreparedStatement statement = connection.prepareStatement(query); statement.executeUpdate();
statement.executeUpdate(); statement.close();
statement.close();
} catch (SQLException e) {
// TODO: 12/10/22 Handle error
throw new RuntimeException(e);
}
} }
public Connection getConnection() { public Connection getConnection() {