From eb62c515418e57f6101e8f4ca56154c8244b1778 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Tue, 29 Aug 2023 07:34:10 +0200 Subject: [PATCH] feat: mariadb works --- .../storage/provider/sql/SqlStatements.java | 19 ++++++++++----- .../provider/sql/SqlStorageProvider.java | 23 ++++++++----------- 2 files changed, 22 insertions(+), 20 deletions(-) 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 index 8c56a0a..9a95bb4 100644 --- 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 @@ -4,20 +4,24 @@ import dev.xhyrom.lighteco.common.storage.StorageType; 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}_users (uuid, currency_identifier, balance) VALUES (?1, ?2, ?3) ON DUPLICATE KEY UPDATE balance=?3;" + "INSERT INTO {prefix}_{context}_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;", + "INSERT INTO {prefix}_{context}_users (uuid, currency_identifier, balance) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE balance=?;" ), 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;" + "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;", + "INSERT INTO {prefix}_users (uuid, currency_identifier, balance) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE balance=?;" ); public final String sqlite; public final String mysql; + public final String mariadb; - SqlStatements(String sqlite, String mysql) { + SqlStatements(String sqlite, String mysql, String mariadb) { this.sqlite = sqlite; this.mysql = mysql; + this.mariadb = mariadb; } public String get(StorageType implementationName) { @@ -25,9 +29,12 @@ public enum SqlStatements { case SQLITE -> { return this.sqlite; } - case H2, MYSQL, MARIADB -> { + case H2, MYSQL -> { return this.mysql; } + case MARIADB -> { + return this.mariadb; + } } 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 4519400..5a67277 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 @@ -20,19 +20,7 @@ import java.util.function.Function; public class SqlStorageProvider implements StorageProvider { 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 - ( - SELECT currency_identifier, balance - FROM '{prefix}_users' - WHERE uuid=?1 - UNION ALL - SELECT currency_identifier, balance - FROM '{prefix}_{context}_users' - WHERE uuid=?1 - ) AS combined_currencies; - """; + private static final String LOAD_WHOLE_USER = "SELECT currency_identifier, balance FROM ( SELECT currency_identifier, balance FROM '{prefix}_users' WHERE uuid = ? UNION ALL SELECT currency_identifier, balance FROM '{prefix}_{context}_users' WHERE uuid = ? ) AS combined_currencies;"; private final LightEcoPlugin plugin; private final ConnectionFactory connectionFactory; @@ -86,7 +74,8 @@ FROM try (Connection c = this.connectionFactory.getConnection()) { try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(LOAD_WHOLE_USER))) { ps.setString(1, uniqueIdString); - ps.setString(2, uniqueIdString); + if (this.connectionFactory.getImplementationName() == StorageType.MARIADB) + ps.setString(2, uniqueIdString); ResultSet rs = ps.executeQuery(); @@ -122,6 +111,8 @@ FROM psGlobal.setString(1, uniqueIdString); psGlobal.setString(2, currency.getIdentifier()); psGlobal.setBigDecimal(3, balance); + if (this.connectionFactory.getImplementationName() == StorageType.MARIADB) + psGlobal.setBigDecimal(4, balance); psGlobal.addBatch(); } @@ -129,6 +120,8 @@ FROM psLocal.setString(1, uniqueIdString); psLocal.setString(2, currency.getIdentifier()); psLocal.setBigDecimal(3, balance); + if (this.connectionFactory.getImplementationName() == StorageType.MARIADB) + psLocal.setBigDecimal(4, balance); psLocal.addBatch(); } @@ -137,6 +130,8 @@ FROM psGlobal.executeBatch(); psLocal.executeBatch(); + } catch (SQLException e) { + throw new RuntimeException("Failed to save user " + user.getUniqueId(), e); } } }