KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > IdExpr


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.Table;
34 import com.caucho.amber.type.EntityType;
35 import com.caucho.util.CharBuffer;
36
37 /**
38  * Bound identifier expression.
39  */

40 public class IdExpr extends AbstractPathExpr {
41   private FromItem _fromItem;
42
43   /**
44    * Creates a new unbound id expression.
45    */

46   public IdExpr(FromItem fromItem)
47   {
48     _fromItem = fromItem;
49
50     //if (fromItem.getEntityType() == null)
51
// throw new NullPointerException();
52
}
53
54   /**
55    * Returns the name.
56    */

57   String JavaDoc getId()
58   {
59     return _fromItem.getName();
60   }
61
62   /**
63    * Returns the from item
64    */

65   public FromItem getFromItem()
66   {
67     return _fromItem;
68   }
69
70   /**
71    * Returns the table
72    */

73   Table getTable()
74   {
75     return _fromItem.getTable();
76   }
77
78   /**
79    * Returns the from item
80    */

81   public FromItem getChildFromItem()
82   {
83     return getFromItem();
84   }
85
86   /**
87    * Returns the entity class.
88    */

89   public EntityType getTargetType()
90   {
91     return _fromItem.getEntityType();
92   }
93
94   /**
95    * Binds the expression as a select item.
96    */

97   public AmberExpr bindSelect(QueryParser parser)
98   {
99     return this;
100   }
101
102   /**
103    * Binds the expression as a select item.
104    */

105   public FromItem bindSubPath(QueryParser parser)
106   {
107     return _fromItem;
108   }
109
110   /**
111    * Returns true if the expression uses the from item.
112    */

113   public boolean usesFrom(FromItem from, int type, boolean isNot)
114   {
115     return (type == IS_INNER_JOIN && _fromItem == from);
116   }
117
118   /**
119    * Returns true if the expression uses the from item.
120    */

121   public AmberExpr replaceJoin(JoinExpr join)
122   {
123     return join.replace(this);
124   }
125
126   /**
127    * Generates the where expression.
128    */

129   public void generateWhere(CharBuffer cb)
130   {
131     generateInternalWhere(cb, true);
132   }
133
134   /**
135    * Generates the (update) where expression.
136    */

137   public void generateUpdateWhere(CharBuffer cb)
138   {
139     generateInternalWhere(cb, false);
140   }
141
142   /**
143    * Generates the having expression.
144    */

145   public void generateHaving(CharBuffer cb)
146   {
147     generateWhere(cb);
148   }
149
150   public String JavaDoc toString()
151   {
152     return getId();
153   }
154
155   public int hashCode()
156   {
157     return _fromItem.hashCode();
158   }
159
160   public boolean equals(Object JavaDoc o)
161   {
162     if (o == null || ! getClass().equals(o.getClass()))
163       return false;
164
165     IdExpr id = (IdExpr) o;
166
167     return _fromItem.equals(id._fromItem);
168   }
169
170   //
171
// private
172

173   private void generateInternalWhere(CharBuffer cb,
174                                      boolean select)
175   {
176     if (select) {
177       cb.append(_fromItem.getName());
178       cb.append('.');
179     }
180
181     cb.append(getTargetType().getId().getColumns().get(0).getName());
182   }
183 }
184
Popular Tags