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

de.gcoding.boot.database.expressions.matchagainst.LikeMatchAgainst Maven / Gradle / Ivy

package de.gcoding.boot.database.expressions.matchagainst;

import org.hibernate.dialect.Dialect;
import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.type.BasicType;

import java.util.List;

public class LikeMatchAgainst extends AbstractMatchAgainstDescriptor {

    LikeMatchAgainst(Dialect dialect, BasicType returnType) {
        super(dialect, returnType);
    }

    @Override
    public void render(SqlAppender sqlAppender, List sqlAstArguments, SqlAstTranslator walker) {
        final var pattern = sqlAstArguments.getFirst();

        sqlAppender.appendSql("(");
        for (var i = 1; i < sqlAstArguments.size(); i++) {
            if (i > 1) {
                sqlAppender.appendSql(" or ");
            }

            final var field = sqlAstArguments.get(i);
            renderSingleLike(sqlAppender, walker, field, pattern);
        }
        sqlAppender.appendSql(")");
    }

    private void renderSingleLike(SqlAppender sqlAppender, SqlAstTranslator walker, SqlAstNode field, SqlAstNode pattern) {
        if (dialect.supportsCaseInsensitiveLike()) {
            renderCaseInsensitiveLike(sqlAppender, walker, field, pattern);
        } else {
            renderCaseSensitiveLike(sqlAppender, walker, field, pattern);
        }

    }

    private void renderCaseInsensitiveLike(SqlAppender sqlAppender, SqlAstTranslator walker, SqlAstNode field, SqlAstNode pattern) {
        field.accept(walker);
        sqlAppender.appendSql(" " + dialect.getCaseInsensitiveLike() + " ");
        pattern.accept(walker);
    }

    private void renderCaseSensitiveLike(SqlAppender sqlAppender, SqlAstTranslator walker, SqlAstNode field, SqlAstNode pattern) {
        final var lower = dialect.getLowercaseFunction();

        sqlAppender.appendSql(lower);
        sqlAppender.appendSql("(");
        field.accept(walker);
        sqlAppender.appendSql(")");

        sqlAppender.appendSql(" like ");

        sqlAppender.appendSql(lower);
        sqlAppender.appendSql("(");
        pattern.accept(walker);
        sqlAppender.appendSql(")");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy