KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.db.sql;
30
31 import com.caucho.db.table.TableIterator;
32 import com.caucho.log.Log;
33
34 import java.io.IOException JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 class LeftOuterJoinExpr extends RowIterateExpr {
40   private static final Logger JavaDoc log = Log.open(LeftOuterJoinExpr.class);
41
42   private Expr _expr;
43   private FromItem _table;
44   private int _tableIndex;
45
46   LeftOuterJoinExpr(FromItem table, Expr expr)
47   {
48     _table = table;
49     _expr = expr;
50   }
51
52   /**
53    * Returns the expected result type of the expression.
54    */

55   public Class JavaDoc getType()
56   {
57     return boolean.class;
58   }
59
60   /**
61    * Returns the cost based on the given FromList.
62    */

63   public long subCost(ArrayList JavaDoc<FromItem> fromList)
64   {
65     if (! fromList.contains(_table))
66       return Integer.MAX_VALUE;
67     else if (fromList.get(fromList.size() - 1) == _table)
68       return 0;
69     else
70       return 100 * 100;
71   }
72
73   /**
74    * Returns an index expression if available.
75    */

76   public RowIterateExpr getIndexExpr(FromItem fromItem)
77   {
78     if (_table == fromItem)
79       return this;
80     else
81       return null;
82   }
83
84   /**
85    * Binds the expression.
86    */

87   protected Expr bind(Query query)
88     throws SQLException JavaDoc
89   {
90     _expr = _expr.bind(query);
91
92     FromItem []fromItems = query.getFromItems();
93
94     for (int i = 0; i < fromItems.length; i++) {
95       if (_table == fromItems[i])
96     _tableIndex = i;
97     }
98     
99     return this;
100   }
101
102   /**
103    * Sets the initial row.
104    */

105   boolean init(QueryContext context, TableIterator rowIter)
106     throws SQLException JavaDoc, IOException JavaDoc
107   {
108     rowIter.init(context);
109
110     if (! rowIter.next()) {
111       return false;
112     }
113
114     return true;
115   }
116
117   /**
118    * Sets the initial row.
119    */

120   boolean initRow(QueryContext context, TableIterator rowIter)
121     throws SQLException JavaDoc, IOException JavaDoc
122   {
123     rowIter.init(context);
124
125     if (! rowIter.next())
126       return false;
127     
128     rowIter.initRow();
129
130     Expr expr = _expr;
131     do {
132       if (rowIter.nextRow()) {
133       }
134       else if (rowIter.next()) {
135     rowIter.initRow();
136       }
137       else {
138     rowIter.initNullRow();
139     return true;
140       }
141     } while (expr.evalBoolean(context) != TRUE);
142     TableIterator parentIter = context.getTableIterators()[1];
143     
144     return true;
145   }
146
147   /**
148    * Returns true if shifing the child rows will make a difference.
149    */

150   boolean allowChildRowShift(QueryContext context, TableIterator rowIter)
151   {
152     return false;
153   }
154
155   /**
156    * Returns the next row.
157    */

158   boolean nextRow(QueryContext context, TableIterator rowIter)
159     throws IOException JavaDoc, SQLException JavaDoc
160   {
161     if (rowIter.isNullRow())
162       return false;
163     else {
164       Expr expr = _expr;
165
166       while (rowIter.nextRow() || rowIter.next()) {
167     if (expr.evalBoolean(context) == TRUE) {
168       return true;
169     }
170       }
171       
172       return false;
173     }
174   }
175
176   /**
177    * Returns the next row.
178    */

179   boolean nextBlock(QueryContext context, TableIterator rowIter)
180     throws IOException JavaDoc
181   {
182     return false;
183   }
184
185   public String JavaDoc toString()
186   {
187     return "LeftOuterJoinExpr(" + _expr + ")";
188   }
189 }
190
Popular Tags