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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.amber.expr;
031:
032: import com.caucho.amber.entity.EntityItem;
033: import com.caucho.amber.manager.AmberConnection;
034: import com.caucho.amber.query.FromItem;
035: import com.caucho.amber.query.QueryParseException;
036: import com.caucho.amber.query.QueryParser;
037: import com.caucho.amber.type.StringType;
038: import com.caucho.amber.type.Type;
039: import com.caucho.util.CharBuffer;
040: import com.caucho.util.L10N;
041:
042: import java.sql.ResultSet;
043: import java.sql.SQLException;
044:
045: /**
046: * Represents an Amber query expression
047: */
048: abstract public class AbstractAmberExpr implements AmberExpr {
049: private static final L10N L = new L10N(AbstractAmberExpr.class);
050:
051: /**
052: * Returns true for a boolean expression.
053: */
054: public boolean isBoolean() {
055: return false;
056: }
057:
058: /**
059: * Returns the expr type.
060: */
061: public Type getType() {
062: return StringType.create();
063: }
064:
065: /**
066: * Returns true if this expr has any relationship.
067: */
068: public boolean hasRelationship() {
069: return false;
070: }
071:
072: /**
073: * Converts to a boolean expression.
074: */
075: public AmberExpr createBoolean() throws QueryParseException {
076: if (isBoolean())
077: return this ;
078: else
079: throw new QueryParseException(L.l(
080: "'{0}' can't be used as a boolean", this ));
081: }
082:
083: /**
084: * Binds the expression as a select item.
085: */
086: abstract public AmberExpr bindSelect(QueryParser parser);
087:
088: /**
089: * Returns true if the expression uses the from item.
090: */
091: public boolean usesFrom(FromItem from, int type) {
092: return usesFrom(from, type, false);
093: }
094:
095: /**
096: * Returns true if the expression uses the from item.
097: */
098: public boolean usesFrom(FromItem from, int type, boolean isNot) {
099: return false;
100: }
101:
102: /**
103: * Returns true if the expression must exist
104: */
105: public boolean exists(FromItem from) {
106: return false;
107: }
108:
109: /**
110: * Returns true if the expression must exist
111: */
112: public boolean exists() {
113: return false;
114: }
115:
116: /**
117: * Returns true if the expression uses the from item.
118: */
119: public AmberExpr replaceJoin(JoinExpr join) {
120: return this ;
121: }
122:
123: /**
124: * Returns the number of columns.
125: */
126: public int getColumnCount() {
127: return 1;
128: }
129:
130: /**
131: * Generates the where expression.
132: */
133: public void generateWhere(CharBuffer cb) {
134: throw new UnsupportedOperationException(getClass().getName());
135: }
136:
137: /**
138: * Generates the (update) where expression.
139: */
140: public void generateUpdateWhere(CharBuffer cb) {
141: generateWhere(cb);
142: }
143:
144: /**
145: * Generates the having expression.
146: */
147: public void generateHaving(CharBuffer cb) {
148: generateWhere(cb);
149: }
150:
151: /**
152: * Generates the where in a join expression.
153: */
154: public void generateJoin(CharBuffer cb) {
155: generateWhere(cb);
156: }
157:
158: /**
159: * Generates the select expression.
160: */
161: public void generateSelect(CharBuffer cb) {
162: generateWhere(cb);
163: }
164:
165: /**
166: * Returns the object for the expr.
167: */
168: public Object getObject(AmberConnection aConn, ResultSet rs,
169: int index) throws SQLException {
170: return getType().getObject(aConn, rs, index);
171: }
172:
173: /**
174: * Returns the object for the expr.
175: */
176: public Object getCacheObject(AmberConnection aConn, ResultSet rs,
177: int index) throws SQLException {
178: return getObject(aConn, rs, index);
179: }
180:
181: /**
182: * Returns the object for the expr.
183: */
184: public EntityItem findItem(AmberConnection aConn, ResultSet rs,
185: int index) throws SQLException {
186: return getType().findItem(aConn, rs, index);
187: }
188:
189: /**
190: * Binds the argument type based on another expr.
191: */
192: public void setInternalArgType(AmberExpr other) {
193: if ((this instanceof ArgExpr)
194: && (other instanceof AbstractAmberExpr)) {
195: ArgExpr arg = (ArgExpr) this ;
196: arg.setType(((AbstractAmberExpr) other).getType());
197: }
198: }
199: }
|