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

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

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

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.google.common.collect.Lists;

import com.rapleaf.jack.queries.where_operators.IWhereOperator;

public class GenericConstraint implements QueryCondition {
  private final Column column;
  private final IWhereOperator operator;
  private final List> chainedOrConstraints;

  public GenericConstraint(Column column, IWhereOperator operator) {
    this.column = column;
    this.operator = operator;
    this.chainedOrConstraints = Lists.newArrayList();
  }

  public GenericConstraint or(GenericConstraint constraint, GenericConstraint... otherConstraints) {
    List chainedAndConstraints = Lists.newArrayList(constraint);
    chainedAndConstraints.addAll(Arrays.asList(otherConstraints));
    chainedOrConstraints.add(chainedAndConstraints);
    return this;
  }

  @SuppressWarnings("unchecked")
  public List getParameters() {
    List parameters = Lists.newArrayList();
    parameters.addAll(operator.getParameters());
    for (List orConstraint : chainedOrConstraints) {
      for (GenericConstraint andConstraint : orConstraint) {
        parameters.addAll(andConstraint.getParameters());
      }
    }
    return parameters;
  }

  @Override
  public String getSqlStatement() {
    StringBuilder statement = new StringBuilder("(")
        .append(column.getSqlKeyword()).append(" ")
        .append(operator.getSqlStatement());

    for (List orConstraints : chainedOrConstraints) {
      statement.append(" OR ");

      Iterator it = orConstraints.iterator();
      while (it.hasNext()) {
        statement.append(it.next().getSqlStatement());
        if (it.hasNext()) {
          statement.append(" AND ");
        }
      }
    }

    return statement.append(")").toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy