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.MapElementField;
032: import com.caucho.amber.query.FromItem;
033: import com.caucho.amber.query.QueryParser;
034: import com.caucho.amber.type.EntityType;
035: import com.caucho.amber.type.Type;
036: import com.caucho.util.CharBuffer;
037:
038: /**
039: * Bound identifier expression.
040: */
041: public class MapFieldExpr extends AbstractAmberExpr {
042: protected PathExpr _parent;
043:
044: private MapElementField _field;
045: private AmberExpr _index;
046:
047: private FromItem _fromItem;
048: private FromItem _childFromItem;
049:
050: /**
051: * Creates a new unbound id expression.
052: */
053: public MapFieldExpr(PathExpr parent, MapElementField field,
054: AmberExpr index) {
055: _parent = parent;
056: _field = field;
057: _index = index;
058: }
059:
060: /**
061: * Binds the expression as a select item.
062: */
063: public AmberExpr bindSelect(QueryParser parser) {
064: _fromItem = _parent.bindSubPath(parser);
065:
066: // _childFromItem = parser.addFromItem(null, null, _field.getTable());
067:
068: // _childFromItem.setJoinExpr(new MapLinkExpr());
069:
070: _index = _index.bindSelect(parser);
071:
072: return this ;
073: }
074:
075: /**
076: * Returns true if the expression uses the from item.
077: */
078: public boolean usesFrom(FromItem from, int type, boolean isNot) {
079: return (_childFromItem == from || _parent.usesFrom(from, type) || _index
080: .usesFrom(from, type));
081: }
082:
083: /**
084: * Returns the expr's type.
085: */
086: public Type getType() {
087: return _field.getTargetType();
088: }
089:
090: /**
091: * Returns the expr's type.
092: */
093: public EntityType getTableType() {
094: return (EntityType) getType();
095: }
096:
097: /**
098: * Generates the select expression.
099: */
100: public void generateSelect(CharBuffer cb) {
101: String table = _childFromItem.getName();
102:
103: /*
104: if (_fromItem != null)
105: table = _fromItem.getName();
106: else
107: table = _parent.getTable();
108: */
109:
110: cb.append(_field.generateTargetSelect(table));
111: }
112:
113: /**
114: * Generates the where expression.
115: */
116: public void generateWhere(CharBuffer cb) {
117: cb
118: .append(_field.generateTargetSelect(_childFromItem
119: .getName()));
120: /*
121: cb.append(_childFromItem.getName());
122: ArrayList<Column> column = _field.getColumns();
123:
124: cb.append('.');
125: cb.append(column.get(0).getName());
126: */
127: }
128:
129: /**
130: * Generates the (update) where expression.
131: */
132: public void generateUpdateWhere(CharBuffer cb) {
133: generateWhere(cb);
134: }
135:
136: /**
137: * Generates the having expression.
138: */
139: public void generateHaving(CharBuffer cb) {
140: generateWhere(cb);
141: }
142:
143: public String toString() {
144: return _parent + "." + _field;
145: }
146: }
|