refactor: logic

This commit is contained in:
aro 2023-01-23 15:04:43 +01:00
parent a8b3a1d290
commit 2d2e315793
2 changed files with 29 additions and 21 deletions

View file

@ -6,8 +6,7 @@ import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.storage.Storage;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.*;
import java.util.Optional;
import java.util.UUID;
@ -27,13 +26,24 @@ public class SQLStorage extends Storage {
public ActionResult<Void> store(UUID uuid, NickoProfile profile) {
final Connection connection = getProvider().getConnection();
try {
connection.prepareStatement("");
final String sql = """
INSERT INTO nicko.DATA
(uuid, name, skin, bungeecord)
VALUES
(?, ?, ?, ?)
ON DUPLICATE KEY UPDATE uuid = %s
""".formatted(uuid.toString());
final PreparedStatement statement = connection.prepareStatement(sql);
statement.setObject(0, uuid);
statement.setString(1, profile.getName());
statement.setString(2, profile.getSkin());
statement.setBoolean(3, profile.isBungeecordTransfer());
return new ActionResult<>();
} catch (SQLException e) {
instance.getLogger().warning("Unable to store player.");
return new ActionResult<>(I18NDict.Error.UNEXPECTED_ERROR);
return new ActionResult<>(I18NDict.Error.SQL_ERROR);
}
return new ActionResult<>();
}
@Override

View file

@ -28,24 +28,19 @@ public class SQLStorageProvider implements StorageProvider {
config.getSQLPassword());
final boolean initialized = connection != null && !connection.isClosed();
if (initialized) {
if (!initialized) return false;
instance.getLogger().info("Creating SQL database...");
createDatabase();
instance.getLogger().info("Creating SQL table...");
createTable();
return true;
}
return false;
} catch (SQLException e) {
return false;
}
}
public Connection getConnection() {
return connection;
}
@Override
public boolean close() {
if (connection == null) { return true; }
@ -59,17 +54,16 @@ public class SQLStorageProvider implements StorageProvider {
private void createTable() {
final Connection connection = getConnection();
final String tableName = "DATA";
final String query = """
CREATE TABLE IF NOT EXISTS %s.%s (
CREATE TABLE IF NOT EXISTS %s.DATA (
uuid uuid NOT NULL,
name varchar(16) NOT NULL,
skin varchar(16) NOT NULL,
bungeecord boolean NOT NULL,
PRIMARY KEY (UUID)
)
""".formatted(schemaName, tableName);
""".formatted(schemaName);
try {
final PreparedStatement statement = connection.prepareStatement(query);
@ -97,4 +91,8 @@ public class SQLStorageProvider implements StorageProvider {
throw new RuntimeException(e);
}
}
public Connection getConnection() {
return connection;
}
}