refactor: logic
This commit is contained in:
parent
a8b3a1d290
commit
2d2e315793
2 changed files with 29 additions and 21 deletions
|
@ -6,8 +6,7 @@ import net.artelnatif.nicko.disguise.NickoProfile;
|
||||||
import net.artelnatif.nicko.i18n.I18NDict;
|
import net.artelnatif.nicko.i18n.I18NDict;
|
||||||
import net.artelnatif.nicko.storage.Storage;
|
import net.artelnatif.nicko.storage.Storage;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.*;
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@ -27,13 +26,24 @@ public class SQLStorage extends Storage {
|
||||||
public ActionResult<Void> store(UUID uuid, NickoProfile profile) {
|
public ActionResult<Void> store(UUID uuid, NickoProfile profile) {
|
||||||
final Connection connection = getProvider().getConnection();
|
final Connection connection = getProvider().getConnection();
|
||||||
try {
|
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) {
|
} catch (SQLException e) {
|
||||||
instance.getLogger().warning("Unable to store player.");
|
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
|
@Override
|
||||||
|
|
|
@ -28,24 +28,19 @@ public class SQLStorageProvider implements StorageProvider {
|
||||||
config.getSQLPassword());
|
config.getSQLPassword());
|
||||||
final boolean initialized = connection != null && !connection.isClosed();
|
final boolean initialized = connection != null && !connection.isClosed();
|
||||||
|
|
||||||
if (initialized) {
|
if (!initialized) return false;
|
||||||
|
|
||||||
instance.getLogger().info("Creating SQL database...");
|
instance.getLogger().info("Creating SQL database...");
|
||||||
createDatabase();
|
createDatabase();
|
||||||
|
|
||||||
instance.getLogger().info("Creating SQL table...");
|
instance.getLogger().info("Creating SQL table...");
|
||||||
createTable();
|
createTable();
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection getConnection() {
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean close() {
|
public boolean close() {
|
||||||
if (connection == null) { return true; }
|
if (connection == null) { return true; }
|
||||||
|
@ -59,17 +54,16 @@ public class SQLStorageProvider implements StorageProvider {
|
||||||
|
|
||||||
private void createTable() {
|
private void createTable() {
|
||||||
final Connection connection = getConnection();
|
final Connection connection = getConnection();
|
||||||
final String tableName = "DATA";
|
|
||||||
|
|
||||||
final String query = """
|
final String query = """
|
||||||
CREATE TABLE IF NOT EXISTS %s.%s (
|
CREATE TABLE IF NOT EXISTS %s.DATA (
|
||||||
uuid uuid NOT NULL,
|
uuid uuid NOT NULL,
|
||||||
name varchar(16) NOT NULL,
|
name varchar(16) NOT NULL,
|
||||||
skin varchar(16) NOT NULL,
|
skin varchar(16) NOT NULL,
|
||||||
bungeecord boolean NOT NULL,
|
bungeecord boolean NOT NULL,
|
||||||
PRIMARY KEY (UUID)
|
PRIMARY KEY (UUID)
|
||||||
)
|
)
|
||||||
""".formatted(schemaName, tableName);
|
""".formatted(schemaName);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final PreparedStatement statement = connection.prepareStatement(query);
|
final PreparedStatement statement = connection.prepareStatement(query);
|
||||||
|
@ -97,4 +91,8 @@ public class SQLStorageProvider implements StorageProvider {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Connection getConnection() {
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue