feat(storage): mysql table creation

This commit is contained in:
aro 2022-12-08 16:16:26 +01:00
parent ac95f34ddb
commit 7b82126f40
2 changed files with 59 additions and 6 deletions

View file

@ -3,8 +3,9 @@ package net.artelnatif.nicko.storage.sql;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.storage.Storage;
import net.artelnatif.nicko.storage.StorageProvider;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Optional;
import java.util.UUID;
@ -16,12 +17,18 @@ public class SQLStorage extends Storage {
}
@Override
public StorageProvider getProvider() {
public SQLStorageProvider getProvider() {
return new SQLStorageProvider(instance);
}
@Override
public void store(UUID uuid, NickoProfile profile) {
final Connection connection = getProvider().getConnection();
try {
connection.prepareStatement("");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override

View file

@ -4,9 +4,7 @@ import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.config.NickoConfiguration;
import net.artelnatif.nicko.storage.StorageProvider;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
public class SQLStorageProvider implements StorageProvider {
private final NickoBukkit instance;
@ -21,12 +19,24 @@ public class SQLStorageProvider implements StorageProvider {
try {
final NickoConfiguration config = instance.getNickoConfig();
connection = DriverManager.getConnection("jdbc://" + config.getStorageAddress(), config.getStorageUsername(), config.getStoragePassword());
return !connection.isClosed() && connection != null;
final boolean initialized = connection != null && !connection.isClosed();
if (initialized) {
if (!doesTableExist()) {
return createTables();
}
return true;
}
return false;
} catch (SQLException e) {
return false;
}
}
public Connection getConnection() {
return connection;
}
@Override
public boolean close() {
try {
@ -36,4 +46,40 @@ public class SQLStorageProvider implements StorageProvider {
return false;
}
}
private boolean createTables() {
final Connection connection = getConnection();
final String query = """
CREATE TABLE IF NOT EXISTS 'NICKO' (
uuid uuid NOT NULL,
name varchar(16) NOT NULL,
skin varchar(16) NOT NULL,
bungeecord boolean NOT NULL,
PRIMARY KEY (UUID)
)
""";
try {
final PreparedStatement statement = connection.prepareStatement(query);
ResultSet result = statement.executeQuery();
return result.next();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private boolean doesTableExist() {
final Connection connection = getConnection();
final String query = "SELECT UUID FROM 'NICKO'";
try {
final PreparedStatement statement = connection.prepareStatement(query);
ResultSet result = statement.executeQuery();
return result.next();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}