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.util.Debug;
049:
050: /**
051: * Implementation of the SQL <b>UPDATE</b> statement.
052: *
053: * @author Stan Bailes
054: */
055:
056: public class StmtUpdate implements Stmt {
057: String tableName;
058: SelectExpression select;
059: Vector items;
060:
061: /**
062: * Default constructor
063: */
064: public StmtUpdate() {
065: }
066:
067: /**
068: * Explicit constructor from table name only
069: */
070: public StmtUpdate(String tableName) {
071: this .tableName = tableName;
072: this .items = new Vector();
073: }
074:
075: /**
076: * Add a new update item
077: */
078: public void addItem(UpdateItem item) {
079: items.addElement(item);
080: }
081:
082: /**
083: * Add a 'where' expression
084: */
085: public void addWhere(Expression where) {
086: select = new SelectExpression(tableName, where);
087: }
088:
089: /**
090: * Excute the update operation
091: */
092: public void execute(Session session) throws IOException,
093: SQLException {
094: Database db = session.getDatabase();
095: Relation t = db.getRelation(tableName);
096: if (t == null) {
097: throw new SQLException("No such table/view: " + tableName,
098: "42000");
099: }
100: if (!t.isUpdatable()) {
101: throw new SQLException("Not updatable: " + tableName,
102: "42000");
103: }
104: session.getTableWriteLock(tableName);
105: Cursor updateCursor = TableOps.getCursor(session, t, select);
106: try {
107: while (updateCursor.next()) {
108: Row row = updateCursor.getRow();
109: for (int i = 0; i < items.size(); i++) {
110: UpdateItem item = (UpdateItem) items.elementAt(i);
111: item.evaluate(session, t, updateCursor);
112: }
113: for (int i = 0; i < items.size(); i++) {
114: UpdateItem item = (UpdateItem) items.elementAt(i);
115: item.update(session, row);
116: }
117: session.clearViewCheck();
118: updateCursor.updateRow(row); // ZZZ hot
119: }
120: } finally {
121: updateCursor.close();
122: }
123: }
124: }
|