From 5c0867c348b6fb3fb608d6924f1784276d99b600 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Mon, 28 Aug 2023 18:23:31 +0200 Subject: [PATCH] feat: sql impl working must do dependency downloading --- .../provider/sql/SqlImplementation.java | 7 ++++ .../storage/provider/sql/SqlStatements.java | 33 +++++++++++++++++++ .../provider/sql/SqlStorageProvider.java | 27 ++++----------- .../sql/connection/ConnectionFactory.java | 3 +- .../connection/file/H2ConnectionFactory.java | 5 +-- .../file/SqliteConnectionFactory.java | 5 +-- 6 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlImplementation.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStatements.java diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlImplementation.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlImplementation.java new file mode 100644 index 0000000..c3b0012 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlImplementation.java @@ -0,0 +1,7 @@ +package dev.xhyrom.lighteco.common.storage.provider.sql; + +public enum SqlImplementation { + H2, + SQLITE, + MYSQL; +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStatements.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStatements.java new file mode 100644 index 0000000..160015b --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStatements.java @@ -0,0 +1,33 @@ +package dev.xhyrom.lighteco.common.storage.provider.sql; + +public enum SqlStatements { + SAVE_USER_LOCAL_CURRENCY( + "INSERT INTO {prefix}_users (uuid, currency_identifier, balance) VALUES (?1, ?2, ?3) ON CONFLICT (uuid, currency_identifier) DO UPDATE SET balance=?3;", + "INSERT INTO {prefix}_{context}_users (uuid, currency_identifier, balance) VALUES (?1, ?2, ?3) ON DUPLICATE KEY UPDATE balance=?3;" + ), + SAVE_USER_GLOBAL_CURRENCY( + "INSERT INTO {prefix}_users (uuid, currency_identifier, balance) VALUES (?1, ?2, ?3) ON CONFLICT (uuid, currency_identifier) DO UPDATE SET balance=?3;", + "INSERT INTO {prefix}_users (uuid, currency_identifier, balance) VALUES (?1, ?2, ?3) ON DUPLICATE KEY UPDATE balance=?3;" + ); + + public final String sqlite; + public final String mysql; + + SqlStatements(String sqlite, String mysql) { + this.sqlite = sqlite; + this.mysql = mysql; + } + + public String get(SqlImplementation implementationName) { + switch (implementationName) { + case SQLITE -> { + return this.sqlite; + } + case MYSQL -> { + return this.mysql; + } + } + + throw new IllegalArgumentException("Unknown implementation: " + implementationName); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java index 61e8904..9b90c14 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java @@ -17,11 +17,8 @@ import java.util.UUID; import java.util.function.Function; public class SqlStorageProvider implements StorageProvider { - private static final String SAVE_USER_LOCAL_CURRENCY_MYSQL = "INSERT INTO {prefix}_{context}_users (uuid, currency_identifier, balance) VALUES (?1, ?2, ?3) ON DUPLICATE KEY UPDATE balance=?3;"; - private static final String SAVE_USER_GLOBAL_CURRENCY_MYSQL = "INSERT INTO {prefix}_users (uuid, currency_identifier, balance) VALUES (?1, ?2, ?3) ON DUPLICATE KEY UPDATE balance=?3;"; - private static final String SAVE_USER_LOCAL_CURRENCY = "INSERT INTO {prefix}_users (uuid, currency_identifier, balance) VALUES (?1, ?2, ?3) ON CONFLICT (uuid, currency_identifier) DO UPDATE SET balance=?3;"; - private static final String SAVE_USER_GLOBAL_CURRENCY = "INSERT INTO {prefix}_users (uuid, currency_identifier, balance) VALUES (?1, ?2, ?3) ON CONFLICT (uuid, currency_identifier) DO UPDATE SET balance=?3;"; - + private final String SAVE_USER_LOCAL_CURRENCY; + private final String SAVE_USER_GLOBAL_CURRENCY; private static final String LOAD_WHOLE_USER = """ SELECT currency_identifier, balance FROM @@ -48,6 +45,10 @@ public class SqlStorageProvider implements StorageProvider { .replace("{prefix}", plugin.getConfig().storage.tablePrefix) .replace("{context}", plugin.getConfig().server) ); + + final SqlImplementation implementationName = this.connectionFactory.getImplementationName(); + this.SAVE_USER_LOCAL_CURRENCY = SqlStatements.SAVE_USER_LOCAL_CURRENCY.get(implementationName); + this.SAVE_USER_GLOBAL_CURRENCY = SqlStatements.SAVE_USER_GLOBAL_CURRENCY.get(implementationName); } @Override @@ -55,7 +56,7 @@ public class SqlStorageProvider implements StorageProvider { this.connectionFactory.init(this.plugin); List statements; - String schemaFileName = "schema/" + this.connectionFactory.getImplementationName().toLowerCase() + ".sql"; + String schemaFileName = "schema/" + this.connectionFactory.getImplementationName().name().toLowerCase() + ".sql"; try (InputStream is = this.plugin.getBootstrap().getResourceStream(schemaFileName)) { if (is == null) throw new IOException("Failed to load schema file: " + schemaFileName); @@ -132,23 +133,9 @@ public class SqlStorageProvider implements StorageProvider { } } - System.out.println(psGlobal.toString()); - System.out.println(psLocal.toString()); psGlobal.executeBatch(); psLocal.executeBatch(); } } } - - private static boolean doesTableExists(Connection c, String table) throws SQLException { - try (ResultSet rs = c.getMetaData().getTables(c.getCatalog(), null, "%s", null)) { - while (rs.next()) { - if (rs.getString(3).equalsIgnoreCase(table)) { - return true; - } - } - } - - return false; - } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/ConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/ConnectionFactory.java index 476fc04..83a65e6 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/ConnectionFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/ConnectionFactory.java @@ -1,12 +1,13 @@ package dev.xhyrom.lighteco.common.storage.provider.sql.connection; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import dev.xhyrom.lighteco.common.storage.provider.sql.SqlImplementation; import java.sql.Connection; import java.util.function.Function; public interface ConnectionFactory { - String getImplementationName(); + SqlImplementation getImplementationName(); void init(LightEcoPlugin plugin); void shutdown() throws Exception; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java index d9a2699..727e310 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.common.storage.provider.sql.connection.file; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import dev.xhyrom.lighteco.common.storage.provider.sql.SqlImplementation; import java.io.File; import java.lang.reflect.Constructor; @@ -18,8 +19,8 @@ public class H2ConnectionFactory extends FileConnectionFactory { } @Override - public String getImplementationName() { - return "h2"; + public SqlImplementation getImplementationName() { + return SqlImplementation.H2; } @Override diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java index 13c7cc0..a495bba 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.common.storage.provider.sql.connection.file; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import dev.xhyrom.lighteco.common.storage.provider.sql.SqlImplementation; import java.lang.reflect.Constructor; import java.nio.file.Path; @@ -16,8 +17,8 @@ public class SqliteConnectionFactory extends FileConnectionFactory { } @Override - public String getImplementationName() { - return "sqlite"; + public SqlImplementation getImplementationName() { + return SqlImplementation.SQLITE; } @Override