KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > JoinCursor


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.util.Hashtable JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 import java.sql.SQLException JavaDoc;
50
51 import com.quadcap.sql.types.Value;
52 import com.quadcap.sql.types.ValueBoolean;
53
54 import com.quadcap.util.Debug;
55
56 /**
57  * Cursor implementation of cross-join (cartesion product)
58  *
59  * @author Stan Bailes
60  */

61 public abstract class JoinCursor extends CursorImpl {
62     Cursor ca;
63     Cursor cb;
64     JoinMapRow row;
65     Expression where;
66     boolean left;
67     boolean inner;
68
69     Row ra;
70     Row rb;
71     Expression aWhere;
72
73     public JoinCursor(Session session, Cursor outer, Cursor ca, Cursor cb,
74                       Expression where, Tuple tuple, JoinMapRow row,
75                       boolean left, boolean inner)
76     throws SQLException JavaDoc
77     {
78     super(session, null, outer);
79         this.ca = ca;
80     this.cb = cb;
81         addColumns(session, tuple);
82         // XXX This is the problem. It's not the fact that the rows
83
// XXX are flipped, per se, it's just that the key fields
84
// XXX must be in the outer, rather than the inner row.
85
// XXX So flip() must know what the key fields are in each
86
// XXX row.
87
this.row = row;
88         this.where = where;
89         this.left = left;
90         this.inner = inner;
91
92         if (!(ca instanceof JoinCursor)) {
93             this.aWhere = new Analyze(session, where).factorTable(ca);
94         }
95
96         //Debug.println("ca = " + ca.getClass().getName() + ": " + ca);
97
//Debug.println("cb = " + cb.getClass().getName() + ": " + cb);
98
}
99
100     public Row getRow() {
101     return row;
102     }
103
104     public void updateRow(Row row) throws SQLException JavaDoc {
105     throw new SQLException JavaDoc("JOIN cursors not updateable", "42000");
106     }
107
108     public void deleteRow() throws SQLException JavaDoc {
109     throw new SQLException JavaDoc("JOIN cursor: delete not supported", "42000");
110     }
111
112     public void afterLast() throws SQLException JavaDoc {
113         throw new SQLException JavaDoc("JOIN cursor: afterLast not supported",
114                                "42000");
115     }
116
117     public boolean isWritable(int column) { return false; }
118
119     public void beforeFirst() throws SQLException JavaDoc {
120         ca.beforeFirst();
121         cb.beforeFirst();
122         ra = null;
123         row.setA(null);
124         rb = null;
125         row.setB(null);
126     }
127
128     protected boolean anext() throws SQLException JavaDoc {
129         boolean ret = ca.next();
130         while (ret && !passRow(ca, aWhere)) {
131             ret = ca.next();
132         }
133         if (ret) {
134             ra = ca.getRow();
135         } else {
136             ra = null;
137         }
138         row.setA(ra);
139         //Debug.println("anext: ra = " + ra + ", row = " + row);
140
return ret;
141     }
142
143     protected void bfirst() throws SQLException JavaDoc {
144         cb.beforeFirst();
145     }
146
147     protected boolean bnext() throws SQLException JavaDoc {
148         boolean ret = cb.next();
149         if (ret) {
150             rb = cb.getRow();
151         } else {
152             rb = null;
153         }
154         row.setB(rb);
155         //Debug.println("bnext: rb = " + rb + ", row = " + row);
156
return ret;
157     }
158
159     public boolean next() throws SQLException JavaDoc {
160     boolean ret = false;
161         while (ra != null && !ret && inner && bnext()) {
162             ret = true;
163         }
164         if (!ret) {
165             while (anext()) {
166                 boolean anyInner = false;
167                 bfirst();
168                 while (bnext()) {
169                     anyInner = true;
170                     if (inner) {
171                         return true; // single exit is "inconvenient"
172
}
173                 }
174                 if (left && !anyInner) {
175                     ret = true;
176                     break;
177                 }
178             }
179         }
180         return ret;
181     }
182
183     final boolean passRow(Cursor c, Expression w)
184         throws SQLException JavaDoc
185     {
186         boolean ret = true;
187         if (w != null) {
188             Value val = w.getValue(session, c);
189             if (val instanceof ValueBoolean) {
190                 ValueBoolean vb = (ValueBoolean)val;
191                 ret = vb.isTrue();
192             } else {
193                 ret = false;
194             }
195     }
196     return ret;
197     }
198
199     public void close() throws SQLException JavaDoc {
200         SQLException JavaDoc ex = null;
201         try {
202             ca.close();
203         } catch (SQLException JavaDoc e) {
204             ex = e;
205         } finally {
206             ca = null;
207             try {
208                 cb.close();
209             } catch (SQLException JavaDoc e) {
210                 if (ex != null) ex = e;
211             } finally {
212                 cb = null;
213             }
214         }
215         if (ex != null) throw ex;
216     }
217         
218     public long size() throws SQLException JavaDoc { return -1; }
219 }
220
Popular Tags