001: package org.mandarax.jdbc.server.sql;
002:
003: /*
004: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import org.mandarax.jdbc.server.parser.ParseException;
022:
023: /**
024: * Class for complex column terms using functions with one parameter
025: * (in particular, aggregation functions).
026: * @see Functions
027: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
028: * @version 3.3.2 <29 December 2004>
029: * @since 3.0
030: */
031: public class ComplexTerm1 extends ColumnTerm implements
032: ColumnTermContext, Functions {
033: private Function function = null;
034: private ColumnTerm colTerm = null;
035:
036: /**
037: * Add a condition to this context.
038: * @param colTerm a column term
039: */
040: public void add(ColumnTerm colTerm) throws ParseException {
041: if (this .function.isAggregationFunction()
042: && !(colTerm instanceof ColumnTerm))
043: throw new ParseException(
044: "Column term expected as argument by aggregation function "
045: + function);
046: this .colTerm = colTerm;
047: colTerm.setOwner(this );
048: }
049:
050: /**
051: * Set the function.
052: * @param function a function
053: */
054: public void setFunction(Function function) {
055: this .function = function;
056: }
057:
058: /**
059: * Gather the host variables.
060: * @param variables the list used to collect the variables
061: */
062: public void prepare(java.util.List variables) {
063: if (colTerm != null)
064: colTerm.prepare(variables);
065: }
066:
067: /**
068: * Compares objects.
069: * @param obj another object.
070: * @return a boolean
071: */
072: public boolean sameAs(Object obj) {
073: if (obj instanceof ComplexTerm1) {
074: ComplexTerm1 ct1 = (ComplexTerm1) obj;
075: return function == ct1.function
076: && colTerm.sameAs(ct1.colTerm);
077: }
078: return false;
079: }
080:
081: /**
082: * Get the associated variable name.
083: * @return a variable name
084: */
085: public String asVariableName() {
086: StringBuffer buf = new StringBuffer();
087: function.print(buf, new ColumnTerm[] { colTerm });
088: return buf.toString();
089: }
090:
091: /**
092: * Get the function.
093: * @return the function
094: */
095: Function getFunction() {
096: return function;
097: }
098:
099: /**
100: * Get the column term.
101: * @return ColumnTerm
102: */
103: ColumnTerm getColTerm() {
104: return colTerm;
105: }
106:
107: /**
108: * Print the object on a buffer in order to display the parsed SQL.
109: * @param out a string bufer to print on
110: */
111: public void print(StringBuffer out) {
112: function.print(out, new ColumnTerm[] { colTerm });
113: }
114:
115: }
|