KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > expressions > AliasedExpression


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.expressions;
16
17 import org.josql.Query;
18 import org.josql.QueryExecutionException;
19 import org.josql.QueryParseException;
20
21 import org.josql.internal.Utilities;
22
23 /**
24  * Represents an expression that also has an alias. SELECT columns may have aliases
25  * as may the functions in the "EXECUTE ON" clause.
26  * <p>
27  * Last Modified By: $Author: barrygently $<br />
28  * Last Modified On: $Date: 2004/12/20 16:22:43 $<br />
29  * Current Revision: $Revision: 1.2 $<br />
30  */

31 public class AliasedExpression extends Expression
32 {
33
34     private String JavaDoc alias = null;
35     protected Expression exp = null;
36     private boolean fixedResult = false;
37
38     /**
39      * Return whether this expression has a fixed result.
40      * See: {@link Expression#hasFixedResult(Query)} for more details.
41      *
42      * @param q The Query object.
43      * @return <code>true</code> if the expression returns a fixed result, <code>false</code> otherwise.
44      */

45     public boolean hasFixedResult (Query q)
46     {
47
48     return this.fixedResult;
49
50     }
51
52     /**
53      * Get the expected return type for the expression.
54      *
55      * @param q The Query object.
56      * @return The class of the return type.
57      * @throws QueryParseException If an error occurs whilst trying to determine the
58      * return type.
59      */

60     public Class JavaDoc getExpectedReturnType (Query q)
61                                     throws QueryParseException
62     {
63
64     return this.exp.getExpectedReturnType (q);
65
66     }
67
68     /**
69      * Init this expression. All that occurs here is that the aliased expression is
70      * inited via: {@link Expression#init(Query)}.
71      *
72      * @param q The Query object.
73      * @throws QueryParseException If an error occurs during the initialisation of the
74      * expression.
75     */

76     public void init (Query q)
77                   throws QueryParseException
78     {
79
80     this.exp.init (q);
81
82     this.fixedResult = this.exp.hasFixedResult (q);
83
84     }
85
86     /**
87      * Get the alias for the expression.
88      *
89      * @return The alias.
90      */

91     public String JavaDoc getAlias ()
92     {
93
94     return this.alias;
95
96     }
97
98     public void setAlias (String JavaDoc a)
99     {
100
101     this.alias = Utilities.stripQuotes (a);
102
103     }
104
105     /**
106      * Get the expression being aliased.
107      *
108      * @return The expression.
109      */

110     public Expression getExpression ()
111     {
112
113     return this.exp;
114
115     }
116
117     public void setExpression (Expression exp)
118     {
119
120     this.exp = exp;
121
122     }
123
124     /**
125      * Indicate whether the expression evaluates to <code>true</code>.
126      *
127      * @param o The object to perform the expression on.
128      * @param q The Query object.
129      * @return <code>true</code> if the expression evaulates to <code>true</code>, <code>false</code>
130      * otherwise.
131      * @throws QueryExecutionException If something goes wrong during execution of the:
132      * {@link Expression#isTrue(Object,Query)} method.
133      * @see Expression#isTrue(Object,Query)
134      */

135     public boolean isTrue (Object JavaDoc o,
136                Query q)
137                        throws QueryExecutionException
138     {
139
140     return this.exp.isTrue (o,
141                 q);
142
143     }
144
145     /**
146      * Get the value for this expression.
147      *
148      * @param o The object to perform the expression on.
149      * @param q The Query object.
150      * @return The result of calling: {@link Expression#getValue(Object,Query)}.
151      * @throws QueryExecutionException If something goes wrong with the execution of the
152      * expression.
153      */

154     public Object JavaDoc getValue (Object JavaDoc o,
155                 Query q)
156                         throws QueryExecutionException
157     {
158
159     return this.exp.getValue (o,
160                   q);
161
162     }
163
164     /**
165      * Return a string representation of the aliased expression.
166      * Returns in the form: <b>Expression AS Alias</b>.
167      *
168      * @return The result of calling: {@link Expression#toString()} + AS + {@link #getAlias()}.
169      */

170     public String JavaDoc toString ()
171     {
172
173     return this.exp.toString () + " AS " + this.alias;
174
175     }
176
177 }
178
Popular Tags