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: *
023: * Free SoftwareFoundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.db.sql;
031:
032: import com.caucho.log.Log;
033:
034: import java.sql.SQLException;
035: import java.util.ArrayList;
036: import java.util.logging.Logger;
037:
038: class EqExpr extends Expr {
039: private static final Logger log = Log.open(EqExpr.class);
040:
041: private Expr _left;
042: private Expr _right;
043:
044: EqExpr(Expr left, Expr right) {
045: _left = left;
046: _right = right;
047:
048: if (left == null || right == null)
049: throw new NullPointerException();
050:
051: if (right instanceof UnboundIdentifierExpr
052: && !(left instanceof UnboundIdentifierExpr)) {
053: Expr temp = _right;
054: _right = _left;
055: _left = temp;
056: }
057: }
058:
059: protected Expr bind(Query query) throws SQLException {
060: Expr newLeft = _left.bind(query);
061: Expr newRight = _right.bind(query);
062:
063: if (newLeft instanceof ColumnExpr
064: && newLeft.getType().equals(String.class)) {
065: return new StringEqExpr((ColumnExpr) newLeft, newRight);
066: } else if (newRight instanceof ColumnExpr
067: && newRight.getType().equals(String.class)) {
068: return new StringEqExpr((ColumnExpr) newRight, newLeft);
069: }
070:
071: if (_left == newLeft && _right == newRight)
072: return this ;
073: else
074: return new EqExpr(newLeft, newRight);
075: }
076:
077: /**
078: * Returns an index expression if available.
079: */
080: public IndexExpr getIndexExpr(FromItem item) {
081: if (_left instanceof IdExpr) {
082: IdExpr expr = (IdExpr) _left;
083:
084: if (expr.getColumn().getIndex() != null
085: && item == expr.getFromItem()) {
086: return new IndexExpr(expr, _right);
087: }
088: }
089:
090: if (_right instanceof IdExpr) {
091: IdExpr expr = (IdExpr) _right;
092:
093: if (expr.getColumn().getIndex() != null
094: && item == expr.getFromItem()) {
095: return new IndexExpr(expr, _left);
096: }
097: }
098:
099: return null;
100: }
101:
102: /**
103: * Returns the type of the expression.
104: */
105: public Class getType() {
106: return boolean.class;
107: }
108:
109: /**
110: * Returns the cost based on the given FromList.
111: */
112: public long cost(ArrayList<FromItem> fromList) {
113: if (_left instanceof UnboundIdentifierExpr
114: && _right.cost(fromList) == 0) {
115: UnboundIdentifierExpr id = (UnboundIdentifierExpr) _left;
116:
117: return id.lookupCost(fromList);
118: } else if (_right instanceof UnboundIdentifierExpr
119: && _left.cost(fromList) == 0) {
120: UnboundIdentifierExpr id = (UnboundIdentifierExpr) _right;
121:
122: return id.lookupCost(fromList);
123: } else {
124: return subCost(fromList);
125: }
126: }
127:
128: /**
129: * Returns the cost based on a subitem.
130: */
131: public long subCost(ArrayList<FromItem> fromList) {
132: return _left.subCost(fromList) + _right.subCost(fromList);
133: }
134:
135: /**
136: * Evaluates the expression for nulls
137: */
138: public boolean isNull(QueryContext context) throws SQLException {
139: return (_left.isNull(context) || _right.isNull(context));
140: }
141:
142: /**
143: * Evaluates the expression as a boolean.
144: */
145: public int evalBoolean(QueryContext context) throws SQLException {
146: if (_left.isNull(context) || _right.isNull(context))
147: return UNKNOWN;
148:
149: if (_left.isLong() && _right.isLong()) {
150: if (_left.evalLong(context) == _right.evalLong(context))
151: return TRUE;
152: else
153: return FALSE;
154: } else if (_left.isDouble() && _right.isDouble()) {
155: if (_left.evalDouble(context) == _right.evalDouble(context))
156: return TRUE;
157: else
158: return FALSE;
159: } else {
160: String leftValue = _left.evalString(context);
161: String rightValue = _right.evalString(context);
162:
163: if (leftValue == rightValue || leftValue.equals(rightValue))
164: return TRUE;
165: else
166: return FALSE;
167: }
168: }
169:
170: public String evalString(QueryContext context) throws SQLException {
171: throw new SQLException("can't convert string to boolean");
172: }
173:
174: /**
175: * Evaluates aggregate functions during the group phase.
176: *
177: * @param state the current database tuple
178: */
179: public void evalGroup(QueryContext context) throws SQLException {
180: _left.evalGroup(context);
181: _right.evalGroup(context);
182: }
183:
184: public String toString() {
185: return "(" + _left + " = " + _right + ")";
186: }
187: }
|