KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > query > MemberExpr


1 /*
2  * Copyright (c) 1998-2004 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.amber.query;
30
31 import com.caucho.util.CharBuffer;
32
33 import com.caucho.amber.table.Table;
34 import com.caucho.amber.table.LinkColumns;
35
36 /**
37  * Represents a member query expression
38  */

39 public class MemberExpr extends AbstractAmberExpr {
40   private boolean _isNot;
41   private PathExpr _itemExpr;
42   private AmberExpr _collectionExpr;
43
44   private MemberExpr(PathExpr itemExpr,
45              AmberExpr collectionExpr, boolean isNot)
46   {
47     _itemExpr = itemExpr;
48     _collectionExpr = collectionExpr;
49   }
50
51   static AmberExpr create(QueryParser parser,
52               PathExpr itemExpr,
53               AmberExpr collectionExpr,
54               boolean isNot)
55   {
56     if (collectionExpr instanceof IdExpr)
57       collectionExpr = ((CollectionIdExpr) collectionExpr).getPath();
58     
59     if (collectionExpr instanceof OneToManyExpr) {
60       OneToManyExpr oneToMany = (OneToManyExpr) collectionExpr;
61       PathExpr parent = oneToMany.getParent();
62
63       AmberExpr expr;
64       expr = new ManyToOneJoinExpr(oneToMany.getLinkColumns(),
65                    itemExpr.getChildFromItem(),
66                    parent.getChildFromItem());
67
68       if (isNot)
69     return new UnaryExpr(QueryParser.NOT, expr);
70       else
71     return expr;
72     }
73     else
74       return new MemberExpr(itemExpr, collectionExpr, isNot);
75   }
76
77   /**
78    * Binds the expression as a select item.
79    */

80   public AmberExpr bindSelect(QueryParser parser)
81   {
82     return this;
83   }
84
85   /**
86    * Returns true if the expression uses the from item.
87    */

88   public boolean usesFrom(FromItem from, int type, boolean isNot)
89   {
90     return (_collectionExpr.usesFrom(from, type) ||
91         _itemExpr.usesFrom(from, type));
92   }
93
94   /**
95    * Returns true if the expression uses the from item.
96    */

97   public AmberExpr replaceJoin(JoinExpr join)
98   {
99     _collectionExpr = _collectionExpr.replaceJoin(join);
100     _itemExpr = (PathExpr) _itemExpr.replaceJoin(join);
101     
102     return this;
103   }
104   
105   /**
106    * Generates the where expression.
107    */

108   public void generateWhere(CharBuffer cb)
109   {
110     if (_collectionExpr instanceof OneToManyExpr) {
111       OneToManyExpr oneToMany = (OneToManyExpr) _collectionExpr;
112
113       LinkColumns join = oneToMany.getLinkColumns();
114       
115       cb.append("EXISTS(SELECT *");
116       Table table = join.getSourceTable();
117       cb.append(" FROM " + table.getName() + " caucho");
118       cb.append(')');
119     }
120     else
121       throw new UnsupportedOperationException();
122   }
123 }
124
Popular Tags