diff --git a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/FakeplayerGuiceModule.java b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/FakeplayerGuiceModule.java index 2c16012..5ee3585 100644 --- a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/FakeplayerGuiceModule.java +++ b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/FakeplayerGuiceModule.java @@ -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( diff --git a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/UserConfigRepository.java b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/UserConfigRepository.java index e8e5600..8c1e2a7 100644 --- a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/UserConfigRepository.java +++ b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/UserConfigRepository.java @@ -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 { +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 { 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 selectList(@NotNull UUID playerId) { @@ -44,12 +47,8 @@ public class UserConfigRepository extends Repository { 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 int saveOrUpdate(@NotNull UUID playerId, @NotNull Config config, @NotNull T value) { @@ -62,41 +61,32 @@ public class UserConfigRepository extends Repository { ?, ? ) - """; + """; - 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`); + """); } } diff --git a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/model/UserConfig.java b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/model/UserConfig.java index 6530258..4edcfaf 100644 --- a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/model/UserConfig.java +++ b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/model/UserConfig.java @@ -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 ) { diff --git a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/model/UserConfigRowMapper.java b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/model/UserConfigRowMapper.java new file mode 100644 index 0000000..de0fa88 --- /dev/null +++ b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/repository/model/UserConfigRowMapper.java @@ -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 { + + 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") + ); + } +}