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

org.babyfish.jimmer.sql.ast.impl.Constants 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.NumericExpression;
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;

public class Constants {

    public static > NumericExpression number(N value) {
        return new Num<>(value);
    }

    public static StringExpression string(String value) {
        return new Str(value);
    }

    private static class Num> extends AbstractExpression implements NumericExpressionImplementor {

        private N value;

        public Num(N value) {
            this.value = Objects.requireNonNull(value, "`value` cannot be null");
        }

        @SuppressWarnings("unchecked")
        @Override
        public Class getType() {
            return (Class)value.getClass();
        }

        @Override
        public void accept(@NotNull AstVisitor visitor) {
        }

        @Override
        public void renderTo(@NotNull AbstractSqlBuilder builder) {
            builder.sql(value.toString());
        }

        @Override
        protected boolean determineHasVirtualPredicate() {
            return false;
        }

        @Override
        protected Ast onResolveVirtualPredicate(AstContext ctx) {
            return this;
        }

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Num num = (Num) o;
            return value.equals(num.value);
        }

        @Override
        public int hashCode() {
            return Objects.hash(value);
        }
    }

    private static class Str extends AbstractExpression implements StringExpressionImplementor {

        private final String value;

        private Str(String value) {
            this.value = '\'' + value.replace("'", "''") + '\'';
        }

        @Override
        public void accept(@NotNull AstVisitor visitor) {}

        @Override
        public void renderTo(@NotNull AbstractSqlBuilder builder) {
            builder.sql(value);
        }

        @Override
        protected boolean determineHasVirtualPredicate() {
            return false;
        }

        @Override
        protected Ast onResolveVirtualPredicate(AstContext ctx) {
            return this;
        }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy