|
|
@ -20,12 +20,11 @@
|
|
|
|
package net.william278.husksync.database;
|
|
|
|
package net.william278.husksync.database;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
|
|
import net.william278.husksync.HuskSync;
|
|
|
|
import net.william278.husksync.HuskSync;
|
|
|
|
import net.william278.husksync.adapter.DataAdapter;
|
|
|
|
import net.william278.husksync.adapter.DataAdapter;
|
|
|
|
import net.william278.husksync.data.DataSnapshot;
|
|
|
|
import net.william278.husksync.data.DataSnapshot;
|
|
|
|
import net.william278.husksync.user.User;
|
|
|
|
import net.william278.husksync.user.User;
|
|
|
|
import net.william278.husksync.util.OptionalUtil;
|
|
|
|
|
|
|
|
import org.inksnow.cputil.db.AuroraDatabase;
|
|
|
|
|
|
|
|
import org.jetbrains.annotations.Blocking;
|
|
|
|
import org.jetbrains.annotations.Blocking;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
|
|
|
@ -35,7 +34,6 @@ import java.sql.*;
|
|
|
|
import java.time.OffsetDateTime;
|
|
|
|
import java.time.OffsetDateTime;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static net.william278.husksync.config.Settings.DatabaseSettings;
|
|
|
|
import static net.william278.husksync.config.Settings.DatabaseSettings;
|
|
|
|
|
|
|
|
|
|
|
@ -43,17 +41,15 @@ public class MySqlDatabase extends Database {
|
|
|
|
|
|
|
|
|
|
|
|
private static final String DATA_POOL_NAME = "HuskSyncHikariPool";
|
|
|
|
private static final String DATA_POOL_NAME = "HuskSyncHikariPool";
|
|
|
|
private final String flavor;
|
|
|
|
private final String flavor;
|
|
|
|
private final org.inksnow.cputil.db.Database databaseType;
|
|
|
|
private final String driverClass;
|
|
|
|
private AuroraDatabase dataSource;
|
|
|
|
private HikariDataSource dataSource;
|
|
|
|
|
|
|
|
|
|
|
|
public MySqlDatabase(@NotNull HuskSync plugin) {
|
|
|
|
public MySqlDatabase(@NotNull HuskSync plugin) {
|
|
|
|
super(plugin);
|
|
|
|
super(plugin);
|
|
|
|
|
|
|
|
|
|
|
|
final Type type = plugin.getSettings().getDatabase().getType();
|
|
|
|
final Type type = plugin.getSettings().getDatabase().getType();
|
|
|
|
this.flavor = type.getProtocol();
|
|
|
|
this.flavor = type.getProtocol();
|
|
|
|
this.databaseType = type == Type.MARIADB
|
|
|
|
this.driverClass = type == Type.MARIADB ? "org.mariadb.jdbc.Driver" : "com.mysql.cj.jdbc.Driver";
|
|
|
|
? new org.inksnow.cputil.db.mariadb.MariadbDatabase()
|
|
|
|
|
|
|
|
: new org.inksnow.cputil.db.mysql.MysqlDatabase();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -76,20 +72,21 @@ public class MySqlDatabase extends Database {
|
|
|
|
public void initialize() throws IllegalStateException {
|
|
|
|
public void initialize() throws IllegalStateException {
|
|
|
|
// Initialize the Hikari pooled connection
|
|
|
|
// Initialize the Hikari pooled connection
|
|
|
|
final DatabaseSettings.DatabaseCredentials credentials = plugin.getSettings().getDatabase().getCredentials();
|
|
|
|
final DatabaseSettings.DatabaseCredentials credentials = plugin.getSettings().getDatabase().getCredentials();
|
|
|
|
|
|
|
|
dataSource = new HikariDataSource();
|
|
|
|
try {
|
|
|
|
dataSource.setDriverClassName(driverClass);
|
|
|
|
dataSource = AuroraDatabase.builder()
|
|
|
|
dataSource.setJdbcUrl(String.format("jdbc:%s://%s:%s/%s%s",
|
|
|
|
.databaseType(databaseType)
|
|
|
|
|
|
|
|
.jdbcUrl(String.format("jdbc:%s://%s:%s/%s%s",
|
|
|
|
|
|
|
|
flavor,
|
|
|
|
flavor,
|
|
|
|
credentials.getHost(),
|
|
|
|
credentials.getHost(),
|
|
|
|
credentials.getPort(),
|
|
|
|
credentials.getPort(),
|
|
|
|
credentials.getDatabase(),
|
|
|
|
credentials.getDatabase(),
|
|
|
|
credentials.getParameters()
|
|
|
|
credentials.getParameters()
|
|
|
|
))
|
|
|
|
));
|
|
|
|
.username(credentials.getUsername())
|
|
|
|
|
|
|
|
.password(credentials.getPassword())
|
|
|
|
// Authenticate with the database
|
|
|
|
.extension(dataSource -> {
|
|
|
|
dataSource.setUsername(credentials.getUsername());
|
|
|
|
|
|
|
|
dataSource.setPassword(credentials.getPassword());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set connection pool options
|
|
|
|
final DatabaseSettings.PoolSettings pool = plugin.getSettings().getDatabase().getConnectionPool();
|
|
|
|
final DatabaseSettings.PoolSettings pool = plugin.getSettings().getDatabase().getConnectionPool();
|
|
|
|
dataSource.setMaximumPoolSize(pool.getMaximumPoolSize());
|
|
|
|
dataSource.setMaximumPoolSize(pool.getMaximumPoolSize());
|
|
|
|
dataSource.setMinimumIdle(pool.getMinimumIdle());
|
|
|
|
dataSource.setMinimumIdle(pool.getMinimumIdle());
|
|
|
@ -97,23 +94,26 @@ public class MySqlDatabase extends Database {
|
|
|
|
dataSource.setKeepaliveTime(pool.getKeepaliveTime());
|
|
|
|
dataSource.setKeepaliveTime(pool.getKeepaliveTime());
|
|
|
|
dataSource.setConnectionTimeout(pool.getConnectionTimeout());
|
|
|
|
dataSource.setConnectionTimeout(pool.getConnectionTimeout());
|
|
|
|
dataSource.setPoolName(DATA_POOL_NAME);
|
|
|
|
dataSource.setPoolName(DATA_POOL_NAME);
|
|
|
|
})
|
|
|
|
|
|
|
|
.driverProperty("cachePrepStmts", "true")
|
|
|
|
// Set additional connection pool properties
|
|
|
|
.driverProperty("prepStmtCacheSize", "250")
|
|
|
|
final Properties properties = new Properties();
|
|
|
|
.driverProperty("prepStmtCacheSqlLimit", "2048")
|
|
|
|
properties.putAll(
|
|
|
|
.driverProperty("useServerPrepStmts", "true")
|
|
|
|
Map.of("cachePrepStmts", "true",
|
|
|
|
.driverProperty("useLocalSessionState", "true")
|
|
|
|
"prepStmtCacheSize", "250",
|
|
|
|
.driverProperty("useLocalTransactionState", "true")
|
|
|
|
"prepStmtCacheSqlLimit", "2048",
|
|
|
|
|
|
|
|
"useServerPrepStmts", "true",
|
|
|
|
.driverProperty("rewriteBatchedStatements", "true")
|
|
|
|
"useLocalSessionState", "true",
|
|
|
|
.driverProperty("cacheResultSetMetadata", "true")
|
|
|
|
"useLocalTransactionState", "true"
|
|
|
|
.driverProperty("cacheServerConfiguration", "true")
|
|
|
|
));
|
|
|
|
.driverProperty("elideSetAutoCommits", "true")
|
|
|
|
properties.putAll(
|
|
|
|
.driverProperty("maintainTimeStats", "false")
|
|
|
|
Map.of(
|
|
|
|
.build();
|
|
|
|
"rewriteBatchedStatements", "true",
|
|
|
|
} catch (IOException e) {
|
|
|
|
"cacheResultSetMetadata", "true",
|
|
|
|
throw new IllegalStateException("Failed to initialize the Aurora database", e);
|
|
|
|
"cacheServerConfiguration", "true",
|
|
|
|
}
|
|
|
|
"elideSetAutoCommits", "true",
|
|
|
|
|
|
|
|
"maintainTimeStats", "false")
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
dataSource.setDataSourceProperties(properties);
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare database schema; make tables if they don't exist
|
|
|
|
// Prepare database schema; make tables if they don't exist
|
|
|
|
try (Connection connection = dataSource.getConnection()) {
|
|
|
|
try (Connection connection = dataSource.getConnection()) {
|
|
|
@ -135,14 +135,15 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Blocking
|
|
|
|
@Blocking
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void ensureUser(@NotNull User user) {
|
|
|
|
public void ensureUser(@NotNull User user) {
|
|
|
|
OptionalUtil.ifPresentOrElse(getUser(user.getUuid()),
|
|
|
|
getUser(user.getUuid()).ifPresentOrElse(
|
|
|
|
existingUser -> {
|
|
|
|
existingUser -> {
|
|
|
|
if (!existingUser.getUsername().equals(user.getUsername())) {
|
|
|
|
if (!existingUser.getUsername().equals(user.getUsername())) {
|
|
|
|
// Update a user's name if it has changed in the database
|
|
|
|
// Update a user's name if it has changed in the database
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"UPDATE `%users_table%` SET `username`=? WHERE `uuid`=?"
|
|
|
|
UPDATE `%users_table%`
|
|
|
|
))) {
|
|
|
|
SET `username`=?
|
|
|
|
|
|
|
|
WHERE `uuid`=?"""))) {
|
|
|
|
|
|
|
|
|
|
|
|
statement.setString(1, user.getUsername());
|
|
|
|
statement.setString(1, user.getUsername());
|
|
|
|
statement.setString(2, existingUser.getUuid().toString());
|
|
|
|
statement.setString(2, existingUser.getUuid().toString());
|
|
|
@ -157,9 +158,9 @@ public class MySqlDatabase extends Database {
|
|
|
|
() -> {
|
|
|
|
() -> {
|
|
|
|
// Insert new player data into the database
|
|
|
|
// Insert new player data into the database
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"INSERT INTO `%users_table%` (`uuid`,`username`) VALUES (?,?);"
|
|
|
|
INSERT INTO `%users_table%` (`uuid`,`username`)
|
|
|
|
))) {
|
|
|
|
VALUES (?,?);"""))) {
|
|
|
|
|
|
|
|
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(2, user.getUsername());
|
|
|
|
statement.setString(2, user.getUsername());
|
|
|
@ -176,9 +177,10 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Optional<User> getUser(@NotNull UUID uuid) {
|
|
|
|
public Optional<User> getUser(@NotNull UUID uuid) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"SELECT `uuid`, `username` FROM `%users_table%` WHERE `uuid`=?"
|
|
|
|
SELECT `uuid`, `username`
|
|
|
|
))) {
|
|
|
|
FROM `%users_table%`
|
|
|
|
|
|
|
|
WHERE `uuid`=?"""))) {
|
|
|
|
|
|
|
|
|
|
|
|
statement.setString(1, uuid.toString());
|
|
|
|
statement.setString(1, uuid.toString());
|
|
|
|
|
|
|
|
|
|
|
@ -198,9 +200,10 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Optional<User> getUserByName(@NotNull String username) {
|
|
|
|
public Optional<User> getUserByName(@NotNull String username) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"SELECT `uuid`, `username` FROM `%users_table%` WHERE `username`=?"
|
|
|
|
SELECT `uuid`, `username`
|
|
|
|
))) {
|
|
|
|
FROM `%users_table%`
|
|
|
|
|
|
|
|
WHERE `username`=?"""))) {
|
|
|
|
statement.setString(1, username);
|
|
|
|
statement.setString(1, username);
|
|
|
|
|
|
|
|
|
|
|
|
final ResultSet resultSet = statement.executeQuery();
|
|
|
|
final ResultSet resultSet = statement.executeQuery();
|
|
|
@ -219,13 +222,12 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Optional<DataSnapshot.Packed> getLatestSnapshot(@NotNull User user) {
|
|
|
|
public Optional<DataSnapshot.Packed> getLatestSnapshot(@NotNull User user) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"SELECT `version_uuid`, `timestamp`, `data` " +
|
|
|
|
SELECT `version_uuid`, `timestamp`, `data`
|
|
|
|
"FROM `%user_data_table%` " +
|
|
|
|
FROM `%user_data_table%`
|
|
|
|
"WHERE `player_uuid`=? " +
|
|
|
|
WHERE `player_uuid`=?
|
|
|
|
"ORDER BY `timestamp` DESC " +
|
|
|
|
ORDER BY `timestamp` DESC
|
|
|
|
"LIMIT 1;"
|
|
|
|
LIMIT 1;"""))) {
|
|
|
|
))) {
|
|
|
|
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
final ResultSet resultSet = statement.executeQuery();
|
|
|
|
final ResultSet resultSet = statement.executeQuery();
|
|
|
|
if (resultSet.next()) {
|
|
|
|
if (resultSet.next()) {
|
|
|
@ -251,12 +253,11 @@ public class MySqlDatabase extends Database {
|
|
|
|
public List<DataSnapshot.Packed> getAllSnapshots(@NotNull User user) {
|
|
|
|
public List<DataSnapshot.Packed> getAllSnapshots(@NotNull User user) {
|
|
|
|
final List<DataSnapshot.Packed> retrievedData = Lists.newArrayList();
|
|
|
|
final List<DataSnapshot.Packed> retrievedData = Lists.newArrayList();
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"SELECT `version_uuid`, `timestamp`, `data` " +
|
|
|
|
SELECT `version_uuid`, `timestamp`, `data`
|
|
|
|
"FROM `%user_data_table%` " +
|
|
|
|
FROM `%user_data_table%`
|
|
|
|
"WHERE `player_uuid`=? " +
|
|
|
|
WHERE `player_uuid`=?
|
|
|
|
"ORDER BY `timestamp` DESC;"
|
|
|
|
ORDER BY `timestamp` DESC;"""))) {
|
|
|
|
))) {
|
|
|
|
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
final ResultSet resultSet = statement.executeQuery();
|
|
|
|
final ResultSet resultSet = statement.executeQuery();
|
|
|
|
while (resultSet.next()) {
|
|
|
|
while (resultSet.next()) {
|
|
|
@ -281,13 +282,12 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Optional<DataSnapshot.Packed> getSnapshot(@NotNull User user, @NotNull UUID versionUuid) {
|
|
|
|
public Optional<DataSnapshot.Packed> getSnapshot(@NotNull User user, @NotNull UUID versionUuid) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"SELECT `version_uuid`, `timestamp`, `data` " +
|
|
|
|
SELECT `version_uuid`, `timestamp`, `data`
|
|
|
|
"FROM `%user_data_table%` " +
|
|
|
|
FROM `%user_data_table%`
|
|
|
|
"WHERE `player_uuid`=? AND `version_uuid`=? " +
|
|
|
|
WHERE `player_uuid`=? AND `version_uuid`=?
|
|
|
|
"ORDER BY `timestamp` DESC " +
|
|
|
|
ORDER BY `timestamp` DESC
|
|
|
|
"LIMIT 1;"
|
|
|
|
LIMIT 1;"""))) {
|
|
|
|
))) {
|
|
|
|
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(2, versionUuid.toString());
|
|
|
|
statement.setString(2, versionUuid.toString());
|
|
|
|
final ResultSet resultSet = statement.executeQuery();
|
|
|
|
final ResultSet resultSet = statement.executeQuery();
|
|
|
@ -311,17 +311,17 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void rotateSnapshots(@NotNull User user) {
|
|
|
|
protected void rotateSnapshots(@NotNull User user) {
|
|
|
|
final List<DataSnapshot.Packed> unpinnedUserData = getAllSnapshots(user).stream()
|
|
|
|
final List<DataSnapshot.Packed> unpinnedUserData = getAllSnapshots(user).stream()
|
|
|
|
.filter(dataSnapshot -> !dataSnapshot.isPinned())
|
|
|
|
.filter(dataSnapshot -> !dataSnapshot.isPinned()).toList();
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
final int maxSnapshots = plugin.getSettings().getSynchronization().getMaxUserDataSnapshots();
|
|
|
|
final int maxSnapshots = plugin.getSettings().getSynchronization().getMaxUserDataSnapshots();
|
|
|
|
if (unpinnedUserData.size() > maxSnapshots) {
|
|
|
|
if (unpinnedUserData.size() > maxSnapshots) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"DELETE FROM `%user_data_table%` " +
|
|
|
|
DELETE FROM `%user_data_table%`
|
|
|
|
"WHERE `player_uuid`=? " +
|
|
|
|
WHERE `player_uuid`=?
|
|
|
|
"AND `pinned` IS FALSE " +
|
|
|
|
AND `pinned` IS FALSE
|
|
|
|
"ORDER BY `timestamp` ASC " +
|
|
|
|
ORDER BY `timestamp` ASC
|
|
|
|
"LIMIT " + (unpinnedUserData.size() - maxSnapshots) + ";"))) {
|
|
|
|
LIMIT %entry_count%;""".replace("%entry_count%",
|
|
|
|
|
|
|
|
Integer.toString(unpinnedUserData.size() - maxSnapshots))))) {
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.executeUpdate();
|
|
|
|
statement.executeUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -335,11 +335,10 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean deleteSnapshot(@NotNull User user, @NotNull UUID versionUuid) {
|
|
|
|
public boolean deleteSnapshot(@NotNull User user, @NotNull UUID versionUuid) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"DELETE FROM `%user_data_table%` " +
|
|
|
|
DELETE FROM `%user_data_table%`
|
|
|
|
"WHERE `player_uuid`=? AND `version_uuid`=? " +
|
|
|
|
WHERE `player_uuid`=? AND `version_uuid`=?
|
|
|
|
"LIMIT 1;"
|
|
|
|
LIMIT 1;"""))) {
|
|
|
|
))) {
|
|
|
|
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(2, versionUuid.toString());
|
|
|
|
statement.setString(2, versionUuid.toString());
|
|
|
|
return statement.executeUpdate() > 0;
|
|
|
|
return statement.executeUpdate() > 0;
|
|
|
@ -354,12 +353,11 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void rotateLatestSnapshot(@NotNull User user, @NotNull OffsetDateTime within) {
|
|
|
|
protected void rotateLatestSnapshot(@NotNull User user, @NotNull OffsetDateTime within) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"DELETE FROM `%user_data_table%` " +
|
|
|
|
DELETE FROM `%user_data_table%`
|
|
|
|
"WHERE `player_uuid`=? AND `timestamp`>? AND `pinned` IS FALSE " +
|
|
|
|
WHERE `player_uuid`=? AND `timestamp`>? AND `pinned` IS FALSE
|
|
|
|
"ORDER BY `timestamp` ASC " +
|
|
|
|
ORDER BY `timestamp` ASC
|
|
|
|
"LIMIT 1;"
|
|
|
|
LIMIT 1;"""))) {
|
|
|
|
))) {
|
|
|
|
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setTimestamp(2, Timestamp.from(within.toInstant()));
|
|
|
|
statement.setTimestamp(2, Timestamp.from(within.toInstant()));
|
|
|
|
statement.executeUpdate();
|
|
|
|
statement.executeUpdate();
|
|
|
@ -373,11 +371,10 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void createSnapshot(@NotNull User user, @NotNull DataSnapshot.Packed data) {
|
|
|
|
protected void createSnapshot(@NotNull User user, @NotNull DataSnapshot.Packed data) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"INSERT INTO `%user_data_table%` " +
|
|
|
|
INSERT INTO `%user_data_table%`
|
|
|
|
"(`player_uuid`,`version_uuid`,`timestamp`,`save_cause`,`pinned`,`data`) " +
|
|
|
|
(`player_uuid`,`version_uuid`,`timestamp`,`save_cause`,`pinned`,`data`)
|
|
|
|
"VALUES (?,?,?,?,?,?);"
|
|
|
|
VALUES (?,?,?,?,?,?);"""))) {
|
|
|
|
))) {
|
|
|
|
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(2, data.getId().toString());
|
|
|
|
statement.setString(2, data.getId().toString());
|
|
|
|
statement.setTimestamp(3, Timestamp.from(data.getTimestamp().toInstant()));
|
|
|
|
statement.setTimestamp(3, Timestamp.from(data.getTimestamp().toInstant()));
|
|
|
@ -395,12 +392,11 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void updateSnapshot(@NotNull User user, @NotNull DataSnapshot.Packed data) {
|
|
|
|
public void updateSnapshot(@NotNull User user, @NotNull DataSnapshot.Packed data) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (Connection connection = getConnection()) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
|
|
|
"UPDATE `%user_data_table%` " +
|
|
|
|
UPDATE `%user_data_table%`
|
|
|
|
"SET `save_cause`=?,`pinned`=?,`data`=? " +
|
|
|
|
SET `save_cause`=?,`pinned`=?,`data`=?
|
|
|
|
"WHERE `player_uuid`=? AND `version_uuid`=? " +
|
|
|
|
WHERE `player_uuid`=? AND `version_uuid`=?
|
|
|
|
"LIMIT 1;"
|
|
|
|
LIMIT 1;"""))) {
|
|
|
|
))) {
|
|
|
|
|
|
|
|
statement.setString(1, data.getSaveCause().name());
|
|
|
|
statement.setString(1, data.getSaveCause().name());
|
|
|
|
statement.setBoolean(2, data.isPinned());
|
|
|
|
statement.setBoolean(2, data.isPinned());
|
|
|
|
statement.setBlob(3, new ByteArrayInputStream(data.asBytes(plugin)));
|
|
|
|
statement.setBlob(3, new ByteArrayInputStream(data.asBytes(plugin)));
|
|
|
|