Skip to content

Commit

Permalink
Fixed SQLFeatureNotSupportedException when logging entity data (fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelli committed Sep 25, 2023
1 parent f76b0d4 commit d944c2b
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 26 deletions.
11 changes: 10 additions & 1 deletion src/main/java/net/coreprotect/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public static void setMultiInt(PreparedStatement statement, int value, int count
}
}

public static boolean hasReturningKeys() {
return (!Config.getGlobal().MYSQL && ConfigHandler.SERVER_VERSION >= 20);
}

public static void containerBreakCheck(String user, Material type, Object container, ItemStack[] contents, Location location) {
if (BlockGroup.CONTAINERS.contains(type) && !BlockGroup.SHULKER_BOXES.contains(type)) {
if (Config.getConfig(location.getWorld()).ITEM_TRANSACTIONS) {
Expand Down Expand Up @@ -286,7 +290,12 @@ private static PreparedStatement prepareStatement(Connection connection, String
PreparedStatement preparedStatement = null;
try {
if (keys) {
preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
if (hasReturningKeys()) {
preparedStatement = connection.prepareStatement(query + " RETURNING rowid");
}
else {
preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
}
}
else {
preparedStatement = connection.prepareStatement(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.EntityStatement;
import net.coreprotect.database.statement.UserStatement;
Expand Down Expand Up @@ -43,11 +44,21 @@ public static void log(PreparedStatement preparedStmt, PreparedStatement prepare
int x = block.getX();
int y = block.getY();
int z = block.getZ();
EntityStatement.insert(preparedStmt2, time, data);
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
int entity_key = keys.getInt(1);
keys.close();
int entity_key = 0;

ResultSet resultSet = EntityStatement.insert(preparedStmt2, time, data);
if (Database.hasReturningKeys()) {
resultSet.next();
entity_key = resultSet.getInt(1);
resultSet.close();
}
else {
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
entity_key = keys.getInt(1);
keys.close();
}

BlockStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, type, entity_key, null, null, 3, 0);
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.block.Skull;

import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.SkullStatement;
import net.coreprotect.utility.Util;

Expand All @@ -29,11 +30,18 @@ public static void log(PreparedStatement preparedStmt, PreparedStatement prepare
int skullKey = 0;
if (skull.hasOwner()) {
skullOwner = skull.getOwningPlayer().getUniqueId().toString();
SkullStatement.insert(preparedStmt2, time, skullOwner);
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
skullKey = keys.getInt(1);
keys.close();
ResultSet resultSet = SkullStatement.insert(preparedStmt2, time, skullOwner);
if (Database.hasReturningKeys()) {
resultSet.next();
skullKey = resultSet.getInt(1);
resultSet.close();
}
else {
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
skullKey = keys.getInt(1);
keys.close();
}
}

BlockBreakLogger.log(preparedStmt, batchCount, user, block.getLocation(), type, skullKey, null, block.getBlockData().getAsString(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.block.Skull;

import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.SkullStatement;

public class SkullPlaceLogger {
Expand All @@ -31,11 +32,18 @@ public static void log(PreparedStatement preparedStmt, PreparedStatement prepare
String skullOwner = "";
if (skull.hasOwner()) {
skullOwner = skull.getOwningPlayer().getUniqueId().toString();
SkullStatement.insert(preparedStmt2, time, skullOwner);
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
skullKey = keys.getInt(1);
keys.close();
ResultSet resultSet = SkullStatement.insert(preparedStmt2, time, skullOwner);
if (Database.hasReturningKeys()) {
resultSet.next();
skullKey = resultSet.getInt(1);
resultSet.close();
}
else {
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
skullKey = keys.getInt(1);
keys.close();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;

import net.coreprotect.database.Database;

public class EntityStatement {

private EntityStatement() {
throw new IllegalStateException("Database class");
}

public static void insert(PreparedStatement preparedStmt, int time, List<Object> data) {
public static ResultSet insert(PreparedStatement preparedStmt, int time, List<Object> data) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BukkitObjectOutputStream oos = new BukkitObjectOutputStream(bos);
Expand All @@ -30,11 +32,18 @@ public static void insert(PreparedStatement preparedStmt, int time, List<Object>
byte[] byte_data = bos.toByteArray();
preparedStmt.setInt(1, time);
preparedStmt.setObject(2, byte_data);
preparedStmt.executeUpdate();
if (Database.hasReturningKeys()) {
return preparedStmt.executeQuery();
}
else {
preparedStmt.executeUpdate();
}
}
catch (Exception e) {
e.printStackTrace();
}

return null;
}

public static List<Object> getData(Statement statement, BlockState block, String query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@
import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;

import net.coreprotect.database.Database;

public class SkullStatement {

private SkullStatement() {
throw new IllegalStateException("Database class");
}

public static void insert(PreparedStatement preparedStmt, int time, String owner) {
public static ResultSet insert(PreparedStatement preparedStmt, int time, String owner) {
try {
preparedStmt.setInt(1, time);
preparedStmt.setString(2, owner);
preparedStmt.executeUpdate();
if (Database.hasReturningKeys()) {
return preparedStmt.executeQuery();
}
else {
preparedStmt.executeUpdate();
}
}
catch (Exception e) {
e.printStackTrace();
}

return null;
}

public static void getData(Statement statement, BlockState block, String query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;

public class UserStatement {

Expand All @@ -21,14 +22,32 @@ public static int insert(Connection connection, String user) {

try {
int unixtimestamp = (int) (System.currentTimeMillis() / 1000L);
PreparedStatement preparedStmt = connection.prepareStatement("INSERT INTO " + ConfigHandler.prefix + "user (time, user) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);

PreparedStatement preparedStmt = null;
if (Database.hasReturningKeys()) {
preparedStmt = connection.prepareStatement("INSERT INTO " + ConfigHandler.prefix + "user (time, user) VALUES (?, ?) RETURNING rowid");
}
else {
preparedStmt = connection.prepareStatement("INSERT INTO " + ConfigHandler.prefix + "user (time, user) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);
}

preparedStmt.setInt(1, unixtimestamp);
preparedStmt.setString(2, user);
preparedStmt.executeUpdate();
ResultSet keys = preparedStmt.getGeneratedKeys();
keys.next();
id = keys.getInt(1);
keys.close();

if (Database.hasReturningKeys()) {
ResultSet resultSet = preparedStmt.executeQuery();
resultSet.next();
id = resultSet.getInt(1);
resultSet.close();
}
else {
preparedStmt.executeUpdate();
ResultSet keys = preparedStmt.getGeneratedKeys();
keys.next();
id = keys.getInt(1);
keys.close();
}

preparedStmt.close();
}
catch (Exception e) {
Expand Down

0 comments on commit d944c2b

Please sign in to comment.