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

com.dmj.sqldsl.service.Wrapper Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
package com.dmj.sqldsl.service;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import com.dmj.sqldsl.builder.DslQueryBuilder;
import com.dmj.sqldsl.builder.SelectBuilder;
import com.dmj.sqldsl.builder.column.ColumnBuilder;
import com.dmj.sqldsl.builder.column.DateRange;
import com.dmj.sqldsl.builder.column.LikeValue;
import com.dmj.sqldsl.builder.column.NormalColumnsBuilder;
import com.dmj.sqldsl.builder.column.type.BooleanLambda;
import com.dmj.sqldsl.builder.column.type.ColumnLambda;
import com.dmj.sqldsl.builder.column.type.DateLambda;
import com.dmj.sqldsl.builder.column.type.LongLambda;
import com.dmj.sqldsl.builder.column.type.NumberLambda;
import com.dmj.sqldsl.builder.column.type.StringLambda;
import com.dmj.sqldsl.builder.condition.ConditionBuilders;
import com.dmj.sqldsl.builder.condition.ConditionsBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

public class Wrapper {
  private final ConditionsBuilder conditionsBuilder;
  private SelectBuilder selectBuilder;
  private final List> orderByConditions;
  private List> groupByLambdas;
  private Limit limit;
  private ConditionsBuilder havingConditionsBuilder;
  private final Class entityClass;


  static class OrderByCondition {
    private ColumnLambda lambda;
    private boolean isAsc;

    public ColumnLambda getLambda() {
      return this.lambda;
    }

    public boolean isAsc() {
      return this.isAsc;
    }

    public void setLambda(final ColumnLambda lambda) {
      this.lambda = lambda;
    }

    public void setAsc(final boolean isAsc) {
      this.isAsc = isAsc;
    }

    @Override
    public String toString() {
      return "Wrapper.OrderByCondition(lambda=" + this.getLambda() + ", isAsc=" + this.isAsc() + ")";
    }

    public OrderByCondition(final ColumnLambda lambda, final boolean isAsc) {
      this.lambda = lambda;
      this.isAsc = isAsc;
    }

    public OrderByCondition() {
    }

    @Override
    public boolean equals(final Object o) {
      if (o == this) return true;
      if (!(o instanceof Wrapper.OrderByCondition)) return false;
      final Wrapper.OrderByCondition other = (Wrapper.OrderByCondition) o;
      if (!other.canEqual((Object) this)) return false;
      if (this.isAsc() != other.isAsc()) return false;
      final Object this$lambda = this.getLambda();
      final Object other$lambda = other.getLambda();
      if (this$lambda == null ? other$lambda != null : !this$lambda.equals(other$lambda)) return false;
      return true;
    }

    protected boolean canEqual(final Object other) {
      return other instanceof Wrapper.OrderByCondition;
    }

    @Override
    public int hashCode() {
      final int PRIME = 59;
      int result = 1;
      result = result * PRIME + (this.isAsc() ? 79 : 97);
      final Object $lambda = this.getLambda();
      result = result * PRIME + ($lambda == null ? 43 : $lambda.hashCode());
      return result;
    }
  }


  static class Limit {
    private int offset;
    private int size;

    public int getOffset() {
      return this.offset;
    }

    public int getSize() {
      return this.size;
    }

    public void setOffset(final int offset) {
      this.offset = offset;
    }

    public void setSize(final int size) {
      this.size = size;
    }

    @Override
    public boolean equals(final Object o) {
      if (o == this) return true;
      if (!(o instanceof Wrapper.Limit)) return false;
      final Wrapper.Limit other = (Wrapper.Limit) o;
      if (!other.canEqual((Object) this)) return false;
      if (this.getOffset() != other.getOffset()) return false;
      if (this.getSize() != other.getSize()) return false;
      return true;
    }

    protected boolean canEqual(final Object other) {
      return other instanceof Wrapper.Limit;
    }

