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.log.Log;
032:
033: import java.sql.SQLException;
034: import java.util.ArrayList;
035: import java.util.logging.Logger;
036:
037: class SubSelectParamExpr extends Expr {
038: private static final Logger log = Log
039: .open(SubSelectParamExpr.class);
040:
041: private SelectQuery _subselect;
042: private Expr _expr;
043: private int _index;
044:
045: SubSelectParamExpr(Query subselect, Expr expr, int index) {
046: _subselect = (SelectQuery) subselect;
047: _expr = expr;
048: _index = index;
049: }
050:
051: /**
052: * Returns the type of the expression.
053: */
054: public Class getType() {
055: return _expr.getType();
056: }
057:
058: /**
059: * Returns the expr.
060: */
061: public Expr getExpr() {
062: return _expr;
063: }
064:
065: /**
066: * Returns the cost based on the given FromList.
067: */
068: public long subCost(ArrayList<FromItem> fromList) {
069: return _subselect.getSubSelect().cost(fromList) + 1;
070: }
071:
072: /**
073: * Binds the expression.
074: */
075: public Expr bind(Query parent) throws SQLException {
076: _expr = _expr.bind(parent);
077:
078: return this ;
079: }
080:
081: /**
082: * Sets the value.
083: */
084: public void eval(QueryContext parent, QueryContext context)
085: throws SQLException {
086: Class type = getType();
087:
088: if (_expr.isNull(parent))
089: context.setNull(_index);
090: else if (long.class.equals(type))
091: context.setLong(_index, _expr.evalLong(parent));
092: else if (int.class.equals(type))
093: context.setLong(_index, _expr.evalLong(parent));
094: else {
095: context.setString(_index, _expr.evalString(parent));
096: }
097: }
098:
099: /**
100: * Evaluates the expression as a string.
101: *
102: * @param rows the current database tuple
103: *
104: * @return the string value
105: */
106: public boolean isNull(QueryContext context) throws SQLException {
107: return context.isNull(_index);
108: }
109:
110: /**
111: * Evaluates the expression as a string.
112: *
113: * @param rows the current database tuple
114: *
115: * @return the string value
116: */
117: public String evalString(QueryContext context) throws SQLException {
118: return context.getString(_index);
119: }
120:
121: /**
122: * Evaluates the expression as a boolean.
123: *
124: * @param rows the current database tuple
125: *
126: * @return the boolean value
127: */
128: public int evalBoolean(QueryContext context) throws SQLException {
129: return context.getBoolean(_index);
130: }
131:
132: /**
133: * Evaluates the expression as a long.
134: *
135: * @param rows the current database tuple
136: *
137: * @return the long value
138: */
139: public long evalLong(QueryContext context) throws SQLException {
140: return context.getLong(_index);
141: }
142:
143: /**
144: * Evaluates the expression as a double.
145: *
146: * @param rows the current database tuple
147: *
148: * @return the double value
149: */
150: public double evalDouble(QueryContext context) throws SQLException {
151: return context.getDouble(_index);
152: }
153:
154: /**
155: * Evaluates the expression as a date
156: *
157: * @param rows the current database tuple
158: *
159: * @return the date value
160: */
161: public long evalDate(QueryContext context) throws SQLException {
162: return context.getDate(_index);
163: }
164:
165: public String toString() {
166: return "SubSelectParamExpr[" + _expr + "]";
167: }
168: }
|