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: import com.caucho.util.L10N;
033:
034: import java.sql.SQLException;
035: import java.util.ArrayList;
036: import java.util.logging.Logger;
037:
038: public class SubSelectExpr extends Expr {
039: protected static final L10N L = new L10N(SubSelectExpr.class);
040: private static final Logger log = Log.open(SubSelectExpr.class);
041:
042: private int _groupIndex;
043: private SelectQuery _subselect;
044:
045: private Query _parentQuery;
046:
047: SubSelectExpr(SelectQuery query) {
048: _subselect = query;
049: }
050:
051: /**
052: * Returns the query.
053: */
054: public SelectQuery getSubSelect() {
055: return _subselect;
056: }
057:
058: /**
059: * Binds the expression to the query.
060: */
061: protected Expr bind(Query query) throws SQLException {
062: if (_parentQuery != null)
063: return this ;
064:
065: _parentQuery = query;
066: _groupIndex = query.getDataFields();
067:
068: query.setDataFields(_groupIndex + 1);
069:
070: _subselect.bind();
071:
072: return this ;
073: }
074:
075: /**
076: * Returns the expected result type of the expression.
077: */
078: public Class getType() {
079: return _subselect.getType();
080: }
081:
082: ArrayList<SubSelectParamExpr> getParamExprs() {
083: return _subselect.getParamExprs();
084: }
085:
086: /**
087: * Returns the cost based on the given FromList.
088: */
089: public long subCost(ArrayList<FromItem> fromList) {
090: ArrayList<SubSelectParamExpr> paramExprs = getParamExprs();
091:
092: long cost = 10;
093:
094: for (int i = 0; i < paramExprs.size(); i++)
095: cost += paramExprs.get(i).getExpr().cost(fromList);
096:
097: return 2 * cost;
098: }
099:
100: /**
101: * Evaluates the subselect.
102: */
103: void evaluate(QueryContext context) throws SQLException {
104: QueryContext subcontext = QueryContext.allocate();
105:
106: ArrayList<SubSelectParamExpr> paramExprs = getParamExprs();
107:
108: for (int i = 0; i < paramExprs.size(); i++) {
109: paramExprs.get(i).eval(context, subcontext);
110: }
111:
112: _subselect.execute(subcontext, context.getTransaction());
113:
114: SelectResult result = subcontext.getResult();
115:
116: Data data = context.getGroupData(_groupIndex);
117:
118: if (result.next()) {
119: Class type = _subselect.getType();
120:
121: if (long.class.equals(type))
122: data.setLong(result.getLong(0));
123: else
124: data.setString(result.getString(0));
125: } else {
126: data.clear();
127: }
128:
129: result.close();
130: QueryContext.free(subcontext);
131: }
132:
133: /**
134: * Evaluates the expression as a string.
135: *
136: * @param rows the current database tuple
137: *
138: * @return the string value
139: */
140: public String evalString(QueryContext context) throws SQLException {
141: Data data = context.getGroupData(_groupIndex);
142:
143: return data.getString();
144: }
145:
146: public String toString() {
147: return "SubSelectExpr[" + _subselect + "]";
148: }
149: }
|