KickJava   Java API By Example, From Geeks To Geeks.

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


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.Type;
34 import com.caucho.amber.type.EntityType;
35
36 import com.caucho.amber.table.Table;
37 import com.caucho.amber.table.LinkColumns;
38
39 /**
40  * Expression to a collection of rows
41  *
42  * The relation is maintained by a link from the child objects
43  * to the parent object.
44  */

45 public class OneToManyExpr extends AbstractPathExpr {
46   private PathExpr _parent;
47   
48   // Link from the target to the parent
49
private LinkColumns _linkColumns;
50
51   private FromItem _fromItem;
52   private FromItem _childFromItem;
53
54   /**
55    * Creates a new expression to the child objects.
56    */

57   public OneToManyExpr(QueryParser parser,
58                PathExpr parent,
59                LinkColumns linkColumns)
60   {
61     _parent = parent;
62     
63     _linkColumns = linkColumns;
64   }
65
66   /**
67    * Returns the link columns.
68    */

69   public LinkColumns getLinkColumns()
70   {
71     return _linkColumns;
72   }
73
74   /**
75    * Returns the expr type.
76    */

77   public Type getType()
78   {
79     return _linkColumns.getSourceTable().getType();
80   }
81
82   /**
83    * Returns the expr type.
84    */

85   public EntityType getTargetType()
86   {
87     return (EntityType) getType();
88   }
89
90   /**
91    * Returns true if the expression uses the from item.
92    */

93   public boolean usesFrom(FromItem from, int type, boolean isNot)
94   {
95     return (from == _childFromItem ||
96         type == IS_INNER_JOIN && _parent.usesFrom(from, type));
97   }
98
99   /**
100    * Returns the parent.
101    */

102   PathExpr getParent()
103   {
104     return _parent;
105   }
106
107   /**
108    * Binds the expression as a select item.
109    */

110   public AmberExpr bindSelect(QueryParser parser)
111   {
112     return bindSelect(parser, null);
113   }
114
115   /**
116    * Binds the expression as a select item.
117    */

118   public PathExpr bindSelect(QueryParser parser, String id)
119   {
120     if (_fromItem != null)
121       return this;
122       
123     _fromItem = _parent.bindSubPath(parser);
124
125     Table sourceTable = _linkColumns.getSourceTable();
126     _childFromItem = parser.addFromItem(sourceTable, id);
127
128     JoinExpr joinExpr;
129     joinExpr = new OneToManyJoinExpr(_linkColumns,
130                      _childFromItem,
131                      _fromItem);
132                            
133     _childFromItem.setJoinExpr(joinExpr);
134     _childFromItem.setCollectionExpr(this);
135     
136     return this;
137   }
138
139   /**
140    * Returns the child from item.
141    */

142   public FromItem getChildFromItem()
143   {
144     return _childFromItem;
145   }
146
147   /**
148    * Binds the expression as a subpath.
149    */

150   public FromItem bindSubPath(QueryParser parser)
151   {
152     if (_childFromItem == null)
153       bindSelect(parser, null);
154     
155     return _childFromItem;
156   }
157
158   /**
159    * Returns the table.
160    */

161   public Table getTable()
162   {
163     // return _field.getTable();
164
return _fromItem.getTable();
165   }
166   
167   /**
168    * Generates the where expression.
169    */

170   public void generateWhere(CharBuffer cb)
171   {
172     throw new IllegalStateException(getClass().getName());
173   }
174   
175   /**
176    * Generates the where expression.
177    */

178   public void generateSelect(CharBuffer cb)
179   {
180     String id = _childFromItem.getName();
181
182     cb.append(_linkColumns.generateSelectSQL(id));
183   }
184
185   public String toString()
186   {
187     return "OneToManyExpr[" + _parent + "," + _linkColumns + "]";
188   }
189 }
190
Popular Tags