示例#1
0
 /**
  * Returns the topmost condition, never {@code null}.
  *
  * @return the topmost condition following up the parent chain or {@code this} if parent is {@code
  *     null}.
  */
 public BaseCondition getRoot() {
   BaseCondition p = this;
   while (p.getParent() != null) {
     p = p.getParent();
   }
   return p;
 }
  @Override
  public <T extends Query> String visit(BaseQueryBuilder<T> baseQueryBuilder) {
    StringBuilder sb = new StringBuilder();

    if (baseQueryBuilder.getProjection() != null && baseQueryBuilder.getProjection().length != 0) {
      sb.append("SELECT ");
      boolean isFirst = true;
      for (String projection : baseQueryBuilder.getProjection()) {
        if (isFirst) {
          isFirst = false;
        } else {
          sb.append(", ");
        }
        sb.append(alias).append('.').append(projection);
      }
      sb.append(' ');
    }

    sb.append("FROM ")
        .append(renderEntityName(baseQueryBuilder.getRootTypeName()))
        .append(' ')
        .append(alias);

    if (baseQueryBuilder.getFilterCondition() != null) {
      BaseCondition baseCondition = baseQueryBuilder.getFilterCondition().getRoot();
      String whereCondition = baseCondition.accept(this);
      if (!whereCondition.isEmpty()) {
        sb.append(" WHERE ").append(whereCondition);
      }
    }

    if (baseQueryBuilder.getSortCriteria() != null
        && !baseQueryBuilder.getSortCriteria().isEmpty()) {
      sb.append(" ORDER BY ");
      boolean isFirst = true;
      for (SortCriteria sortCriteria : baseQueryBuilder.getSortCriteria()) {
        if (isFirst) {
          isFirst = false;
        } else {
          sb.append(", ");
        }
        sb.append(alias)
            .append('.')
            .append(sortCriteria.getAttributePath())
            .append(' ')
            .append(sortCriteria.getSortOrder().name());
      }
    }

    return sb.toString();
  }
示例#3
0
  private void combine(boolean isConjunction, FilterConditionContext fcc) {
    BaseCondition rightCondition = ((BaseCondition) fcc).getRoot();

    if (isConjunction && parent instanceof OrCondition) {
      BooleanCondition p = new AndCondition(this, rightCondition);
      ((BooleanCondition) parent).replaceChildCondition(this, p);
      parent = p;
      rightCondition.setParent(p);
    } else {
      BaseCondition root = getRoot();
      BooleanCondition p =
          isConjunction
              ? new AndCondition(root, rightCondition)
              : new OrCondition(root, rightCondition);
      root.setParent(p);
      rightCondition.setParent(p);
    }

    rightCondition.setQueryBuilder(queryBuilder);
  }