1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-12-22 12:31:05 +01:00

feat: sql impl working

must do dependency downloading
This commit is contained in:
Jozef Steinhübl 2023-08-28 18:23:31 +02:00
parent 39b2211131
commit 5c0867c348
6 changed files with 55 additions and 25 deletions

View file

@ -0,0 +1,7 @@
package dev.xhyrom.lighteco.common.storage.provider.sql;
public enum SqlImplementation {
H2,
SQLITE,
MYSQL;
}

View file

@ -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);
}
}

View file

@ -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<String> 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;
}
}

View file

@ -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;

View file

@ -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

View file

@ -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