Skip to content

Commit

Permalink
Fix SQLite JDBC no longer supporting returning generated keys
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Sep 30, 2023
1 parent 5dd6e9b commit 3ad9a86
Showing 1 changed file with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit 3ad9a86

Please sign in to comment.