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

ph.com.nightowlstudios.persistence.query.JoinSelectQueryBuilder Maven / Gradle / Ivy

There is a newer version: 6.21.1
Show newest version
package ph.com.nightowlstudios.persistence.query;

import org.apache.commons.lang3.StringUtils;
import ph.com.nightowlstudios.entity.Entity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author Joseph Harvey Angeles - yev
 * @since 5/14/21
 **/
public class JoinSelectQueryBuilder {

  private final String tableName;
  private List columns;

  private String whereColumn;
  private String whereOp;
  private Object whereValue;

  private List logicals;

   JoinSelectQueryBuilder(Class fromTable) {
    this(Entity.getTableName(fromTable));
  }

  JoinSelectQueryBuilder(String fromTable) {
    this.tableName = fromTable;
  }

  public JoinSelectQueryBuilder column(String column) {
    if (this.columns == null) {
      this.columns = new ArrayList<>();
    }
    this.columns.add(column);
    return this;
  }

  public JoinSelectQueryBuilder columns(String... columns) {
    if (this.columns == null) {
      this.columns = new ArrayList<>();
    }
    this.columns.addAll(Arrays.asList(columns));
    return this;
  }

  public JoinSelectQueryBuilder where(String column, Object value) {
    this.whereColumn = column;
    this.whereOp = "=";
    this.whereValue = value.toString();
    return this;
  }

  public JoinSelectQueryBuilder where(String column, String op, Object value) {
    this.whereColumn = column;
    this.whereOp = op;
    this.whereValue = value.toString();
    return this;
  }

  public JoinSelectQueryBuilder and(String column, Object value) {
    return and(column, "=", value);
  }

  public JoinSelectQueryBuilder and(String column, String op, Object value) {
    return addLogical("AND", column, op, value);
  }

  public JoinSelectQueryBuilder or(String column, Object value) {
    return or(column, "=", value);
  }

  public JoinSelectQueryBuilder or(String column, String op, Object value) {
    return addLogical("OR", column, op, value);
  }

  private JoinSelectQueryBuilder addLogical(String logicOp, String column, String op, Object value) {
    if (logicals == null) {
      logicals = new ArrayList<>();
    }
    logicals.add(String.format(
      "%s %s %s '%s'",
      logicOp, column, op, value.toString()).trim());
    return this;
  }

  String build() {
    StringBuilder query = new StringBuilder();
    query.append("SELECT ")
      .append(StringUtils.join(this.columns, QueryBuilder.COMMA))
      .append(String.format(" FROM %s", tableName));


    if (StringUtils.isNotEmpty(whereColumn)) {
      query.append(String.format(" WHERE %s %s '%s'", whereColumn, whereOp, whereValue));
      if (logicals != null) {
        this.logicals.forEach(line -> query
          .append(QueryBuilder.WHITESPACE)
          .append(line)
          .append(QueryBuilder.WHITESPACE)
        );
      }
    }

    String[] groupColumns = this.columns
      .stream()                 // include only plain column
      .filter(column -> !column.contains("("))
      .map(column -> {
        String[] tokens = column.split(QueryBuilder.WHITESPACE);
        if (tokens.length >= 1) {
          return tokens[0].trim();
        }
        return column.trim();
      })
      .toArray(String[]::new);

    return query
      .append(" GROUP BY ")
      .append(StringUtils.join(groupColumns, QueryBuilder.COMMA))
      .toString().trim();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy