mirror of
https://github.com/tanyaofei/minecraft-fakeplayer.git
synced 2025-07-13 21:02:27 +08:00
change database implementation
This commit is contained in:
parent
f9bfbf6963
commit
c8fd35c591
@ -1,6 +1,9 @@
|
||||
package io.github.hello09x.fakeplayer.core;
|
||||
|
||||
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.TranslatorUtils;
|
||||
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.Invsee;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.File;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
public class FakeplayerGuiceModule extends AbstractModule {
|
||||
@ -17,7 +23,10 @@ public class FakeplayerGuiceModule extends AbstractModule {
|
||||
@Override
|
||||
protected void configure() {
|
||||
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(PluginTranslator.class).toInstance(pluginTranslator);
|
||||
super.bind(NMSBridge.class).toInstance(this.nmsBridge());
|
||||
@ -28,6 +37,17 @@ public class FakeplayerGuiceModule extends AbstractModule {
|
||||
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() {
|
||||
return PluginTranslator.of(
|
||||
|
@ -1,24 +1,27 @@
|
||||
package io.github.hello09x.fakeplayer.core.repository;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import io.github.hello09x.bedrock.database.Repository;
|
||||
import io.github.hello09x.fakeplayer.core.Main;
|
||||
import io.github.hello09x.devtools.database.jdbc.JdbcTemplate;
|
||||
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.UserConfigRowMapper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Singleton
|
||||
public class UserConfigRepository extends Repository<UserConfig> {
|
||||
public class UserConfigRepository {
|
||||
|
||||
public UserConfigRepository() {
|
||||
super(Main.getInstance());
|
||||
private final JdbcTemplate jdbc;
|
||||
|
||||
@Inject
|
||||
public UserConfigRepository(JdbcTemplate jdbc) {
|
||||
this.jdbc = jdbc;
|
||||
this.initTables();
|
||||
}
|
||||
|
||||
public @Nullable String select(@NotNull UUID playerId, @NotNull Config<?> config) {
|
||||
@ -28,15 +31,15 @@ public class UserConfigRepository extends Repository<UserConfig> {
|
||||
and `key` = ?
|
||||
""";
|
||||
|
||||
return execute(connection -> {
|
||||
try (PreparedStatement stm = connection.prepareStatement(sql)) {
|
||||
stm.setString(1, playerId.toString());
|
||||
stm.setString(2, config.key());
|
||||
return Optional.ofNullable(mapOne(stm.executeQuery()))
|
||||
.map(UserConfig::value)
|
||||
.orElse(null);
|
||||
}
|
||||
});
|
||||
return Optional
|
||||
.ofNullable(jdbc.queryForObject(
|
||||
sql,
|
||||
UserConfigRowMapper.instance,
|
||||
playerId.toString(),
|
||||
config.key())
|
||||
)
|
||||
.map(UserConfig::value)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public @NotNull List<UserConfig> selectList(@NotNull UUID playerId) {
|
||||
@ -44,12 +47,8 @@ public class UserConfigRepository extends Repository<UserConfig> {
|
||||
select * from user_config
|
||||
where player_id = ?
|
||||
""";
|
||||
return execute(connection -> {
|
||||
try (PreparedStatement stm = connection.prepareStatement(sql)) {
|
||||
stm.setString(1, playerId.toString());
|
||||
return mapMany(stm.executeQuery());
|
||||
}
|
||||
});
|
||||
|
||||
return jdbc.query(sql, UserConfigRowMapper.instance, playerId.toString());
|
||||
}
|
||||
|
||||
public <T> int saveOrUpdate(@NotNull UUID playerId, @NotNull Config<T> config, @NotNull T value) {
|
||||
@ -62,41 +61,32 @@ public class UserConfigRepository extends Repository<UserConfig> {
|
||||
?,
|
||||
?
|
||||
)
|
||||
""";
|
||||
""";
|
||||
|
||||
return execute(connection -> {
|
||||
try (PreparedStatement stm = connection.prepareStatement(sql)) {
|
||||
int i = 1;
|
||||
stm.setString(i++, playerId.toString());
|
||||
stm.setString(i++, config.key());
|
||||
stm.setString(i++, playerId.toString());
|
||||
stm.setString(i++, config.key());
|
||||
stm.setString(i++, value.toString());
|
||||
return stm.executeUpdate();
|
||||
}
|
||||
});
|
||||
return jdbc.update(
|
||||
sql,
|
||||
playerId.toString(),
|
||||
config.key(),
|
||||
playerId.toString(),
|
||||
config.key(),
|
||||
value.toString()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void initTables() {
|
||||
jdbc.execute("""
|
||||
create table if not exists user_config
|
||||
(
|
||||
id integer not null primary key autoincrement,
|
||||
player_id text(36) not null,
|
||||
`key` text not null,
|
||||
`value` text not null
|
||||
);
|
||||
""");
|
||||
|
||||
execute(connection -> {
|
||||
try (Statement stm = connection.createStatement()) {
|
||||
stm.execute("""
|
||||
create table if not exists user_config
|
||||
(
|
||||
id integer not null primary key autoincrement,
|
||||
player_id text(36) not null,
|
||||
`key` text not null,
|
||||
`value` text not null
|
||||
);
|
||||
""");
|
||||
stm.execute("""
|
||||
create unique index if not exists table_name_player_id_key_uindex
|
||||
on user_config (player_id, `key`);
|
||||
""");
|
||||
}
|
||||
});
|
||||
jdbc.execute("""
|
||||
create unique index if not exists table_name_player_id_key_uindex
|
||||
on user_config (player_id, `key`);
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,14 @@
|
||||
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(
|
||||
|
||||
@TableId("id")
|
||||
Integer id,
|
||||
|
||||
@TableField("player_id")
|
||||
String playerId,
|
||||
|
||||
@TableField("key")
|
||||
String key,
|
||||
|
||||
@TableField("value")
|
||||
String value
|
||||
|
||||
) {
|
||||
|
@ -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")
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user