KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

46   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 getId()
58   {
59     return _fromItem.getName();
60   }
61
62   /**
63    * Returns the from item
64    */

65   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     cb.append(_fromItem.getName());
132     cb.append('.');
133     cb.append(getTargetType().getId().getColumns().get(0).getName());
134   }
135
136   public String toString()
137   {
138     return getId();
139   }
140
141   public int hashCode()
142   {
143     return _fromItem.hashCode();
144   }
145
146   public boolean equals(Object o)
147   {
148     if (o == null || ! getClass().equals(o.getClass()))
149       return false;
150
151     IdExpr id = (IdExpr) o;
152
153     return _fromItem.equals(id._fromItem);
154   }
155 }
156
Popular Tags