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.KeyPropertyField;
032: import com.caucho.amber.query.FromItem;
033: import com.caucho.amber.query.QueryParser;
034: import com.caucho.amber.table.Column;
035: import com.caucho.amber.type.EntityType;
036: import com.caucho.amber.type.Type;
037:
038: /**
039: * Bound identifier expression.
040: */
041: public class KeyPropertyExpr extends AbstractAmberExpr implements
042: IdFieldExpr {
043: protected PathExpr _parent;
044: private KeyPropertyField _field;
045:
046: /**
047: * Creates a new unbound id expression.
048: */
049: public KeyPropertyExpr(PathExpr parent, KeyPropertyField field) {
050: _parent = parent;
051: _field = field;
052:
053: // XXX: ejb/0a08
054: if (_field.getType() instanceof EntityType)
055: throw new IllegalStateException();
056: }
057:
058: /**
059: * Binds the expression as a select item.
060: */
061: public AmberExpr bindSelect(QueryParser parser) {
062: _parent = (PathExpr) _parent.bindSelect(parser);
063:
064: return this ;
065: }
066:
067: /**
068: * Returns the parent expression.
069: */
070: public PathExpr getParent() {
071: return _parent;
072: }
073:
074: /**
075: * Returns the parent expression.
076: */
077: public KeyPropertyField getField() {
078: return _field;
079: }
080:
081: /**
082: * Returns the expr type
083: */
084: public Type getType() {
085: return getField().getType();
086: }
087:
088: /**
089: * Returns the parent expression.
090: */
091: public Column getColumn() {
092: return getField().getColumn();
093: }
094:
095: /**
096: * Creates the expr from the path.
097: */
098: /*
099: public PathExpr createField(QueryParser parser, String name)
100: {
101: Type type = getType();
102:
103: if (! (type instanceof EntityType))
104: return null;
105:
106: EntityType entityType = (EntityType) type;
107:
108: AbstractField field = entityType.getField(name);
109:
110: if (field == null)
111: return null;
112: else {
113: EntityPathExpr dst = (EntityPathExpr) getField().createExpr(parser,
114: new KeyManyToOneExpr(this));
115: PathExpr result = field.createExpr(parser, (EntityPathExprdst);
116:
117: return result;
118: }
119: }
120: */
121:
122: /**
123: * Returns true if the expression uses the from item.
124: */
125: public boolean usesFrom(FromItem from, int type, boolean isNot) {
126: if (_parent instanceof IdExpr) {
127: // IdExpr parent = (IdExpr) _parent;
128: // return (parent.getFromItem() == from && type == USES_ANY);
129:
130: return type == IS_INNER_JOIN
131: && _parent.usesFrom(from, type);
132: } else
133: return _parent.usesFrom(from, type);
134: }
135:
136: /**
137: * Replaces linked join to eliminate a table.
138: */
139: /*
140: public AmberExpr replaceJoin(JoinExpr join)
141: {
142: // _parent = (EntityPathExpr) _parent.replaceJoin(join);
143:
144: if (_parent instanceof IdExpr)
145: return join.replace(this);
146: else
147: return super.replaceJoin(join);
148: }
149: */
150:
151: /**
152: * Returns the child from item.
153: *
154: * XXX: untested
155: */
156: public FromItem getChildFromItem() {
157: return _parent.getChildFromItem();
158: }
159:
160: /**
161: * Returns the field string.
162: */
163: public String toString() {
164: return "KeyPropertyExpr[" + _parent + "," + _field + "]";
165: }
166: }
|