=== modified file 'CHANGES' --- CHANGES 2009-01-23 18:21:18 +0000 +++ CHANGES 2009-03-02 21:44:28 +0000 @@ -11,6 +11,9 @@ - Fixed Bug #42309 - Statement.getGeneratedKeys() returns 2 keys when using ON DUPLICATE KEY UPDATE + - Fixed Bug #40439 - Error rewriting batched statement if table name ends + with "values". + 10-22-08 - Version 5.1.7 - Fixed BUG#33861 - Added global blacklist for LoadBalancingConnectionProxy and implemented in RandomBalanceStrategy and BestResponseTimeBalanceStrategy. === modified file 'src/com/mysql/jdbc/PreparedStatement.java' --- src/com/mysql/jdbc/PreparedStatement.java 2009-01-23 18:21:18 +0000 +++ src/com/mysql/jdbc/PreparedStatement.java 2009-03-02 21:44:28 +0000 @@ -2100,16 +2100,37 @@ String quoteCharStr = this.connection.getMetaData() .getIdentifierQuoteString(); + int valuesSearchStart = this.statementAfterCommentsPos; int indexOfValues = -1; - if (quoteCharStr.length() > 0) { - indexOfValues = StringUtils.indexOfIgnoreCaseRespectQuotes( - this.statementAfterCommentsPos, - this.originalSql, "VALUES ", quoteCharStr.charAt(0), false); - } else { - indexOfValues = StringUtils.indexOfIgnoreCase(this.statementAfterCommentsPos, - this.originalSql, - "VALUES "); + while (indexOfValues == -1) { + if (quoteCharStr.length() > 0) { + indexOfValues = StringUtils.indexOfIgnoreCaseRespectQuotes( + valuesSearchStart, + this.originalSql, "VALUES ", quoteCharStr.charAt(0), false); + } else { + indexOfValues = StringUtils.indexOfIgnoreCase(valuesSearchStart, + this.originalSql, + "VALUES "); + } + /* check if the char immediately preceding VALUES may be part of the table name */ + if (indexOfValues > 0) { + char c = this.originalSql.charAt(indexOfValues - 1); + switch(c) { + case ' ': + case ')': + case '`': + case '\t': + case '\n': + break; + default: + valuesSearchStart = indexOfValues + 7; + indexOfValues = -1; + break; + } + } else { + break; + } } if (indexOfValues == -1) { === modified file 'src/testsuite/regression/StatementRegressionTest.java' --- src/testsuite/regression/StatementRegressionTest.java 2009-02-02 17:00:13 +0000 +++ src/testsuite/regression/StatementRegressionTest.java 2009-03-02 21:44:28 +0000 @@ -5825,4 +5825,25 @@ closeMemberJDBCResources(); } } + + /* + * Bug #40439 - Error rewriting batched statement if table name ends with "values". + */ + public void testBug40439() throws Exception { + Connection conn2 = null; + try { + createTable("testBug40439VALUES", "(x int)"); + conn2 = getConnectionWithProps("rewriteBatchedStatements=true"); + PreparedStatement ps = conn2.prepareStatement("insert into testBug40439VALUES (x) values (?)"); + ps.setInt(1, 1); + ps.addBatch(); + ps.setInt(1, 2); + ps.addBatch(); + ps.executeBatch(); + } finally { + if(conn2 != null) + try { conn2.close(); } catch(SQLException ex) {} + closeMemberJDBCResources(); + } + } } \ No newline at end of file