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

com.querydsl.sql.RelationalPathExtractor Maven / Gradle / Ivy

There is a newer version: 6.10.1
Show newest version
/*
 * Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
 *
 * 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.querydsl.sql;

import static com.querydsl.core.util.CollectionUtils.add;

import com.querydsl.core.JoinExpression;
import com.querydsl.core.QueryMetadata;
import com.querydsl.core.types.Constant;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.FactoryExpression;
import com.querydsl.core.types.Operation;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.ParamExpression;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.SubQueryExpression;
import com.querydsl.core.types.TemplateExpression;
import com.querydsl.core.types.Visitor;
import java.util.Collections;
import java.util.Set;

/**
 * {@code RelationalPathExtractor} extracts {@link RelationalPath} 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 = Collections.emptySet();
    known = DEFAULT.visitJoins(md.getJoins(), known);
    if (md.getProjection() != null) {
      known = md.getProjection().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, Collections.emptySet());
  }

  @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) {
    var old = known;
    final var md = expr.getMetadata();
    known = visitJoins(md.getJoins(), known);
    if (md.getProjection() != null) {
      known = md.getProjection().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 - 2025 Weber Informatics LLC | Privacy Policy