    @Override
    public int hashCode() {
      final int PRIME = 59;
      int result = 1;
      result = result * PRIME + this.getOffset();
      result = result * PRIME + this.getSize();
      return result;
    }

    @Override
    public String toString() {
      return "Wrapper.Limit(offset=" + this.getOffset() + ", size=" + this.getSize() + ")";
    }

    public Limit(final int offset, final int size) {
      this.offset = offset;
      this.size = size;
    }

    public Limit() {
    }
  }

  public Wrapper(Class targetClass) {
    this(new SelectBuilder(), targetClass);
  }

  protected Wrapper(SelectBuilder selectBuilder, Class targetClass) {
    this.entityClass = targetClass;
    this.conditionsBuilder = ConditionBuilders.empty();
    this.selectBuilder = selectBuilder;
    this.orderByConditions = new ArrayList<>();
    this.groupByLambdas = emptyList();
    this.havingConditionsBuilder = ConditionBuilders.empty();
  }

  public Wrapper eq(StringLambda lambda, String value) {
    this.conditionsBuilder.eq(lambda, value);
    return this;
  }

  public Wrapper eq(NumberLambda lambda, Number value) {
    this.conditionsBuilder.eq(lambda, value);
    return this;
  }

  public Wrapper eq(BooleanLambda lambda, Boolean value) {
    this.conditionsBuilder.eq(lambda, value);
    return this;
  }

  public Wrapper eq(ColumnBuilder columnBuilder, Boolean value) {
    this.conditionsBuilder.eq(columnBuilder, value);
    return this;
  }

  public Wrapper eq(ColumnBuilder columnBuilder, String value) {
    this.conditionsBuilder.eq(columnBuilder, value);
    return this;
  }

  public Wrapper eq(ColumnBuilder columnBuilder, Number value) {
    this.conditionsBuilder.eq(columnBuilder, value);
    return this;
  }

  public Wrapper ne(StringLambda lambda, String value) {
    this.conditionsBuilder.ne(lambda, value);
    return this;
  }

  public Wrapper ne(NumberLambda lambda, Number value) {
    this.conditionsBuilder.ne(lambda, value);
    return this;
  }

  public Wrapper ne(BooleanLambda lambda, Boolean value) {
    this.conditionsBuilder.ne(lambda, value);
    return this;
  }

  public Wrapper ne(ColumnBuilder columnBuilder, Boolean value) {
    this.conditionsBuilder.ne(columnBuilder, value);
    return this;
  }

  public Wrapper ne(ColumnBuilder columnBuilder, String value) {
    this.conditionsBuilder.ne(columnBuilder, value);
    return this;
  }

  public Wrapper ne(ColumnBuilder columnBuilder, Number value) {
    this.conditionsBuilder.ne(columnBuilder, value);
    return this;
  }

  public Wrapper gt(StringLambda lambda, String value) {
    this.conditionsBuilder.gt(lambda, value);
    return this;
  }

  public Wrapper gt(NumberLambda lambda, Number value) {
    this.conditionsBuilder.gt(lambda, value);
    return this;
  }

  public Wrapper gt(BooleanLambda lambda, Boolean value) {
    this.conditionsBuilder.gt(lambda, value);
    return this;
  }

  public Wrapper gt(ColumnBuilder columnBuilder, Boolean value) {
    this.conditionsBuilder.gt(columnBuilder, value);
    return this;
  }

  public Wrapper gt(ColumnBuilder columnBuilder, String value) {
    this.conditionsBuilder.gt(columnBuilder, value);
    return this;
  }

  public Wrapper gt(ColumnBuilder columnBuilder, Number value) {
    this.conditionsBuilder.gt(columnBuilder, value);
    return this;
  }

  public Wrapper ge(StringLambda lambda, String value) {
    this.conditionsBuilder.ge(lambda, value);
    return this;
  }

  public Wrapper ge(NumberLambda lambda, Number value) {
    this.conditionsBuilder.ge(lambda, value);
    return this;
  }

  public Wrapper ge(BooleanLambda lambda, Boolean value) {
    this.conditionsBuilder.ge(lambda, value);
    return this;
  }

  public Wrapper ge(ColumnBuilder columnBuilder, Boolean value) {
    this.conditionsBuilder.ge(columnBuilder, value);
    return this;
  }

  public Wrapper ge(ColumnBuilder columnBuilder, String value) {
    this.conditionsBuilder.ge(columnBuilder, value);
    return this;
  }

  public Wrapper ge(ColumnBuilder columnBuilder, Number value) {
    this.conditionsBuilder.ge(columnBuilder, value);
    return this;
  }

  public Wrapper lt(StringLambda lambda, String value) {
    this.conditionsBuilder.lt(lambda, value);
    return this;
  }

  public Wrapper lt(NumberLambda lambda, Number value) {
    this.conditionsBuilder.lt(lambda, value);
    return this;
  }

  public Wrapper lt(BooleanLambda lambda, Boolean value) {
    this.conditionsBuilder.lt(lambda, value);
    return this;
  }

  public Wrapper lt(ColumnBuilder columnBuilder, Boolean value) {
    this.conditionsBuilder.lt(columnBuilder, value);
    return this;
  }

  public Wrapper lt(ColumnBuilder columnBuilder, String value) {
    this.conditionsBuilder.lt(columnBuilder, value);
    return this;
  }

  public Wrapper lt(ColumnBuilder columnBuilder, Number value) {
    this.conditionsBuilder.lt(columnBuilder, value);
    return this;
  }

  public Wrapper le(StringLambda lambda, String value) {
    this.conditionsBuilder.le(lambda, value);
    return this;
  }

  public Wrapper le(NumberLambda lambda, Number value) {
    this.conditionsBuilder.le(lambda, value);
    return this;
  }

  public Wrapper le(BooleanLambda lambda, Boolean value) {
    this.conditionsBuilder.le(lambda, value);
    return this;
  }

  public Wrapper le(ColumnBuilder columnBuilder, Boolean value) {
    this.conditionsBuilder.le(columnBuilder, value);
    return this;
  }

  public Wrapper le(ColumnBuilder columnBuilder, String value) {
    this.conditionsBuilder.le(columnBuilder, value);
    return this;
  }

  public Wrapper le(ColumnBuilder columnBuilder, Number value) {
    this.conditionsBuilder.le(columnBuilder, value);
    return this;
  }

  public Wrapper in(NumberLambda lambda, Collection valueList) {
    this.conditionsBuilder.in(lambda, valueList);
    return this;
  }

  public Wrapper in(StringLambda lambda, Collection valueList) {
    this.conditionsBuilder.in(lambda, valueList);
    return this;
  }

  public Wrapper in(NumberLambda lambda, Number... valueList) {
    this.conditionsBuilder.in(lambda, valueList);
    return this;
  }

  public Wrapper in(StringLambda lambda, String... valueList) {
    this.conditionsBuilder.in(lambda, valueList);
    return this;
  }

  public Wrapper notIn(NumberLambda lambda, Collection valueList) {
    this.conditionsBuilder.notIn(lambda, valueList);
    return this;
  }

  public Wrapper notIn(StringLambda lambda, Collection valueList) {
    this.conditionsBuilder.notIn(lambda, valueList);
    return this;
  }

  public Wrapper notIn(NumberLambda lambda, Number... valueList) {
    this.conditionsBuilder.notIn(lambda, valueList);
    return this;
  }

  public Wrapper notIn(StringLambda lambda, String... valueList) {
    this.conditionsBuilder.notIn(lambda, valueList);
    return this;
  }

  public Wrapper like(StringLambda lambda, LikeValue likeValue) {
    this.conditionsBuilder.like(lambda, likeValue);
    return this;
  }

