From 3ad9a869db5ea5a62252a90a34c0d99d8b2c69c4 Mon Sep 17 00:00:00 2001 From: William278 Date: Sat, 30 Sep 2023 14:11:52 +0100 Subject: [PATCH] Fix SQLite JDBC no longer supporting returning generated keys --- .../huskhomes/database/SqLiteDatabase.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java b/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java index adade6a71..95f96c499 100644 --- a/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java @@ -129,8 +129,8 @@ protected int setPosition(@NotNull Position position, @NotNull Connection connec INSERT INTO `%positions_table%` (`x`,`y`,`z`,`yaw`,`pitch`,`world_name`,`world_uuid`,`server_name`) VALUES - (?,?,?,?,?,?,?,?);"""), - Statement.RETURN_GENERATED_KEYS)) { + (?,?,?,?,?,?,?,?) + RETURNING `id`;"""))) { statement.setDouble(1, position.getX()); statement.setDouble(2, position.getY()); @@ -140,15 +140,14 @@ protected int setPosition(@NotNull Position position, @NotNull Connection connec statement.setString(6, position.getWorld().getName()); statement.setString(7, position.getWorld().getUuid().toString()); statement.setString(8, position.getServer()); - statement.executeUpdate(); // Return the ID of the newly inserted row - try (ResultSet resultSet = statement.getGeneratedKeys()) { + try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { - return resultSet.getInt(1); + return resultSet.getInt("id"); } } - throw new SQLException("No generated key found"); + throw new SQLException("No generated ID found"); } } @@ -186,23 +185,22 @@ protected int setSavedPosition(@NotNull SavedPosition position, INSERT INTO `%saved_positions_table%` (`position_id`, `name`, `description`, `tags`, `timestamp`) VALUES - (?,?,?,?,?);"""), - Statement.RETURN_GENERATED_KEYS)) { + (?,?,?,?,?) + RETURNING `id`;"""))) { statement.setInt(1, setPosition(position, connection)); statement.setString(2, position.getName()); statement.setString(3, position.getMeta().getDescription()); statement.setString(4, position.getMeta().getSerializedTags()); statement.setTimestamp(5, Timestamp.from(position.getMeta().getCreationTime())); - statement.executeUpdate(); // Return the ID of the newly inserted row - try (ResultSet resultSet = statement.getGeneratedKeys()) { + try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { - return resultSet.getInt(1); + return resultSet.getInt("id"); } } - throw new SQLException("No generated key found"); + throw new SQLException("No generated ID found"); } }