001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.mandarax.jdbc.server.sql;
020:
021: import java.util.*;
022: import org.mandarax.kernel.InferenceException;
023: import org.mandarax.kernel.VariableTerm;
024: import org.mandarax.util.resultsetfilters.*;
025:
026: /**
027: * Represents the SQL ORDER BY clause.
028: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
029: * @version 3.3.2 <29 December 2004>
030: * @since 3.0
031: */
032:
033: public class OrderByClause extends SQLObject implements
034: ColumnTermContext {
035: // constants representing sort directions
036: public static final boolean ASC = false;
037: public static final boolean DESC = true;
038:
039: private SelectStatement owner = null;
040: private List columns = new ArrayList();
041: private BitSet sortDirections = new BitSet();
042: private Map variablesByName = null;
043: private OrderByCondition[] conditions = null;
044:
045: /**
046: * Constructor.
047: */
048: public OrderByClause() {
049: super ();
050: }
051:
052: /**
053: * @return SelectStatement
054: */
055: public SelectStatement getOwner() {
056: return owner;
057: }
058:
059: /**
060: * Sets the owner.
061: * @param owner The owner to set
062: */
063: void setOwner(SelectStatement owner) {
064: this .owner = owner;
065: }
066:
067: /**
068: * Normalize the object.
069: * @param typesByColumn associations between column names and (SQL) types
070: */
071: public void normalize(Map typesByColumn) {
072: // nothing to do here
073: }
074:
075: /**
076: * Compares objects.
077: * @param obj another object.
078: * @return a boolean
079: */
080: public boolean sameAs(Object obj) {
081: if (obj != null && obj instanceof OrderByClause) {
082: OrderByClause s = (OrderByClause) obj;
083: boolean result = true;
084: result = result && s.columns.equals(s.columns);
085: return result;
086: }
087: return false;
088: }
089:
090: /**
091: * Add a column to the list.
092: * @param item a new column.
093: */
094: public void add(ColumnTerm colTerm) {
095: columns.add(colTerm);
096: conditions = null;
097: }
098:
099: /**
100: * Set the sort direction.
101: * @param a boolean specifying the sort direction (ASC or DESC)
102: */
103: public void setSortDirection(boolean direction) {
104: sortDirections.set(columns.size() - 1, direction == DESC);
105: conditions = null;
106: }
107:
108: /**
109: * Set variable names / variable associations.
110: * @param variablesByNames a map containing variable name -> variable term associations
111: */
112: void setVariablesByName(Map variablesByName) {
113: this .variablesByName = variablesByName;
114: }
115:
116: /**
117: * Indicates whether the order by clause is empty (has no columns).
118: * @return a boolean
119: */
120: boolean isEmpty() {
121: return this .columns == null || this .columns.size() == 0;
122: }
123:
124: /**
125: * Get the mandarax order by conditions.
126: * @return an array of order by conditions
127: */
128: OrderByCondition[] getConditions() throws InferenceException {
129: if (conditions == null) {
130: conditions = new OrderByCondition[columns.size()];
131: for (int i = 0; i < conditions.length; i++) {
132: ColumnTerm columnTerm = (ColumnTerm) columns.get(i);
133: String column = columnTerm.asVariableName();
134: VariableTerm var = (VariableTerm) variablesByName
135: .get(column);
136: if (var == null)
137: throw new InferenceException(
138: "Cannot apply ORDER BY condition - no variable found for name "
139: + column);
140: if (sortDirections.get(i))
141: conditions[i] = new DESC(var);
142: else
143: conditions[i] = new ASC(var);
144:
145: }
146: }
147: return conditions;
148: }
149:
150: /**
151: * Print the object on a buffer in order to display the parsed SQL.
152: * @param out a string bufer to print on
153: */
154: public void print(StringBuffer out) {
155: for (int i = 0; i < columns.size(); i++) {
156: if (i > 0)
157: out.append(',');
158: if (ASC == sortDirections.get(i))
159: out.append("ASC ");
160: else
161: out.append("DESC ");
162: ((ColumnTerm) columns.get(i)).print(out);
163: }
164: }
165:
166: }
|