KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 class UnaryExpr extends Expr {
38   private static final Logger JavaDoc log = Log.open(UnaryExpr.class);
39
40   private Expr _sub;
41   private int _op;
42
43   UnaryExpr(Expr sub, int op)
44   {
45     _sub = sub;
46     _op = op;
47   }
48
49   /**
50    * Binds the expression to the actual tables.
51    */

52   protected Expr bind(Query query)
53     throws SQLException JavaDoc
54   {
55     Expr newSub = _sub.bind(query);
56
57     switch (_op) {
58     case '-':
59       if (! newSub.isDouble())
60     throw new SQLException JavaDoc(L.l("unary minus requires a numeric expression at '{0}'", newSub));
61       break;
62     }
63
64     if (newSub == _sub)
65       return this;
66     else
67       return new UnaryExpr(newSub, _op);
68   }
69
70   /**
71    * Returns the type of the expression.
72    */

73   public Class JavaDoc getType()
74   {
75     switch (_op) {
76     case '-':
77       if (_sub.isLong())
78     return long.class;
79       else
80     return double.class;
81
82     default:
83       return Object JavaDoc.class;
84     }
85   }
86
87   /**
88    * Returns the cost based on the given FromList.
89    */

90   public long subCost(ArrayList JavaDoc<FromItem> fromList)
91   {
92     return _sub.subCost(fromList);
93   }
94
95   /**
96    * Evaluates the expression for nulls
97    */

98   public boolean isNull(QueryContext context)
99     throws SQLException JavaDoc
100   {
101     return _sub.isNull(context);
102   }
103
104   /**
105    * Evaluates the expression as a boolean
106    *
107    * @param rows the current tuple being evaluated
108    *
109    * @return the boolean value
110    */

111   public int evalBoolean(QueryContext context)
112     throws SQLException JavaDoc
113   {
114     switch (_op) {
115     default:
116       throw new IllegalStateException JavaDoc();
117     }
118   }
119
120   /**
121    * Evaluates the expression as a long.
122    *
123    * @param rows the current tuple being evaluated
124    *
125    * @return the long value
126    */

127   public long evalLong(QueryContext context)
128     throws SQLException JavaDoc
129   {
130     switch (_op) {
131     case '-':
132       return - _sub.evalLong(context);
133
134     default:
135       throw new IllegalStateException JavaDoc();
136     }
137   }
138
139   /**
140    * Evaluates the expression as a double.
141    *
142    * @param rows the current tuple being evaluated
143    *
144    * @return the long value
145    */

146   public double evalDouble(QueryContext context)
147     throws SQLException JavaDoc
148   {
149     switch (_op) {
150     case '-':
151       return - _sub.evalDouble(context);
152
153     default:
154       throw new IllegalStateException JavaDoc();
155     }
156   }
157
158   /**
159    * Evaluates the expression as a string.
160    *
161    * @param rows the current tuple being evaluated
162    *
163    * @return the string value
164    */

165   public String JavaDoc evalString(QueryContext context)
166     throws SQLException JavaDoc
167   {
168     switch (_op) {
169     case '-':
170       if (isLong())
171     return String.valueOf(evalLong(context));
172       else
173     return String.valueOf(evalDouble(context));
174
175     default:
176       throw new IllegalStateException JavaDoc();
177     }
178   }
179
180   /**
181    * Evaluates aggregate functions during the group phase.
182    *
183    * @param state the current database tuple
184    */

185   public void evalGroup(QueryContext context)
186     throws SQLException JavaDoc
187   {
188     _sub.evalGroup(context);
189   }
190
191   public String JavaDoc toString()
192   {
193     switch (_op) {
194     case '-':
195       return "- " + _sub;
196
197     default:
198       throw new IllegalStateException JavaDoc("can't compare:" + _op);
199     }
200   }
201 }
202
Popular Tags