001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, Geotools Project Managment Committee (PMC)
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;
009: * version 2.1 of the License.
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: */
017: package org.geotools.arcsde.data.view;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import net.sf.jsqlparser.expression.BinaryExpression;
025: import net.sf.jsqlparser.expression.CaseExpression;
026: import net.sf.jsqlparser.expression.DateValue;
027: import net.sf.jsqlparser.expression.DoubleValue;
028: import net.sf.jsqlparser.expression.Expression;
029: import net.sf.jsqlparser.expression.ExpressionVisitor;
030: import net.sf.jsqlparser.expression.Function;
031: import net.sf.jsqlparser.expression.InverseExpression;
032: import net.sf.jsqlparser.expression.JdbcParameter;
033: import net.sf.jsqlparser.expression.LongValue;
034: import net.sf.jsqlparser.expression.NullValue;
035: import net.sf.jsqlparser.expression.Parenthesis;
036: import net.sf.jsqlparser.expression.StringValue;
037: import net.sf.jsqlparser.expression.TimeValue;
038: import net.sf.jsqlparser.expression.TimestampValue;
039: import net.sf.jsqlparser.expression.WhenClause;
040: import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
041: import net.sf.jsqlparser.expression.operators.arithmetic.Division;
042: import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
043: import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
044: import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
045: import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
046: import net.sf.jsqlparser.expression.operators.relational.Between;
047: import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
048: import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
049: import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
050: import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
051: import net.sf.jsqlparser.expression.operators.relational.InExpression;
052: import net.sf.jsqlparser.expression.operators.relational.IsNullExpression;
053: import net.sf.jsqlparser.expression.operators.relational.ItemsList;
054: import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
055: import net.sf.jsqlparser.expression.operators.relational.MinorThan;
056: import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
057: import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
058: import net.sf.jsqlparser.schema.Column;
059: import net.sf.jsqlparser.statement.select.SubSelect;
060:
061: import com.esri.sde.sdk.client.SeConnection;
062:
063: /**
064: * Qualifies the column references (aliased or not) the ArcSDE "table.user."
065: * prefix as required by the ArcSDE java api to not get confused when using
066: * joined tables.
067: *
068: * @author Gabriel Roldan, Axios Engineering
069: * @version $Id: ExpressionQualifier.java 29135 2008-02-07 19:49:09Z desruisseaux $
070: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/arcsde/datastore/src/main/java/org/geotools/arcsde/data/view/ExpressionQualifier.java $
071: * @since 2.3.x
072: */
073: class ExpressionQualifier implements ExpressionVisitor {
074: /** DOCUMENT ME! */
075: private Expression qualifiedExpression;
076:
077: /** DOCUMENT ME! */
078: private SeConnection conn;
079:
080: private Map tableAliases;
081:
082: /**
083: * Creates a new ExpressionQualifier object.
084: *
085: * @param conn
086: * DOCUMENT ME!
087: */
088: private ExpressionQualifier(SeConnection conn, Map tableAliases) {
089: this .conn = conn;
090: this .tableAliases = tableAliases;
091: }
092:
093: /**
094: * DOCUMENT ME!
095: *
096: * @param conn
097: * DOCUMENT ME!
098: * @param exp
099: * DOCUMENT ME!
100: *
101: * @return DOCUMENT ME!
102: */
103: public static Expression qualify(SeConnection conn,
104: Map tableAliases, Expression exp) {
105: if (exp == null) {
106: return null;
107: }
108:
109: ExpressionQualifier qualifier = new ExpressionQualifier(conn,
110: tableAliases);
111:
112: exp.accept(qualifier);
113:
114: return qualifier.qualifiedExpression;
115: }
116:
117: /**
118: * DOCUMENT ME!
119: *
120: * @param nullValue
121: * DOCUMENT ME!
122: */
123: public void visit(NullValue nullValue) {
124: qualifiedExpression = nullValue;
125: }
126:
127: /**
128: * DOCUMENT ME!
129: *
130: * @param function
131: * DOCUMENT ME!
132: */
133: public void visit(Function function) {
134: Function qfunction = new Function();
135: qfunction.setAllColumns(function.isAllColumns());
136: qfunction.setEscaped(function.isEscaped());
137: qfunction.setName(function.getName());
138:
139: ExpressionList parameters = function.getParameters();
140: ExpressionList qualifiedParams;
141:
142: qualifiedParams = (ExpressionList) ItemsListQualifier.qualify(
143: conn, tableAliases, parameters);
144:
145: qfunction.setParameters(qualifiedParams);
146:
147: this .qualifiedExpression = qfunction;
148: }
149:
150: /**
151: * DOCUMENT ME!
152: *
153: * @param inverseExpression
154: * DOCUMENT ME!
155: */
156: public void visit(InverseExpression inverseExpression) {
157: InverseExpression qInv = new InverseExpression();
158:
159: Expression exp = inverseExpression.getExpression();
160: Expression qExp = ExpressionQualifier.qualify(conn,
161: tableAliases, exp);
162:
163: qInv.setExpression(qExp);
164: this .qualifiedExpression = qInv;
165: }
166:
167: /**
168: * DOCUMENT ME!
169: *
170: * @param jdbcParameter
171: * DOCUMENT ME!
172: */
173: public void visit(JdbcParameter jdbcParameter) {
174: this .qualifiedExpression = jdbcParameter;
175: }
176:
177: /**
178: * DOCUMENT ME!
179: *
180: * @param doubleValue
181: * DOCUMENT ME!
182: */
183: public void visit(DoubleValue doubleValue) {
184: this .qualifiedExpression = doubleValue;
185: }
186:
187: /**
188: * DOCUMENT ME!
189: *
190: * @param longValue
191: * DOCUMENT ME!
192: */
193: public void visit(LongValue longValue) {
194: this .qualifiedExpression = longValue;
195: }
196:
197: /**
198: * DOCUMENT ME!
199: *
200: * @param dateValue
201: * DOCUMENT ME!
202: */
203: public void visit(DateValue dateValue) {
204: this .qualifiedExpression = dateValue;
205: }
206:
207: /**
208: * DOCUMENT ME!
209: *
210: * @param timeValue
211: * DOCUMENT ME!
212: */
213: public void visit(TimeValue timeValue) {
214: this .qualifiedExpression = timeValue;
215: }
216:
217: /**
218: * DOCUMENT ME!
219: *
220: * @param timestampValue
221: * DOCUMENT ME!
222: */
223: public void visit(TimestampValue timestampValue) {
224: this .qualifiedExpression = timestampValue;
225: }
226:
227: /**
228: * DOCUMENT ME!
229: *
230: * @param parenthesis
231: * DOCUMENT ME!
232: */
233: public void visit(Parenthesis parenthesis) {
234: Expression pExp = parenthesis.getExpression();
235: Expression qualifiedExpression;
236: qualifiedExpression = qualify(conn, tableAliases, pExp);
237:
238: Parenthesis qualified = new Parenthesis();
239: qualified.setExpression(qualifiedExpression);
240: this .qualifiedExpression = qualified;
241: }
242:
243: /**
244: * DOCUMENT ME!
245: *
246: * @param stringValue
247: * DOCUMENT ME!
248: */
249: public void visit(StringValue stringValue) {
250: this .qualifiedExpression = stringValue;
251: }
252:
253: private void visitBinaryExpression(BinaryExpression exp) {
254:
255: Expression left = ExpressionQualifier.qualify(conn,
256: tableAliases, exp.getLeftExpression());
257: Expression right = ExpressionQualifier.qualify(conn,
258: tableAliases, exp.getRightExpression());
259:
260: BinaryExpression qualified;
261:
262: if (exp instanceof Addition)
263: qualified = new Addition();
264: else if (exp instanceof Division)
265: qualified = new Division();
266: else if (exp instanceof Multiplication)
267: qualified = new Multiplication();
268: else if (exp instanceof Subtraction)
269: qualified = new Subtraction();
270: else if (exp instanceof EqualsTo)
271: qualified = new EqualsTo();
272: else if (exp instanceof GreaterThan)
273: qualified = new GreaterThan();
274: else if (exp instanceof GreaterThanEquals)
275: qualified = new GreaterThanEquals();
276: else if (exp instanceof LikeExpression)
277: qualified = new LikeExpression();
278: else if (exp instanceof MinorThan)
279: qualified = new MinorThan();
280: else if (exp instanceof MinorThanEquals)
281: qualified = new MinorThanEquals();
282: else if (exp instanceof NotEqualsTo)
283: qualified = new NotEqualsTo();
284: else
285: throw new IllegalArgumentException(
286: "Unkown binary expression: " + exp);
287:
288: qualified.setLeftExpression(left);
289: qualified.setRightExpression(right);
290:
291: this .qualifiedExpression = qualified;
292: }
293:
294: /**
295: * DOCUMENT ME!
296: *
297: * @param addition
298: * DOCUMENT ME!
299: */
300: public void visit(Addition addition) {
301: visitBinaryExpression(addition);
302: }
303:
304: /**
305: * DOCUMENT ME!
306: *
307: * @param division
308: * DOCUMENT ME!
309: */
310: public void visit(Division division) {
311: visitBinaryExpression(division);
312: }
313:
314: /**
315: * DOCUMENT ME!
316: *
317: * @param multiplication
318: * DOCUMENT ME!
319: */
320: public void visit(Multiplication multiplication) {
321: visitBinaryExpression(multiplication);
322: }
323:
324: /**
325: * DOCUMENT ME!
326: *
327: * @param subtraction
328: * DOCUMENT ME!
329: */
330: public void visit(Subtraction subtraction) {
331: visitBinaryExpression(subtraction);
332: }
333:
334: /**
335: * DOCUMENT ME!
336: *
337: * @param andExpression
338: * DOCUMENT ME!
339: */
340: public void visit(AndExpression andExpression) {
341: Expression left = qualify(conn, tableAliases, andExpression
342: .getLeftExpression());
343: Expression rigth = qualify(conn, tableAliases, andExpression
344: .getRightExpression());
345:
346: AndExpression and = new AndExpression(left, rigth);
347: this .qualifiedExpression = and;
348: }
349:
350: /**
351: * DOCUMENT ME!
352: *
353: * @param orExpression
354: * DOCUMENT ME!
355: */
356: public void visit(OrExpression orExpression) {
357: Expression left = qualify(conn, tableAliases, orExpression
358: .getLeftExpression());
359: Expression rigth = qualify(conn, tableAliases, orExpression
360: .getRightExpression());
361:
362: OrExpression or = new OrExpression(left, rigth);
363: this .qualifiedExpression = or;
364: }
365:
366: /**
367: * DOCUMENT ME!
368: *
369: * @param between
370: * DOCUMENT ME!
371: */
372: public void visit(Between between) {
373: Between qualified = new Between();
374:
375: Expression start = qualify(conn, tableAliases, between
376: .getBetweenExpressionStart());
377: Expression end = qualify(conn, tableAliases, between
378: .getBetweenExpressionEnd());
379: Expression left = qualify(conn, tableAliases, between
380: .getLeftExpression());
381:
382: qualified.setBetweenExpressionStart(start);
383: qualified.setBetweenExpressionEnd(end);
384: qualified.setLeftExpression(left);
385: this .qualifiedExpression = qualified;
386: }
387:
388: /**
389: * DOCUMENT ME!
390: *
391: * @param equalsTo
392: * DOCUMENT ME!
393: */
394: public void visit(EqualsTo equalsTo) {
395: visitBinaryExpression(equalsTo);
396: }
397:
398: /**
399: * DOCUMENT ME!
400: *
401: * @param greaterThan
402: * DOCUMENT ME!
403: */
404: public void visit(GreaterThan greaterThan) {
405: visitBinaryExpression(greaterThan);
406: }
407:
408: /**
409: * DOCUMENT ME!
410: *
411: * @param greaterThanEquals
412: * DOCUMENT ME!
413: */
414: public void visit(GreaterThanEquals greaterThanEquals) {
415: visitBinaryExpression(greaterThanEquals);
416: }
417:
418: /**
419: * DOCUMENT ME!
420: *
421: * @param inExpression
422: * DOCUMENT ME!
423: */
424: public void visit(InExpression inExpression) {
425: Expression left = qualify(conn, tableAliases, inExpression
426: .getLeftExpression());
427: ItemsList itemsList = ItemsListQualifier.qualify(conn,
428: tableAliases, inExpression.getItemsList());
429:
430: InExpression qualified = new InExpression();
431: qualified.setLeftExpression(left);
432: qualified.setItemsList(itemsList);
433: this .qualifiedExpression = qualified;
434: }
435:
436: /**
437: * DOCUMENT ME!
438: *
439: * @param isNullExpression
440: * DOCUMENT ME!
441: */
442: public void visit(IsNullExpression isNullExpression) {
443: IsNullExpression qualified = new IsNullExpression();
444: Expression left = qualify(conn, tableAliases, isNullExpression
445: .getLeftExpression());
446:
447: qualified.setLeftExpression(left);
448: qualified.setNot(isNullExpression.isNot());
449: this .qualifiedExpression = qualified;
450: }
451:
452: /**
453: * DOCUMENT ME!
454: *
455: * @param likeExpression
456: * DOCUMENT ME!
457: */
458: public void visit(LikeExpression likeExpression) {
459: visitBinaryExpression(likeExpression);
460: }
461:
462: /**
463: * DOCUMENT ME!
464: *
465: * @param minorThan
466: * DOCUMENT ME!
467: */
468: public void visit(MinorThan minorThan) {
469: visitBinaryExpression(minorThan);
470: }
471:
472: /**
473: * DOCUMENT ME!
474: *
475: * @param minorThanEquals
476: * DOCUMENT ME!
477: */
478: public void visit(MinorThanEquals minorThanEquals) {
479: visitBinaryExpression(minorThanEquals);
480: }
481:
482: /**
483: * DOCUMENT ME!
484: *
485: * @param notEqualsTo
486: * DOCUMENT ME!
487: */
488: public void visit(NotEqualsTo notEqualsTo) {
489: visitBinaryExpression(notEqualsTo);
490: }
491:
492: /**
493: * DOCUMENT ME!
494: *
495: * @param tableColumn
496: * DOCUMENT ME!
497: */
498: public void visit(Column tableColumn) {
499:
500: Column qualified = ColumnQualifier.qualify(conn, tableAliases,
501: tableColumn);
502: this .qualifiedExpression = qualified;
503:
504: }
505:
506: /**
507: * DOCUMENT ME!
508: *
509: * @param subSelect
510: * DOCUMENT ME!
511: */
512: public void visit(SubSelect subSelect) {
513: SubSelect qualified = SubSelectQualifier.qualify(conn,
514: subSelect);
515: this .qualifiedExpression = qualified;
516: }
517:
518: /**
519: * DOCUMENT ME!
520: *
521: * @param caseExpression
522: * DOCUMENT ME!
523: */
524: public void visit(CaseExpression caseExpression) {
525: Expression switchExpr = qualify(conn, tableAliases,
526: caseExpression.getSwitchExpression());
527: Expression elseExpr = qualify(conn, tableAliases,
528: caseExpression.getElseExpression());
529:
530: List whenClauses = null;
531: if (caseExpression.getWhenClauses() != null) {
532: whenClauses = new ArrayList();
533: for (Iterator it = caseExpression.getWhenClauses()
534: .iterator(); it.hasNext();) {
535: WhenClause whenClause = (WhenClause) it.next();
536: WhenClause qWhen = (WhenClause) qualify(conn,
537: tableAliases, whenClause);
538: whenClauses.add(qWhen);
539: }
540: }
541:
542: CaseExpression qualifiedWhen = new CaseExpression();
543: qualifiedWhen.setElseExpression(elseExpr);
544: qualifiedWhen.setSwitchExpression(switchExpr);
545: qualifiedWhen.setWhenClauses(whenClauses);
546: this .qualifiedExpression = qualifiedWhen;
547: }
548:
549: /**
550: * DOCUMENT ME!
551: *
552: * @param whenClause
553: * DOCUMENT ME!
554: */
555: public void visit(WhenClause whenClause) {
556: Expression whenExpr = qualify(conn, tableAliases, whenClause
557: .getWhenExpression());
558: Expression thenExpr = qualify(conn, tableAliases, whenClause
559: .getThenExpression());
560:
561: WhenClause q = new WhenClause();
562: q.setWhenExpression(whenExpr);
563: q.setThenExpression(thenExpr);
564: this.qualifiedExpression = q;
565: }
566: }
|