KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > DefaultExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.db.sql;
30
31 import com.caucho.log.Log;
32
33 import java.sql.SQLException JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 class DefaultExpr extends Expr {
37   private static final Logger JavaDoc log = Log.open(DefaultExpr.class);
38
39   private Expr _expr;
40   private Expr _default;
41
42   DefaultExpr(Expr expr, Expr defaultExpr)
43   {
44     _expr = expr;
45     _default = defaultExpr;
46   }
47
48   protected Expr bind(Query query)
49     throws SQLException JavaDoc
50   {
51     Expr newExpr = _expr.bind(query);
52     
53     if (_expr == newExpr)
54       return this;
55     else
56       return new DefaultExpr(newExpr, _default);
57   }
58
59   /**
60    * Returns the type of the expression.
61    */

62   public Class JavaDoc getType()
63   {
64     return _expr.getType();
65   }
66
67   /**
68    * Returns true if the expression is null.
69    *
70    * @param rows the current database tuple
71    *
72    * @return true if null
73    */

74   public boolean isNull(QueryContext context)
75     throws SQLException JavaDoc
76   {
77     return _expr.isNull(context) && _default.isNull(context);
78   }
79
80   /**
81    * Evaluates the expression as a boolean.
82    *
83    * @param rows the current database tuple
84    *
85    * @return the boolean value
86    */

87   public int evalBoolean(QueryContext context)
88     throws SQLException JavaDoc
89   {
90     if (! _expr.isNull(context))
91       return _expr.evalBoolean(context);
92     else
93       return _default.evalBoolean(context);
94   }
95
96   /**
97    * Evaluates the expression as a string.
98    *
99    * @param rows the current database tuple
100    *
101    * @return the string value
102    */

103   public String JavaDoc evalString(QueryContext context)
104     throws SQLException JavaDoc
105   {
106     if (! _expr.isNull(context))
107       return _expr.evalString(context);
108     else
109       return _default.evalString(context);
110   }
111
112   /**
113    * Evaluates the expression as a long.
114    *
115    * @param rows the current database tuple
116    *
117    * @return the long value
118    */

119   public long evalLong(QueryContext context)
120     throws SQLException JavaDoc
121   {
122     if (! _expr.isNull(context))
123       return _expr.evalLong(context);
124     else
125       return _default.evalLong(context);
126   }
127
128   /**
129    * Evaluates the expression as a double.
130    *
131    * @param rows the current database tuple
132    *
133    * @return the double value
134    */

135   public double evalDouble(QueryContext context)
136     throws SQLException JavaDoc
137   {
138     if (! _expr.isNull(context))
139       return _expr.evalDouble(context);
140     else
141       return _default.evalDouble(context);
142   }
143
144   /**
145    * Evaluates the expression as a date.
146    *
147    * @param rows the current database tuple
148    *
149    * @return the double value
150    */

151   public long evalDate(QueryContext context)
152     throws SQLException JavaDoc
153   {
154     if (! _expr.isNull(context))
155       return _expr.evalDate(context);
156     else
157       return _default.evalDate(context);
158   }
159
160   /**
161    * Evaluates the expression, writing to the output stream.
162    *
163    * @param ws the output write stream
164
165    */

166   public void evalToResult(QueryContext context, SelectResult result)
167     throws SQLException JavaDoc
168   {
169     if (! _expr.isNull(context))
170       _expr.evalToResult(context, result);
171     else
172       _default.evalToResult(context, result);
173   }
174
175   public String JavaDoc toString()
176   {
177     return "(" + _expr + " DEFAULT " + _default + ")";
178   }
179 }
180
Popular Tags