001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.db.sql;
031:
032: import com.caucho.db.Database;
033: import com.caucho.db.store.Transaction;
034: import com.caucho.db.table.Column;
035: import com.caucho.db.table.Table;
036: import com.caucho.db.table.TableIterator;
037: import com.caucho.log.Log;
038:
039: import java.sql.SQLException;
040: import java.util.logging.Logger;
041:
042: class UpdateQuery extends Query {
043: private static final Logger log = Logger
044: .getLogger(UpdateQuery.class.getName());
045:
046: private final Table _table;
047: private SetItem[] _setItems;
048:
049: UpdateQuery(Database db, String sql, Table table)
050: throws SQLException {
051: super (db, sql, null);
052:
053: _table = table;
054:
055: setFromItems(new FromItem[] { new FromItem(table, table
056: .getName()) });
057: }
058:
059: public void setSetItems(SetItem[] setItems) {
060: _setItems = setItems;
061: }
062:
063: public boolean isReadOnly() {
064: return false;
065: }
066:
067: /**
068: * Binds the query.
069: */
070: protected void bind() throws SQLException {
071: super .bind();
072:
073: for (int i = 0; i < _setItems.length; i++) {
074: Expr expr = _setItems[i].getExpr();
075:
076: expr = expr.bind(this );
077:
078: _setItems[i].setExpr(expr);
079: }
080: }
081:
082: /**
083: * Executes the query.
084: */
085: public void execute(QueryContext context, Transaction xa)
086: throws SQLException {
087: int count = 0;
088: SetItem[] setItems = _setItems;
089: TableIterator[] rows = new TableIterator[1];
090: rows[0] = _table.createTableIterator();
091: context.init(xa, rows, isReadOnly());
092:
093: try {
094: if (!start(rows, rows.length, context, xa))
095: return;
096:
097: do {
098: TableIterator iter = rows[0];
099: iter.setDirty();
100:
101: for (int i = 0; i < setItems.length; i++) {
102: Column column = setItems[i].getColumn();
103: Expr expr = setItems[i].getExpr();
104:
105: column.set(xa, iter, expr, context);
106: }
107:
108: context.setRowUpdateCount(++count);
109: } while (nextTuple(rows, rows.length, context, xa));
110: } finally {
111: // autoCommitWrite must be before freeRows in case freeRows
112: // throws an exception
113: context.unlock();
114:
115: freeRows(rows, rows.length);
116: }
117: }
118:
119: public String toString() {
120: StringBuilder cb = new StringBuilder();
121: cb.append("UpdateQuery[");
122: if (_whereExpr != null) {
123: cb.append(",where:" + _whereExpr);
124: }
125: cb.append("]");
126:
127: return cb.toString();
128: }
129: }
|