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.logging.Logger;
035:
036: class DefaultExpr extends Expr {
037: private static final Logger log = Log.open(DefaultExpr.class);
038:
039: private Expr _expr;
040: private Expr _default;
041:
042: DefaultExpr(Expr expr, Expr defaultExpr) {
043: _expr = expr;
044: _default = defaultExpr;
045: }
046:
047: protected Expr bind(Query query) throws SQLException {
048: Expr newExpr = _expr.bind(query);
049:
050: if (_expr == newExpr)
051: return this ;
052: else
053: return new DefaultExpr(newExpr, _default);
054: }
055:
056: /**
057: * Returns the type of the expression.
058: */
059: public Class getType() {
060: return _expr.getType();
061: }
062:
063: /**
064: * Returns true if the expression is null.
065: *
066: * @param rows the current database tuple
067: *
068: * @return true if null
069: */
070: public boolean isNull(QueryContext context) throws SQLException {
071: return _expr.isNull(context) && _default.isNull(context);
072: }
073:
074: /**
075: * Evaluates the expression as a boolean.
076: *
077: * @param rows the current database tuple
078: *
079: * @return the boolean value
080: */
081: public int evalBoolean(QueryContext context) throws SQLException {
082: if (!_expr.isNull(context))
083: return _expr.evalBoolean(context);
084: else
085: return _default.evalBoolean(context);
086: }
087:
088: /**
089: * Evaluates the expression as a string.
090: *
091: * @param rows the current database tuple
092: *
093: * @return the string value
094: */
095: public String evalString(QueryContext context) throws SQLException {
096: if (!_expr.isNull(context))
097: return _expr.evalString(context);
098: else
099: return _default.evalString(context);
100: }
101:
102: /**
103: * Evaluates the expression as a long.
104: *
105: * @param rows the current database tuple
106: *
107: * @return the long value
108: */
109: public long evalLong(QueryContext context) throws SQLException {
110: if (!_expr.isNull(context))
111: return _expr.evalLong(context);
112: else
113: return _default.evalLong(context);
114: }
115:
116: /**
117: * Evaluates the expression as a double.
118: *
119: * @param rows the current database tuple
120: *
121: * @return the double value
122: */
123: public double evalDouble(QueryContext context) throws SQLException {
124: if (!_expr.isNull(context))
125: return _expr.evalDouble(context);
126: else
127: return _default.evalDouble(context);
128: }
129:
130: /**
131: * Evaluates the expression as a date.
132: *
133: * @param rows the current database tuple
134: *
135: * @return the double value
136: */
137: public long evalDate(QueryContext context) throws SQLException {
138: if (!_expr.isNull(context))
139: return _expr.evalDate(context);
140: else
141: return _default.evalDate(context);
142: }
143:
144: /**
145: * Evaluates the expression, writing to the output stream.
146: *
147: * @param ws the output write stream
148:
149: */
150: public void evalToResult(QueryContext context, SelectResult result)
151: throws SQLException {
152: if (!_expr.isNull(context))
153: _expr.evalToResult(context, result);
154: else
155: _default.evalToResult(context, result);
156: }
157:
158: public String toString() {
159: return "(" + _expr + " DEFAULT " + _default + ")";
160: }
161: }
|