KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > 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 Software Foundation, 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.expr;
30
31 import com.caucho.amber.query.FromItem;
32 import com.caucho.amber.query.QueryParser;
33 import com.caucho.amber.table.LinkColumns;
34 import com.caucho.amber.table.Table;
35 import com.caucho.util.CharBuffer;
36
37 /**
38  * Represents a member query expression
39  */

40 public class EmptyExpr extends AbstractAmberExpr {
41   private AmberExpr _collectionExpr;
42
43   private String JavaDoc _tableName;
44
45   public EmptyExpr(AmberExpr collectionExpr)
46   {
47     _collectionExpr = collectionExpr;
48   }
49
50   /**
51    * Returns true as a boolean expression.
52    */

53   public boolean isBoolean()
54   {
55     return true;
56   }
57
58   /**
59    * Binds the expression as a select item.
60    */

61   public AmberExpr bindSelect(QueryParser parser)
62   {
63     _tableName = parser.createTableName();
64
65     return this;
66   }
67
68   /**
69    * Returns true if the expression uses the from item.
70    */

71   public boolean usesFrom(FromItem from, int type, boolean isNot)
72   {
73     return (_collectionExpr.usesFrom(from, type));
74   }
75
76   /**
77    * Returns true if the expression uses the from item.
78    */

79   public AmberExpr replaceJoin(JoinExpr join)
80   {
81     _collectionExpr = _collectionExpr.replaceJoin(join);
82
83     return this;
84   }
85
86   /**
87    * Generates the where expression.
88    */

89   public void generateWhere(CharBuffer cb)
90   {
91     generateInternalWhere(cb, true);
92   }
93
94   /**
95    * Generates the (update) where expression.
96    */

97   public void generateUpdateWhere(CharBuffer cb)
98   {
99     generateInternalWhere(cb, false);
100   }
101
102   /**
103    * Generates the having expression.
104    */

105   public void generateHaving(CharBuffer cb)
106   {
107     generateWhere(cb);
108   }
109
110   //
111
// private
112

113   private void generateInternalWhere(CharBuffer cb,
114                                      boolean select)
115   {
116     OneToManyExpr oneToMany = null;
117
118     // ManyToMany is implemented as a
119
// ManyToOne[embeddeding OneToMany]
120
if (_collectionExpr instanceof ManyToOneExpr) {
121       PathExpr expr = ((ManyToOneExpr) _collectionExpr).getParent();
122       if (expr instanceof OneToManyExpr)
123         oneToMany = (OneToManyExpr) expr;
124
125     } else if (_collectionExpr instanceof OneToManyExpr) {
126       oneToMany = (OneToManyExpr) _collectionExpr;
127     }
128     else
129       throw new UnsupportedOperationException JavaDoc();
130
131     LinkColumns join = oneToMany.getLinkColumns();
132
133     Table table = join.getSourceTable();
134     cb.append("EXISTS(SELECT ");
135
136     if (table.getIdColumns().size() > 0)
137       cb.append(table.getIdColumns().get(0).getName());
138     else
139       cb.append('*');
140
141     cb.append(" FROM " + table.getName() + " " + _tableName);
142     cb.append(" WHERE ");
143
144     String JavaDoc targetTable = oneToMany.getParent().getChildFromItem().getName();
145
146     cb.append(join.generateJoin(_tableName, targetTable));
147
148     cb.append(')');
149   }
150 }
151
Popular Tags