001: /*
002: * $Id: AxionQueryContext.java,v 1.13 2005/12/20 18:32:28 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.commands;
042:
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.Iterator;
046: import java.util.List;
047:
048: import org.axiondb.FromNode;
049: import org.axiondb.Literal;
050: import org.axiondb.OrderNode;
051: import org.axiondb.RowDecorator;
052: import org.axiondb.RowIterator;
053: import org.axiondb.Selectable;
054: import org.axiondb.TableIdentifier;
055:
056: /**
057: * AxionQueryContext holds metadata for the Query or Sub-Query.
058: *
059: * @version $Revision: 1.13 $ $Date: 2005/12/20 18:32:28 $
060: * @author Ahimanikya Satapathy
061: */
062: public class AxionQueryContext {
063:
064: public AxionQueryContext() {
065: }
066:
067: public void addAllSelectToResolvedSelect() {
068: if (_resolvedSelect == null) {
069: _resolvedSelect = new ArrayList(_select.size());
070: }
071: _resolvedSelect.addAll(_select);
072: }
073:
074: /**
075: * Adds a {@link TableIdentifier}to the list of tables being selected from.
076: *
077: * @param table a {@link TableIdentifier}
078: * @throws IllegalStateException if I have already been resolved
079: */
080: public void addFrom(TableIdentifier table) {
081: if (_resolved) {
082: throw new IllegalStateException("Already resolved.");
083: }
084: if (_from == null) {
085: _from = new FromNode();
086: _from.setType(FromNode.TYPE_SINGLE);
087: }
088:
089: if (_from.getLeft() == null) {
090: _from.setLeft(table);
091: } else {
092: _from.setRight(table);
093: _from.setType(FromNode.TYPE_INNER);
094: }
095: }
096:
097: /**
098: * Appends an {@link OrderNode}to the order by clause for this query
099: *
100: * @param orderby an {@link OrderNode}to append
101: * @throws IllegalStateException if I have already been resolved
102: */
103: public void addOrderBy(OrderNode orderby) {
104: if (_resolved) {
105: throw new IllegalStateException("Already resolved.");
106: }
107: if (_orderBy == null) {
108: _orderBy = new ArrayList(4);
109: }
110: _orderBy.add(orderby);
111: }
112:
113: /**
114: * Adds a {@link Selectable}to the list of items being selected.
115: *
116: * @param column the {@link Selectable}to add
117: * @throws IllegalStateException if I have already been resolved
118: */
119: public void addSelect(Selectable column) {
120: if (_resolved) {
121: throw new IllegalStateException("Already resolved.");
122: }
123: _select.add(column);
124: }
125:
126: public boolean foundAggregateFunction() {
127: return _foundAggregateFunction;
128: }
129:
130: public String getAliasName() {
131: return _aliasName;
132: }
133:
134: /**
135: * Indicates if the {@link java.sql.ResultSet}generated from this object will contain
136: * distinct tuples.
137: *
138: * @return <code>true</code> for distinct tuples
139: */
140: public boolean getDistinct() {
141: return _distinct;
142: }
143:
144: /**
145: * Gets the root {@link FromNode}for the select statement.
146: */
147: public FromNode getFrom() {
148: return _from;
149: }
150:
151: /**
152: * Gets the <i>i </i> <sup>th </sup> table being selected. Clients should treat the
153: * returned value as immutable.
154: *
155: * @param i the zero-based index
156: */
157: public TableIdentifier getFrom(int i) {
158: TableIdentifier[] tableIDs = _from.toTableArray();
159: return (tableIDs[i]);
160: }
161:
162: public TableIdentifier[] getFromArray() {
163: if (_from != null) {
164: return (_from.toTableArray());
165: }
166: return (null);
167: }
168:
169: /**
170: * Gets the number of tables being from.
171: */
172: public int getFromCount() {
173: return _from.getTableCount();
174: }
175:
176: /**
177: * Gets Selectable in Group by clause.
178: */
179: public List getGroupBy() {
180: if (_groupBy != null) {
181: return Collections.unmodifiableList(_groupBy);
182: }
183: return Collections.EMPTY_LIST;
184: }
185:
186: /**
187: * Gets Selectable in Group by clause. Clients should treat the returned value as
188: * immutable.
189: *
190: * @param i the zero-based index
191: */
192: public Selectable getGroupBy(int i) {
193: if (_groupBy != null) {
194: return (Selectable) (_groupBy.get(i));
195: }
196: return null;
197: }
198:
199: /**
200: * Gets the number of {@link Slectable}s group by in my query.
201: */
202: public int getGroupByCount() {
203: return (null == _groupBy ? 0 : _groupBy.size());
204: }
205:
206: public Literal getLimit() {
207: return _limit;
208: }
209:
210: public Literal getOffset() {
211: return _offset;
212: }
213:
214: /**
215: * Gets the List of {@link OrderNode}in my order by clause.
216: */
217: public List getOrderBy() {
218: if (_orderBy != null) {
219: return Collections.unmodifiableList(_orderBy);
220: }
221: return Collections.EMPTY_LIST;
222: }
223:
224: /**
225: * Gets the <i>i </i> <sup>th </sup> {@link OrderNode}in my order by clause. Clients
226: * should treat the returned value as immutable.
227: *
228: * @param i the zero-based index
229: */
230: public OrderNode getOrderBy(int i) {
231: if (_orderBy != null) {
232: return (OrderNode) (_orderBy.get(i));
233: }
234: return null;
235: }
236:
237: /**
238: * Gets the number of {@link OrderNode}s in my query.
239: */
240: public int getOrderByCount() {
241: return (null == _orderBy ? 0 : _orderBy.size());
242: }
243:
244: public RowDecorator getParentRow() {
245: return _parentRow;
246: }
247:
248: public TableIdentifier[] getParentTables() {
249: return _parentTables;
250: }
251:
252: public List getResolvedSelect() {
253: if (_resolvedSelect == null) {
254: _resolvedSelect = new ArrayList();
255: }
256: return _resolvedSelect;
257: }
258:
259: public RowIterator getRows() {
260: return _rows;
261: }
262:
263: public List getSelect() {
264: return _select;
265: }
266:
267: /**
268: * Gets the <i>i </i> <sup>th </sup> {@link Selectable}being selected. Clients should
269: * treat the returned value as immutable.
270: *
271: * @param i the zero-based index
272: */
273: public Selectable getSelect(int i) {
274: return (Selectable) (_select.get(i));
275: }
276:
277: /**
278: * Gets the number of {@link Selectable}s being selected.
279: */
280: public int getSelectCount() {
281: return _select.size();
282: }
283:
284: public Selectable[] getSelected() {
285: return _selected;
286: }
287:
288: public TableIdentifier[] getTables() {
289: return _tables;
290: }
291:
292: public TableIdentifier getTables(int i) {
293: return _tables[i];
294: }
295:
296: public int getTableCount() {
297: return (null == _tables ? 0 : _tables.length);
298: }
299:
300: /**
301: * Returns the {@link Selectable where tree}for this query. Clients should treat the
302: * returned value as immutable.
303: *
304: * @return the {@link Selectable where tree}for this query, or <tt>null</tt>.
305: */
306: public Selectable getWhere() {
307: return _where;
308: }
309:
310: public Selectable getHaving() {
311: return _having;
312: }
313:
314: public boolean isCorrelatedSubQuery() {
315: return _isCorrelatedSubQuery;
316: }
317:
318: public boolean isExplain() {
319: return _explain;
320: }
321:
322: public boolean isResolved() {
323: return _resolved;
324: }
325:
326: public boolean isTablePartOfSelect(TableIdentifier tid) {
327: for (int i = 0; i < _tables.length; i++) {
328: if (tid.equals(_tables[i])) {
329: return true;
330: }
331: }
332: return false;
333: }
334:
335: public void setAliasName(String name) {
336: _aliasName = name;
337: }
338:
339: public void setCorrelatedSubQuery(boolean isCorrelatedSubQuery) {
340: _isCorrelatedSubQuery = isCorrelatedSubQuery;
341: }
342:
343: /**
344: * Determines if the {@link java.sql.ResultSet}generated from this object will
345: * contain distinct tuples (default is false).
346: *
347: * @param distinct true for distinct tuples
348: */
349: public void setDistinct(boolean distinct) {
350: if (_resolved) {
351: throw new IllegalStateException("Already resolved.");
352: }
353: _distinct = distinct;
354: }
355:
356: public void setExplain(boolean explain) {
357: _explain = explain;
358: }
359:
360: public void setFoundAggregateFunction(boolean found) {
361: _foundAggregateFunction = found;
362: }
363:
364: /**
365: * Sets the root {@link FromNode}for the select statement.
366: */
367: public void setFrom(FromNode from) {
368: if (_resolved) {
369: throw new IllegalStateException("Already resolved.");
370: }
371: _from = from;
372: }
373:
374: /**
375: * Sets the group by clause for this query.
376: *
377: * @param groupby a {@link List}of {@link Selectable}s.
378: * @throws IllegalStateException if I have already been resolved
379: */
380: public void setGroupBy(List groupby) {
381: if (_resolved) {
382: throw new IllegalStateException("Already resolved.");
383: }
384: _groupBy = groupby;
385: }
386:
387: void setGroupBy(int i, Selectable column) {
388: if (_groupBy == null) {
389: throw new IllegalArgumentException("GroupBy List is Empty");
390: }
391: _groupBy.set(i, column);
392: }
393:
394: public void setLimit(Literal limit) {
395: _limit = limit;
396: }
397:
398: public void setOffset(Literal offset) {
399: _offset = offset;
400: }
401:
402: /**
403: * Sets the order by clause for this query.
404: *
405: * @param orderby a {@link List}of {@link OrderNode}s.
406: * @throws IllegalStateException if I have already been resolved
407: */
408: public void setOrderBy(List orderby) {
409: if (_resolved) {
410: throw new IllegalStateException("Already resolved.");
411: }
412: _orderBy = orderby;
413: }
414:
415: public void setParentRow(RowDecorator row) {
416: _parentRow = row;
417: }
418:
419: public void setParentTables(TableIdentifier[] tables) {
420: _parentTables = tables;
421: }
422:
423: public void setResolved(boolean resolved) {
424: _resolved = resolved;
425: }
426:
427: public void setResolvedSelect(List select) {
428: _resolvedSelect = select;
429: }
430:
431: public void setRows(RowIterator rows) {
432: _rows = rows;
433: }
434:
435: /**
436: * Sets the <i>i </i> <sup>th </sup> {@link Selectable}being selected.
437: *
438: * @param i the zero-based index
439: * @param sel the new {@link Selectable}
440: * @throws IllegalStateException if I have already been resolved
441: */
442: public void setSelect(int i, Selectable sel) {
443: if (_resolved) {
444: throw new IllegalStateException("Already resolved.");
445: }
446: _select.set(i, sel);
447: }
448:
449: public void setSelect(List columns) {
450: if (_resolved) {
451: throw new IllegalStateException("Already resolved.");
452: }
453: _select = columns;
454: }
455:
456: public void setSelected(Selectable[] selected) {
457: _selected = selected;
458: }
459:
460: public void setTables(TableIdentifier[] tables) {
461: _tables = tables;
462: }
463:
464: /**
465: * Sets the {@link Selectable where tree}for this query.
466: *
467: * @param where a boolean valued {@link Selectable}
468: * @throws IllegalStateException if I have already been resolved
469: */
470: public void setWhere(Selectable where) {
471: if (_resolved) {
472: throw new IllegalStateException("Already resolved.");
473: }
474: _where = where;
475: }
476:
477: public void setHaving(Selectable having) {
478: if (_resolved) {
479: throw new IllegalStateException("Already resolved.");
480: }
481: _having = having;
482: }
483:
484: public String toString() {
485: StringBuffer buf = new StringBuffer();
486: buf.append("SELECT ");
487: if (_distinct) {
488: buf.append("DISTINCT ");
489: }
490: {
491: Iterator iter = _select.iterator();
492: while (iter.hasNext()) {
493: buf.append(iter.next());
494: if (iter.hasNext()) {
495: buf.append(", ");
496: }
497: }
498: }
499: if (null != _from) {
500: buf.append(" FROM ");
501: buf.append(_from);
502: }
503:
504: if (null != _where) {
505: buf.append(" WHERE ");
506: buf.append(_where);
507: }
508: if (null != _groupBy && !_groupBy.isEmpty()) {
509: buf.append(" GROUP BY ");
510: {
511: Iterator iter = _groupBy.iterator();
512: while (iter.hasNext()) {
513: buf.append(iter.next());
514: if (iter.hasNext()) {
515: buf.append(", ");
516: }
517: }
518: }
519: }
520:
521: if (null != _having) {
522: buf.append(" HAVING ");
523: buf.append(_having);
524: }
525:
526: if (null != _orderBy && !_orderBy.isEmpty()) {
527: buf.append(" ORDER BY ");
528: {
529: Iterator iter = _orderBy.iterator();
530: while (iter.hasNext()) {
531: buf.append(iter.next());
532: if (iter.hasNext()) {
533: buf.append(", ");
534: }
535: }
536: }
537: }
538: return buf.toString();
539: }
540:
541: public List createLiteralList() {
542: List literals = null;
543: for (int i = 0, I = getSelectCount(); i < I; i++) {
544: if (getSelect(i) instanceof Literal) {
545: if (null == literals) {
546: literals = new ArrayList();
547: }
548: literals.add(getSelect(i));
549: }
550: }
551: return literals;
552: }
553:
554: private String _aliasName;
555: private boolean _distinct = false;
556:
557: private boolean _explain = false;
558: private boolean _foundAggregateFunction = false;
559: private FromNode _from;
560: private List _groupBy;
561: private Selectable _having;
562: private boolean _isCorrelatedSubQuery;
563: private Literal _limit;
564:
565: private Literal _offset;
566: private List _orderBy;
567: private RowDecorator _parentRow;
568: private TableIdentifier[] _parentTables;
569: private boolean _resolved = false;
570: private List _resolvedSelect;
571: private RowIterator _rows;
572:
573: private List _select = new ArrayList();
574: private Selectable[] _selected;
575: private TableIdentifier[] _tables;
576: private Selectable _where;
577: }
|