提交 55869c8f authored 作者: XEHA6284's avatar XEHA6284

Add support for INSERT IGNORE from SELECT in MySQL Mode

上级 9f7ef4b1
...@@ -205,10 +205,16 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -205,10 +205,16 @@ public class Insert extends Prepared implements ResultTarget {
query.query(0, this); query.query(0, this);
} else { } else {
ResultInterface rows = query.query(0); ResultInterface rows = query.query(0);
int updatedRows = 0;
while (rows.next()) { while (rows.next()) {
generatedKeys.nextRow(); generatedKeys.nextRow();
Value[] r = rows.currentRow(); Value[] r = rows.currentRow();
try { try {
Expression[] exp = new Expression[columns.length];
for (int j = 0, len = columns.length; j < len; j++) {
exp[j] = ValueExpression.get(r[j]);
}
addRow(exp);
Row newRow = addRowImpl(r); Row newRow = addRowImpl(r);
if (newRow != null) { if (newRow != null) {
generatedKeys.confirmRow(newRow); generatedKeys.confirmRow(newRow);
...@@ -217,7 +223,7 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -217,7 +223,7 @@ public class Insert extends Prepared implements ResultTarget {
if (handleOnDuplicate(de)) { if (handleOnDuplicate(de)) {
// MySQL returns 2 for updated row // MySQL returns 2 for updated row
// TODO: detect no-op change // TODO: detect no-op change
rowNumber++; updatedRows++;
} else { } else {
// INSERT IGNORE case // INSERT IGNORE case
rowNumber--; rowNumber--;
...@@ -225,6 +231,7 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -225,6 +231,7 @@ public class Insert extends Prepared implements ResultTarget {
} }
} }
rows.close(); rows.close();
rowNumber += updatedRows;
} }
} }
table.fire(session, Trigger.INSERT, false); table.fire(session, Trigger.INSERT, false);
...@@ -239,14 +246,12 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -239,14 +246,12 @@ public class Insert extends Prepared implements ResultTarget {
private Row addRowImpl(Value[] values) { private Row addRowImpl(Value[] values) {
Row newRow = table.getTemplateRow(); Row newRow = table.getTemplateRow();
setCurrentRowNumber(++rowNumber); setCurrentRowNumber(++rowNumber);
Expression[] exp = new Expression[columns.length];
for (int j = 0, len = columns.length; j < len; j++) { for (int j = 0, len = columns.length; j < len; j++) {
Column c = columns[j]; Column c = columns[j];
int index = c.getColumnId(); int index = c.getColumnId();
try { try {
Value v = c.convert(values[j], session.getDatabase().getMode()); Value v = c.convert(values[j], session.getDatabase().getMode());
newRow.setValue(index, v); newRow.setValue(index, v);
exp[j] = ValueExpression.get(v);
} catch (DbException ex) { } catch (DbException ex) {
throw setRow(ex, rowNumber, getSQL(values)); throw setRow(ex, rowNumber, getSQL(values));
} }
...@@ -254,7 +259,6 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -254,7 +259,6 @@ public class Insert extends Prepared implements ResultTarget {
table.validateConvertUpdateSequence(session, newRow); table.validateConvertUpdateSequence(session, newRow);
boolean done = table.fireBeforeRow(session, null, newRow); boolean done = table.fireBeforeRow(session, null, newRow);
if (!done) { if (!done) {
addRow(exp);
table.addRow(session, newRow); table.addRow(session, newRow);
session.log(table, UndoLogRecord.INSERT, newRow); session.log(table, UndoLogRecord.INSERT, newRow);
table.fireAfterRow(session, null, newRow, false); table.fireAfterRow(session, null, newRow, false);
......
...@@ -76,3 +76,23 @@ SELECT * FROM TEST ORDER BY ID; ...@@ -76,3 +76,23 @@ SELECT * FROM TEST ORDER BY ID;
> 6 61 > 6 61
> 7 71 > 7 71
> rows (ordered): 7 > rows (ordered): 7
INSERT INTO TESTREF VALUES (8, 81), (9, 91);
> update count: 2
INSERT INTO TEST (ID, VALUE) SELECT ID, VALUE FROM TESTREF ON DUPLICATE KEY UPDATE VALUE=83;
> update count: 10
SELECT * FROM TEST ORDER BY ID;
> ID VALUE
> -- -----
> 1 83
> 2 83
> 3 30
> 4 40
> 5 52
> 6 83
> 7 83
> 8 81
> 9 91
> rows (ordered): 9
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论