KickJava   Java API By Example, From Geeks To Geeks.

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


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.bytecode.JClass;
32 import com.caucho.config.ConfigException;
33 import com.caucho.util.CharBuffer;
34
35 /**
36  * An 'is' expression
37  */

38 class IsExpr extends Expr {
39   // value expression
40
private Expr _value;
41   // operation
42
private int _op;
43   // true if this is a negative between
44
private boolean _isNot;
45
46   /**
47    * Creates a like expression.
48    *
49    * @param value the value expression
50    * @param op the operation
51    * @param isNot if true, this the like is negated
52    */

53   IsExpr(Query query, Expr value, int op, boolean isNot)
54     throws ConfigException
55   {
56     _query = query;
57     
58     _value = value;
59     _op = op;
60     _isNot = isNot;
61
62     evalTypes();
63   }
64
65   /**
66    * Evaluates the types for the expression
67    */

68   void evalTypes()
69     throws ConfigException
70   {
71     if (getJavaType() != null)
72       return;
73
74     if (_op == Query.EMPTY && ! _value.isCollection())
75       throw error(L.l("IS EMPTY requires a collection at `{0}'",
76                       _value));
77     else if (_op == Query.NULL && _value.isCollection())
78       throw error(L.l("IS NULL requires a single value at `{0}'",
79                       _value));
80
81     setJavaType(boolean.class);
82   }
83   
84
85   /**
86    * Prints the where SQL for this expression
87    *
88    * @param gen the java code generator
89    */

90   void generateWhere(CharBuffer cb)
91   {
92     _value.generateWhereSubExpr(cb);
93
94     JClass valueType = _value.getJavaType();
95       /*
96     if (javax.ejb.EJBLocalObject.class.isAssignableFrom(valueType)) {
97       EjbEntityBean bean = _query.getBeanByType(valueType);
98
99       valueType = bean.getPrimaryKey().getJavaType();
100       
101       if (byte.class.equals(valueType) ||
102           short.class.equals(valueType) ||
103           int.class.equals(valueType) ||
104           long.class.equals(valueType)) {
105         if (_op == Query.NULL) {
106           if (_isNot)
107             cb.append(" <> 0");
108           else
109             cb.append(" = 0");
110           return;
111         }
112       }
113       throw new UnsupportedOperationException();
114     }
115       */

116
117     if (_isNot)
118       cb.append(" IS NOT ");
119     else
120       cb.append(" IS ");
121
122     switch (_op) {
123     case Query.NULL:
124       cb.append("NULL");
125       break;
126     case Query.EMPTY:
127       cb.append("EMPTY");
128       break;
129     }
130   }
131
132   
133   public String JavaDoc toString()
134   {
135     String JavaDoc str = _value.toString();
136
137     if (_isNot)
138       str += " IS NOT ";
139     else
140       str += " IS ";
141
142     switch (_op) {
143     case Query.NULL:
144       return str + "NULL";
145     case Query.EMPTY:
146       return str + "EMPTY";
147     default:
148       return super.toString();
149     }
150   }
151 }
152
Popular Tags