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

org.babyfish.jimmer.sql.ast.impl.LikePredicate Maven / Gradle / Ivy

There is a newer version: 0.8.180
Show newest version
package org.babyfish.jimmer.sql.ast.impl;

import org.babyfish.jimmer.sql.ast.LikeMode;
import org.babyfish.jimmer.sql.ast.Predicate;
import org.babyfish.jimmer.sql.ast.StringExpression;
import org.babyfish.jimmer.sql.ast.impl.render.AbstractSqlBuilder;
import org.babyfish.jimmer.sql.runtime.SqlBuilder;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

class LikePredicate extends AbstractPredicate {

    private StringExpression expression;

    private final String pattern;

    private final boolean insensitive;

    private final boolean negative;

    public static LikePredicate of(
            StringExpression expression,
            String pattern,
            boolean insensitive,
            LikeMode likeMode
    ) {
        if (pattern == null) {
            pattern = "";
        }
        if (!likeMode.isStartExact() && !pattern.startsWith("%")) {
            pattern = '%' + pattern;
        }
        if (!likeMode.isEndExact() && !pattern.endsWith("%")) {
            pattern = pattern + '%';
        }
        if (insensitive) {
            pattern = pattern.toLowerCase();
        }
        return new LikePredicate(expression, pattern, insensitive, false);
    }

    private LikePredicate(
            StringExpression expression,
            String pattern,
            boolean insensitive,
            boolean negative
    ) {
        this.expression = expression;
        this.pattern = pattern;
        this.insensitive = insensitive;
        this.negative = negative;
    }

    @Override
    public int precedence() {
        return 0;
    }

    @Override
    public Predicate not() {
        return new LikePredicate(
                expression,
                pattern,
                insensitive,
                !negative
        );
    }

    @Override
    public void accept(@NotNull AstVisitor visitor) {
        ((Ast)expression).accept(visitor);
    }

    @Override
    public void renderTo(@NotNull AbstractSqlBuilder builder) {
        if (pattern.equals("%")) {
            builder.sql("1 = 1");
        } else {
            boolean ignoreCaseLikeSupported = builder.sqlClient().getDialect().isIgnoreCaseLikeSupported();
            if (insensitive && !ignoreCaseLikeSupported) {
                builder.sql("lower(");
                renderChild((Ast) expression, builder);
                builder.sql(")");
            } else {
                renderChild((Ast) expression, builder);
            }
            if (insensitive && ignoreCaseLikeSupported) {
                builder.sql(negative ? " not ilike " : " ilike ");
            } else {
                builder.sql(negative ? " not like " : " like ");
            }
            builder.rawVariable(pattern);
        }
    }

    @Override
    protected boolean determineHasVirtualPredicate() {
        return hasVirtualPredicate(expression);
    }

    @Override
    protected Ast onResolveVirtualPredicate(AstContext ctx) {
        expression = ctx.resolveVirtualPredicate(expression);
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof LikePredicate)) return false;
        LikePredicate that = (LikePredicate) o;
        return insensitive == that.insensitive && negative == that.negative && expression.equals(that.expression) && pattern.equals(that.pattern);
    }

    @Override
    public int hashCode() {
        return Objects.hash(expression, pattern, insensitive, negative);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy