KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkColumns;
36
37 /**
38  * Bound identifier expression.
39  */

40 public class DependentEntityOneToOneExpr extends AbstractPathExpr {
41   private PathExpr _parent;
42
43   private LinkColumns _linkColumns;
44
45   private FromItem _fromItem;
46   private FromItem _childFromItem;
47
48   /**
49    * Creates a new unbound id expression.
50    */

51   public DependentEntityOneToOneExpr(PathExpr parent,
52                      LinkColumns linkColumns)
53   {
54     _parent = parent;
55     _linkColumns = linkColumns;
56   }
57
58   /**
59    * Returns the entity class.
60    */

61   public EntityType getTargetType()
62   {
63     return _linkColumns.getSourceTable().getType();
64   }
65
66   /**
67    * Binds the expression as a select item.
68    */

69   public AmberExpr bindSelect(QueryParser parser)
70   {
71     if (_fromItem == null)
72       _fromItem = _parent.bindSubPath(parser);
73
74     return this;
75   }
76
77   /**
78    * Return the child from item.
79    */

80   public FromItem getChildFromItem()
81   {
82     return _childFromItem;
83   }
84
85   /**
86    * Binds the expression as a subpath.
87    */

88   public FromItem bindSubPath(QueryParser parser)
89   {
90     if (_childFromItem != null)
91       return _childFromItem;
92
93     DependentEntityOneToOneExpr pathExpr;
94     pathExpr = (DependentEntityOneToOneExpr) parser.addPath(this);
95
96     if (pathExpr != this) {
97       _fromItem = pathExpr._fromItem;
98       _childFromItem = pathExpr._childFromItem;
99
100       return _childFromItem;
101     }
102     
103     _fromItem = _parent.bindSubPath(parser);
104
105     if (_fromItem != null)
106       _childFromItem = _fromItem.getQuery().createFromItem(_linkColumns.getSourceTable(),
107                                parser.createTableName());
108     else
109       _childFromItem = parser.getSelectQuery().createFromItem(_linkColumns.getSourceTable(),
110                                   parser.createTableName());
111
112     JoinExpr join = new ManyToOneJoinExpr(_linkColumns,
113                       _childFromItem,
114                       _fromItem);
115
116     _childFromItem.setJoinExpr(join);
117     
118     return _childFromItem;
119   }
120
121   /**
122    * Returns true if the expression uses the from item.
123    */

124   public boolean usesFrom(FromItem from, int type, boolean isNot)
125   {
126     return (_childFromItem == from && type == IS_INNER_JOIN ||
127         _fromItem == from ||
128         _parent.usesFrom(from, type));
129   }
130
131   /**
132    * Returns the table.
133    */

134   /*
135   public Table getTable()
136   {
137     if (_childFromItem != null)
138       return _childFromItem.getTable();
139     else if (_fromItem != null)
140       return _fromItem.getTable();
141     else
142       return _parent.getTable();
143   }
144   */

145   
146   /**
147    * Generates the where expression.
148    */

149   public void generateMatchArgWhere(CharBuffer cb)
150   {
151     if (_fromItem != null) {
152       cb.append(_linkColumns.generateMatchArgSQL(_fromItem.getName()));
153     }
154     else {
155       cb.append(_linkColumns.generateMatchArgSQL(_parent.getChildFromItem().getName()));
156     }
157   }
158
159   public String toString()
160   {
161     return "OneToOneExpr[" + _parent + "," + _linkColumns + "]";
162   }
163
164   public int hashCode()
165   {
166     return 65521 * _parent.hashCode() + _linkColumns.hashCode();
167   }
168
169   public boolean equals(Object o)
170   {
171     if (o == null || ! getClass().equals(o.getClass()))
172       return false;
173
174     DependentEntityOneToOneExpr oneToOne = (DependentEntityOneToOneExpr) o;
175
176     return (_parent.equals(oneToOne._parent) &&
177         _linkColumns.equals(oneToOne._linkColumns));
178   }
179 }
180
Popular Tags