|
|
@ -20,11 +20,12 @@
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
@ -34,6 +35,7 @@ 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;
|
|
|
|
|
|
|
|
|
|
|
@ -41,15 +43,17 @@ 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 String driverClass;
|
|
|
|
private final org.inksnow.cputil.db.Database databaseType;
|
|
|
|
private HikariDataSource dataSource;
|
|
|
|
private AuroraDatabase 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.driverClass = type == Type.MARIADB ? "org.mariadb.jdbc.Driver" : "com.mysql.cj.jdbc.Driver";
|
|
|
|
this.databaseType = type == Type.MARIADB
|
|
|
|
|
|
|
|
? new org.inksnow.cputil.db.mariadb.MariadbDatabase()
|
|
|
|
|
|
|
|
: new org.inksnow.cputil.db.mysql.MysqlDatabase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -72,48 +76,44 @@ 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();
|
|
|
|
|
|
|
|
dataSource.setDriverClassName(driverClass);
|
|
|
|
try {
|
|
|
|
dataSource.setJdbcUrl(String.format("jdbc:%s://%s:%s/%s%s",
|
|
|
|
dataSource = AuroraDatabase.builder()
|
|
|
|
flavor,
|
|
|
|
.databaseType(databaseType)
|
|
|
|
credentials.getHost(),
|
|
|
|
.jdbcUrl(String.format("jdbc:%s://%s:%s/%s%s",
|
|
|
|
credentials.getPort(),
|
|
|
|
flavor,
|
|
|
|
credentials.getDatabase(),
|
|
|
|
credentials.getHost(),
|
|
|
|
credentials.getParameters()
|
|
|
|
credentials.getPort(),
|
|
|
|
));
|
|
|
|
credentials.getDatabase(),
|
|
|
|
|
|
|
|
credentials.getParameters()
|
|
|
|
// Authenticate with the database
|
|
|
|
))
|
|
|
|
dataSource.setUsername(credentials.getUsername());
|
|
|
|
.username(credentials.getUsername())
|
|
|
|
dataSource.setPassword(credentials.getPassword());
|
|
|
|
.password(credentials.getPassword())
|
|
|
|
|
|
|
|
.extension(dataSource -> {
|
|
|
|
// 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());
|
|
|
|
dataSource.setMaxLifetime(pool.getMaximumLifetime());
|
|
|
|
dataSource.setMaxLifetime(pool.getMaximumLifetime());
|
|
|
|
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,15 +135,14 @@ public class MySqlDatabase extends Database {
|
|
|
|
@Blocking
|
|
|
|
@Blocking
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void ensureUser(@NotNull User user) {
|
|
|
|
public void ensureUser(@NotNull User user) {
|
|
|
|
getUser(user.getUuid()).ifPresentOrElse(
|
|
|
|
OptionalUtil.ifPresentOrElse(getUser(user.getUuid()),
|
|
|
|
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%`
|
|
|
|
"UPDATE `%users_table%` SET `username`=? WHERE `uuid`=?"
|
|
|
|
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());
|
|
|
@ -158,9 +157,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`)
|
|
|
|
"INSERT INTO `%users_table%` (`uuid`,`username`) VALUES (?,?);"
|
|
|
|
VALUES (?,?);"""))) {
|
|
|
|
))) {
|
|
|
|
|
|
|
|
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(2, user.getUsername());
|
|
|
|
statement.setString(2, user.getUsername());
|
|
|
@ -177,10 +176,9 @@ 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`
|
|
|
|
"SELECT `uuid`, `username` FROM `%users_table%` WHERE `uuid`=?"
|
|
|
|
FROM `%users_table%`
|
|
|
|
))) {
|
|
|
|
WHERE `uuid`=?"""))) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
statement.setString(1, uuid.toString());
|
|
|
|
statement.setString(1, uuid.toString());
|
|
|
|
|
|
|
|
|
|
|
@ -200,10 +198,9 @@ 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`
|
|
|
|
"SELECT `uuid`, `username` FROM `%users_table%` WHERE `username`=?"
|
|
|
|
FROM `%users_table%`
|
|
|
|
))) {
|
|
|
|
WHERE `username`=?"""))) {
|
|
|
|
|
|
|
|
statement.setString(1, username);
|
|
|
|
statement.setString(1, username);
|
|
|
|
|
|
|
|
|
|
|
|
final ResultSet resultSet = statement.executeQuery();
|
|
|
|
final ResultSet resultSet = statement.executeQuery();
|
|
|
@ -222,12 +219,13 @@ 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()) {
|
|
|
@ -253,11 +251,12 @@ 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()) {
|
|
|
@ -282,12 +281,13 @@ 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()).toList();
|
|
|
|
.filter(dataSnapshot -> !dataSnapshot.isPinned())
|
|
|
|
|
|
|
|
.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 %entry_count%;""".replace("%entry_count%",
|
|
|
|
"LIMIT " + (unpinnedUserData.size() - maxSnapshots) + ";"))) {
|
|
|
|
Integer.toString(unpinnedUserData.size() - maxSnapshots))))) {
|
|
|
|
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.setString(1, user.getUuid().toString());
|
|
|
|
statement.executeUpdate();
|
|
|
|
statement.executeUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -335,10 +335,11 @@ 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;
|
|
|
@ -353,11 +354,12 @@ 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();
|
|
|
@ -371,10 +373,11 @@ 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()));
|
|
|
@ -392,11 +395,12 @@ 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)));
|
|
|
|