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

org.babyfish.jimmer.sql.ast.impl.BinaryExpression 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.Expression;
import org.babyfish.jimmer.sql.ast.impl.render.AbstractSqlBuilder;
import org.babyfish.jimmer.sql.ast.impl.render.BatchSqlBuilder;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

abstract class BinaryExpression> extends AbstractExpression implements NumericExpressionImplementor {

    private final Class type;
    
    private Expression left;

    private Expression right;

    protected BinaryExpression(Class type, Expression left, Expression right) {
        this.type = type;
        this.left = left;
        this.right = right;
    }

    @Override
    public Class getType() {
        return type;
    }

    protected abstract String operator();

    @Override
    public void accept(@NotNull AstVisitor visitor) {
        Ast.of(left).accept(visitor);
        Ast.of(right).accept(visitor);
    }

    @Override
    public void renderTo(@NotNull AbstractSqlBuilder builder) {
        renderChild(Ast.of(left), builder);
        builder.sql(" ");
        builder.sql(operator());
        builder.sql(" ");
        renderChild(Ast.of(right), builder);
    }

    @Override
    protected boolean determineHasVirtualPredicate() {
        return hasVirtualPredicate(left) || hasVirtualPredicate(right);
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BinaryExpression that = (BinaryExpression) o;
        return type.equals(that.type) && left.equals(that.left) && right.equals(that.right);
    }

    @Override
    public int hashCode() {
        return Objects.hash(type, left, right);
    }

    static class Plus> extends BinaryExpression {
        
        public Plus(Class type, Expression left, Expression right) {
            super(type, left, right);
        }

        @Override
        public int precedence() {
            return ExpressionPrecedences.PLUS;
        }

        @Override
        protected String operator() {
            return "+";
        }
    }

    static class Minus> extends BinaryExpression {

        public Minus(Class type, Expression left, Expression right) {
            super(type, left, right);
        }

        @Override
        public int precedence() {
            return ExpressionPrecedences.PLUS;
        }

        @Override
        protected String operator() {
            return "-";
        }
    }

    static class Times> extends BinaryExpression {

        public Times(Class type, Expression left, Expression right) {
            super(type, left, right);
        }

        @Override
        public int precedence() {
            return ExpressionPrecedences.TIMES;
        }

        @Override
        protected String operator() {
            return "*";
        }
    }

    static class Div> extends BinaryExpression {

        public Div(Class type, Expression left, Expression right) {
            super(type, left, right);
        }

        @Override
        public int precedence() {
            return ExpressionPrecedences.TIMES;
        }

        @Override
        protected String operator() {
            return "/";
        }
    }

    static class Rem> extends BinaryExpression {

        public Rem(Class type, Expression left, Expression right) {
            super(type, left, right);
        }

        @Override
        public int precedence() {
            return ExpressionPrecedences.TIMES;
        }

        @Override
        protected String operator() {
            return "%";
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy