Unverified 提交 03e45626 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #899 from katzyn/on_duplicate_key_update

Fix ON DUPLICATE KEY UPDATE for inserts with multiple rows
...@@ -375,12 +375,13 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -375,12 +375,13 @@ public class Insert extends Prepared implements ResultTarget {
ArrayList<String> variableNames = new ArrayList<>( ArrayList<String> variableNames = new ArrayList<>(
duplicateKeyAssignmentMap.size()); duplicateKeyAssignmentMap.size());
Expression[] row = list.get(getCurrentRowNumber() - 1);
for (int i = 0; i < columns.length; i++) { for (int i = 0; i < columns.length; i++) {
String key = table.getSchema().getName() + "." + String key = table.getSchema().getName() + "." +
table.getName() + "." + columns[i].getName(); table.getName() + "." + columns[i].getName();
variableNames.add(key); variableNames.add(key);
session.setVariable(key, session.setVariable(key,
list.get(getCurrentRowNumber() - 1)[i].getValue(session)); row[i].getValue(session));
} }
StatementBuilder buff = new StatementBuilder("UPDATE "); StatementBuilder buff = new StatementBuilder("UPDATE ");
...@@ -425,6 +426,7 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -425,6 +426,7 @@ public class Insert extends Prepared implements ResultTarget {
indexedColumns = foundIndex.getColumns(); indexedColumns = foundIndex.getColumns();
} }
Expression[] row = list.get(getCurrentRowNumber() - 1);
Expression condition = null; Expression condition = null;
for (Column column : indexedColumns) { for (Column column : indexedColumns) {
ExpressionColumn expr = new ExpressionColumn(session.getDatabase(), ExpressionColumn expr = new ExpressionColumn(session.getDatabase(),
...@@ -433,14 +435,12 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -433,14 +435,12 @@ public class Insert extends Prepared implements ResultTarget {
for (int i = 0; i < columns.length; i++) { for (int i = 0; i < columns.length; i++) {
if (expr.getColumnName().equals(columns[i].getName())) { if (expr.getColumnName().equals(columns[i].getName())) {
if (condition == null) { if (condition == null) {
condition = new Comparison(session, Comparison.EQUAL, condition = new Comparison(session, Comparison.EQUAL, expr, row[i]);
expr, list.get(getCurrentRowNumber() - 1)[i++]);
} else { } else {
condition = new ConditionAndOr(ConditionAndOr.AND, condition = new ConditionAndOr(ConditionAndOr.AND, condition,
condition, new Comparison(session, Comparison.EQUAL, expr, row[i]));
new Comparison(session, Comparison.EQUAL, expr,
list.get(0)[i++]));
} }
break;
} }
} }
} }
......
...@@ -109,12 +109,14 @@ public class TestDuplicateKeyUpdate extends TestBase { ...@@ -109,12 +109,14 @@ public class TestDuplicateKeyUpdate extends TestBase {
assertEquals("UPDATE", rs.getNString(1)); assertEquals("UPDATE", rs.getNString(1));
stat.execute("INSERT INTO table_test2 (a_text, some_text, updatable_text ) " + stat.execute("INSERT INTO table_test2 (a_text, some_text, updatable_text ) " +
"VALUES ('b', 'b', 'test') " + "VALUES ('b', 'b', 'test'), ('c', 'c', 'test2') " +
"ON DUPLICATE KEY UPDATE updatable_text=values(updatable_text)"); "ON DUPLICATE KEY UPDATE updatable_text=values(updatable_text)");
rs = stat.executeQuery("SELECT updatable_text " + rs = stat.executeQuery("SELECT updatable_text " +
"FROM table_test2 where a_text = 'b'"); "FROM table_test2 where a_text in ('b', 'c') order by a_text");
rs.next(); rs.next();
assertEquals("test", rs.getNString(1)); assertEquals("test", rs.getNString(1));
rs.next();
assertEquals("test2", rs.getNString(1));
} }
private void testDuplicateCache(Connection conn) throws SQLException { private void testDuplicateCache(Connection conn) throws SQLException {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论