change database implementation

This commit is contained in:
tanyaofei 2024-07-27 20:51:12 +08:00
parent f9bfbf6963
commit c8fd35c591
4 changed files with 90 additions and 62 deletions

View File

@ -1,6 +1,9 @@
package io.github.hello09x.fakeplayer.core; package io.github.hello09x.fakeplayer.core;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.github.hello09x.devtools.database.jdbc.JdbcTemplate;
import io.github.hello09x.devtools.transaction.PluginTranslator; import io.github.hello09x.devtools.transaction.PluginTranslator;
import io.github.hello09x.devtools.transaction.TranslatorUtils; import io.github.hello09x.devtools.transaction.TranslatorUtils;
import io.github.hello09x.fakeplayer.api.spi.NMSBridge; import io.github.hello09x.fakeplayer.api.spi.NMSBridge;
@ -8,7 +11,10 @@ import io.github.hello09x.fakeplayer.core.config.FakeplayerConfig;
import io.github.hello09x.fakeplayer.core.manager.invsee.DefaultInvseeImpl; import io.github.hello09x.fakeplayer.core.manager.invsee.DefaultInvseeImpl;
import io.github.hello09x.fakeplayer.core.manager.invsee.Invsee; import io.github.hello09x.fakeplayer.core.manager.invsee.Invsee;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import javax.sql.DataSource;
import java.io.File;
import java.util.ServiceLoader; import java.util.ServiceLoader;
public class FakeplayerGuiceModule extends AbstractModule { public class FakeplayerGuiceModule extends AbstractModule {
@ -17,7 +23,10 @@ public class FakeplayerGuiceModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
var pluginTranslator = this.pluginTranslator(); var pluginTranslator = this.pluginTranslator();
var dataSource = this.dataSource();
super.bind(DataSource.class).toInstance(dataSource);
super.bind(JdbcTemplate.class).toInstance(jdbcTemplate(dataSource));
super.bind(FakeplayerConfig.class).toInstance(this.fakeplayerConfig()); super.bind(FakeplayerConfig.class).toInstance(this.fakeplayerConfig());
super.bind(PluginTranslator.class).toInstance(pluginTranslator); super.bind(PluginTranslator.class).toInstance(pluginTranslator);
super.bind(NMSBridge.class).toInstance(this.nmsBridge()); super.bind(NMSBridge.class).toInstance(this.nmsBridge());
@ -28,6 +37,17 @@ public class FakeplayerGuiceModule extends AbstractModule {
return new FakeplayerConfig(Main.getInstance(), "13"); return new FakeplayerConfig(Main.getInstance(), "13");
} }
private DataSource dataSource() {
var config = new HikariConfig();
config.setDriverClassName("org.sqlite.JDBC");
config.setMaximumPoolSize(1);
config.setJdbcUrl("jdbc:sqlite:" + new File(Main.getInstance().getDataFolder(), "data.db").getAbsolutePath());
return new HikariDataSource(config);
}
private JdbcTemplate jdbcTemplate(@NotNull DataSource dataSource) {
return new JdbcTemplate(Main.getInstance(), dataSource);
}
private PluginTranslator pluginTranslator() { private PluginTranslator pluginTranslator() {
return PluginTranslator.of( return PluginTranslator.of(

View File

@ -1,24 +1,27 @@
package io.github.hello09x.fakeplayer.core.repository; package io.github.hello09x.fakeplayer.core.repository;
import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import io.github.hello09x.bedrock.database.Repository; import io.github.hello09x.devtools.database.jdbc.JdbcTemplate;
import io.github.hello09x.fakeplayer.core.Main;
import io.github.hello09x.fakeplayer.core.repository.model.Config; import io.github.hello09x.fakeplayer.core.repository.model.Config;
import io.github.hello09x.fakeplayer.core.repository.model.UserConfig; import io.github.hello09x.fakeplayer.core.repository.model.UserConfig;
import io.github.hello09x.fakeplayer.core.repository.model.UserConfigRowMapper;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@Singleton @Singleton
public class UserConfigRepository extends Repository<UserConfig> { public class UserConfigRepository {
public UserConfigRepository() { private final JdbcTemplate jdbc;
super(Main.getInstance());
@Inject
public UserConfigRepository(JdbcTemplate jdbc) {
this.jdbc = jdbc;
this.initTables();
} }
public @Nullable String select(@NotNull UUID playerId, @NotNull Config<?> config) { public @Nullable String select(@NotNull UUID playerId, @NotNull Config<?> config) {
@ -28,28 +31,24 @@ public class UserConfigRepository extends Repository<UserConfig> {
and `key` = ? and `key` = ?
"""; """;
return execute(connection -> { return Optional
try (PreparedStatement stm = connection.prepareStatement(sql)) { .ofNullable(jdbc.queryForObject(
stm.setString(1, playerId.toString()); sql,
stm.setString(2, config.key()); UserConfigRowMapper.instance,
return Optional.ofNullable(mapOne(stm.executeQuery())) playerId.toString(),
config.key())
)
.map(UserConfig::value) .map(UserConfig::value)
.orElse(null); .orElse(null);
} }
});
}
public @NotNull List<UserConfig> selectList(@NotNull UUID playerId) { public @NotNull List<UserConfig> selectList(@NotNull UUID playerId) {
var sql = """ var sql = """
select * from user_config select * from user_config
where player_id = ? where player_id = ?
"""; """;
return execute(connection -> {
try (PreparedStatement stm = connection.prepareStatement(sql)) { return jdbc.query(sql, UserConfigRowMapper.instance, playerId.toString());
stm.setString(1, playerId.toString());
return mapMany(stm.executeQuery());
}
});
} }
public <T> int saveOrUpdate(@NotNull UUID playerId, @NotNull Config<T> config, @NotNull T value) { public <T> int saveOrUpdate(@NotNull UUID playerId, @NotNull Config<T> config, @NotNull T value) {
@ -64,26 +63,18 @@ public class UserConfigRepository extends Repository<UserConfig> {
) )
"""; """;
return execute(connection -> { return jdbc.update(
try (PreparedStatement stm = connection.prepareStatement(sql)) { sql,
int i = 1; playerId.toString(),
stm.setString(i++, playerId.toString()); config.key(),
stm.setString(i++, config.key()); playerId.toString(),
stm.setString(i++, playerId.toString()); config.key(),
stm.setString(i++, config.key()); value.toString()
stm.setString(i++, value.toString()); );
return stm.executeUpdate();
}
});
} }
@Override
protected void initTables() { protected void initTables() {
jdbc.execute("""
execute(connection -> {
try (Statement stm = connection.createStatement()) {
stm.execute("""
create table if not exists user_config create table if not exists user_config
( (
id integer not null primary key autoincrement, id integer not null primary key autoincrement,
@ -92,11 +83,10 @@ public class UserConfigRepository extends Repository<UserConfig> {
`value` text not null `value` text not null
); );
"""); """);
stm.execute("""
jdbc.execute("""
create unique index if not exists table_name_player_id_key_uindex create unique index if not exists table_name_player_id_key_uindex
on user_config (player_id, `key`); on user_config (player_id, `key`);
"""); """);
} }
});
}
} }

View File

@ -1,23 +1,14 @@
package io.github.hello09x.fakeplayer.core.repository.model; package io.github.hello09x.fakeplayer.core.repository.model;
import io.github.hello09x.bedrock.database.Table;
import io.github.hello09x.bedrock.database.TableField;
import io.github.hello09x.bedrock.database.TableId;
@Table("user_config")
public record UserConfig( public record UserConfig(
@TableId("id")
Integer id, Integer id,
@TableField("player_id")
String playerId, String playerId,
@TableField("key")
String key, String key,
@TableField("value")
String value String value
) { ) {

View File

@ -0,0 +1,27 @@
package io.github.hello09x.fakeplayer.core.repository.model;
import io.github.hello09x.devtools.database.jdbc.RowMapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author tanyaofei
* @since 2024/7/27
**/
public class UserConfigRowMapper implements RowMapper<UserConfig> {
public final static UserConfigRowMapper instance = new UserConfigRowMapper();
@Override
public @Nullable UserConfig mapRow(@NotNull ResultSet rs, int rowNum) throws SQLException {
return new UserConfig(
rs.getInt("id"),
rs.getString("player_id"),
rs.getString("key"),
rs.getString("value")
);
}
}