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.db.table.TableIterator;
034: import com.caucho.log.Log;
035: import com.caucho.util.L10N;
036:
037: import java.sql.SQLException;
038: import java.util.ArrayList;
039: import java.util.logging.Logger;
040:
041: class IdExpr extends Expr {
042: private static final L10N L = new L10N(IdExpr.class);
043: private static final Logger log = Log.open(IdExpr.class);
044:
045: private FromItem _fromItem;
046: private Column _column;
047:
048: private int _tableIndex = -1;
049:
050: /**
051: * Creates an unbound identifier with just a column name.
052: */
053: IdExpr(FromItem fromItem, Column column) {
054: _fromItem = fromItem;
055: _column = column;
056: }
057:
058: public Class getType() {
059: return _column.getJavaType();
060: }
061:
062: /**
063: * Returns the column.
064: */
065: public Column getColumn() {
066: return _column;
067: }
068:
069: /**
070: * Returns any column name.
071: */
072: public String getName() {
073: return _column.getName();
074: }
075:
076: /**
077: * Returns the from item
078: */
079: public FromItem getFromItem() {
080: return _fromItem;
081: }
082:
083: /**
084: * Returns the column's table.
085: */
086: public Table getTable() {
087: return _fromItem.getTable();
088: }
089:
090: /**
091: * The cost of a match of the expr.
092: */
093: protected long lookupCost(ArrayList<FromItem> fromList) {
094: if (!fromList.contains(_fromItem))
095: return COST_NO_TABLE;
096:
097: if (fromList.indexOf(_fromItem) < fromList.size() - 1)
098: return 0;
099:
100: if (_column.isPrimaryKey())
101: return COST_INDEX;
102: else if (_column.isUnique())
103: return COST_UNIQUE;
104: else
105: return COST_SCAN;
106: }
107:
108: /**
109: * The cost of a match of the expr.
110: */
111: public long subCost(ArrayList<FromItem> fromList) {
112: if (!fromList.contains(_fromItem))
113: return Integer.MAX_VALUE;
114:
115: return 10 * 100 * 100 * 100;
116: }
117:
118: protected Expr bind(Query query) throws SQLException {
119: FromItem[] fromItems = query.getFromItems();
120:
121: for (int i = 0; i < fromItems.length; i++) {
122: if (fromItems[i] == _fromItem)
123: _tableIndex = i;
124: }
125:
126: return this ;
127: }
128:
129: /**
130: * Returns true if the expression is null.
131: */
132: public boolean isNull(QueryContext context) throws SQLException {
133: TableIterator[] rows = context.getTableIterators();
134: TableIterator row = rows[_tableIndex];
135:
136: return row.isNull(_column);
137: }
138:
139: /**
140: * Evaluates the expression as a string.
141: */
142: public String evalString(QueryContext context) throws SQLException {
143: TableIterator[] rows = context.getTableIterators();
144: TableIterator row = rows[_tableIndex];
145:
146: return row.getString(_column);
147: }
148:
149: /**
150: * Evaluates the expression as a boolean.
151: */
152: public int evalBoolean(QueryContext context) throws SQLException {
153: TableIterator[] rows = context.getTableIterators();
154: TableIterator row = rows[_tableIndex];
155:
156: String value = row.getString(_column);
157:
158: if (value == null)
159: return UNKNOWN;
160: else if (value.equals("1"))
161: return TRUE;
162: else if (value.equalsIgnoreCase("t"))
163: return TRUE;
164: else if (value.equalsIgnoreCase("y"))
165: return TRUE;
166: else
167: return FALSE;
168: }
169:
170: public int evalInt(QueryContext context) throws SQLException {
171: TableIterator[] rows = context.getTableIterators();
172: TableIterator row = rows[_tableIndex];
173:
174: return row.getInteger(_column);
175: }
176:
177: public long evalLong(QueryContext context) throws SQLException {
178: TableIterator[] rows = context.getTableIterators();
179: TableIterator row = rows[_tableIndex];
180:
181: return row.getLong(_column);
182: }
183:
184: public double evalDouble(QueryContext context) throws SQLException {
185: TableIterator[] rows = context.getTableIterators();
186: TableIterator row = rows[_tableIndex];
187:
188: return row.getDouble(_column);
189: }
190:
191: /**
192: * Evaluates the expression, writing to the result stream.
193: *
194: * @param context the query context
195: * @param result the output result
196: */
197: public void evalToResult(QueryContext context, SelectResult result)
198: throws SQLException {
199: TableIterator[] rows = context.getTableIterators();
200: TableIterator row = rows[_tableIndex];
201:
202: row.evalToResult(_column, result);
203: }
204:
205: public boolean evalEqual(QueryContext context, byte[] matchBuffer)
206: throws SQLException {
207: TableIterator[] rows = context.getTableIterators();
208: TableIterator row = rows[_tableIndex];
209:
210: return row.isEqual(_column, matchBuffer);
211: }
212:
213: public boolean evalEqual(QueryContext context, String string)
214: throws SQLException {
215: TableIterator[] rows = context.getTableIterators();
216: TableIterator row = rows[_tableIndex];
217:
218: return row.isEqual(_column, string);
219: }
220:
221: public boolean equals(Object o) {
222: if (o == null || !IdExpr.class.equals(o.getClass()))
223: return false;
224:
225: IdExpr expr = (IdExpr) o;
226:
227: return (_fromItem == expr._fromItem && _column == expr._column);
228: }
229:
230: public String toString() {
231: return _fromItem.getName() + "." + _column.getName();
232: }
233: }
|