From f13933c62094cf44d9aadab3e3a8b0201c8d82ac Mon Sep 17 00:00:00 2001 From: Tadashi Yamasaki Date: Mon, 5 Sep 2022 16:12:52 +0900 Subject: [PATCH] Remove checking withinValuesClause to support INSERT SET Statement --- .../core-api/java/com/mysql/cj/QueryInfo.java | 2 +- .../java/testsuite/simple/StatementsTest.java | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/core-api/java/com/mysql/cj/QueryInfo.java b/src/main/core-api/java/com/mysql/cj/QueryInfo.java index b9ed2604d..15faf6b86 100644 --- a/src/main/core-api/java/com/mysql/cj/QueryInfo.java +++ b/src/main/core-api/java/com/mysql/cj/QueryInfo.java @@ -225,7 +225,7 @@ public QueryInfo(String sql, Session session, String encoding) { this.valuesEndpoints.add(valuesClauseEnd); } - } else if (withinValuesClause && parensLevel == 0 && isInsert && (matchEnd = strInspector.matchesIgnoreCase(ODKU_CLAUSE)) != -1) { // End of VALUES clause. + } else if (parensLevel == 0 && isInsert && (matchEnd = strInspector.matchesIgnoreCase(ODKU_CLAUSE)) != -1) { // End of VALUES clause. if (valuesClauseEnd == -1) { valuesClauseEnd = strInspector.getPosition(); } diff --git a/src/test/java/testsuite/simple/StatementsTest.java b/src/test/java/testsuite/simple/StatementsTest.java index 506ff942d..33857d78b 100644 --- a/src/test/java/testsuite/simple/StatementsTest.java +++ b/src/test/java/testsuite/simple/StatementsTest.java @@ -5401,4 +5401,32 @@ public T preProcess(Supplier sql, Query intercepte return super.preProcess(sql, interceptedQuery); } } + + @Test + public void testGetGeneratedKeysWithOnDuplicateKeyUpdate() throws SQLException { + createTable("statement_test", "(id int not null primary key auto_increment, strdata1 varchar(255) not null, strdata2 varchar(255))"); + + this.pstmt = this.conn.prepareStatement("INSERT INTO statement_test (id, strdata1) VALUES (?, ?)"); + this.pstmt.setInt(1, 1); + this.pstmt.setString(2, "foo"); + this.pstmt.execute(); + + this.pstmt = this.conn.prepareStatement("INSERT INTO statement_test (id, strdata1) VALUES (?, ?) ON DUPLICATE KEY UPDATE strdata1 = VALUES(strdata1)", Statement.RETURN_GENERATED_KEYS); + this.pstmt.setInt(1, 1); + this.pstmt.setString(2, "bar"); + this.pstmt.execute(); + this.rs = this.pstmt.getGeneratedKeys(); + this.rs.last(); + assertEquals(1, this.rs.getRow(), "The number of generatedKeys isn't one."); + this.rs.close(); + + this.pstmt = this.conn.prepareStatement("INSERT INTO statement_test SET id = ?, strdata1 = ? ON DUPLICATE KEY UPDATE strdata1 = VALUES(strdata1)", Statement.RETURN_GENERATED_KEYS); + this.pstmt.setInt(1, 1); + this.pstmt.setString(2, "hoge"); + this.pstmt.execute(); + this.rs = this.pstmt.getGeneratedKeys(); + this.rs.last(); + assertEquals(1, this.rs.getRow(), "The number of generatedKeys isn't one."); + this.rs.close(); + } }