1 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 ; 35 import java.sql.SQLException ; 36 import java.util.ArrayList ; 37 import java.util.logging.Logger ; 38 39 class LeftOuterJoinExpr extends RowIterateExpr { 40 private static final Logger 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 55 public Class getType() 56 { 57 return boolean.class; 58 } 59 60 63 public long subCost(ArrayList <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 76 public RowIterateExpr getIndexExpr(FromItem fromItem) 77 { 78 if (_table == fromItem) 79 return this; 80 else 81 return null; 82 } 83 84 87 protected Expr bind(Query query) 88 throws SQLException 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 105 boolean init(QueryContext context, TableIterator rowIter) 106 throws SQLException , IOException 107 { 108 rowIter.init(context); 109 110 if (! rowIter.next()) { 111 return false; 112 } 113 114 return true; 115 } 116 117 120 boolean initRow(QueryContext context, TableIterator rowIter) 121 throws SQLException , IOException 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 150 boolean allowChildRowShift(QueryContext context, TableIterator rowIter) 151 { 152 return false; 153 } 154 155 158 boolean nextRow(QueryContext context, TableIterator rowIter) 159 throws IOException , SQLException 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 179 boolean nextBlock(QueryContext context, TableIterator rowIter) 180 throws IOException 181 { 182 return false; 183 } 184 185 public String toString() 186 { 187 return "LeftOuterJoinExpr(" + _expr + ")"; 188 } 189 } 190 | Popular Tags |