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

com.alilitech.mybatis.jpa.criteria.CriteriaBuilder Maven / Gradle / Ivy

The newest version!
/*
 *    Copyright 2017-2022 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.alilitech.mybatis.jpa.criteria;

import com.alilitech.mybatis.jpa.criteria.expression.*;
import com.alilitech.mybatis.jpa.criteria.expression.operator.OperatorExpression;
import com.alilitech.mybatis.jpa.criteria.expression.operator.comparison.*;
import com.alilitech.mybatis.jpa.criteria.expression.operator.like.*;
import com.alilitech.mybatis.jpa.definition.MethodDefinition;
import com.alilitech.mybatis.jpa.domain.Direction;
import org.apache.ibatis.reflection.property.PropertyNamer;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Zhou Xiaoxiang
 * @since 1.1
 */
public class CriteriaBuilder {

    private final Class domainClass;

    private MethodDefinition methodDefinition;

    public CriteriaBuilder(Class domainClass, MethodDefinition methodDefinition) {
        this.domainClass = domainClass;
        this.methodDefinition = methodDefinition;
    }

    public PredicateExpression and(PredicateExpression ...predicates) {
        return new CompoundPredicateExpression<>(PredicateExpression.BooleanOperator.AND, predicates);
    }

    public PredicateExpression or(PredicateExpression ...predicates) {
        return new CompoundPredicateExpression<>(PredicateExpression.BooleanOperator.OR, predicates);
    }

    @SuppressWarnings("java:S1221")
    public PredicateExpression equal(String property, Object value) {
        return this.buildPredicate(property, new EqualExpression<>(), value);
    }

    public  PredicateExpression equal(SerializableFunction propertyFunction, Object value) {
        return this.equal(getProperty(propertyFunction), value);
    }

    public PredicateExpression notEqual(String property, Object value) {
        return this.buildPredicate(property, new NotEqualExpression<>(), value);
    }

    public  PredicateExpression notEqual(SerializableFunction propertyFunction, Object value) {
        return this.notEqual(getProperty(propertyFunction), value);
    }

    public PredicateExpression greaterThan(String property, Object value) {
        return this.buildPredicate(property, new GreaterThanExpression<>(), value);
    }

    public  PredicateExpression greaterThan(SerializableFunction propertyFunction, Object value) {
        return this.greaterThan(getProperty(propertyFunction), value);
    }

    public PredicateExpression greaterThanEqual(String property, Object value) {
        return this.buildPredicate(property, new GreaterThanEqualExpression<>(), value);
    }

    public  PredicateExpression greaterThanEqual(SerializableFunction propertyFunction, Object value) {
        return this.greaterThanEqual(getProperty(propertyFunction), value);
    }

    public PredicateExpression lessThan(String property, Object value) {
        return this.buildPredicate(property, new LessThanExpression<>(), value);
    }

    public  PredicateExpression lessThan(SerializableFunction propertyFunction, Object value) {
        return this.lessThan(getProperty(propertyFunction), value);
    }

    public PredicateExpression lessThanEqual(String property, Object value) {
        return this.buildPredicate(property, new LessThanEqualExpression<>(), value);
    }

    public  PredicateExpression lessThanEqual(SerializableFunction propertyFunction, Object value) {
        return this.lessThanEqual(getProperty(propertyFunction), value);
    }

    public PredicateExpression isNull(String property) {
        return this.buildPredicate(property, new IsNullExpression<>());
    }

    public  PredicateExpression isNull(SerializableFunction propertyFunction) {
        return this.isNull(getProperty(propertyFunction));
    }

    public PredicateExpression isNotNull(String property) {
        return this.buildPredicate(property, new IsNotNullExpression<>());
    }

    public  PredicateExpression isNotNull(SerializableFunction propertyFunction) {
        return this.isNotNull(getProperty(propertyFunction));
    }

    public PredicateExpression between(String property, Object value1, Object value2) {
        return this.buildPredicate(property, new BetweenExpression<>(), value1, value2);
    }

    public  PredicateExpression between(SerializableFunction propertyFunction, Object value1, Object value2) {
        return this.between(getProperty(propertyFunction), value1, value2);
    }

    public PredicateExpression notBetween(String property, Object value1, Object value2) {
        return this.buildPredicate(property, new NotBetweenExpression<>(), value1, value2);
    }

