001: /*
002: * $Id: FindBindVariableVisitor.java,v 1.3 2005/12/22 09:02:29 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2004 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.visitors;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046: import java.util.ListIterator;
047:
048: import org.axiondb.BindVariable;
049: import org.axiondb.Function;
050: import org.axiondb.Selectable;
051: import org.axiondb.engine.commands.AxionQueryContext;
052: import org.axiondb.engine.commands.InsertIntoClause;
053: import org.axiondb.engine.commands.SelectCommand;
054: import org.axiondb.engine.commands.UpdateCommand;
055: import org.axiondb.engine.commands.UpsertCommand;
056:
057: /**
058: * Returns a set of BindVarible used in a Selectable and in various commands.
059: * <p>
060: * Note: Almost all Commands could use BindVariables, we need to find out what is the ANSI
061: * and/or JDBC standards are for this. This almost allow us to simulate Dynamic SQL, e.g.
062: * if we can use prepare statement for things like column name, table properties, default
063: * value etc in CreateCommand
064: *
065: * @author Ahimanikya Satapathy
066: */
067: public class FindBindVariableVisitor {
068:
069: private List bvUsedInSelectable = new ArrayList(10);
070: private volatile ListIterator bvUsedInSelectableIter;
071:
072: public List getBindVariables() {
073: return bvUsedInSelectable;
074: }
075:
076: public Iterator getBindVariableIterator() {
077: if (bvUsedInSelectableIter == null) {
078: return bvUsedInSelectableIter = bvUsedInSelectable
079: .listIterator();
080: } else {
081: while (bvUsedInSelectableIter.hasPrevious()) {
082: bvUsedInSelectableIter.previous();
083: }
084: return bvUsedInSelectableIter;
085: }
086: }
087:
088: public void visit(Selectable sel) {
089: if (sel instanceof BindVariable) {
090: bvUsedInSelectable.add(sel);
091: } else if (sel instanceof Function) {
092: visit((Function) sel);
093: } else if (sel instanceof SelectCommand) {
094: visit((SelectCommand) sel);
095: }
096: }
097:
098: public void visit(Function fn) {
099: for (int i = 0, I = fn.getArgumentCount(); i < I; i++) {
100: visit(fn.getArgument(i));
101: }
102: }
103:
104: public void visit(SelectCommand select) {
105: AxionQueryContext context = select.getQueryContext();
106: for (int i = 0, I = context.getSelectCount(); i < I; i++) {
107: visit(context.getSelect(i));
108: }
109: visit(context.getWhere());
110: visit(context.getLimit());
111: visit(context.getOffset());
112: }
113:
114: public void visit(InsertIntoClause insertInto) {
115: if (insertInto.getWhenClause() != null) {
116: visit(insertInto.getWhenClause().getCondition());
117: }
118:
119: for (Iterator iter = insertInto.getValueIterator(); iter
120: .hasNext();) {
121: visit((Selectable) iter.next());
122: }
123: }
124:
125: public void visit(UpsertCommand upsert) {
126: if (upsert.getUsingSubSelectCommand() != null) {
127: visit((SelectCommand) upsert.getUsingSubSelectCommand());
128: }
129: visit(upsert.getCondition());
130: for (Iterator iter = upsert.getUpdateValueIterator(); iter
131: .hasNext();) {
132: visit((Selectable) iter.next());
133: }
134:
135: for (Iterator iter = upsert.getInsertValueIterator(); iter
136: .hasNext();) {
137: visit((Selectable) iter.next());
138: }
139:
140: InsertIntoClause exceptionWhen = upsert
141: .getExceptionWhenClause();
142: if (exceptionWhen != null) {
143: visit(exceptionWhen);
144: }
145: }
146:
147: public void visit(UpdateCommand update) {
148: for (Iterator iter = update.getValueIterator(); iter.hasNext();) {
149: visit((Selectable) iter.next());
150: }
151: visit(update.getWhere());
152:
153: InsertIntoClause exceptionWhen = update
154: .getExceptionWhenClause();
155: if (exceptionWhen != null) {
156: visit(exceptionWhen);
157: }
158: }
159: }
|