001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.sql;
030:
031: import com.caucho.db.table.Column;
032: import com.caucho.db.table.Table;
033: import com.caucho.log.Log;
034: import com.caucho.util.L10N;
035:
036: import java.sql.SQLException;
037: import java.util.ArrayList;
038: import java.util.logging.Logger;
039:
040: class UnboundIdentifierExpr extends Expr {
041: private static final L10N L = new L10N(UnboundIdentifierExpr.class);
042: private static final Logger log = Log
043: .open(UnboundIdentifierExpr.class);
044:
045: private String _table;
046: private String _column;
047:
048: /**
049: * Creates an unbound identifier with just a column name.
050: */
051: UnboundIdentifierExpr(String column) {
052: _column = column;
053: }
054:
055: /**
056: * Creates an unbound identifier with a table and a column name.
057: */
058: UnboundIdentifierExpr(String table, String column) {
059: _table = table;
060: _column = column;
061: }
062:
063: /**
064: * The cost of a match of the expr.
065: */
066: protected long lookupCost(ArrayList<FromItem> fromList) {
067: Column column = findColumn(fromList);
068: if (column == null)
069: return Integer.MAX_VALUE;
070:
071: FromItem fromItem = fromList.get(fromList.size() - 1);
072:
073: Table table = fromItem.getTable();
074:
075: column = table.getColumn(_column);
076:
077: if (column == null)
078: return 100 * 100 * 100;
079: else if (column.isPrimaryKey())
080: return 100;
081: else if (column.isUnique())
082: return 100 * 100;
083: else
084: return 100 * 100 * 100;
085: }
086:
087: /**
088: * The cost of a match of the expr.
089: */
090: public long subCost(ArrayList<FromItem> fromList) {
091: Column column = findColumn(fromList);
092:
093: if (column == null)
094: return Integer.MAX_VALUE;
095: else
096: return 10 * 100 * 100 * 100;
097: }
098:
099: /**
100: * Finds the column in the from list.
101: */
102: private Column findColumn(ArrayList<FromItem> fromItems) {
103: if (_table == null) {
104: for (int i = 0; i < fromItems.size(); i++) {
105: FromItem fromItem = fromItems.get(i);
106:
107: Table table = fromItem.getTable();
108:
109: Column column = table.getColumn(_column);
110:
111: if (column != null)
112: return column;
113: }
114:
115: return null;
116: } else {
117: for (int i = 0; i < fromItems.size(); i++) {
118: FromItem fromItem = fromItems.get(i);
119:
120: if (_table.equals(fromItem.getName())) {
121: Table table = fromItem.getTable();
122:
123: return table.getColumn(_column);
124: }
125: }
126:
127: return null;
128: }
129: }
130:
131: protected Expr bind(Query query) throws SQLException {
132: return query.bind(_table, _column);
133: }
134:
135: public String toString() {
136: if (_table != null)
137: return "UnboundIdentifier[" + _table + "," + _column + "]";
138: else
139: return "UnboundIdentifier[" + _column + "]";
140: }
141: }
|