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

feat(sql): impl load user

This commit is contained in:
Jozef Steinhübl 2023-08-28 12:12:56 +02:00
parent 2a5a7133bb
commit e340b32e86
4 changed files with 43 additions and 5 deletions

View file

@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.api.model.user;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.math.BigDecimal;
import java.util.UUID;
@ -19,7 +20,7 @@ public interface User {
*
* @return the username
*/
@NonNull String getUsername();
@Nullable String getUsername();
/**
* Get the balance of this user for the specified currency.

View file

@ -2,12 +2,17 @@ package dev.xhyrom.lighteco.common.storage.provider.sql;
import dev.xhyrom.lighteco.api.model.user.User;
import dev.xhyrom.lighteco.api.storage.StorageProvider;
import dev.xhyrom.lighteco.common.model.currency.Currency;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import dev.xhyrom.lighteco.common.storage.provider.sql.connection.ConnectionFactory;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.UUID;
import java.util.function.Function;
public class SqlStorageProvider implements StorageProvider {
private static final String SAVE_USER_LOCAL_CURRENCY = "";
@ -19,29 +24,52 @@ public class SqlStorageProvider implements StorageProvider {
(
SELECT currency_identifier, balance
FROM {prefix}_users
WHERE uuid=?
WHERE uuid=?1
UNION ALL
SELECT currency_identifier, balance
FROM {prefix}_{context}_users
WHERE uuid=?
WHERE uuid=?1
);
""";
private final LightEcoPlugin plugin;
private final ConnectionFactory connectionFactory;
private final Function<String, String> statementProcessor;
public SqlStorageProvider(LightEcoPlugin plugin, ConnectionFactory connectionFactory) {
this.plugin = plugin;
this.connectionFactory = connectionFactory;
this.statementProcessor = connectionFactory.getStatementProcessor().compose(
s -> s
.replace("{prefix}", plugin.getConfig().storage.tablePrefix)
.replace("{context}", plugin.getConfig().server)
);
}
@Override
public @NonNull User loadUser(@NonNull UUID uniqueId) throws Exception {
try (Connection c = this.connectionFactory.getConnection()) {
String uniqueIdString = uniqueId.toString();
dev.xhyrom.lighteco.common.model.user.User user = this.plugin.getUserManager().getOrMake(uniqueId);
try (Connection c = this.connectionFactory.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(LOAD_WHOLE_USER)) {
ps.setString(1, uniqueIdString);
ps.setString(2, uniqueIdString);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String currencyIdentifier = rs.getString("currency_identifier");
Currency currency = this.plugin.getCurrencyManager().getIfLoaded(currencyIdentifier);
BigDecimal balance = rs.getBigDecimal("balance");
user.setBalance(currency, balance);
}
}
}
return null;
return user.getProxy();
}
@Override

View file

@ -3,10 +3,13 @@ package dev.xhyrom.lighteco.common.storage.provider.sql.connection;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import java.sql.Connection;
import java.util.function.Function;
public interface ConnectionFactory {
void init(LightEcoPlugin plugin);
void shutdown() throws Exception;
Function<String, String> getStatementProcessor();
Connection getConnection() throws Exception;
}

View file

@ -7,6 +7,7 @@ import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.function.Function;
public class H2ConnectionFactory extends FileConnectionFactory {
private Constructor<?> connectionConstructor;
@ -36,4 +37,9 @@ public class H2ConnectionFactory extends FileConnectionFactory {
throw new SQLException("Failed to create connection", e);
}
}
@Override
public Function<String, String> getStatementProcessor() {
return s -> s.replace('\'', '`');
}
}