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.query.FromItem;
032: import com.caucho.amber.query.QueryParser;
033: import com.caucho.amber.table.Table;
034: import com.caucho.amber.type.EntityType;
035: import com.caucho.util.CharBuffer;
036:
037: /**
038: * Bound identifier expression.
039: */
040: public class IdExpr extends AbstractPathExpr {
041: private FromItem _fromItem;
042:
043: /**
044: * Creates a new unbound id expression.
045: */
046: public IdExpr(FromItem fromItem) {
047: _fromItem = fromItem;
048:
049: //if (fromItem.getEntityType() == null)
050: // throw new NullPointerException();
051: }
052:
053: /**
054: * Returns the name.
055: */
056: String getId() {
057: return _fromItem.getName();
058: }
059:
060: /**
061: * Returns the from item
062: */
063: public FromItem getFromItem() {
064: return _fromItem;
065: }
066:
067: /**
068: * Returns the table
069: */
070: Table getTable() {
071: return _fromItem.getTable();
072: }
073:
074: /**
075: * Returns the from item
076: */
077: public FromItem getChildFromItem() {
078: return getFromItem();
079: }
080:
081: /**
082: * Returns the entity class.
083: */
084: public EntityType getTargetType() {
085: return _fromItem.getEntityType();
086: }
087:
088: /**
089: * Binds the expression as a select item.
090: */
091: public AmberExpr bindSelect(QueryParser parser) {
092: return this ;
093: }
094:
095: /**
096: * Binds the expression as a select item.
097: */
098: public FromItem bindSubPath(QueryParser parser) {
099: return _fromItem;
100: }
101:
102: /**
103: * Returns true if the expression uses the from item.
104: */
105: public boolean usesFrom(FromItem from, int type, boolean isNot) {
106: return (type == IS_INNER_JOIN && _fromItem == from);
107: }
108:
109: /**
110: * Returns true if the expression uses the from item.
111: */
112: public AmberExpr replaceJoin(JoinExpr join) {
113: return join.replace(this );
114: }
115:
116: /**
117: * Generates the where expression.
118: */
119: public void generateWhere(CharBuffer cb) {
120: generateInternalWhere(cb, true);
121: }
122:
123: /**
124: * Generates the (update) where expression.
125: */
126: public void generateUpdateWhere(CharBuffer cb) {
127: generateInternalWhere(cb, false);
128: }
129:
130: /**
131: * Generates the having expression.
132: */
133: public void generateHaving(CharBuffer cb) {
134: generateWhere(cb);
135: }
136:
137: public String toString() {
138: return getId();
139: }
140:
141: public int hashCode() {
142: return _fromItem.hashCode();
143: }
144:
145: public boolean equals(Object o) {
146: if (o == null || !getClass().equals(o.getClass()))
147: return false;
148:
149: IdExpr id = (IdExpr) o;
150:
151: return _fromItem.equals(id._fromItem);
152: }
153:
154: //
155: // private
156:
157: private void generateInternalWhere(CharBuffer cb, boolean select) {
158: if (select) {
159: cb.append(_fromItem.getName());
160: cb.append('.');
161: }
162:
163: cb
164: .append(getTargetType().getId().getColumns().get(0)
165: .getName());
166: }
167: }
|