| 1 package com.quadcap.sql; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.util.Hashtable ; 47 import java.util.Vector ; 48 49 import java.sql.SQLException ; 50 51 import com.quadcap.sql.types.Value; 52 import com.quadcap.sql.types.ValueBoolean; 53 54 import com.quadcap.util.Debug; 55 56 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  77 { 78 super(session, null, outer); 79 this.ca = ca; 80 this.cb = cb; 81 addColumns(session, tuple); 82 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 } 99 100 public Row getRow() { 101 return row; 102 } 103 104 public void updateRow(Row row) throws SQLException { 105 throw new SQLException ("JOIN cursors not updateable", "42000"); 106 } 107 108 public void deleteRow() throws SQLException { 109 throw new SQLException ("JOIN cursor: delete not supported", "42000"); 110 } 111 112 public void afterLast() throws SQLException { 113 throw new SQLException ("JOIN cursor: afterLast not supported", 114 "42000"); 115 } 116 117 public boolean isWritable(int column) { return false; } 118 119 public void beforeFirst() throws SQLException { 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 { 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 return ret; 141 } 142 143 protected void bfirst() throws SQLException { 144 cb.beforeFirst(); 145 } 146 147 protected boolean bnext() throws SQLException { 148 boolean ret = cb.next(); 149 if (ret) { 150 rb = cb.getRow(); 151 } else { 152 rb = null; 153 } 154 row.setB(rb); 155 return ret; 157 } 158 159 public boolean next() throws SQLException { 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; } 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  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 { 200 SQLException ex = null; 201 try { 202 ca.close(); 203 } catch (SQLException e) { 204 ex = e; 205 } finally { 206 ca = null; 207 try { 208 cb.close(); 209 } catch (SQLException 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 { return -1; } 219 } 220 | Popular Tags |