1 28 29 package com.caucho.db.sql; 30 31 import com.caucho.db.table.Column; 32 import com.caucho.db.table.Table; 33 import com.caucho.db.table.TableIterator; 34 import com.caucho.log.Log; 35 import com.caucho.util.L10N; 36 37 import java.sql.SQLException ; 38 import java.util.ArrayList ; 39 import java.util.logging.Logger ; 40 41 class IdExpr extends Expr { 42 private static final L10N L = new L10N(IdExpr.class); 43 private static final Logger log = Log.open(IdExpr.class); 44 45 private FromItem _fromItem; 46 private Column _column; 47 48 private int _tableIndex = -1; 49 50 53 IdExpr(FromItem fromItem, Column column) 54 { 55 _fromItem = fromItem; 56 _column = column; 57 } 58 59 public Class getType() 60 { 61 return _column.getJavaType(); 62 } 63 64 67 public Column getColumn() 68 { 69 return _column; 70 } 71 72 75 public String getName() 76 { 77 return _column.getName(); 78 } 79 80 83 public FromItem getFromItem() 84 { 85 return _fromItem; 86 } 87 88 91 public Table getTable() 92 { 93 return _fromItem.getTable(); 94 } 95 96 99 protected long lookupCost(ArrayList <FromItem> fromList) 100 { 101 if (! fromList.contains(_fromItem)) 102 return COST_NO_TABLE; 103 104 if (fromList.indexOf(_fromItem) < fromList.size() - 1) 105 return 0; 106 107 if (_column.isPrimaryKey()) 108 return COST_INDEX; 109 else if (_column.isUnique()) 110 return COST_UNIQUE; 111 else 112 return COST_SCAN; 113 } 114 115 118 public long subCost(ArrayList <FromItem> fromList) 119 { 120 if (! fromList.contains(_fromItem)) 121 return Integer.MAX_VALUE; 122 123 124 return 10 * 100 * 100 * 100; 125 } 126 127 protected Expr bind(Query query) 128 throws SQLException 129 { 130 FromItem []fromItems = query.getFromItems(); 131 132 for (int i = 0; i < fromItems.length; i++) { 133 if (fromItems[i] == _fromItem) 134 _tableIndex = i; 135 } 136 137 return this; 138 } 139 140 143 public boolean isNull(QueryContext context) 144 throws SQLException 145 { 146 TableIterator []rows = context.getTableIterators(); 147 TableIterator row = rows[_tableIndex]; 148 149 return row.isNull(_column); 150 } 151 152 155 public String evalString(QueryContext context) 156 throws SQLException 157 { 158 TableIterator []rows = context.getTableIterators(); 159 TableIterator row = rows[_tableIndex]; 160 161 return row.getString(_column); 162 } 163 164 167 public int evalBoolean(QueryContext context) 168 throws SQLException 169 { 170 TableIterator []rows = context.getTableIterators(); 171 TableIterator row = rows[_tableIndex]; 172 173 String value = row.getString(_column); 174 175 if (value == null) 176 return UNKNOWN; 177 else if (value.equals("1")) 178 return TRUE; 179 else if (value.equalsIgnoreCase("t")) 180 return TRUE; 181 else if (value.equalsIgnoreCase("y")) 182 return TRUE; 183 else 184 return FALSE; 185 } 186 187 public int evalInt(QueryContext context) 188 throws SQLException 189 { 190 TableIterator []rows = context.getTableIterators(); 191 TableIterator row = rows[_tableIndex]; 192 193 return row.getInteger(_column); 194 } 195 196 public long evalLong(QueryContext context) 197 throws SQLException 198 { 199 TableIterator []rows = context.getTableIterators(); 200 TableIterator row = rows[_tableIndex]; 201 202 return row.getLong(_column); 203 } 204 205 public double evalDouble(QueryContext context) 206 throws SQLException 207 { 208 TableIterator []rows = context.getTableIterators(); 209 TableIterator row = rows[_tableIndex]; 210 211 return row.getDouble(_column); 212 } 213 214 220 public void evalToResult(QueryContext context, SelectResult result) 221 throws SQLException 222 { 223 TableIterator []rows = context.getTableIterators(); 224 TableIterator row = rows[_tableIndex]; 225 226 row.evalToResult(_column, result); 227 } 228 229 public boolean evalEqual(QueryContext context, byte []matchBuffer) 230 throws SQLException 231 { 232 TableIterator []rows = context.getTableIterators(); 233 TableIterator row = rows[_tableIndex]; 234 235 return row.isEqual(_column, matchBuffer); 236 } 237 238 public boolean evalEqual(QueryContext context, String string) 239 throws SQLException 240 { 241 TableIterator []rows = context.getTableIterators(); 242 TableIterator row = rows[_tableIndex]; 243 244 return row.isEqual(_column, string); 245 } 246 247 public boolean equals(Object o) 248 { 249 if (o == null || ! IdExpr.class.equals(o.getClass())) 250 return false; 251 252 IdExpr expr = (IdExpr) o; 253 254 return (_fromItem == expr._fromItem && 255 _column == expr._column); 256 } 257 258 public String toString() 259 { 260 return _fromItem.getName() + "." + _column.getName(); 261 } 262 } 263 | Popular Tags |