  public Wrapper like(ColumnBuilder lambda, LikeValue likeValue) {
    this.conditionsBuilder.like(lambda, likeValue);
    return this;
  }

  public Wrapper notLike(StringLambda lambda, LikeValue likeValue) {
    this.conditionsBuilder.notLike(lambda, likeValue);
    return this;
  }

  public Wrapper notLike(ColumnBuilder lambda, LikeValue likeValue) {
    this.conditionsBuilder.notLike(lambda, likeValue);
    return this;
  }

  public Wrapper between(DateLambda lambda, DateRange range) {
    this.conditionsBuilder.between(lambda, range);
    return this;
  }

  public Wrapper and() {
    this.conditionsBuilder.and();
    return this;
  }

  public Wrapper and(Consumer> consumer) {
    Wrapper wrapper = new Wrapper<>(selectBuilder, entityClass);
    consumer.accept(wrapper);
    this.conditionsBuilder.and(wrapper.getConditionsBuilder());
    return this;
  }

  public Wrapper or() {
    this.conditionsBuilder.or();
    return this;
  }

  public Wrapper or(Consumer> consumer) {
    Wrapper wrapper = new Wrapper<>(selectBuilder, entityClass);
    consumer.accept(wrapper);
    this.conditionsBuilder.or(wrapper.getConditionsBuilder());
    return this;
  }

  public final  Wrapper orderBy(ColumnLambda lambda, boolean isAsc) {
    this.orderByConditions.add(new OrderByCondition<>(lambda, isAsc));
    return this;
  }

  @SafeVarargs
  public final  Wrapper groupBy(ColumnLambda... lambdas) {
    this.groupByLambdas = Arrays.asList(lambdas);
    return this;
  }

  public Wrapper having(Consumer> wrapperConsumer) {
    Wrapper wrapper = new Wrapper<>(entityClass);
    wrapperConsumer.accept(wrapper);
    this.havingConditionsBuilder = wrapper.getConditionsBuilder();
    return this;
  }

  public Wrapper selectAs(ColumnBuilder columnBuilder, StringLambda alias) {
    this.selectBuilder.selectAs(columnBuilder, alias);
    return this;
  }

  public Wrapper selectAs(ColumnBuilder columnBuilder, LongLambda alias) {
    this.selectBuilder.selectAs(columnBuilder, alias);
    return this;
  }

  public Wrapper selectAs(ColumnBuilder columnBuilder, NumberLambda alias) {
    this.selectBuilder.selectAs(columnBuilder, alias);
    return this;
  }

  public Wrapper selectAs(ColumnBuilder columnBuilder, BooleanLambda alias) {
    this.selectBuilder.selectAs(columnBuilder, alias);
    return this;
  }

  public Wrapper limit(int size) {
    this.limit = new Limit(0, size);
    return this;
  }

  private ConditionsBuilder getConditionsBuilder() {
    return this.conditionsBuilder;
  }

  protected Class getEntityClass() {
    return this.entityClass;
  }

  protected DslQueryBuilder toQueryBuilder() {
    if (!groupByLambdas.isEmpty()) {
      List> columnBuilders = groupByLambdas.stream().map(ColumnLambda::getColumnBuilder).collect(toList());
      this.selectBuilder.add(new SelectBuilder(new NormalColumnsBuilder(columnBuilders)));
    }
    if (this.selectBuilder.isEmpty()) {
      this.selectBuilder = new SelectBuilder().selectAll(entityClass);
    }
    DslQueryBuilder from = this.selectBuilder.from(entityClass);
    DslQueryBuilder queryBuilder = from.where(conditionsBuilder).groupBy(groupByLambdas).having(havingConditionsBuilder);
    orderByConditions.forEach(c -> queryBuilder.orderBy(c.getLambda(), c.isAsc()));
    if (this.limit != null) {
      return queryBuilder.limit(this.limit.getOffset(), this.limit.getSize());
    }
    return queryBuilder;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy