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: import com.caucho.sql.SQLExceptionWrapper;
039:
040: import java.sql.SQLException;
041: import java.util.ArrayList;
042: import java.util.logging.Logger;
043:
044: class InsertQuery extends Query {
045: private static final Logger log = Log.open(InsertQuery.class);
046:
047: private Table _table;
048:
049: private ArrayList<Column> _columns;
050: private ArrayList<Expr> _values;
051:
052: InsertQuery(Database db, String sql, Table table,
053: ArrayList<Column> columns) throws SQLException {
054: super (db, sql, null);
055:
056: _table = table;
057:
058: _columns = columns;
059: }
060:
061: public boolean isReadOnly() {
062: return false;
063: }
064:
065: public void setValues(ArrayList<Expr> values) {
066: _values = values;
067: }
068:
069: void init() throws SQLException {
070: Column[] tableColumns = _table.getColumns();
071:
072: for (int i = 0; i < tableColumns.length; i++) {
073: Column column = tableColumns[i];
074:
075: Expr defaultExpr = column.getDefault();
076:
077: if (column.getAutoIncrement() > 0) {
078: defaultExpr = new AutoIncrementExpr(column.getTable());
079: }
080:
081: if (defaultExpr == null)
082: continue;
083:
084: int j = 0;
085: for (; j < _columns.size(); j++) {
086: if (_columns.get(j) == column)
087: break;
088: }
089:
090: if (j == _columns.size()) {
091: _columns.add(column);
092: _values.add(new NullExpr());
093: }
094:
095: _values
096: .set(
097: j,
098: new DefaultExpr(_values.get(j), defaultExpr));
099: }
100: }
101:
102: /**
103: * Executes the query.
104: */
105: public void execute(QueryContext queryContext, Transaction xa)
106: throws SQLException {
107: TableIterator[] rows = new TableIterator[1];
108: rows[0] = _table.createTableIterator();
109: queryContext.init(xa, rows, isReadOnly());
110:
111: try {
112: _table.insert(queryContext, xa, _columns, _values);
113:
114: queryContext.setRowUpdateCount(1);
115: } catch (java.io.IOException e) {
116: throw new SQLExceptionWrapper(e);
117: } finally {
118: queryContext.unlock();
119: }
120: }
121:
122: public String toString() {
123: return "InsertQuery[]";
124: }
125: }
|