Modified:
branches/branch_5_1/CHANGES
branches/branch_5_1/src/com/mysql/jdbc/StatementImpl.java
branches/branch_5_1/src/testsuite/BaseTestCase.java
branches/branch_5_1/src/testsuite/regression/StatementRegressionTest.java
Log:
Fixed BUG#30493 - Statements with batched values do not return correct values for
getGeneratedKeys() when "rewriteBatchedStatements" is set to "true", and the
statement has an "ON DUPLICATE KEY UPDATE" clause.
Modified: branches/branch_5_1/CHANGES
===================================================================
--- branches/branch_5_1/CHANGES 2008-01-31 04:01:20 UTC (rev 6716)
+++ branches/branch_5_1/CHANGES 2008-01-31 04:59:17 UTC (rev 6717)
@@ -102,7 +102,11 @@
- For any SQLException caused by another Throwable, besides dumping the message or stack
trace as a string into the message, set the underlying Throwable as the cause for
the SQLException, making it accessible via getCause().
-
+
+ - Fixed BUG#30493 - Statements with batched values do not return correct values for
+ getGeneratedKeys() when "rewriteBatchedStatements" is set to "true", and the
+ statement has an "ON DUPLICATE KEY UPDATE" clause.
+
10-09-07 - Version 5.1.5
- Released instead of 5.1.4 to pickup patch for BUG#31053
Modified: branches/branch_5_1/src/com/mysql/jdbc/StatementImpl.java
===================================================================
--- branches/branch_5_1/src/com/mysql/jdbc/StatementImpl.java 2008-01-31 04:01:20 UTC (rev 6716)
+++ branches/branch_5_1/src/com/mysql/jdbc/StatementImpl.java 2008-01-31 04:59:17 UTC (rev 6717)
@@ -1181,15 +1181,14 @@
updateCounts[updateCountCounter++] = batchedStatement.getUpdateCount();
boolean doGenKeys = this.batchedGeneratedKeys != null;
-
- long generatedKeyStart = 0;
+
byte[][] row = null;
if (doGenKeys) {
- generatedKeyStart = batchedStatement.getLastInsertID();
+ long generatedKey = batchedStatement.getLastInsertID();
row = new byte[1][];
- row[0] = Long.toString(generatedKeyStart++).getBytes();
+ row[0] = Long.toString(generatedKey).getBytes();
this.batchedGeneratedKeys.add(new ByteArrayRow(row));
}
@@ -1198,8 +1197,10 @@
updateCounts[updateCountCounter++] = batchedStatement.getUpdateCount();
if (doGenKeys) {
+ long generatedKey = batchedStatement.getLastInsertID();
+
row = new byte[1][];
- row[0] = Long.toString(generatedKeyStart++).getBytes();
+ row[0] = Long.toString(generatedKey).getBytes();
this.batchedGeneratedKeys.add(new ByteArrayRow(row));
}
}
Modified: branches/branch_5_1/src/testsuite/BaseTestCase.java
===================================================================
--- branches/branch_5_1/src/testsuite/BaseTestCase.java 2008-01-31 04:01:20 UTC (rev 6716)
+++ branches/branch_5_1/src/testsuite/BaseTestCase.java 2008-01-31 04:59:17 UTC (rev 6717)
@@ -664,4 +664,38 @@
}
}
}
+
+ protected void assertResultSetsEqual(ResultSet control, ResultSet test)
+ throws Exception {
+ int controlNumCols = control.getMetaData().getColumnCount();
+ int testNumCols = test.getMetaData().getColumnCount();
+ assertEquals(controlNumCols, testNumCols);
+
+ while (control.next()) {
+ test.next();
+
+ for (int i = 0; i < controlNumCols; i++) {
+ Object controlObj = control.getObject(i + 1);
+ Object testObj = test.getObject(i + 1);
+
+ if (controlObj == null) {
+ assertNull(testObj);
+ } else {
+ assertNotNull(testObj);
+ }
+
+ if (controlObj instanceof Float) {
+ assertEquals(((Float)controlObj).floatValue(),
+ ((Float)testObj).floatValue(), 0.1);
+ } else if (controlObj instanceof Double) {
+ assertEquals(((Double)controlObj).doubleValue(),
+ ((Double)testObj).doubleValue(), 0.1);
+ } else {
+ assertEquals(controlObj, testObj);
+ }
+ }
+ }
+
+ assertFalse(test.next());
+ }
}
\ No newline at end of file
Modified: branches/branch_5_1/src/testsuite/regression/StatementRegressionTest.java
===================================================================
--- branches/branch_5_1/src/testsuite/regression/StatementRegressionTest.java 2008-01-31 04:01:20 UTC (rev 6716)
+++ branches/branch_5_1/src/testsuite/regression/StatementRegressionTest.java 2008-01-31 04:59:17 UTC (rev 6717)
@@ -5332,4 +5332,59 @@
}
};
}
-}
+
+ /**
+ * Tests fix for BUG#30493 - Statements with batched values do not
+ * return correct values for getGeneratedKeys() when "rewriteBatchedStatements"
+ * is set to "true", and the statement has an "ON DUPLICATE KEY UPDATE" clause.
+ *
+ * @throws Exception if the test fails.
+ */
+ public void testBug30493() throws Exception {
+ Connection rewriteConn = null;
+
+ try {
+ String ddl = "(autoIncId INT NOT NULL PRIMARY KEY AUTO_INCREMENT, uniqueTextKey VARCHAR(255) UNIQUE KEY)";
+ createTable("testBug30493", ddl);
+
+ String [] sequence = {"c", "a", "d", "b"};
+ String sql = "insert into testBug30493 (uniqueTextKey) values (?) on duplicate key UPDATE autoIncId = last_insert_id( autoIncId )";
+ String tablePrimeSql = "INSERT INTO testBug30493 (uniqueTextKey) VALUES ('a'), ('b'), ('c'), ('d')";
+
+ this.stmt.executeUpdate(tablePrimeSql);
+ this.pstmt = this.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
+
+ for (int i = 0; i < sequence.length; i++) {
+ this.pstmt.setString(1, sequence[i]);
+ this.pstmt.addBatch();
+ }
+
+ this.pstmt.executeBatch();
+
+ ResultSet nonRewrittenRsKeys = this.pstmt.getGeneratedKeys();
+
+ createTable("testBug30493", ddl);
+ this.stmt.executeUpdate(tablePrimeSql);
+
+ rewriteConn = getConnectionWithProps("rewriteBatchedStatements=true");
+
+ this.pstmt = rewriteConn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
+
+ for (int i = 0; i < sequence.length; i++) {
+ this.pstmt.setString(1, sequence[i]);
+ this.pstmt.addBatch();
+ }
+
+ this.pstmt.executeBatch();
+ ResultSet rewrittenRsKeys = this.pstmt.getGeneratedKeys();
+
+ assertResultSetsEqual(nonRewrittenRsKeys, rewrittenRsKeys);
+ } finally {
+ closeMemberJDBCResources();
+
+ if (rewriteConn != null) {
+ rewriteConn.close();
+ }
+ }
+ }
+}
\ No newline at end of file
| Thread |
|---|
| • Connector/J commit: r6717 - in branches/branch_5_1: . src/com/mysql/jdbc src/testsuite src/testsuite/regression | mmatthews | 31 Jan |