KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.sql;
31
32 import com.caucho.log.Log;
33
34 import java.sql.SQLException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 class BinaryExpr extends Expr {
39   private static final Logger JavaDoc log = Log.open(BinaryExpr.class);
40
41   private Expr _left;
42   private Expr _right;
43   private int _op;
44
45   BinaryExpr(Expr left, Expr right, int op)
46   {
47     _left = left;
48     _right = right;
49     _op = op;
50   }
51
52   /**
53    * Binds the expression to the actual tables.
54    */

55   protected Expr bind(Query query)
56     throws SQLException JavaDoc
57   {
58     Expr newLeft = _left.bind(query);
59     Expr newRight = _right.bind(query);
60
61     switch (_op) {
62     case '+':
63     case '-':
64     case '*':
65     case '/':
66     case '%':
67       if (newLeft.isLong() && newRight.isLong())
68     return new BinaryLongExpr(newLeft, newRight, _op);
69       else
70     return new BinaryDoubleExpr(newLeft, newRight, _op);
71     }
72
73     throw new SQLException JavaDoc("can't cope: " + newLeft + " " + newLeft.getType() + " " + newRight);
74   }
75
76   /**
77    * Returns the cost based on the given FromList.
78    */

79   public long subCost(ArrayList JavaDoc<FromItem> fromList)
80   {
81     return _left.subCost(fromList) + _right.subCost(fromList);
82   }
83
84   /**
85    * Returns the type of the expression.
86    */

87   public Class JavaDoc getType()
88   {
89     return Object JavaDoc.class;
90   }
91
92   /**
93    * Evaluates aggregate functions during the group phase.
94    *
95    * @param state the current database tuple
96    */

97   public void evalGroup(QueryContext context)
98     throws SQLException JavaDoc
99   {
100     _left.evalGroup(context);
101     _right.evalGroup(context);
102   }
103
104   public String JavaDoc toString()
105   {
106     switch (_op) {
107     case '+':
108       return "(" + _left + " + " + _right + ")";
109
110     case '-':
111       return "(" + _left + " - " + _right + ")";
112
113     case '*':
114       return "(" + _left + " * " + _right + ")";
115
116     case '/':
117       return "(" + _left + " / " + _right + ")";
118
119     case '%':
120       return "(" + _left + " % " + _right + ")";
121
122     default:
123       throw new IllegalStateException JavaDoc("can't compare");
124     }
125   }
126 }
127
Popular Tags