提交 a15f2efc authored 作者: Thomas Mueller's avatar Thomas Mueller

The pseudo-column "_ROWID_" now supports insert, update, and merge (only enabled…

The pseudo-column "_ROWID_" now supports insert, update, and merge (only enabled for version 1.3.x).
上级 497a41c3
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
*/ */
package org.h2.command.dml; package org.h2.command.dml;
import java.util.ArrayList;
import java.util.HashMap;
import org.h2.api.Trigger; import org.h2.api.Trigger;
import org.h2.command.CommandInterface; import org.h2.command.CommandInterface;
import org.h2.command.Prepared; import org.h2.command.Prepared;
...@@ -23,6 +25,7 @@ import org.h2.table.Column; ...@@ -23,6 +25,7 @@ import org.h2.table.Column;
import org.h2.table.PlanItem; import org.h2.table.PlanItem;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.New;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.value.Value; import org.h2.value.Value;
...@@ -35,7 +38,8 @@ public class Update extends Prepared { ...@@ -35,7 +38,8 @@ public class Update extends Prepared {
private Expression condition; private Expression condition;
private TableFilter tableFilter; private TableFilter tableFilter;
private Expression[] expressions; private ArrayList<Column> columns = New.arrayList();
private HashMap<Column, Expression> expressionMap = New.hashMap();
public Update(Session session) { public Update(Session session) {
super(session); super(session);
...@@ -43,8 +47,6 @@ public class Update extends Prepared { ...@@ -43,8 +47,6 @@ public class Update extends Prepared {
public void setTableFilter(TableFilter tableFilter) { public void setTableFilter(TableFilter tableFilter) {
this.tableFilter = tableFilter; this.tableFilter = tableFilter;
Table table = tableFilter.getTable();
expressions = new Expression[table.getColumns().length];
} }
public void setCondition(Expression condition) { public void setCondition(Expression condition) {
...@@ -58,12 +60,12 @@ public class Update extends Prepared { ...@@ -58,12 +60,12 @@ public class Update extends Prepared {
* @param expression the expression * @param expression the expression
*/ */
public void setAssignment(Column column, Expression expression) { public void setAssignment(Column column, Expression expression) {
int id = column.getColumnId(); if (expressionMap.containsKey(column)) {
if (expressions[id] != null) {
throw DbException.get(ErrorCode.DUPLICATE_COLUMN_NAME_1, column throw DbException.get(ErrorCode.DUPLICATE_COLUMN_NAME_1, column
.getName()); .getName());
} }
expressions[id] = expression; columns.add(column);
expressionMap.put(column, expression);
if (expression instanceof Parameter) { if (expression instanceof Parameter) {
Parameter p = (Parameter) expression; Parameter p = (Parameter) expression;
p.setColumn(column); p.setColumn(column);
...@@ -83,13 +85,14 @@ public class Update extends Prepared { ...@@ -83,13 +85,14 @@ public class Update extends Prepared {
// get the old rows, compute the new rows // get the old rows, compute the new rows
setCurrentRowNumber(0); setCurrentRowNumber(0);
int count = 0; int count = 0;
Column[] columns = table.getColumns();
while (tableFilter.next()) { while (tableFilter.next()) {
setCurrentRowNumber(count+1); setCurrentRowNumber(count+1);
if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) { if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
Row oldRow = tableFilter.get(); Row oldRow = tableFilter.get();
Row newRow = table.getTemplateRow(); Row newRow = table.getTemplateRow();
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
Expression newExpr = expressions[i]; Expression newExpr = expressionMap.get(columns[i]);
Value newValue; Value newValue;
if (newExpr == null) { if (newExpr == null) {
newValue = oldRow.getValue(i); newValue = oldRow.getValue(i);
...@@ -141,15 +144,11 @@ public class Update extends Prepared { ...@@ -141,15 +144,11 @@ public class Update extends Prepared {
public String getPlanSQL() { public String getPlanSQL() {
StatementBuilder buff = new StatementBuilder("UPDATE "); StatementBuilder buff = new StatementBuilder("UPDATE ");
buff.append(tableFilter.getPlanSQL(false)).append("\nSET\n "); buff.append(tableFilter.getPlanSQL(false)).append("\nSET\n ");
Table table = tableFilter.getTable(); for (int i = 0, size = columns.size(); i < size; i++) {
int columnCount = table.getColumns().length; Column c = columns.get(i);
for (int i = 0; i < columnCount; i++) { Expression e = expressionMap.get(c);
Expression newExpr = expressions[i]; buff.appendExceptFirst(",\n ");
if (newExpr != null) { buff.append(c.getName()).append(" = ").append(e.getSQL());
Column column = table.getColumn(i);
buff.appendExceptFirst(",\n ");
buff.append(column.getName()).append(" = ").append(newExpr.getSQL());
}
} }
if (condition != null) { if (condition != null) {
buff.append("\nWHERE ").append(StringUtils.unEnclose(condition.getSQL())); buff.append("\nWHERE ").append(StringUtils.unEnclose(condition.getSQL()));
...@@ -163,12 +162,11 @@ public class Update extends Prepared { ...@@ -163,12 +162,11 @@ public class Update extends Prepared {
condition = condition.optimize(session); condition = condition.optimize(session);
condition.createIndexConditions(session, tableFilter); condition.createIndexConditions(session, tableFilter);
} }
for (int i = 0, len = expressions.length; i < len; i++) { for (int i = 0, size = columns.size(); i < size; i++) {
Expression expr = expressions[i]; Column c = columns.get(i);
if (expr != null) { Expression e = expressionMap.get(c);
expr.mapColumns(tableFilter, 0); e.mapColumns(tableFilter, 0);
expressions[i] = expr.optimize(session); expressionMap.put(c, e.optimize(session));
}
} }
PlanItem item = tableFilter.getBestPlanItem(session, 1); PlanItem item = tableFilter.getBestPlanItem(session, 1);
tableFilter.setPlanItem(item); tableFilter.setPlanItem(item);
......
...@@ -5960,7 +5960,7 @@ EXPLAIN SELECT * FROM TEST WHERE ID=1 GROUP BY NAME, ID; ...@@ -5960,7 +5960,7 @@ EXPLAIN SELECT * FROM TEST WHERE ID=1 GROUP BY NAME, ID;
EXPLAIN PLAN FOR UPDATE TEST SET NAME='Hello', ID=1 WHERE NAME LIKE 'T%' ESCAPE 'x'; EXPLAIN PLAN FOR UPDATE TEST SET NAME='Hello', ID=1 WHERE NAME LIKE 'T%' ESCAPE 'x';
> PLAN > PLAN
> --------------------------------------------------------------------------------------------------------- > ---------------------------------------------------------------------------------------------------------
> UPDATE PUBLIC.TEST /* PUBLIC.TEST.tableScan */ SET ID = 1, NAME = 'Hello' WHERE NAME LIKE 'T%' ESCAPE 'x' > UPDATE PUBLIC.TEST /* PUBLIC.TEST.tableScan */ SET NAME = 'Hello', ID = 1 WHERE NAME LIKE 'T%' ESCAPE 'x'
> rows: 1 > rows: 1
EXPLAIN PLAN FOR DELETE FROM TEST; EXPLAIN PLAN FOR DELETE FROM TEST;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论