    public  PredicateExpression notBetween(SerializableFunction propertyFunction, Object value1, Object value2) {
        return this.notBetween(getProperty(propertyFunction), value1, value2);
    }

    public PredicateExpression in(String property, Object ...values) {
        return this.buildPredicate(property, new InExpression<>(), values);
    }

    public  PredicateExpression in(SerializableFunction propertyFunction, Object ...values) {
        return this.in(getProperty(propertyFunction), values);
    }

    public PredicateExpression in(String property, Collection values) {
        return this.buildPredicate(property, new InExpression<>(), values.toArray());
    }

    public  PredicateExpression in(SerializableFunction propertyFunction, Collection values) {
        return this.in(getProperty(propertyFunction), values);
    }

    public PredicateExpression freeLike(String property, Object value) {
        return this.buildPredicate(property, new FreeLikeExpression<>(), value);
    }

    public  PredicateExpression freeLike(SerializableFunction propertyFunction, Object value) {
        return this.freeLike(getProperty(propertyFunction), value);
    }

    public PredicateExpression notFreeLike(String property, Object value) {
        return this.buildPredicate(property, new NotFreeLikeExpression<>(), value);
    }

    public  PredicateExpression notFreeLike(SerializableFunction propertyFunction, Object value) {
        return this.notFreeLike(getProperty(propertyFunction), value);
    }

    public PredicateExpression like(String property, Object value) {
        return this.buildPredicate(property, new LikeExpression<>(), value);
    }

    public  PredicateExpression like(SerializableFunction propertyFunction, Object value) {
        return this.like(getProperty(propertyFunction), value);
    }

    public PredicateExpression notLike(String property, Object value) {
        return this.buildPredicate(property, new NotLikeExpression<>(), value);
    }

    public  PredicateExpression notLike(SerializableFunction propertyFunction, Object value) {
        return this.notLike(getProperty(propertyFunction), value);
    }

    public PredicateExpression startsWith(String property, Object value) {
        return this.buildPredicate(property, new StartsWithExpression<>(), value);
    }

    public  PredicateExpression startsWith(SerializableFunction propertyFunction, Object value) {
        return this.startsWith(getProperty(propertyFunction), value);
    }

    public PredicateExpression endsWith(String property, Object value) {
        return this.buildPredicate(property, new EndsWithExpression<>(), value);
    }

    public  PredicateExpression endsWith(SerializableFunction propertyFunction, Object value) {
        return this.endsWith(getProperty(propertyFunction), value);
    }

    private PredicateExpression buildPredicate(String property, OperatorExpression operator, Object ...values) {
        VariableExpression variable = new VariableExpression<>(domainClass, property, methodDefinition);
        if(values != null && values.length > 0) {
            List> parameters = Arrays.stream(values).map((Function>) ParameterExpression::new).collect(Collectors.toList());
            return new SinglePredicateExpression<>(variable, operator, parameters);
        } else {
            return new SinglePredicateExpression<>(variable, operator);
        }
    }

    private String getProperty(SerializableFunction function) {
        return PropertyNamer.methodToProperty(function.getImplMethodName());
    }

    public OrderExpression desc(String property) {
        VariableExpression variable = new VariableExpression<>(domainClass, property, methodDefinition);
        return new OrderExpression<>(variable, Direction.DESC);
    }

    public  OrderExpression desc(SerializableFunction propertyFunction) {
        VariableExpression variable = new VariableExpression<>(domainClass, getProperty(propertyFunction), methodDefinition);
        return new OrderExpression<>(variable, Direction.DESC);
    }

    public OrderExpression asc(String property) {
        VariableExpression variable = new VariableExpression<>(domainClass, property, methodDefinition);
        return new OrderExpression<>(variable, Direction.ASC);
    }

    public  OrderExpression asc(SerializableFunction propertyFunction) {
        VariableExpression variable = new VariableExpression<>(domainClass, getProperty(propertyFunction), methodDefinition);
        return new OrderExpression<>(variable, Direction.ASC);
    }

    public SetExpression set(String property, Object value) {
        return new SetExpression<>(new VariableExpression<>(domainClass, property, methodDefinition), new ParameterExpression<>(value));
    }

    public  SetExpression set(SerializableFunction propertyFunction, Object value) {
        return new SetExpression<>(new VariableExpression<>(domainClass, getProperty(propertyFunction), methodDefinition), new ParameterExpression<>(value));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy