KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A 'member' expression
39  */

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

57   MemberExpr(boolean isNot, Expr item, Expr collection)
58     throws ConfigException
59   {
60     _isNot = isNot;
61     
62     if (collection instanceof CollectionExpr)
63       _collection = (CollectionExpr) collection;
64     else if (collection instanceof CollectionIdExpr) {
65       _collectionId = (CollectionIdExpr) collection;
66     }
67     else
68       throw error(L.l("MEMBER OF needs a collection-valued field at `{0}'.",
69                       collection));
70     
71     if (! (item instanceof PathExpr))
72       throw error(L.l("MEMBER OF needs a single-valued field at `{0}'.",
73                       item));
74     
75     _item = (PathExpr) item;
76
77     setJavaType(boolean.class);
78   }
79
80   public String JavaDoc addRelation(EjbEntityBean bean, FieldExpr id)
81     throws ConfigException
82   {
83     return null;
84   }
85
86   public Method JavaDoc getMethod()
87   {
88     //return _parent.getMethod();
89
throw new UnsupportedOperationException JavaDoc();
90   }
91
92   public EjbEntityBean getPersistentBean()
93   {
94     //return _parent.getPersistentBean();
95
throw new UnsupportedOperationException JavaDoc();
96   }
97   
98   public void addArg(Expr arg)
99   {
100     //_parent.addArg(arg);
101
throw new UnsupportedOperationException JavaDoc();
102   }
103
104   /**
105    * Prints the where SQL for this expression
106    *
107    * @param gen the java code generator
108    */

109   void generateWhere(CharBuffer cb)
110   {
111     if (_isNot)
112       cb.append("NOT ");
113
114     _item.generateWhere(cb);
115
116     cb.append(" MEMBER OF (");
117
118     if (_collection != null)
119       _collection.generateSelect(cb);
120     else
121       _collectionId.generateSelect(cb);
122
123     cb.append(")");
124     
125     /*
126     if (_collection != null) {
127       if (_isNot)
128         cb.append("NOT ");
129
130       String tableSQL = _collection.getRelation().getSQLTable();
131     
132       String id = "caucho" + "666";//gen.getUnique();
133     
134       cb.append("EXISTS(SELECT * FROM " + tableSQL + " " + id + " ");
135       cb.append("WHERE ");
136
137       for (int i = 0; i < tableSrc.length; i++) {
138         if (i != 0)
139           cb.append(" AND ");
140         
141         cb.append(id + "." + tableSrc[i] + " = ");
142
143         _collection.getBase().generateComponent(cb, i);
144       }
145
146       cb.append(" AND ");
147
148       for (int i = 0; i < tableDst.length; i++) {
149         if (i != 0)
150           cb.append(" AND ");
151         
152         cb.append(id + "." + tableDst[i] + " = ");
153     
154         _item.generateComponent(cb, i);
155       }
156     
157       cb.append(")");
158
159       throw new UnsupportedOperationException();
160     }
161     else {
162       int len = _collectionId.getKeyFields().length;
163
164       if (_isNot)
165         cb.append("NOT (");
166       
167       for (int i = 0; i < len; i++) {
168         if (i != 0)
169           cb.append(" AND ");
170         
171         _collectionId.generateComponent(cb, i);
172         cb.append(" = ");
173         _item.generateComponent(cb, i);
174       }
175       
176       if (_isNot)
177         cb.append(")");
178     }
179       */

180   }
181   
182   public String JavaDoc toString()
183   {
184     String JavaDoc str = _item.toString();
185
186     if (_isNot)
187       return str + " NOT MEMBER OF " + _collection;
188     else
189       return str + " MEMBER OF " + _collection;
190   }
191 }
192
Popular Tags