1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-21 22:41:06 +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 dev.xhyrom.lighteco.api.model.currency.Currency;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.UUID; import java.util.UUID;
@ -19,7 +20,7 @@ public interface User {
* *
* @return the username * @return the username
*/ */
@NonNull String getUsername(); @Nullable String getUsername();
/** /**
* Get the balance of this user for the specified currency. * 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.model.user.User;
import dev.xhyrom.lighteco.api.storage.StorageProvider; 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.plugin.LightEcoPlugin;
import dev.xhyrom.lighteco.common.storage.provider.sql.connection.ConnectionFactory; import dev.xhyrom.lighteco.common.storage.provider.sql.connection.ConnectionFactory;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.math.BigDecimal;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.UUID; import java.util.UUID;
import java.util.function.Function;
public class SqlStorageProvider implements StorageProvider { public class SqlStorageProvider implements StorageProvider {
private static final String SAVE_USER_LOCAL_CURRENCY = ""; private static final String SAVE_USER_LOCAL_CURRENCY = "";
@ -19,29 +24,52 @@ public class SqlStorageProvider implements StorageProvider {
( (
SELECT currency_identifier, balance SELECT currency_identifier, balance
FROM {prefix}_users FROM {prefix}_users
WHERE uuid=? WHERE uuid=?1
UNION ALL UNION ALL
SELECT currency_identifier, balance SELECT currency_identifier, balance
FROM {prefix}_{context}_users FROM {prefix}_{context}_users
WHERE uuid=? WHERE uuid=?1
); );
"""; """;
private final LightEcoPlugin plugin; private final LightEcoPlugin plugin;
private final ConnectionFactory connectionFactory; private final ConnectionFactory connectionFactory;
private final Function<String, String> statementProcessor;
public SqlStorageProvider(LightEcoPlugin plugin, ConnectionFactory connectionFactory) { public SqlStorageProvider(LightEcoPlugin plugin, ConnectionFactory connectionFactory) {
this.plugin = plugin; this.plugin = plugin;
this.connectionFactory = connectionFactory; this.connectionFactory = connectionFactory;
this.statementProcessor = connectionFactory.getStatementProcessor().compose(
s -> s
.replace("{prefix}", plugin.getConfig().storage.tablePrefix)
.replace("{context}", plugin.getConfig().server)
);
} }
@Override @Override
public @NonNull User loadUser(@NonNull UUID uniqueId) throws Exception { 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 @Override

View file

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

View file

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