1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-10 01:18:07 +01:00

feat: mariadb works

This commit is contained in:
Jozef Steinhübl 2023-08-29 07:34:10 +02:00
parent e556356e56
commit eb62c51541
2 changed files with 22 additions and 20 deletions

View file

@ -4,20 +4,24 @@ import dev.xhyrom.lighteco.common.storage.StorageType;
public enum SqlStatements { public enum SqlStatements {
SAVE_USER_LOCAL_CURRENCY( 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 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 DUPLICATE KEY UPDATE balance=?3;",
"INSERT INTO {prefix}_{context}_users (uuid, currency_identifier, balance) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE balance=?;"
), ),
SAVE_USER_GLOBAL_CURRENCY( 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 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 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 sqlite;
public final String mysql; public final String mysql;
public final String mariadb;
SqlStatements(String sqlite, String mysql) { SqlStatements(String sqlite, String mysql, String mariadb) {
this.sqlite = sqlite; this.sqlite = sqlite;
this.mysql = mysql; this.mysql = mysql;
this.mariadb = mariadb;
} }
public String get(StorageType implementationName) { public String get(StorageType implementationName) {
@ -25,9 +29,12 @@ public enum SqlStatements {
case SQLITE -> { case SQLITE -> {
return this.sqlite; return this.sqlite;
} }
case H2, MYSQL, MARIADB -> { case H2, MYSQL -> {
return this.mysql; return this.mysql;
} }
case MARIADB -> {
return this.mariadb;
}
} }
throw new IllegalArgumentException("Unknown implementation: " + implementationName); throw new IllegalArgumentException("Unknown implementation: " + implementationName);

View file

@ -20,19 +20,7 @@ import java.util.function.Function;
public class SqlStorageProvider implements StorageProvider { public class SqlStorageProvider implements StorageProvider {
private final String SAVE_USER_LOCAL_CURRENCY; private final String SAVE_USER_LOCAL_CURRENCY;
private final String SAVE_USER_GLOBAL_CURRENCY; private final String SAVE_USER_GLOBAL_CURRENCY;
private static final String LOAD_WHOLE_USER = """ 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;";
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 final LightEcoPlugin plugin; private final LightEcoPlugin plugin;
private final ConnectionFactory connectionFactory; private final ConnectionFactory connectionFactory;
@ -86,6 +74,7 @@ FROM
try (Connection c = this.connectionFactory.getConnection()) { try (Connection c = this.connectionFactory.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(LOAD_WHOLE_USER))) { try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(LOAD_WHOLE_USER))) {
ps.setString(1, uniqueIdString); ps.setString(1, uniqueIdString);
if (this.connectionFactory.getImplementationName() == StorageType.MARIADB)
ps.setString(2, uniqueIdString); ps.setString(2, uniqueIdString);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
@ -122,6 +111,8 @@ FROM
psGlobal.setString(1, uniqueIdString); psGlobal.setString(1, uniqueIdString);
psGlobal.setString(2, currency.getIdentifier()); psGlobal.setString(2, currency.getIdentifier());
psGlobal.setBigDecimal(3, balance); psGlobal.setBigDecimal(3, balance);
if (this.connectionFactory.getImplementationName() == StorageType.MARIADB)
psGlobal.setBigDecimal(4, balance);
psGlobal.addBatch(); psGlobal.addBatch();
} }
@ -129,6 +120,8 @@ FROM
psLocal.setString(1, uniqueIdString); psLocal.setString(1, uniqueIdString);
psLocal.setString(2, currency.getIdentifier()); psLocal.setString(2, currency.getIdentifier());
psLocal.setBigDecimal(3, balance); psLocal.setBigDecimal(3, balance);
if (this.connectionFactory.getImplementationName() == StorageType.MARIADB)
psLocal.setBigDecimal(4, balance);
psLocal.addBatch(); psLocal.addBatch();
} }
@ -137,6 +130,8 @@ FROM
psGlobal.executeBatch(); psGlobal.executeBatch();
psLocal.executeBatch(); psLocal.executeBatch();
} catch (SQLException e) {
throw new RuntimeException("Failed to save user " + user.getUniqueId(), e);
} }
} }
} }