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.log.Log; 34 import com.caucho.util.L10N; 35 36 import java.sql.SQLException ; 37 import java.util.ArrayList ; 38 import java.util.logging.Logger ; 39 40 class UnboundIdentifierExpr extends Expr { 41 private static final L10N L = new L10N(UnboundIdentifierExpr.class); 42 private static final Logger log = Log.open(UnboundIdentifierExpr.class); 43 44 private String _table; 45 private String _column; 46 47 50 UnboundIdentifierExpr(String column) 51 { 52 _column = column; 53 } 54 55 58 UnboundIdentifierExpr(String table, String column) 59 { 60 _table = table; 61 _column = column; 62 } 63 64 67 protected long lookupCost(ArrayList <FromItem> fromList) 68 { 69 Column column = findColumn(fromList); 70 if (column == null) 71 return Integer.MAX_VALUE; 72 73 FromItem fromItem = fromList.get(fromList.size() - 1); 74 75 Table table = fromItem.getTable(); 76 77 column = table.getColumn(_column); 78 79 if (column == null) 80 return 100 * 100 * 100; 81 else if (column.isPrimaryKey()) 82 return 100; 83 else if (column.isUnique()) 84 return 100 * 100; 85 else 86 return 100 * 100 * 100; 87 } 88 89 92 public long subCost(ArrayList <FromItem> fromList) 93 { 94 Column column = findColumn(fromList); 95 96 if (column == null) 97 return Integer.MAX_VALUE; 98 else 99 return 10 * 100 * 100 * 100; 100 } 101 102 105 private Column findColumn(ArrayList <FromItem> fromItems) 106 { 107 if (_table == null) { 108 for (int i = 0; i < fromItems.size(); i++) { 109 FromItem fromItem = fromItems.get(i); 110 111 Table table = fromItem.getTable(); 112 113 Column column = table.getColumn(_column); 114 115 if (column != null) 116 return column; 117 } 118 119 return null; 120 } 121 else { 122 for (int i = 0; i < fromItems.size(); i++) { 123 FromItem fromItem = fromItems.get(i); 124 125 if (_table.equals(fromItem.getName())) { 126 Table table = fromItem.getTable(); 127 128 return table.getColumn(_column); 129 } 130 } 131 132 return null; 133 } 134 } 135 136 protected Expr bind(Query query) 137 throws SQLException 138 { 139 return query.bind(_table, _column); 140 } 141 142 public String toString() 143 { 144 if (_table != null) 145 return "UnboundIdentifier[" + _table + "," + _column + "]"; 146 else 147 return "UnboundIdentifier[" + _column + "]"; 148 } 149 } 150 | Popular Tags |