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

com.rapleaf.jack.queries.ModelQuery Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
package com.rapleaf.jack.queries;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.google.common.base.Optional;

public class ModelQuery {

  private WhereClause whereClause;
  private List orderCriteria;
  private List selectedFields;
  private List groupByFields;
  private Optional limitCriterion;

  /**
   * This class should only be constructed in {@link AbstractQueryBuilder}.
   */
  ModelQuery() {
    this.whereClause = new WhereClause();
    this.orderCriteria = new ArrayList<>();
    this.selectedFields = new ArrayList<>();
    this.groupByFields = new ArrayList<>();
    //By default, no limit criteria
    this.limitCriterion = Optional.absent();
  }

  public List getSelectedFields() {
    return selectedFields;
  }

  public List getWhereConstraints() {
    return whereClause.getWhereConstraints();
  }

  public Optional> getIdSet() {
    return whereClause.getIdSet();
  }

  public List getOrderCriteria() {
    return orderCriteria;
  }

  public Optional getLimitCriterion() {
    return limitCriterion;
  }

  public void setLimitCriterion(LimitCriterion limitCriterion) {
    this.limitCriterion = Optional.of(limitCriterion);
  }

  public void addConstraint(WhereConstraint constraint) {
    whereClause.addConstraint(constraint);
  }

  public void addIds(Collection ids) {
    whereClause.addIds(ids);
  }

  public void addId(Long id) {
    whereClause.addId(id);
  }

  public void addOrder(OrderCriterion orderCriterion) {
    orderCriteria.add(orderCriterion);
  }

  public void addSelectedField(FieldSelector fields) {
    selectedFields.add(fields);
  }

  public void addSelectedFields(FieldSelector... fields) {
    selectedFields.addAll(Arrays.asList(fields));
  }

  public void addGroupByFields(Enum... fields) {
    groupByFields.addAll(Arrays.asList(fields));
  }

  public String getSelectClause() {
    StringBuilder sqlClause = new StringBuilder("SELECT ");

    if (selectedFields.isEmpty()) {
      sqlClause.append("*");
      return sqlClause.toString();
    }

    sqlClause.append("id, ");
    Iterator iterator = selectedFields.iterator();
    while (iterator.hasNext()) {
      FieldSelector selector = iterator.next();
      sqlClause.append(selector.getSqlClause());
      if (iterator.hasNext()) {
        sqlClause.append(", ");
      }
    }
    return sqlClause.toString();
  }

  public WhereClause getWhereClause() {
    return whereClause;
  }

  public String getGroupByClause() {
    StringBuilder sb = new StringBuilder();
    if (!groupByFields.isEmpty()) {
      sb.append("GROUP BY ");
      Iterator it = groupByFields.iterator();
      while (it.hasNext()) {
        sb.append(it.next());
        if (it.hasNext()) {
          sb.append(", ");
        }
      }
      sb.append(" ");
    }
    return sb.toString();
  }

  public String getOrderByClause() {
    StringBuilder sb = new StringBuilder();
    if (!orderCriteria.isEmpty()) {
      sb.append("ORDER BY ");
      Iterator it = orderCriteria.iterator();
      while (it.hasNext()) {
        OrderCriterion orderCriterion = it.next();
        sb.append(orderCriterion.getSqlStatement());
        if (it.hasNext()) {
          sb.append(", ");
        }
      }
      sb.append(" ");
    }
    return sb.toString();
  }

  public String getLimitClause() {
    if (limitCriterion.isPresent()) {
      return limitCriterion.get().getSqlStatement();
    }
    return "";
  }

  public boolean isOnlyIdQuery() {
    return whereClause.hasIdsOnly()
        && selectedFields.isEmpty()
        && orderCriteria.isEmpty()
        && !limitCriterion.isPresent();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy