KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > ql > InExpr


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.ejb.ql;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.util.CharBuffer;
33
34 import java.util.ArrayList JavaDoc;
35
36 /**
37  * An 'in' expression
38  */

39 class InExpr extends Expr {
40   // value expression
41
private Expr _expr;
42   // args
43
private ArrayList JavaDoc<Expr> _args;
44   // true if this is a negative between
45
private boolean _isNot;
46
47   /**
48    * Creates an 'in' expression.
49    *
50    * @param expr the value expression
51    * @param args the args
52    * @param isNot if true, this the like is negated
53    */

54   InExpr(Query query, Expr expr, ArrayList JavaDoc<Expr> args, boolean isNot)
55     throws ConfigException
56   {
57     _query = query;
58     
59     _expr = expr;
60     _args = args;
61     _isNot = isNot;
62
63     evalTypes();
64   }
65
66   /**
67    * Evaluates the types for the expression
68    */

69   void evalTypes()
70     throws ConfigException
71   {
72     if (getJavaType() != null)
73       return;
74
75     for (int i = 0; i < _args.size(); i++) {
76       Expr expr = _args.get(i);
77
78       if (! expr.isString())
79         throw error(L.l("'IN' literal requires strings at `{0}'",
80                         expr));
81     }
82     
83     setJavaType(boolean.class);
84   }
85   
86
87   /**
88    * Prints the where SQL for this expression
89    *
90    * @param gen the java code generator
91    */

92   void generateWhere(CharBuffer cb)
93   {
94     _expr.generateWhereSubExpr(cb);
95
96     if (_isNot)
97       cb.append(" NOT ");
98
99     cb.append(" IN (");
100     for (int i = 0; i < _args.size(); i++) {
101       Expr arg = _args.get(i);
102
103       if (i != 0)
104         cb.append(", ");
105       
106       arg.generateWhereSubExpr(cb);
107     }
108     cb.append(")");
109   }
110
111   
112   public String JavaDoc toString()
113   {
114     String JavaDoc str = _expr.toString();
115
116     if (_isNot)
117       str += " NOT ";
118
119     str += " IN (";
120     for (int i = 0; i < _args.size(); i++) {
121       if (i != 0)
122         str += ", ";
123       
124       Expr arg = _args.get(i);
125
126       str += arg;
127     }
128     str += ")";
129
130     return str;
131   }
132 }
133
Popular Tags