All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mysema.query.sql.RelationalPathExtractor Maven / Gradle / Ivy

There is a newer version: 3.7.4
Show newest version
/*
 * Copyright 2013, Mysema Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mysema.query.sql;

import static com.mysema.query.util.CollectionUtils.add;

import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.mysema.query.JoinExpression;
import com.mysema.query.QueryMetadata;
import com.mysema.query.types.Constant;
import com.mysema.query.types.Expression;
import com.mysema.query.types.FactoryExpression;
import com.mysema.query.types.Operation;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.ParamExpression;
import com.mysema.query.types.Path;
import com.mysema.query.types.SubQueryExpression;
import com.mysema.query.types.TemplateExpression;
import com.mysema.query.types.Visitor;

/**
 * RelationalPathExtractor extracts RelationlPath instances from expressions and queries
 *
 * @author tiwe
 *
 */
public final class RelationalPathExtractor implements Visitor>, Set>> {

    public static final RelationalPathExtractor DEFAULT = new RelationalPathExtractor();

    public static Set> extract(QueryMetadata md) {
        Set> known = ImmutableSet.of();
        known = DEFAULT.visitJoins(md.getJoins(), known);
        for (Expression p : md.getProjection()) {
            known = p.accept(DEFAULT, known);
        }
        for (OrderSpecifier o : md.getOrderBy()) {
            known = o.getTarget().accept(DEFAULT, known);
        }
        for (Expression g : md.getGroupBy()) {
            known = g.accept(DEFAULT, known);
        }
        if (md.getHaving() != null) {
            known = md.getHaving().accept(DEFAULT, known);
        }
        if (md.getWhere() != null) {
            known = md.getWhere().accept(DEFAULT, known);
        }
        return known;
    }

    public static Set> extract(Expression expr) {
        return expr.accept(DEFAULT, ImmutableSet.>of());
    }

    @Override
    public Set> visit(Constant expr, Set> known) {
        return known;
    }

    @Override
    public Set> visit(FactoryExpression expr, Set> known) {
        for (Expression arg : expr.getArgs()) {
            known = arg.accept(this, known);
        }
        return known;
    }

    @Override
    public Set> visit(Operation expr, Set> known) {
        for (Expression arg : expr.getArgs()) {
            known = arg.accept(this, known);
        }
        return known;
    }

    @Override
    public Set> visit(ParamExpression expr, Set> known) {
        return known;
    }

    @Override
    public Set> visit(Path expr, Set> known) {
        if (expr.getMetadata().isRoot()) {
            if (expr instanceof RelationalPath) {
                known = add(known, (RelationalPath)expr);
            }
        } else {
            known = expr.getMetadata().getParent().accept(this, known);
        }
        return known;
    }

    @Override
    public Set> visit(SubQueryExpression expr, Set> known) {
        Set> old = known;
        final QueryMetadata md = expr.getMetadata();
        known = visitJoins(md.getJoins(), known);
        for (Expression p : md.getProjection()) {
            known = p.accept(this, known);
        }
        for (OrderSpecifier o : md.getOrderBy()) {
            known = o.getTarget().accept(this, known);
        }
        for (Expression g : md.getGroupBy()) {
            known = g.accept(this, known);
        }
        if (md.getHaving() != null) {
            known = md.getHaving().accept(this, known);
        }
        if (md.getWhere() != null) {
            md.getWhere().accept(this, known);
        }
        return old;
    }


    @Override
    public Set> visit(TemplateExpression expr, Set> known) {
        for (Object arg : expr.getArgs()) {
            if (arg instanceof Expression) {
                known = ((Expression)arg).accept(this, known);
            }
        }
        return known;
    }

    private Set> visitJoins(Iterable joins, Set> known) {
        for (JoinExpression j : joins) {
            known = j.getTarget().accept(this, known);
            if (j.getCondition() != null) {
                known = j.getCondition().accept(this, known);
            }
        }
        return known;
    }

    private RelationalPathExtractor() {}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy