KickJava   Java API By Example, From Geeks To Geeks.

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


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 EmptyExpr extends AbstractAmberExpr {
40   private AmberExpr _collectionExpr;
41
42   private String _tableName;
43
44   EmptyExpr(AmberExpr collectionExpr)
45   {
46     _collectionExpr = collectionExpr;
47   }
48
49   /**
50    * Binds the expression as a select item.
51    */

52   public AmberExpr bindSelect(QueryParser parser)
53   {
54     _tableName = parser.createTableName();
55     
56     return this;
57   }
58
59   /**
60    * Returns true if the expression uses the from item.
61    */

62   public boolean usesFrom(FromItem from, int type, boolean isNot)
63   {
64     return (_collectionExpr.usesFrom(from, type));
65   }
66
67   /**
68    * Returns true if the expression uses the from item.
69    */

70   public AmberExpr replaceJoin(JoinExpr join)
71   {
72     _collectionExpr = _collectionExpr.replaceJoin(join);
73     
74     return this;
75   }
76   
77   /**
78    * Generates the where expression.
79    */

80   public void generateWhere(CharBuffer cb)
81   {
82     if (_collectionExpr instanceof OneToManyExpr) {
83       OneToManyExpr oneToMany = (OneToManyExpr) _collectionExpr;
84
85       LinkColumns join = oneToMany.getLinkColumns();
86       
87       cb.append("EXISTS(SELECT *");
88       Table table = join.getSourceTable();
89       cb.append(" FROM " + table.getName() + " " + _tableName);
90       cb.append(" WHERE ");
91
92       String targetTable = oneToMany.getParent().getChildFromItem().getName();
93
94       cb.append(join.generateJoin(_tableName, targetTable));
95                   
96       cb.append(')');
97     }
98     else
99       throw new UnsupportedOperationException();
100   }
101 }
102
Popular Tags