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

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

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

import org.babyfish.jimmer.sql.ast.impl.query.MutableStatementImplementor;
import org.babyfish.jimmer.sql.ast.impl.render.AbstractSqlBuilder;

import java.util.Collection;

public abstract class AbstractExpression implements ExpressionImplementor, Ast {

    private boolean isLowestPrecedenceUsing = false;

    private Boolean hasVirtualPredicate;

    protected final void renderChild(Ast ast, AbstractSqlBuilder builder) {
        if (isLowestPrecedenceUsing || !(
                ast instanceof ExpressionImplementor) ||
                ((ExpressionImplementor)ast).precedence() <= precedence()) {
            ast.renderTo(builder);
        } else {
            builder.sql("(").space('\n');
            ast.renderTo(builder);
            builder.space('\n').sql(")");
        }
    }

    protected final void usingLowestPrecedence(Runnable block) {
        if (isLowestPrecedenceUsing) {
            block.run();
        } else {
            isLowestPrecedenceUsing = true;
            try {
                block.run();
            } finally {
                isLowestPrecedenceUsing = false;
            }
        }
    }

    @Override
    public final boolean hasVirtualPredicate() {
        Boolean has = hasVirtualPredicate;
        if (has == null) {
            hasVirtualPredicate = has = determineHasVirtualPredicate();
        }
        return has;
    }

    protected abstract boolean determineHasVirtualPredicate();

    @Override
    public final Ast resolveVirtualPredicate(AstContext ctx) {
        if (!hasVirtualPredicate()) {
            return this;
        }
        return onResolveVirtualPredicate(ctx);
    }

    protected abstract Ast onResolveVirtualPredicate(AstContext ctx);

    protected static boolean hasVirtualPredicate(Object expression) {
        if (expression instanceof Ast && ((Ast) expression).hasVirtualPredicate()) {
            return true;
        }
        return expression instanceof MutableStatementImplementor &&
                ((MutableStatementImplementor) expression).hasVirtualPredicate();
    }

    protected static boolean hasVirtualPredicate(Collection expressions) {
        for (Object expression : expressions) {
            if (hasVirtualPredicate(expression)) {
                return true;
            }
        }
        return false;
    }

    protected static  boolean hasVirtualPredicate(T[] expressions) {
        for (T expression : expressions) {
            if (hasVirtualPredicate(expression)) {
                return true;
            }
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy