001: /*
002: * Copyright 2004-2007 Gary Bentley
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may
005: * not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.josql.expressions;
016:
017: import org.josql.Query;
018: import org.josql.QueryExecutionException;
019: import org.josql.QueryParseException;
020:
021: import org.josql.internal.Utilities;
022:
023: /**
024: * Represents an expression that also has an alias. SELECT columns may have aliases
025: * as may the functions in the "EXECUTE ON" clause.
026: */
027: public class AliasedExpression extends Expression {
028:
029: private String alias = null;
030: protected Expression exp = null;
031: private boolean fixedResult = false;
032:
033: /**
034: * Return whether this expression has a fixed result.
035: * See: {@link Expression#hasFixedResult(Query)} for more details.
036: *
037: * @param q The Query object.
038: * @return <code>true</code> if the expression returns a fixed result, <code>false</code> otherwise.
039: */
040: public boolean hasFixedResult(Query q) {
041:
042: return this .fixedResult;
043:
044: }
045:
046: /**
047: * Get the expected return type for the expression.
048: *
049: * @param q The Query object.
050: * @return The class of the return type.
051: * @throws QueryParseException If an error occurs whilst trying to determine the
052: * return type.
053: */
054: public Class getExpectedReturnType(Query q)
055: throws QueryParseException {
056:
057: return this .exp.getExpectedReturnType(q);
058:
059: }
060:
061: /**
062: * Init this expression. All that occurs here is that the aliased expression is
063: * inited via: {@link Expression#init(Query)}.
064: *
065: * @param q The Query object.
066: * @throws QueryParseException If an error occurs during the initialisation of the
067: * expression.
068: */
069: public void init(Query q) throws QueryParseException {
070:
071: this .exp.init(q);
072:
073: this .fixedResult = this .exp.hasFixedResult(q);
074:
075: }
076:
077: /**
078: * Get the alias for the expression.
079: *
080: * @return The alias.
081: */
082: public String getAlias() {
083:
084: return this .alias;
085:
086: }
087:
088: public void setAlias(String a) {
089:
090: this .alias = Utilities.stripQuotes(a);
091:
092: }
093:
094: /**
095: * Get the expression being aliased.
096: *
097: * @return The expression.
098: */
099: public Expression getExpression() {
100:
101: return this .exp;
102:
103: }
104:
105: public void setExpression(Expression exp) {
106:
107: this .exp = exp;
108:
109: }
110:
111: /**
112: * Indicate whether the expression evaluates to <code>true</code>.
113: *
114: * @param o The object to perform the expression on.
115: * @param q The Query object.
116: * @return <code>true</code> if the expression evaulates to <code>true</code>, <code>false</code>
117: * otherwise.
118: * @throws QueryExecutionException If something goes wrong during execution of the:
119: * {@link Expression#isTrue(Object,Query)} method.
120: * @see Expression#isTrue(Object,Query)
121: */
122: public boolean isTrue(Object o, Query q)
123: throws QueryExecutionException {
124:
125: return this .exp.isTrue(o, q);
126:
127: }
128:
129: /**
130: * Get the value for this expression.
131: *
132: * @param o The object to perform the expression on.
133: * @param q The Query object.
134: * @return The result of calling: {@link Expression#getValue(Object,Query)}.
135: * @throws QueryExecutionException If something goes wrong with the execution of the
136: * expression.
137: */
138: public Object getValue(Object o, Query q)
139: throws QueryExecutionException {
140:
141: return this .exp.getValue(o, q);
142:
143: }
144:
145: /**
146: * Return a string representation of the aliased expression.
147: * Returns in the form: <b>Expression AS Alias</b>.
148: *
149: * @return The result of calling: {@link Expression#toString()} + AS + {@link #getAlias()}.
150: */
151: public String toString() {
152:
153: return this .exp.toString() + " AS " + this.alias;
154:
155: }
156:
157: }
|