KickJava   Java API By Example, From Geeks To Geeks.

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


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.TableIterator;
35 import com.caucho.log.Log;
36 import com.caucho.sql.SQLExceptionWrapper;
37 import com.caucho.util.CharBuffer;
38
39 import java.io.IOException JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 public class SelectQuery extends Query {
45   private static final Logger JavaDoc log = Log.open(SelectQuery.class);
46
47   private Expr []_results;
48   private String JavaDoc []_resultNames;
49
50   private boolean []_groupFields;
51
52   private Order _order;
53
54   SelectQuery(Database db, String JavaDoc sql)
55     throws SQLException JavaDoc
56   {
57     super(db, sql);
58   }
59
60   SelectQuery(Database db, String JavaDoc sql, FromItem []fromItems)
61     throws SQLException JavaDoc
62   {
63     super(db, sql, fromItems);
64   }
65
66   void setResults(Expr []resultExprs)
67     throws SQLException JavaDoc
68   {
69     _results = new Expr[resultExprs.length];
70
71     for (int i = 0; i < resultExprs.length; i++) {
72       _results[i] = resultExprs[i];
73     }
74
75     setDataFields(resultExprs.length);
76   }
77
78   Expr []getResults()
79   {
80     return _results;
81   }
82
83   /**
84    * Sets the result item as group.
85    */

86   public void setGroupResult(int index)
87   {
88     if (_groupFields == null)
89       _groupFields = new boolean[_results.length];
90
91     _groupFields[index] = true;
92   }
93
94   protected void bind()
95     throws SQLException JavaDoc
96   {
97     super.bind();
98
99     for (int i = 0; i < _results.length; i++) {
100       _results[i] = _results[i].bind(this);
101     }
102
103     if (isGroup()) {
104       for (int i = 0; i < _results.length; i++) {
105     if (isGroup() && ! (_results[i] instanceof GroupExpr))
106       _results[i] = new GroupResultExpr(i, _results[i]);
107       }
108     }
109
110     for (int i = 0; i < _results.length; i++) {
111       _results[i] = _results[i].bind(this);
112     }
113
114     _resultNames = new String JavaDoc[_results.length];
115
116     for (int i = 0; i < _resultNames.length; i++)
117       _resultNames[i] = _results[i].getName();
118   }
119
120   void setOrder(Order order)
121   {
122     _order = order;
123   }
124
125   /**
126    * Returns true for select queries.
127    */

128   public boolean isSelect()
129   {
130     return true;
131   }
132
133   /**
134    * Returns the type of the child.
135    */

136   Class JavaDoc getType()
137   {
138     if (_results.length == 1)
139       return _results[0].getType();
140     else
141       return Object JavaDoc.class;
142   }
143
144   /**
145    * Executes the query.
146    */

147   public void execute(QueryContext context, Transaction xa)
148     throws SQLException JavaDoc
149   {
150     SelectResult result = SelectResult.create(_results, _order);
151     FromItem []fromItems = getFromItems();
152     TableIterator []rows = null;
153
154     try {
155       rows = result.initRows(fromItems);
156       context.init(xa, rows, isReadOnly());
157
158       if (isGroup())
159     executeGroup(result, rows, context, xa);
160       else
161     execute(result, rows, context, xa);
162
163       result.initRead();
164
165       context.setResult(result);
166     } catch (IOException JavaDoc e) {
167       throw new SQLExceptionWrapper(e);
168     } finally {
169       // autoCommitRead must be before freeRows in case freeRows
170
// throws an exception
171
context.unlock();
172       
173       if (rows != null)
174     freeRows(rows, rows.length);
175     }
176   }
177
178   /**
179    * Executes the query.
180    */

181   private void execute(SelectResult result,
182                TableIterator []rows,
183                QueryContext context,
184                Transaction xa)
185     throws SQLException JavaDoc, IOException JavaDoc
186   {
187     FromItem []fromItems = getFromItems();
188     int rowLength = fromItems.length;
189
190     if (start(rows, rowLength, context, xa)) {
191       do {
192     result.startRow();
193     for (int i = 0; i < _results.length; i++) {
194       _results[i].evalToResult(context, result);
195     }
196       } while (nextTuple(rows, rowLength, context, xa));
197     }
198   }
199
200   private void executeGroup(SelectResult result,
201                 TableIterator []rows,
202                 QueryContext context,
203                 Transaction transaction)
204     throws SQLException JavaDoc, IOException JavaDoc
205   {
206     FromItem []fromItems = getFromItems();
207     int rowLength = fromItems.length;
208
209     Expr []results = _results;
210     int resultsLength = results.length;
211
212     /*
213     for (int i = 0; i < results.length; i++)
214       results[i].initGroup(context);
215     */

216
217     if (_groupFields == null)
218       _groupFields = new boolean[0];
219
220     boolean []groupByFields = _groupFields;
221     int groupByLength = _groupFields.length;
222
223     if (start(rows, rowLength, context, transaction)) {
224       do {
225     context.initGroup(getDataFields(), _groupFields);
226
227     for (int i = 0; i < groupByLength; i++) {
228       if (groupByFields[i])
229         results[i].evalGroup(context);
230     }
231
232     context.selectGroup();
233
234     for (int i = 0; i < resultsLength; i++) {
235       if (! (i < groupByLength && groupByFields[i]))
236         results[i].evalGroup(context);
237     }
238       } while (nextTuple(rows, rowLength, context, transaction));
239     }
240
241     Iterator<GroupItem> groupIter = context.groupResults();
242
243     while (groupIter.hasNext()) {
244       GroupItem item = groupIter.next();
245
246       context.setGroupItem(item);
247
248       result.startRow();
249       for (int i = 0; i < results.length; i++) {
250     results[i].evalToResult(context, result);
251       }
252     }
253   }
254
255   public String JavaDoc toString()
256   {
257     CharBuffer cb = CharBuffer.allocate();
258     cb.append("SelectQuery[");
259     cb.append("SELECT ");
260     for (int i = 0; i < _results.length; i++) {
261       if (i != 0)
262     cb.append(",");
263       cb.append(_results[i]);
264     }
265     cb.append(" FROM ");
266
267     FromItem []fromItems = getFromItems();
268     for (int i = 0; i < fromItems.length; i++) {
269       if (i != 0)
270     cb.append(",");
271       cb.append(fromItems[i]);
272     }
273
274     if (_whereExpr != null) {
275       cb.append(" WHERE " + _whereExpr);
276     }
277     cb.append("]");
278
279     return cb.close();
280   }
281 }
282
Popular Tags