001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.query.sql.model;
020
021/**
022 * A default implementation of a visitor that visits depth-first in standard expression order.
023 *
024 * @author Florent Guillaume
025 */
026public class DefaultQueryVisitor implements IVisitor {
027
028    private static final long serialVersionUID = 1L;
029
030    @Override
031    public void visitQuery(SQLQuery node) {
032        node.select.accept(this);
033        node.from.accept(this);
034        if (node.where != null) {
035            node.where.accept(this);
036        }
037        if (node.orderBy != null) {
038            node.orderBy.accept(this);
039        }
040        if (node.groupBy != null) {
041            node.groupBy.accept(this);
042        }
043        if (node.having != null) {
044            node.having.accept(this);
045        }
046    }
047
048    @Override
049    public void visitSelectClause(SelectClause node) {
050        node.elements.forEach((alias, operand) -> operand.accept(this));
051    }
052
053    @Override
054    public void visitFromClause(FromClause node) {
055    }
056
057    @Override
058    public void visitWhereClause(WhereClause node) {
059        node.predicate.accept(this);
060    }
061
062    @Override
063    public void visitGroupByClause(GroupByClause node) {
064    }
065
066    @Override
067    public void visitHavingClause(HavingClause node) {
068        if (node.predicate != null) {
069            node.predicate.accept(this);
070        }
071    }
072
073    @Override
074    public void visitOrderByClause(OrderByClause node) {
075        node.elements.accept(this);
076    }
077
078    @Override
079    public void visitOrderByList(OrderByList node) {
080        for (OrderByExpr aNode : node) {
081            aNode.accept(this);
082        }
083    }
084
085    @Override
086    public void visitOrderByExpr(OrderByExpr node) {
087        node.reference.accept(this);
088    }
089
090    @Override
091    public void visitExpression(Expression node) {
092        if (node.rvalue == null) {
093            if (node.isSuffix()) {
094                // IS NULL, IS NOT NULL
095                node.lvalue.accept(this);
096                node.operator.accept(this);
097            } else {
098                // NOT
099                node.operator.accept(this);
100                node.lvalue.accept(this);
101            }
102        } else {
103            node.lvalue.accept(this);
104            node.operator.accept(this);
105            node.rvalue.accept(this);
106        }
107    }
108
109    @Override
110    public void visitMultiExpression(MultiExpression node) {
111        for (Operand operand : node.values) {
112            operand.accept(this);
113        }
114    }
115
116    @Override
117    public void visitOperator(Operator node) {
118    }
119
120    @Override
121    public void visitReference(Reference node) {
122    }
123
124    @Override
125    public void visitReferenceList(ReferenceList node) {
126        for (Reference reference : node) {
127            reference.accept(this);
128        }
129    }
130
131    @Override
132    public void visitLiteral(Literal node) {
133    }
134
135    @Override
136    public void visitLiteralList(LiteralList node) {
137        for (Literal literal : node) {
138            literal.accept(this);
139        }
140    }
141
142    @Override
143    public void visitDateLiteral(DateLiteral node) {
144        visitLiteral(node);
145    }
146
147    @Override
148    public void visitStringLiteral(StringLiteral node) {
149        visitLiteral(node);
150    }
151
152    @Override
153    public void visitDoubleLiteral(DoubleLiteral node) {
154        visitLiteral(node);
155    }
156
157    @Override
158    public void visitIntegerLiteral(IntegerLiteral node) {
159        visitLiteral(node);
160    }
161
162    @Override
163    public void visitBooleanLiteral(BooleanLiteral node) {
164        visitLiteral(node);
165    }
166
167    @Override
168    public void visitFunction(Function node) {
169        for (Operand operand : node.args) {
170            operand.accept(this);
171        }
172    }
173
174    @Override
175    public void visitOperandList(OperandList node) {
176        for (Operand operand : node) {
177            operand.accept(this);
178        }
179    }
180
181}