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.field.EntityEmbeddedField;
032: import com.caucho.amber.field.EmbeddedSubField;
033: import com.caucho.amber.query.FromItem;
034: import com.caucho.amber.query.QueryParser;
035: import com.caucho.amber.table.Column;
036: import com.caucho.amber.type.EmbeddableType;
037: import com.caucho.amber.type.Type;
038: import com.caucho.util.CharBuffer;
039:
040: import java.util.ArrayList;
041: import java.util.Map;
042:
043: /**
044: * Embedded path expression
045: */
046: public class EmbeddedExpr extends AbstractPathExpr {
047: private PathExpr _parent;
048:
049: private EmbeddableType _embeddableType;
050:
051: private ArrayList<EmbeddedSubField> _subFields;
052:
053: private FromItem _fromItem;
054: private FromItem _childFromItem;
055:
056: /**
057: * Creates a new expression.
058: */
059: public EmbeddedExpr(PathExpr parent, EmbeddableType embeddableType,
060: ArrayList<EmbeddedSubField> subFields) {
061: _parent = parent;
062: _embeddableType = embeddableType;
063: _subFields = subFields;
064: }
065:
066: /**
067: * Returns the target type.
068: */
069: public EmbeddableType getTargetType() {
070: return _embeddableType;
071: }
072:
073: /**
074: * Returns the target type.
075: */
076: public Type getType() {
077: return _embeddableType;
078: }
079:
080: /**
081: * Returns column by name.
082: */
083: public Column getColumnByFieldName(String fieldName) {
084: for (int i = 0; i < _subFields.size(); i++) {
085: EmbeddedSubField subField = _subFields.get(i);
086:
087: if (subField.getName().equals(fieldName))
088: return subField.getColumn();
089: }
090:
091: return null;
092: }
093:
094: /**
095: * Binds the expression as a select item.
096: */
097: public AmberExpr bindSelect(QueryParser parser) {
098: _fromItem = _parent.bindSubPath(parser);
099:
100: return this ;
101: }
102:
103: /**
104: * Return the parent from item.
105: */
106: public FromItem getFromItem() {
107: return _fromItem;
108: }
109:
110: /**
111: * Return the child from item.
112: */
113: public FromItem getChildFromItem() {
114: return _childFromItem;
115: }
116:
117: /**
118: * Binds the expression as a subpath.
119: */
120: public FromItem bindSubPath(QueryParser parser) {
121: // if (_childFromItem != null)
122: // return _childFromItem;
123:
124: FromItem parentFromItem = _parent.bindSubPath(parser);
125:
126: _fromItem = parentFromItem;
127:
128: return _fromItem;
129:
130: /*
131: EmbeddedExpr pathExpr = (EmbeddedExpr) parser.addPath(this);
132:
133: if (pathExpr != this) {
134: _fromItem = pathExpr._fromItem;
135: _childFromItem = pathExpr._childFromItem;
136:
137: return _childFromItem;
138: }
139:
140: // XXX: handled at constructor?
141: _parent = _parent.bindSelect(parser, null);
142:
143: bindSelect(parser, parser.createTableName());
144:
145: return _childFromItem;
146: */
147: }
148:
149: /**
150: * Binds the expression as a select item.
151: */
152: public PathExpr bindSelect(QueryParser parser, String id) {
153: _fromItem = bindSubPath(parser);
154:
155: return this ;
156:
157: /*
158: if (_childFromItem != null)
159: return this;
160:
161: if (_fromItem == null)
162: _fromItem = _parent.bindSubPath(parser);
163:
164: return this;
165: */
166: }
167:
168: /**
169: * Returns true if the expression uses the from item.
170: */
171: public boolean usesFrom(FromItem from, int type, boolean isNot) {
172: return (_childFromItem == from && type == IS_INNER_JOIN
173: || _fromItem == from || _parent.usesFrom(from, type));
174: }
175:
176: /**
177: * Generates the where expression.
178: */
179: public void generateMatchArgWhere(CharBuffer cb) {
180: throw new UnsupportedOperationException();
181:
182: /*
183: if (_fromItem != null) {
184: cb.append(_linkColumns.generateMatchArgSQL(_fromItem.getName()));
185: }
186: else {
187: cb.append(_linkColumns.generateMatchArgSQL(_parent.getChildFromItem().getName()));
188: }
189: */
190: }
191:
192: /**
193: * Generates the where expression.
194: */
195: public void generateWhere(CharBuffer cb) {
196: generateInternalWhere(cb, true);
197: }
198:
199: /**
200: * Generates the (update) where expression.
201: */
202: public void generateUpdateWhere(CharBuffer cb) {
203: generateInternalWhere(cb, false);
204: }
205:
206: /**
207: * Generates the select expression.
208: */
209: public void generateSelect(CharBuffer cb) {
210: for (int i = 0; i < _subFields.size(); i++) {
211: if (i > 0)
212: cb.append(", ");
213:
214: cb.append(_subFields.get(i).generateSelect(null));
215: }
216: }
217:
218: /**
219: * Returns the parent.
220: */
221: public PathExpr getParent() {
222: return _parent;
223: }
224:
225: public int hashCode() {
226: return 65521 * _parent.hashCode() + _subFields.hashCode();
227: }
228:
229: public boolean equals(Object o) {
230: if (o == null || !getClass().equals(o.getClass()))
231: return false;
232:
233: EmbeddedExpr embedded = (EmbeddedExpr) o;
234:
235: return (_parent.equals(embedded._parent) && _subFields
236: .equals(embedded._subFields));
237: }
238:
239: public String toString() {
240: return "EmbeddedExpr[" + _childFromItem + "," + _fromItem + ","
241: + _parent + "]";
242: }
243:
244: //
245: // private
246:
247: private void generateInternalWhere(CharBuffer cb, boolean select) {
248: throw new UnsupportedOperationException();
249: }
250: }
|