File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\dml\Delete.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.dml;
8
9 import org.h2.api.Trigger;
10 import org.h2.command.CommandInterface;
11 import org.h2.command.Prepared;
12 import org.h2.engine.Right;
13 import org.h2.engine.Session;
14 import org.h2.engine.UndoLogRecord;
15 import org.h2.expression.Expression;
16 import org.h2.result.ResultInterface;
17 import org.h2.result.Row;
18 import org.h2.result.RowList;
19 import org.h2.table.PlanItem;
20 import org.h2.table.Table;
21 import org.h2.table.TableFilter;
22 import org.h2.util.StringUtils;
23 import org.h2.value.Value;
24 import org.h2.value.ValueNull;
25
26 /**
27 * This class represents the statement
28 * DELETE
29 */
30 public class Delete extends Prepared {
31
32 private Expression condition;
33 private TableFilter tableFilter;
34
35 /**
36 * The limit expression as specified in the LIMIT or TOP clause.
37 */
38 private Expression limitExpr;
39
40 public Delete(Session session) {
41 super(session);
42 }
43
44 public void setTableFilter(TableFilter tableFilter) {
45 this.tableFilter = tableFilter;
46 }
47
48 public void setCondition(Expression condition) {
49 this.condition = condition;
50 }
51
52 public int update() {
53 tableFilter.startQuery(session);
54 tableFilter.reset();
55 Table table = tableFilter.getTable();
56 session.getUser().checkRight(table, Right.DELETE);
57 table.fire(session, Trigger.DELETE, true);
58 table.lock(session, true, false);
59 RowList rows = new RowList(session);
60 int limitRows = -1;
61 if (limitExpr != null) {
62 Value v = limitExpr.getValue(session);
63 if (v != ValueNull.INSTANCE) {
64 limitRows = v.getInt();
65 }
66 }
67 try {
68 setCurrentRowNumber(0);
69 int count = 0;
70 while (limitRows != 0 && tableFilter.next()) {
71 setCurrentRowNumber(rows.size() + 1);
72 if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
73 Row row = tableFilter.get();
74 boolean done = false;
75 if (table.fireRow()) {
76 done = table.fireBeforeRow(session, row, null);
77 }
78 if (!done) {
79 rows.add(row);
80 }
81 count++;
82 if (limitRows >= 0 && count >= limitRows) {
83 break;
84 }
85 }
86 }
87 int rowScanCount = 0;
88 for (rows.reset(); rows.hasNext();) {
89 if ((++rowScanCount & 127) == 0) {
90 checkCanceled();
91 }
92 Row row = rows.next();
93 table.removeRow(session, row);
94 session.log(table, UndoLogRecord.DELETE, row);
95 }
96 if (table.fireRow()) {
97 for (rows.reset(); rows.hasNext();) {
98 Row row = rows.next();
99 table.fireAfterRow(session, row, null, false);
100 }
101 }
102 table.fire(session, Trigger.DELETE, false);
103 return count;
104 } finally {
105 rows.close();
106 }
107 }
108
109 public String getPlanSQL() {
110 StringBuilder buff = new StringBuilder();
111 buff.append("DELETE ");
112 buff.append("FROM ").append(tableFilter.getPlanSQL(false));
113 if (condition != null) {
114 buff.append("\nWHERE ").append(StringUtils.unEnclose(condition.getSQL()));
115 }
116 if (limitExpr != null) {
117 buff.append("\nLIMIT (").append(StringUtils.unEnclose(limitExpr.getSQL())).append(')');
118 }
119 return buff.toString();
120 }
121
122 public void prepare() {
123 if (condition != null) {
124 condition.mapColumns(tableFilter, 0);
125 condition = condition.optimize(session);
126 condition.createIndexConditions(session, tableFilter);
127 }
128 PlanItem item = tableFilter.getBestPlanItem(session, 1);
129 tableFilter.setPlanItem(item);
130 tableFilter.prepare();
131 }
132
133 public boolean isTransactional() {
134 return true;
135 }
136
137 public ResultInterface queryMeta() {
138 return null;
139 }
140
141 public int getType() {
142 return CommandInterface.DELETE;
143 }
144
145 public void setLimit(Expression limit) {
146 this.limitExpr = limit;
147 }
148
149 public boolean isCacheable() {
150 return true;
151 }
152
153 }
154