001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.Vector;
042:
043: import java.io.IOException;
044:
045: import java.sql.ResultSet;
046: import java.sql.SQLException;
047:
048: import com.quadcap.sql.index.Btree;
049:
050: import com.quadcap.util.Debug;
051:
052: /**
053: * Implementation of the SQL <B>DELETE</B> statement.
054: *
055: * @author Stan Bailes
056: */
057:
058: public class StmtDelete implements Stmt {
059: String tableName;
060: SelectExpression select;
061:
062: /**
063: * Default constructor
064: */
065: public StmtDelete() {
066: }
067:
068: /**
069: * Explicit constructor from table name and predicate
070: */
071: public StmtDelete(String tableName, Expression predicate) {
072: this .tableName = tableName;
073: this .select = new SelectExpression(tableName, predicate);
074: }
075:
076: /**
077: * Execute the delete operation: Get a table cursor, maybe one with
078: * a predicate (if there's a "WHERE", implying that we really don't
079: * want to delete *all* the rows.)
080: */
081: public void execute(Session session) throws IOException,
082: SQLException {
083: try {
084: Database db = session.getDatabase();
085: Relation t = db.getRelation(tableName);
086: if (t == null) {
087: throw new SQLException("No such table/view: "
088: + tableName, "42000");
089: }
090: if (!t.isUpdatable()) {
091: throw new SQLException("Not updatable: " + tableName,
092: "42000");
093: }
094: session.getTableWriteLock(tableName);
095: Cursor delCursor = select.getCursor(session, null);
096: try {
097: while (delCursor.next()) {
098: delCursor.deleteRow();
099: }
100: } finally {
101: delCursor.close();
102: }
103: } catch (IOException e) {
104: throw DbException.wrapThrowable(e);
105: }
106:
107: }
108: }
|