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