From e6fa71d1ccf39fb8ec887251fe776e36922a1234 Mon Sep 17 00:00:00 2001 From: diego Dupin Date: Wed, 22 Jun 2022 15:31:43 +0200 Subject: [PATCH] [CONJ-953] PreparedStatement.getGeneratedKeys() returns rows when no keys are generated in insert --- src/main/java/org/mariadb/jdbc/Statement.java | 17 ++++++++++------- .../jdbc/integration/StatementTest.java | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/mariadb/jdbc/Statement.java b/src/main/java/org/mariadb/jdbc/Statement.java index 0d36bae2f..4ea666035 100644 --- a/src/main/java/org/mariadb/jdbc/Statement.java +++ b/src/main/java/org/mariadb/jdbc/Statement.java @@ -853,16 +853,19 @@ public ResultSet getGeneratedKeys() throws SQLException { } if (currResult instanceof OkPacket) { + OkPacket ok = ((OkPacket) currResult); - List insertIds = new ArrayList<>(); - insertIds.add(new String[] {String.valueOf(ok.getLastInsertId())}); - for (Completion result : results) { - if (result instanceof OkPacket) { - insertIds.add(new String[] {String.valueOf(((OkPacket) result).getLastInsertId())}); + if (ok.getLastInsertId() != 0) { + List insertIds = new ArrayList<>(); + insertIds.add(new String[]{String.valueOf(ok.getLastInsertId())}); + for (Completion result : results) { + if (result instanceof OkPacket) { + insertIds.add(new String[]{String.valueOf(((OkPacket) result).getLastInsertId())}); + } } + String[][] ids = insertIds.toArray(new String[0][]); + return CompleteResult.createResultSet("insert_id", DataType.BIGINT, ids, con.getContext()); } - String[][] ids = insertIds.toArray(new String[0][]); - return CompleteResult.createResultSet("insert_id", DataType.BIGINT, ids, con.getContext()); } return new CompleteResult(new ColumnDefinitionPacket[0], new byte[0][], con.getContext()); diff --git a/src/test/java/org/mariadb/jdbc/integration/StatementTest.java b/src/test/java/org/mariadb/jdbc/integration/StatementTest.java index 1a6e182cf..6b0aa02c7 100644 --- a/src/test/java/org/mariadb/jdbc/integration/StatementTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/StatementTest.java @@ -49,6 +49,24 @@ public static void beforeAll2() throws SQLException { stmt.execute("FLUSH TABLES"); } + @Test + public void ensureGetGeneratedKeysReturnsEmptyResult() throws SQLException { + Statement stmt = sharedConn.createStatement(); + stmt.execute("CREATE TABLE IF NOT EXISTS key_test (id INT(11) NOT NULL)"); + try (PreparedStatement ps = sharedConn.prepareStatement("INSERT INTO key_test(id) VALUES(5)", Statement.RETURN_GENERATED_KEYS)) { + ps.execute(); + ResultSet rs = ps.getGeneratedKeys(); + assertFalse(rs.next()); + } + try (PreparedStatement ps = sharedConn.prepareStatement("UPDATE key_test set id=7 WHERE id=5", Statement.RETURN_GENERATED_KEYS)) { + ps.execute(); + ResultSet rs = ps.getGeneratedKeys(); + assertFalse(rs.next()); + } + + stmt.execute("DROP TABLE key_test"); + } + @Test public void longGeneratedId() throws SQLException { longGeneratedId(BigInteger.ONE);