001: /**
002: * Copyright (C) 2006 NetMind Consulting Bt.
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 3 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: */package hu.netmind.persistence.parser;
018:
019: import java.util.List;
020: import java.util.Vector;
021: import java.util.Date;
022: import java.util.Set;
023: import java.util.HashSet;
024:
025: /**
026: * This class represents a query statement. A query statement
027: * has four parts: A table name to select from, additional tables to
028: * left join, a query expression, and order by parts.
029: * @author Brautigam Robert
030: * @version Revision: $Revision$
031: */
032: public class QueryStatement {
033: public static final int MODE_FIND = 1;
034: public static final int MODE_VIEW = 2;
035:
036: private int mode = MODE_FIND;
037: private List selectTerms = new Vector();
038: private List allLeftTableTerms = new Vector();
039: private Expression queryExpression;
040: private List orderByList = new Vector();
041: private TimeControl timeControl;
042:
043: private String staticRepresentation;
044: private Set tables;
045:
046: public Set getTables() {
047: return tables;
048: }
049:
050: public void setTables(Set tables) {
051: this .tables = tables;
052: }
053:
054: public Set computeTables() {
055: HashSet result = new HashSet();
056: for (int i = 0; i < selectTerms.size(); i++)
057: result.add(((TableTerm) selectTerms.get(i)).getTableName());
058: computeTables(result, queryExpression);
059: return result;
060: }
061:
062: private void computeTables(Set tables, Expression queryExpression) {
063: for (int i = 0; (queryExpression != null)
064: && (i < queryExpression.size()); i++) {
065: Object term = queryExpression.get(i);
066: if (term instanceof TableTerm)
067: tables.add(((TableTerm) term).getTableName());
068: if (term instanceof Expression)
069: computeTables(tables, (Expression) term);
070: }
071: }
072:
073: public QueryStatement(QueryStatement stmt) {
074: setStaticRepresentation(stmt.getStaticRepresentation());
075: setAllLeftTableTerms(stmt.getAllLeftTableTerms());
076: setMode(stmt.getMode());
077: setTimeControl(stmt.getTimeControl());
078: setSelectTerms(stmt.getSelectTerms());
079: setQueryExpression(stmt.getQueryExpression());
080: setOrderByList(stmt.getOrderByList());
081: }
082:
083: public QueryStatement deepCopy() {
084: QueryStatement result = new QueryStatement(this );
085: if (getAllLeftTableTerms() != null)
086: setAllLeftTableTerms(new Vector(getAllLeftTableTerms()));
087: result.setSelectTerms(new Vector());
088: for (int i = 0; i < getSelectTerms().size(); i++)
089: result.getSelectTerms().add(
090: ((TableTerm) getSelectTerms().get(i)).deepCopy());
091: if (getQueryExpression() != null)
092: result.setQueryExpression(getQueryExpression().deepCopy());
093: if (getOrderByList() != null)
094: result.setOrderByList(new Vector(getOrderByList()));
095: return result;
096: }
097:
098: public QueryStatement(List selectTerms, Expression queryExpression,
099: List orderByList) {
100: setSelectTerms(selectTerms);
101: setQueryExpression(queryExpression);
102: setOrderByList(orderByList);
103: }
104:
105: public QueryStatement(TableTerm tableTerm,
106: Expression queryExpression, List orderByList) {
107: Vector selectTerms = new Vector();
108: selectTerms.add(tableTerm);
109: setSelectTerms(selectTerms);
110: setQueryExpression(queryExpression);
111: setOrderByList(orderByList);
112: }
113:
114: public QueryStatement(String tableName, Expression queryExpression,
115: List orderByList) {
116: Vector selectTerms = new Vector();
117: selectTerms.add(new TableTerm(tableName, null));
118: setSelectTerms(selectTerms);
119: setQueryExpression(queryExpression);
120: setOrderByList(orderByList);
121: }
122:
123: /**
124: * Remove the given left term.
125: */
126: public void removeLeftTableTerm(TableTerm leftTerm) {
127: // Remove from list
128: allLeftTableTerms.remove(leftTerm);
129: // Remove from expressions
130: if (queryExpression != null)
131: queryExpression.removeLeftTableTerm(leftTerm);
132: }
133:
134: /**
135: * Replace all occurences of the given term with the new term.
136: */
137: public void replace(TableTerm oldTerm, TableTerm newTerm) {
138: // First, replace in select terms
139: for (int i = 0; i < selectTerms.size(); i++) {
140: TableTerm term = (TableTerm) selectTerms.get(i);
141: TableTerm replaceTerm = newTerm;
142: if (term.equals(oldTerm)) {
143: if (term instanceof ReferenceTerm)
144: replaceTerm = new ReferenceTerm(newTerm,
145: ((ReferenceTerm) term).getColumnName(),
146: ((ReferenceTerm) term).getColumnAlias());
147: selectTerms.set(i, replaceTerm);
148: }
149: }
150: // Replace in expression
151: if (getQueryExpression() != null)
152: getQueryExpression().replace(oldTerm, newTerm);
153: // Replace in order bys
154: for (int i = 0; (getOrderByList() != null)
155: && (i < getOrderByList().size()); i++) {
156: OrderBy orderBy = (OrderBy) getOrderByList().get(i);
157: if (orderBy.getReferenceTerm().equals(oldTerm))
158: getOrderByList().set(
159: i,
160: new OrderBy(new ReferenceTerm(newTerm, orderBy
161: .getReferenceTerm().getColumnName()),
162: orderBy.getDirection()));
163: }
164: }
165:
166: public Expression getQueryExpression() {
167: return queryExpression;
168: }
169:
170: public void setQueryExpression(Expression queryExpression) {
171: this .queryExpression = queryExpression;
172: }
173:
174: public List getOrderByList() {
175: return orderByList;
176: }
177:
178: public void setOrderByList(List orderByList) {
179: this .orderByList = orderByList;
180: }
181:
182: public TimeControl getTimeControl() {
183: return timeControl;
184: }
185:
186: public void setTimeControl(TimeControl timeControl) {
187: this .timeControl = timeControl;
188: }
189:
190: public List getSelectTerms() {
191: return selectTerms;
192: }
193:
194: public void setSelectTerms(List selectTerms) {
195: this .selectTerms = selectTerms;
196: }
197:
198: public int getMode() {
199: return mode;
200: }
201:
202: public void setMode(int mode) {
203: this .mode = mode;
204: }
205:
206: public List getAllLeftTableTerms() {
207: return allLeftTableTerms;
208: }
209:
210: public void setAllLeftTableTerms(List allLeftTableTerms) {
211: this .allLeftTableTerms = allLeftTableTerms;
212: }
213:
214: public String getStaticRepresentation() {
215: return staticRepresentation;
216: }
217:
218: public void setStaticRepresentation(String staticRepresentation) {
219: this.staticRepresentation = staticRepresentation;
220: }
221:
222: }
|