KickJava   Java API By Example, From Geeks To Geeks.

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


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.entity.Entity;
32 import com.caucho.amber.entity.EntityItem;
33 import com.caucho.amber.field.AmberField;
34 import com.caucho.amber.manager.AmberConnection;
35 import com.caucho.amber.query.FromItem;
36 import com.caucho.amber.query.QueryParser;
37 import com.caucho.amber.table.LinkColumns;
38 import com.caucho.amber.table.Table;
39 import com.caucho.amber.type.AbstractStatefulType;
40 import com.caucho.amber.type.EmbeddableType;
41 import com.caucho.amber.type.EntityType;
42 import com.caucho.amber.type.RelatedType;
43 import com.caucho.amber.type.Type;
44 import com.caucho.util.CharBuffer;
45
46 import java.lang.reflect.Method JavaDoc;
47 import java.sql.ResultSet JavaDoc;
48 import java.sql.SQLException JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.HashSet JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.Map JavaDoc;
53
54 /**
55  * An embedded or entity expression which should be loaded.
56  */

57 abstract public class LoadExpr extends AbstractAmberExpr {
58   PathExpr _expr;
59   FromItem _fromItem;
60   int _index;
61
62   ArrayList JavaDoc<FromItem> _subItems = new ArrayList JavaDoc<FromItem>();
63
64   public static LoadExpr create(PathExpr expr)
65   {
66     if (expr instanceof EmbeddedExpr)
67       return new LoadEmbeddedExpr(expr);
68
69     return new LoadEntityExpr(expr);
70   }
71
72   LoadExpr(PathExpr expr)
73   {
74     _expr = expr;
75   }
76
77   /**
78    * Returns the type.
79    */

80   public Type getType()
81   {
82     return _expr.getTargetType();
83   }
84
85   /**
86    * Returns the underlying expression
87    */

88   public PathExpr getExpr()
89   {
90     return _expr;
91   }
92
93   /**
94    * Returns the number of columns consumed from
95    * a result set after loading the entity.
96    */

97   public int getIndex()
98   {
99     return _index;
100   }
101
102   /**
103    * Returns the table.
104    */

105   public String JavaDoc getTable()
106   {
107     return _fromItem.getName();
108   }
109
110   /**
111    * Binds the expression as a select item.
112    */

113   public FromItem bindSubPath(QueryParser parser)
114   {
115     throw new UnsupportedOperationException JavaDoc();
116   }
117
118   /**
119    * Returns true if the expression uses the from item.
120    */

121   public boolean usesFrom(FromItem from, int type, boolean isNot)
122   {
123     if (_fromItem == from)
124       return true;
125
126     for (int i = 0; i < _subItems.size(); i++) {
127       FromItem subItem = _subItems.get(i);
128
129       if (from == subItem)
130         return true;
131     }
132
133     return _expr.usesFrom(from, type, isNot);
134   }
135
136   /**
137    * Returns the from item
138    */

139   public FromItem getChildFromItem()
140   {
141     return _expr.getChildFromItem();
142   }
143
144   /**
145    * Generates the where expression.
146    */

147   public void generateSelect(CharBuffer cb)
148   {
149     generateSelect(cb, true);
150   }
151
152   /**
153    * Generates the where expression.
154    */

155   public void generateSelect(CharBuffer cb,
156                              boolean fullSelect)
157   {
158     AbstractStatefulType type = (AbstractStatefulType) getType();
159
160     if (type instanceof EmbeddableType) {
161       _expr.generateSelect(cb);
162       return;
163     }
164
165     if (type instanceof RelatedType) {
166       RelatedType relatedType = (RelatedType) type;
167       cb.append(relatedType.getId().generateSelect(getTable()));
168     }
169
170     if (! fullSelect)
171       return;
172
173     String JavaDoc valueSelect = type.generateLoadSelect(_fromItem.getTable(),
174                                                  _fromItem.getName());
175
176     if (valueSelect != null && ! "".equals(valueSelect)) {
177       cb.append(", ");
178       cb.append(valueSelect);
179     }
180
181     for (int i = 0; i < _subItems.size(); i++) {
182       FromItem item = _subItems.get(i);
183
184       valueSelect = type.generateLoadSelect(item.getTable(), item.getName());
185
186       if (! valueSelect.equals("")) {
187         cb.append(", ");
188         cb.append(valueSelect);
189       }
190     }
191   }
192
193   /**
194    * Generates the where expression.
195    */

196   public void generateWhere(CharBuffer cb, String JavaDoc fieldName)
197   {
198     throw new UnsupportedOperationException JavaDoc();
199   }
200
201   /**
202    * Generates the (update) where expression.
203    */

204   public void generateUpdateWhere(CharBuffer cb, String JavaDoc fieldName)
205   {
206     generateWhere(cb, fieldName);
207   }
208
209   /**
210    * Generates the having expression.
211    */

212   public void generateHaving(CharBuffer cb, String JavaDoc fieldName)
213   {
214     generateWhere(cb, fieldName);
215   }
216 }
217
Popular Tags