001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.expr;
030:
031: import com.caucho.amber.entity.Entity;
032: import com.caucho.amber.entity.EntityItem;
033: import com.caucho.amber.field.AmberField;
034: import com.caucho.amber.manager.AmberConnection;
035: import com.caucho.amber.query.FromItem;
036: import com.caucho.amber.query.QueryParser;
037: import com.caucho.amber.table.LinkColumns;
038: import com.caucho.amber.table.Table;
039: import com.caucho.amber.type.AbstractStatefulType;
040: import com.caucho.amber.type.EmbeddableType;
041: import com.caucho.amber.type.EntityType;
042: import com.caucho.amber.type.RelatedType;
043: import com.caucho.amber.type.Type;
044: import com.caucho.util.CharBuffer;
045:
046: import java.lang.reflect.Method;
047: import java.sql.ResultSet;
048: import java.sql.SQLException;
049: import java.util.ArrayList;
050: import java.util.HashSet;
051: import java.util.Iterator;
052: import java.util.Map;
053:
054: /**
055: * An embedded or entity expression which should be loaded.
056: */
057: abstract public class LoadExpr extends AbstractAmberExpr {
058: PathExpr _expr;
059: FromItem _fromItem;
060: FromItem _rootItem;
061: int _index;
062:
063: ArrayList<FromItem> _subItems = new ArrayList<FromItem>();
064:
065: public static LoadExpr create(PathExpr expr) {
066: if (expr instanceof EmbeddedExpr)
067: return new LoadEmbeddedExpr(expr);
068:
069: return new LoadEntityExpr(expr);
070: }
071:
072: public static LoadExpr create(PathExpr expr, FromItem rootItem) {
073: LoadExpr loadExpr = create(expr);
074:
075: loadExpr._rootItem = rootItem;
076:
077: return loadExpr;
078: }
079:
080: LoadExpr(PathExpr expr) {
081: _expr = expr;
082: }
083:
084: /**
085: * Returns the type.
086: */
087: public Type getType() {
088: return _expr.getTargetType();
089: }
090:
091: /**
092: * Returns the underlying expression
093: */
094: public PathExpr getExpr() {
095: return _expr;
096: }
097:
098: /**
099: * Returns the number of columns consumed from
100: * a result set after loading the entity.
101: */
102: public int getIndex() {
103: return _index;
104: }
105:
106: /**
107: * Returns the table.
108: */
109: public String getTable() {
110: return _fromItem.getName();
111: }
112:
113: /**
114: * Binds the expression as a select item.
115: */
116: public FromItem bindSubPath(QueryParser parser) {
117: throw new UnsupportedOperationException();
118: }
119:
120: /**
121: * Returns true if the expression uses the from item.
122: */
123: public boolean usesFrom(FromItem from, int type, boolean isNot) {
124: if (_fromItem == from)
125: return true;
126:
127: for (int i = 0; i < _subItems.size(); i++) {
128: FromItem subItem = _subItems.get(i);
129:
130: if (from == subItem)
131: return true;
132: }
133:
134: return _expr.usesFrom(from, type, isNot);
135: }
136:
137: /**
138: * Returns the from item
139: */
140: public FromItem getChildFromItem() {
141: return _expr.getChildFromItem();
142: }
143:
144: /**
145: * Generates the where expression.
146: */
147: public void generateSelect(CharBuffer cb) {
148: generateSelect(cb, true);
149: }
150:
151: /**
152: * Generates the where expression.
153: */
154: public void generateSelect(CharBuffer cb, boolean fullSelect) {
155: AbstractStatefulType type = (AbstractStatefulType) getType();
156:
157: if (type instanceof EmbeddableType) {
158: _expr.generateSelect(cb);
159: return;
160: }
161:
162: if (type instanceof RelatedType) {
163: RelatedType relatedType = (RelatedType) type;
164: cb.append(relatedType.getId().generateSelect(getTable()));
165: }
166:
167: if (!fullSelect)
168: return;
169:
170: FromItem item = _fromItem;
171:
172: // jpa/0l4b
173: if (_rootItem != null) {
174: RelatedType parentType = (RelatedType) type;
175:
176: while (parentType.getParentType() != null) {
177: if (parentType.getParentType() instanceof EntityType)
178: parentType = parentType.getParentType();
179: else
180: break;
181: }
182:
183: item = _rootItem;
184: }
185:
186: String valueSelect = "";
187:
188: // jpa/0l12, jpa/0l47
189: valueSelect = type.generateLoadSelect(item.getTable(), item
190: .getName());
191:
192: if (valueSelect != null && !"".equals(valueSelect)) {
193: cb.append(", ");
194: cb.append(valueSelect);
195: }
196:
197: for (int i = 0; i < _subItems.size(); i++) {
198: item = _subItems.get(i);
199:
200: valueSelect = type.generateLoadSelect(item.getTable(), item
201: .getName());
202:
203: if (!valueSelect.equals("")) {
204: cb.append(", ");
205: cb.append(valueSelect);
206: }
207: }
208: }
209:
210: /**
211: * Generates the where expression.
212: */
213: public void generateWhere(CharBuffer cb, String fieldName) {
214: throw new UnsupportedOperationException();
215: }
216:
217: /**
218: * Generates the (update) where expression.
219: */
220: public void generateUpdateWhere(CharBuffer cb, String fieldName) {
221: generateWhere(cb, fieldName);
222: }
223:
224: /**
225: * Generates the having expression.
226: */
227: public void generateHaving(CharBuffer cb, String fieldName) {
228: generateWhere(cb, fieldName);
229: }
230: }
|