KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.util.CharBuffer;
33
34 import java.sql.SQLException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 class AndExpr extends Expr {
39   private static final Logger JavaDoc log = Log.open(AndExpr.class);
40
41   private ArrayList JavaDoc<Expr> _exprs = new ArrayList JavaDoc<Expr>();
42
43   AndExpr()
44   {
45   }
46
47   void add(Expr expr)
48   {
49     _exprs.add(expr);
50   }
51
52   Expr getSingleExpr()
53   {
54     if (_exprs.size() == 0)
55       return null;
56     else if (_exprs.size() == 1)
57       return _exprs.get(0);
58     else
59       return this;
60   }
61
62   protected Expr bind(Query query)
63     throws SQLException JavaDoc
64   {
65     for (int i = 0; i < _exprs.size(); i++) {
66       Expr expr = _exprs.get(i);
67
68       expr = expr.bind(query);
69       
70       if (! expr.getType().equals(boolean.class))
71     throw new SQLException JavaDoc(L.l("AND requires boolean operands at {0}",
72                    expr));
73
74       _exprs.set(i, expr);
75     }
76
77     return this;
78   }
79
80   /**
81    * Returns the type of the expression.
82    */

83   public Class JavaDoc getType()
84   {
85     return boolean.class;
86   }
87
88   /**
89    * Returns the cost based on the given FromList.
90    */

91   public long subCost(ArrayList JavaDoc<FromItem> fromList)
92   {
93     long cost = 0;
94
95     for (int i = 0; i < _exprs.size(); i++) {
96       cost += _exprs.get(i).subCost(fromList);
97     }
98
99     return cost;
100   }
101
102   /**
103    * Splits the expr into and blocks.
104    */

105   public void splitAnd(ArrayList JavaDoc<Expr> andProduct)
106   {
107     for (int i = 0; i < _exprs.size(); i++) {
108       _exprs.get(i).splitAnd(andProduct);
109     }
110   }
111
112   /**
113    * Returns true for a null expression
114    */

115   public boolean isNull(QueryContext context)
116     throws SQLException JavaDoc
117   {
118     boolean isNull = false;
119     for (int i = 0; i < _exprs.size(); i++) {
120       int value = _exprs.get(i).evalBoolean(context);
121
122       if (value == FALSE)
123     return false;
124       else if (value != TRUE)
125     isNull = true;
126     }
127
128     return isNull;
129   }
130
131   /**
132    * Evaluates the expression as a boolean.
133    */

134   public int evalBoolean(QueryContext context)
135     throws SQLException JavaDoc
136   {
137     int value = TRUE;
138     
139     for (int i = 0; i < _exprs.size(); i++) {
140       int subValue = _exprs.get(i).evalBoolean(context);
141       
142       if (subValue == FALSE)
143     return FALSE;
144       else if (subValue == UNKNOWN)
145     value = UNKNOWN;
146     }
147
148     return value;
149   }
150
151   public String JavaDoc evalString(QueryContext context)
152     throws SQLException JavaDoc
153   {
154     switch (evalBoolean(context)) {
155     case TRUE:
156       return "1";
157     case FALSE:
158       return "0";
159     default:
160       return null;
161     }
162   }
163
164   public String JavaDoc toString()
165   {
166     CharBuffer cb = CharBuffer.allocate();
167     cb.append("(");
168
169     for (int i = 0; i < _exprs.size(); i++) {
170       if (i != 0)
171     cb.append(" AND ");
172
173       cb.append(_exprs.get(i));
174     }
175
176     cb.append(")");
177     
178     return cb.close();
179   }
180 }
181
Popular Tags