提交 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 {
query.query(0, this);
} else {
ResultInterface rows = query.query(0);
int updatedRows = 0;
while (rows.next()) {
generatedKeys.nextRow();
Value[] r = rows.currentRow();
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);
if (newRow != null) {
generatedKeys.confirmRow(newRow);
......@@ -217,7 +223,7 @@ public class Insert extends Prepared implements ResultTarget {
if (handleOnDuplicate(de)) {
// MySQL returns 2 for updated row
// TODO: detect no-op change
rowNumber++;
updatedRows++;
} else {
// INSERT IGNORE case
rowNumber--;
......@@ -225,6 +231,7 @@ public class Insert extends Prepared implements ResultTarget {
}
}
rows.close();
rowNumber += updatedRows;
}
}
table.fire(session, Trigger.INSERT, false);
......@@ -239,14 +246,12 @@ public class Insert extends Prepared implements ResultTarget {
private Row addRowImpl(Value[] values) {
Row newRow = table.getTemplateRow();
setCurrentRowNumber(++rowNumber);
Expression[] exp = new Expression[columns.length];
for (int j = 0, len = columns.length; j < len; j++) {
Column c = columns[j];
int index = c.getColumnId();
try {
Value v = c.convert(values[j], session.getDatabase().getMode());
newRow.setValue(index, v);
exp[j] = ValueExpression.get(v);
} catch (DbException ex) {
throw setRow(ex, rowNumber, getSQL(values));
}
......@@ -254,7 +259,6 @@ public class Insert extends Prepared implements ResultTarget {
table.validateConvertUpdateSequence(session, newRow);
boolean done = table.fireBeforeRow(session, null, newRow);
if (!done) {
addRow(exp);
table.addRow(session, newRow);
session.log(table, UndoLogRecord.INSERT, newRow);
table.fireAfterRow(session, null, newRow, false);
......
......@@ -76,3 +76,23 @@ SELECT * FROM TEST ORDER BY ID;
> 6 61
> 7 71
> 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论