001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.command.dml;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.command.Prepared;
011: import org.h2.engine.Right;
012: import org.h2.engine.Session;
013: import org.h2.expression.Expression;
014: import org.h2.log.UndoLogRecord;
015: import org.h2.result.LocalResult;
016: import org.h2.result.Row;
017: import org.h2.result.RowList;
018: import org.h2.table.PlanItem;
019: import org.h2.table.Table;
020: import org.h2.table.TableFilter;
021: import org.h2.util.StringUtils;
022:
023: /**
024: * This class represents the statement
025: * DELETE
026: */
027: public class Delete extends Prepared {
028:
029: private Expression condition;
030: private TableFilter tableFilter;
031:
032: public Delete(Session session) {
033: super (session);
034: }
035:
036: public void setTableFilter(TableFilter tableFilter) {
037: this .tableFilter = tableFilter;
038: }
039:
040: public void setCondition(Expression condition) {
041: this .condition = condition;
042: }
043:
044: public int update() throws SQLException {
045: tableFilter.startQuery(session);
046: tableFilter.reset();
047: Table table = tableFilter.getTable();
048: session.getUser().checkRight(table, Right.DELETE);
049: table.fireBefore(session);
050: table.lock(session, true, false);
051: RowList rows = new RowList(session);
052: try {
053: setCurrentRowNumber(0);
054: while (tableFilter.next()) {
055: checkCancelled();
056: setCurrentRowNumber(rows.size() + 1);
057: if (condition == null
058: || Boolean.TRUE.equals(condition
059: .getBooleanValue(session))) {
060: Row row = tableFilter.get();
061: if (table.fireRow()) {
062: table.fireBeforeRow(session, row, null);
063: }
064: rows.add(row);
065: }
066: }
067: for (rows.reset(); rows.hasNext();) {
068: checkCancelled();
069: Row row = rows.next();
070: table.removeRow(session, row);
071: session.log(table, UndoLogRecord.DELETE, row);
072: }
073: if (table.fireRow()) {
074: for (rows.reset(); rows.hasNext();) {
075: Row row = rows.next();
076: table.fireAfterRow(session, row, null);
077: }
078: }
079: table.fireAfter(session);
080: return rows.size();
081: } finally {
082: rows.close();
083: }
084: }
085:
086: public String getPlanSQL() {
087: StringBuffer buff = new StringBuffer();
088: buff.append("DELETE FROM ");
089: buff.append(tableFilter.getPlanSQL(false));
090: if (condition != null) {
091: buff.append("\nWHERE "
092: + StringUtils.unEnclose(condition.getSQL()));
093: }
094: return buff.toString();
095: }
096:
097: public void prepare() throws SQLException {
098: if (condition != null) {
099: condition.mapColumns(tableFilter, 0);
100: condition = condition.optimize(session);
101: condition.createIndexConditions(session, tableFilter);
102: }
103: PlanItem item = tableFilter.getBestPlanItem(session);
104: tableFilter.setPlanItem(item);
105: tableFilter.prepare();
106: }
107:
108: public boolean isTransactional() {
109: return true;
110: }
111:
112: public LocalResult queryMeta() {
113: return null;
114: }
115:
116: }
|