001: /*
002: * $Id: DeleteCommand.java,v 1.33 2005/12/22 09:02:29 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2006 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.commands;
042:
043: import org.axiondb.AxionException;
044: import org.axiondb.Database;
045: import org.axiondb.Row;
046: import org.axiondb.RowDecorator;
047: import org.axiondb.RowIterator;
048: import org.axiondb.Selectable;
049: import org.axiondb.Table;
050: import org.axiondb.TableIdentifier;
051: import org.axiondb.engine.visitors.FindBindVariableVisitor;
052: import org.axiondb.jdbc.AxionResultSet;
053:
054: /**
055: * A <tt>DELETE</tt> command.
056: *
057: * @version $Revision: 1.33 $ $Date: 2005/12/22 09:02:29 $
058: * @author Rodney Waldhoff
059: * @author Chuck Burdick
060: * @author Ahimanikya Satapathy
061: */
062: public class DeleteCommand extends ChildTableUpdater {
063:
064: public DeleteCommand(String tableName, Selectable where) {
065: setTable(new TableIdentifier(tableName));
066: setWhere(where);
067: }
068:
069: public DeleteCommand(TableIdentifier table, Selectable where) {
070: setTable(table);
071: setWhere(where);
072: }
073:
074: public boolean execute(Database database) throws AxionException {
075: executeUpdate(database);
076: return false;
077: }
078:
079: /** Unsupported */
080: public AxionResultSet executeQuery(Database database)
081: throws AxionException {
082: throw new UnsupportedOperationException("Use executeUpdate.");
083: }
084:
085: public int executeUpdate(org.axiondb.Database db)
086: throws AxionException {
087: assertNotReadOnly(db);
088: resolve(db);
089:
090: Table table = db.getTable(getTable());
091: if (null == table) {
092: throw new AxionException("Table " + getTable()
093: + " not found.");
094: }
095:
096: int deletecount = -1;
097: if (_where == null) {
098: deletecount = tryToTruncate(db);
099: }
100:
101: if (deletecount == -1) {
102: deletecount = 0;
103: if (_dec == null) {
104: _dec = makeRowDecorator(table);
105: }
106: RowIterator rows = getRowIterator(db, getTable(), table,
107: getWhere(), false, _dec);
108: setDeferAllConstraintIfRequired(table);
109: while (rows.hasNext()) {
110: Row row = rows.next();
111: _dec.setRow(row);
112: deleteOrSetNullChildRows(db, table, _dec);
113: rows.remove();
114: deletecount++;
115: }
116: }
117:
118: setEffectedRowCount(deletecount);
119: return deletecount;
120: }
121:
122: private int tryToTruncate(Database db) {
123: TruncateCommand tc = new TruncateCommand();
124: tc.setObjectName(_tableId.getTableName());
125: try {
126: return tc.executeUpdate(db);
127: } catch (AxionException e) {
128: //
129: }
130: return -1;
131: }
132:
133: public final TableIdentifier getTable() {
134: return _tableId;
135: }
136:
137: public final Selectable getWhere() {
138: return _where;
139: }
140:
141: protected void buildBindVariables() {
142: setBindVariableVisitor(new FindBindVariableVisitor());
143: getBindVariableVisitor().visit(getWhere());
144: }
145:
146: protected void resolve(Database db) throws AxionException {
147: if (!_resolved) {
148: TableIdentifier[] tables = new TableIdentifier[] { getTable() };
149: setWhere(resolveSelectable(getWhere(), db, tables));
150:
151: _resolved = true;
152: }
153: }
154:
155: private void setTable(TableIdentifier tableId) {
156: _tableId = tableId;
157: }
158:
159: private void setWhere(Selectable where) {
160: _where = where;
161: }
162:
163: private boolean _resolved = false;
164: private TableIdentifier _tableId;
165: private Selectable _where;
166: private RowDecorator _dec;
167: }
|