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

com.github.wz2cool.dynamic.builder.DynamicQueryBuilder Maven / Gradle / Ivy

There is a newer version: 3.2.32
Show newest version
package com.github.wz2cool.dynamic.builder;

import com.github.wz2cool.dynamic.DynamicQuery;
import com.github.wz2cool.dynamic.builder.direction.ISortDirection;
import com.github.wz2cool.dynamic.builder.opeartor.IFilterOperator;
import com.github.wz2cool.dynamic.lambda.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author Frank
 */
public class DynamicQueryBuilder implements IDynamicQueryBuilder {

    private Class entityClass;
    private SelectClauseBuilder selectClauseBuilder;
    private WhereClauseBuilder whereClauseBuilder;
    private OrderByClauseBuilder orderByClauseBuilder;

    void setSelectClauseBuilder(SelectClauseBuilder selectClauseBuilder) {
        this.selectClauseBuilder = selectClauseBuilder;
    }

    void setWhereClauseBuilder(WhereClauseBuilder whereClauseBuilder) {
        this.whereClauseBuilder = whereClauseBuilder;
    }

    void setOrderByClauseBuilder(OrderByClauseBuilder orderByClauseBuilder) {
        this.orderByClauseBuilder = orderByClauseBuilder;
    }

    public final SelectClauseBuilder selectAll() {
        return new SelectClauseBuilder<>(this);
    }

    private DynamicQueryBuilder(Class entityClass) {
        this.entityClass = entityClass;
    }

    public static  DynamicQueryBuilder create(Class entityClass) {
        return new DynamicQueryBuilder<>(entityClass);
    }

    @SafeVarargs
    public final SelectClauseBuilder select(GetCommonPropertyFunction... getPropertyFunctions) {
        return new SelectClauseBuilder<>(this, getPropertyFunctions);
    }

    @SafeVarargs
    public final WhereClauseBuilder where(
            GetBigDecimalPropertyFunction getPropertyFunction, IFilterOperator filterOperator,
            ConditionClauseBuilder... conditionClauseBuilders) {
        return new WhereClauseBuilder<>(this, getPropertyFunction, filterOperator, conditionClauseBuilders);
    }

    @SafeVarargs
    public final WhereClauseBuilder where(
            GetBytePropertyFunction getPropertyFunction, IFilterOperator filterOperator,
            ConditionClauseBuilder... conditionClauseBuilders) {
        return new WhereClauseBuilder<>(this, getPropertyFunction, filterOperator, conditionClauseBuilders);
    }

    @SafeVarargs
    public final WhereClauseBuilder where(
            GetDatePropertyFunction getPropertyFunction, IFilterOperator filterOperator,
            ConditionClauseBuilder... conditionClauseBuilders) {
        return new WhereClauseBuilder<>(this, getPropertyFunction, filterOperator, conditionClauseBuilders);
    }

    @SafeVarargs
    public final WhereClauseBuilder where(
            GetDoublePropertyFunction getPropertyFunction, IFilterOperator filterOperator,
            ConditionClauseBuilder... conditionClauseBuilders) {
        return new WhereClauseBuilder<>(this, getPropertyFunction, filterOperator, conditionClauseBuilders);
    }

    @SafeVarargs
    public final WhereClauseBuilder where(
            GetFloatPropertyFunction getPropertyFunction, IFilterOperator filterOperator,
            ConditionClauseBuilder... conditionClauseBuilders) {
        return new WhereClauseBuilder<>(this, getPropertyFunction, filterOperator, conditionClauseBuilders);
    }

    @SafeVarargs
    public final WhereClauseBuilder where(
            GetIntegerPropertyFunction getPropertyFunction, IFilterOperator filterOperator,
            ConditionClauseBuilder... conditionClauseBuilders) {
        return new WhereClauseBuilder<>(this, getPropertyFunction, filterOperator, conditionClauseBuilders);
    }

    @SafeVarargs
    public final WhereClauseBuilder where(
            GetLongPropertyFunction getPropertyFunction, IFilterOperator filterOperator,
            ConditionClauseBuilder... conditionClauseBuilders) {
        return new WhereClauseBuilder<>(this, getPropertyFunction, filterOperator, conditionClauseBuilders);
    }

    @SafeVarargs
    public final WhereClauseBuilder where(
            GetShortPropertyFunction getPropertyFunction, IFilterOperator filterOperator,
            ConditionClauseBuilder... conditionClauseBuilders) {
        return new WhereClauseBuilder<>(this, getPropertyFunction, filterOperator, conditionClauseBuilders);
    }

    @SafeVarargs
    public final WhereClauseBuilder where(
            GetStringPropertyFunction getPropertyFunction, IFilterOperator filterOperator,
            ConditionClauseBuilder... conditionClauseBuilders) {
        return new WhereClauseBuilder<>(this, getPropertyFunction, filterOperator, conditionClauseBuilders);
    }

    public OrderByClauseBuilder orderBy(GetCommonPropertyFunction getPropertyFunction) {
        return new OrderByClauseBuilder<>(this, getPropertyFunction);
    }

    public OrderByClauseBuilder orderBy(GetCommonPropertyFunction getPropertyFunction, ISortDirection direction) {
        return new OrderByClauseBuilder<>(this, getPropertyFunction, direction);
    }


    @Override
    public DynamicQuery build() {
        DynamicQuery dynamicQuery = DynamicQuery.createQuery(this.entityClass);
        if (this.selectClauseBuilder != null) {
            dynamicQuery.setSelectedProperties(this.selectClauseBuilder.getSelectedProperties());
        }

        if (this.whereClauseBuilder != null) {
            dynamicQuery.setFilters(this.whereClauseBuilder.getFilters());
        }

        if (this.orderByClauseBuilder != null) {
            dynamicQuery.setSorts(this.orderByClauseBuilder.getSorts());
        }
        return dynamicQuery;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy