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:
036: import java.sql.SQLException;
037: import java.util.logging.Logger;
038:
039: class ColumnExpr extends Expr {
040: private static final Logger log = Log.open(ColumnExpr.class);
041:
042: private Table _table;
043:
044: private int _tableIndex;
045: private Column _column;
046: private int _columnIndex;
047:
048: private String _name;
049: private Class _type;
050:
051: ColumnExpr(String name, Table table, int tableIndex,
052: int columnIndex, Class type) {
053: _name = name;
054: _table = table;
055: _tableIndex = tableIndex;
056: _columnIndex = columnIndex;
057: _column = table.getColumns()[_columnIndex];
058: _type = type;
059: }
060:
061: public Class getType() {
062: return _type;
063: }
064:
065: /**
066: * Returns any column name.
067: */
068: public String getName() {
069: return _name;
070: }
071:
072: /**
073: * Returns the column.
074: */
075: public Column getColumn() {
076: return _column;
077: }
078:
079: /**
080: * Returns the column's table.
081: */
082: public Table getTable() {
083: return _table;
084: }
085:
086: /**
087: * Returns true if the expression is null.
088: */
089: public boolean isNull(QueryContext context) throws SQLException {
090: TableIterator[] rows = context.getTableIterators();
091: TableIterator row = rows[_tableIndex];
092:
093: return row.isNull(_column);
094: }
095:
096: /**
097: * Evaluates the expression as a string.
098: */
099: public String evalString(QueryContext context) throws SQLException {
100: TableIterator[] rows = context.getTableIterators();
101: TableIterator row = rows[_tableIndex];
102:
103: return row.getString(_column);
104: }
105:
106: public int evalInt(QueryContext context) throws SQLException {
107: TableIterator[] rows = context.getTableIterators();
108: TableIterator row = rows[_tableIndex];
109:
110: return row.getInteger(_column);
111: }
112:
113: public long evalLong(QueryContext context) throws SQLException {
114: TableIterator[] rows = context.getTableIterators();
115: TableIterator row = rows[_tableIndex];
116:
117: return row.getLong(_column);
118: }
119:
120: public double evalDouble(QueryContext context) throws SQLException {
121: TableIterator[] rows = context.getTableIterators();
122: TableIterator row = rows[_tableIndex];
123:
124: return row.getDouble(_column);
125: }
126:
127: /**
128: * Evaluates the expression, writing to the result stream.
129: *
130: * @param context the query context
131: * @param result the output result
132: */
133: public void evalToResult(QueryContext context, SelectResult result)
134: throws SQLException {
135: TableIterator[] rows = context.getTableIterators();
136: TableIterator row = rows[_tableIndex];
137:
138: row.evalToResult(_column, result);
139: }
140:
141: public boolean evalEqual(QueryContext context, byte[] matchBuffer)
142: throws SQLException {
143: TableIterator[] rows = context.getTableIterators();
144: TableIterator row = rows[_tableIndex];
145:
146: return row.isEqual(_column, matchBuffer);
147: }
148:
149: public boolean evalEqual(QueryContext context, String string)
150: throws SQLException {
151: TableIterator[] rows = context.getTableIterators();
152: TableIterator row = rows[_tableIndex];
153:
154: return row.isEqual(_column, string);
155: }
156:
157: public boolean equals(Object o) {
158: if (o == null || !ColumnExpr.class.equals(o.getClass()))
159: return false;
160:
161: ColumnExpr expr = (ColumnExpr) o;
162:
163: return (_tableIndex == expr._tableIndex && _column == expr._column);
164: }
165:
166: public String toString() {
167: return "ColumnExpr[" + _tableIndex + "," + _columnIndex + "]";
168: }
169: }
|