KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > InsertQuery


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.sql;
31
32 import com.caucho.db.Database;
33 import com.caucho.db.store.Transaction;
34 import com.caucho.db.table.Column;
35 import com.caucho.db.table.Table;
36 import com.caucho.db.table.TableIterator;
37 import com.caucho.log.Log;
38 import com.caucho.sql.SQLExceptionWrapper;
39
40 import java.sql.SQLException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 class InsertQuery extends Query {
45   private static final Logger JavaDoc log = Log.open(InsertQuery.class);
46
47   private Table _table;
48       
49   private ArrayList JavaDoc<Column> _columns;
50   private ArrayList JavaDoc<Expr> _values;
51
52   InsertQuery(Database db, String JavaDoc sql, Table table, ArrayList JavaDoc<Column> columns)
53     throws SQLException JavaDoc
54   {
55     super(db, sql, null);
56
57     _table = table;
58
59     _columns = columns;
60   }
61
62   public boolean isReadOnly()
63   {
64     return false;
65   }
66
67   public void setValues(ArrayList JavaDoc<Expr> values)
68   {
69     _values = values;
70   }
71
72   void init()
73     throws SQLException JavaDoc
74   {
75     Column []tableColumns = _table.getColumns();
76
77     for (int i = 0; i < tableColumns.length; i++) {
78       Column column = tableColumns[i];
79
80       Expr defaultExpr = column.getDefault();
81
82       if (column.getAutoIncrement() > 0) {
83     defaultExpr = new AutoIncrementExpr(column.getTable());
84       }
85       
86       if (defaultExpr == null)
87     continue;
88
89       int j = 0;
90       for (; j < _columns.size(); j++) {
91     if (_columns.get(j) == column)
92       break;
93       }
94
95       if (j == _columns.size()) {
96     _columns.add(column);
97     _values.add(new NullExpr());
98       }
99
100       _values.set(j, new DefaultExpr(_values.get(j), defaultExpr));
101     }
102   }
103
104   /**
105    * Executes the query.
106    */

107   public void execute(QueryContext queryContext, Transaction xa)
108     throws SQLException JavaDoc
109   {
110     TableIterator []rows = new TableIterator[1];
111     rows[0] = _table.createTableIterator();
112     queryContext.init(xa, rows, isReadOnly());
113     
114     try {
115       _table.insert(queryContext, xa, _columns, _values);
116
117       queryContext.setRowUpdateCount(1);
118     } catch (java.io.IOException JavaDoc e) {
119       throw new SQLExceptionWrapper(e);
120     } finally {
121       queryContext.unlock();
122     }
123   }
124
125   public String JavaDoc toString()
126   {
127     return "InsertQuery[]";
128   }
129 }
130
Popular Tags