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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.sql;
030:
031: import com.caucho.db.table.TableIterator;
032: import com.caucho.log.Log;
033:
034: import java.io.IOException;
035: import java.sql.SQLException;
036: import java.util.ArrayList;
037: import java.util.logging.Logger;
038:
039: class LeftOuterJoinExpr extends RowIterateExpr {
040: private static final Logger log = Log.open(LeftOuterJoinExpr.class);
041:
042: private Expr _expr;
043: private FromItem _table;
044: private int _tableIndex;
045:
046: LeftOuterJoinExpr(FromItem table, Expr expr) {
047: _table = table;
048: _expr = expr;
049: }
050:
051: /**
052: * Returns the expected result type of the expression.
053: */
054: public Class getType() {
055: return boolean.class;
056: }
057:
058: /**
059: * Returns the cost based on the given FromList.
060: */
061: public long subCost(ArrayList<FromItem> fromList) {
062: if (!fromList.contains(_table))
063: return Integer.MAX_VALUE;
064: else if (fromList.get(fromList.size() - 1) == _table)
065: return 0;
066: else
067: return 100 * 100;
068: }
069:
070: /**
071: * Returns an index expression if available.
072: */
073: public RowIterateExpr getIndexExpr(FromItem fromItem) {
074: if (_table == fromItem)
075: return this ;
076: else
077: return null;
078: }
079:
080: /**
081: * Binds the expression.
082: */
083: protected Expr bind(Query query) throws SQLException {
084: _expr = _expr.bind(query);
085:
086: FromItem[] fromItems = query.getFromItems();
087:
088: for (int i = 0; i < fromItems.length; i++) {
089: if (_table == fromItems[i])
090: _tableIndex = i;
091: }
092:
093: return this ;
094: }
095:
096: /**
097: * Sets the initial row.
098: */
099: boolean init(QueryContext context, TableIterator rowIter)
100: throws SQLException, IOException {
101: rowIter.init(context);
102:
103: if (!rowIter.next()) {
104: return false;
105: }
106:
107: return true;
108: }
109:
110: /**
111: * Sets the initial row.
112: */
113: boolean initRow(QueryContext context, TableIterator rowIter)
114: throws SQLException, IOException {
115: rowIter.init(context);
116:
117: if (!rowIter.next())
118: return false;
119:
120: rowIter.initRow();
121:
122: Expr expr = _expr;
123: do {
124: if (rowIter.nextRow()) {
125: } else if (rowIter.next()) {
126: rowIter.initRow();
127: } else {
128: rowIter.initNullRow();
129: return true;
130: }
131: } while (expr.evalBoolean(context) != TRUE);
132: TableIterator parentIter = context.getTableIterators()[1];
133:
134: return true;
135: }
136:
137: /**
138: * Returns true if shifing the child rows will make a difference.
139: */
140: boolean allowChildRowShift(QueryContext context,
141: TableIterator rowIter) {
142: return false;
143: }
144:
145: /**
146: * Returns the next row.
147: */
148: boolean nextRow(QueryContext context, TableIterator rowIter)
149: throws IOException, SQLException {
150: if (rowIter.isNullRow())
151: return false;
152: else {
153: Expr expr = _expr;
154:
155: while (rowIter.nextRow() || rowIter.next()) {
156: if (expr.evalBoolean(context) == TRUE) {
157: return true;
158: }
159: }
160:
161: return false;
162: }
163: }
164:
165: /**
166: * Returns the next row.
167: */
168: boolean nextBlock(QueryContext context, TableIterator rowIter)
169: throws IOException {
170: return false;
171: }
172:
173: public String toString() {
174: return "LeftOuterJoinExpr(" + _expr + ")";
175: }
176: }
|