KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejb.cfg.EjbEntityBean;
33 import com.caucho.util.CharBuffer;
34
35 import java.lang.reflect.Method JavaDoc;
36
37 /**
38  * An 'is' expression
39  */

40 class EmptyExpr extends Expr {
41   // value expression
42
private CollectionExpr _collection;
43   // true for negative
44
private boolean _isNot;
45   //
46
private Query _parent;
47
48   /**
49    * Creates a like expression.
50    *
51    * @param value the value expression
52    * @param isNot if true, this the like is negated
53    */

54   EmptyExpr(Expr collection, boolean isNot)
55     throws ConfigException
56   {
57     _isNot = isNot;
58     
59     if (collection instanceof CollectionExpr)
60       _collection = (CollectionExpr) collection;
61     else
62       throw error(L.l("IS EMPTY needs a collection-path at `{0}'.",
63                       collection));
64
65     evalTypes();
66   }
67
68   /**
69    * Evaluates the types for the expression
70    */

71   void evalTypes()
72     throws ConfigException
73   {
74     setJavaType(boolean.class);
75   }
76
77   public String JavaDoc addRelation(EjbEntityBean bean, FieldExpr id)
78     throws ConfigException
79   {
80     return null;
81   }
82
83   public Method JavaDoc getMethod()
84   {
85     //return _parent.getMethod();
86
throw new UnsupportedOperationException JavaDoc();
87   }
88
89   public EjbEntityBean getPersistentBean()
90   {
91     //return _parent.getPersistentBean();
92
throw new UnsupportedOperationException JavaDoc();
93   }
94   
95   public void addArg(Expr arg)
96   {
97     //_parent.addArg(arg);
98
throw new UnsupportedOperationException JavaDoc();
99   }
100
101   /**
102    * Prints the where SQL for this expression
103    *
104    * @param gen the java code generator
105    */

106   void generateWhere(CharBuffer cb)
107   {
108     if (_collection != null) {
109       _collection.generateSelect(cb);
110       
111       if (_isNot)
112         cb.append(" IS NOT EMPTY");
113       else
114         cb.append(" IS EMPTY");
115
116       /*
117       String tableSQL = _collection.getRelation().getSQLTable();
118       String []tableSrc = _collection.getRelation().getSourceSQLColumns();
119       String []tableDst = _collection.getRelation().getTargetSQLColumns();
120     
121       String id = "caucho" + gen.getUnique();
122     
123       cb.append("EXISTS(SELECT * FROM " + tableSQL + " " + id + " ");
124       cb.append("WHERE ");
125
126       for (int i = 0; i < tableSrc.length; i++) {
127         if (i != 0)
128           cb.append(" AND ");
129         
130         cb.append(id + "." + tableSrc[i] + " = ");
131
132         _collection.getBase().printComponent(gen, i);
133       }
134     
135       cb.append(")");
136       */

137     }
138     else {
139       cb.append("TRUE");
140     }
141   }
142   
143   public String JavaDoc toString()
144   {
145     String JavaDoc str = _collection.toString();
146
147     if (_isNot)
148       return str + " IS NOT EMPTY";
149     else
150       return str + " IS EMPTY";
151   }
152 }
153
Popular Tags