|
| 1 | +package io.github._4drian3d.authmevelocity.lastserver.database; |
| 2 | + |
| 3 | +import com.google.inject.Inject; |
| 4 | +import com.google.inject.Singleton; |
| 5 | +import com.velocitypowered.api.plugin.annotation.DataDirectory; |
| 6 | +import com.zaxxer.hikari.HikariConfig; |
| 7 | +import com.zaxxer.hikari.HikariDataSource; |
| 8 | +import org.slf4j.Logger; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.sql.Connection; |
| 14 | +import java.sql.PreparedStatement; |
| 15 | +import java.sql.ResultSet; |
| 16 | +import java.sql.SQLException; |
| 17 | + |
| 18 | +@Singleton |
| 19 | +public final class Database { |
| 20 | + private static final String CREATE_SENTENCE = """ |
| 21 | + CREATE TABLE IF NOT EXISTS last_server\ |
| 22 | + (\ |
| 23 | + `player` VARCHAR(20) NOT NULL PRIMARY KEY, \ |
| 24 | + `server` VARCHAR(20) NOT NULL\ |
| 25 | + ); |
| 26 | + """; |
| 27 | + private static final String SELECT_BY_PLAYER = "SELECT `server` FROM last_server WHERE `player` = ?;"; |
| 28 | + private static final String INSERT_DATA = "INSERT INTO last_server(`player`, `server`) VALUES (?, ?);"; |
| 29 | + |
| 30 | + private final HikariDataSource source; |
| 31 | + private final Logger logger; |
| 32 | + @Inject |
| 33 | + public Database( |
| 34 | + final @DataDirectory Path dataDirectory, |
| 35 | + final Logger logger |
| 36 | + ) throws IOException { |
| 37 | + if (Files.notExists(dataDirectory)) { |
| 38 | + Files.createDirectory(dataDirectory); |
| 39 | + } |
| 40 | + final Path databasePath = dataDirectory.resolve("database.db").toAbsolutePath(); |
| 41 | + if (Files.notExists(databasePath)) { |
| 42 | + Files.createFile(databasePath); |
| 43 | + } |
| 44 | + final HikariConfig hikariConfig = new HikariConfig(); |
| 45 | + hikariConfig.setDriverClassName("org.h2.Driver"); |
| 46 | + hikariConfig.setJdbcUrl("jdbc:h2:"+databasePath); |
| 47 | + |
| 48 | + this.source = new HikariDataSource(hikariConfig); |
| 49 | + this.logger = logger; |
| 50 | + } |
| 51 | + |
| 52 | + public String lastServerOf(final String playerName) { |
| 53 | + try (final Connection connection = this.source.getConnection(); |
| 54 | + final PreparedStatement statement = fromPlayer(playerName, connection); |
| 55 | + final ResultSet rs = statement.executeQuery() |
| 56 | + ) { |
| 57 | + return rs.getString("server"); |
| 58 | + } catch (final SQLException e) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public void setLastServer(final String playerName, final String server) { |
| 64 | + try (final Connection connection = this.source.getConnection(); |
| 65 | + final PreparedStatement statement = connection.prepareStatement(INSERT_DATA); |
| 66 | + ) { |
| 67 | + statement.setString(1, playerName); |
| 68 | + statement.setString(2, server); |
| 69 | + statement.executeUpdate(); |
| 70 | + } catch (final SQLException e) { |
| 71 | + this.logger.warn("An error occurred updating last server information of player {}", playerName); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private PreparedStatement fromPlayer(final String playerName, final Connection connection) throws SQLException { |
| 76 | + final PreparedStatement statement = connection.prepareStatement(SELECT_BY_PLAYER); |
| 77 | + statement.setString(1, playerName); |
| 78 | + return statement; |
| 79 | + } |
| 80 | + |
| 81 | + public void initDatabase() { |
| 82 | + try (final Connection connection = this.source.getConnection(); |
| 83 | + final PreparedStatement statement = connection.prepareStatement(CREATE_SENTENCE) |
| 84 | + ) { |
| 85 | + statement.executeUpdate(); |
| 86 | + } catch (final Exception e) { |
| 87 | + this.logger.warn("An error occurred loading database", e); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments