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

com.dream.flex.def.ColumnDef Maven / Gradle / Ivy

The newest version!
package com.dream.flex.def;

import com.dream.antlr.smt.*;
import com.dream.flex.function.LazyFunctionStatement;
import com.dream.struct.invoker.TakeMarkInvokerStatement;

import java.util.Arrays;
import java.util.Collection;
import java.util.function.Predicate;

public class ColumnDef {
    private Statement statement;
    private String column;
    private String alias;

    public ColumnDef(Statement statement) {
        this(statement, null);
    }

    public ColumnDef(Statement statement, String column) {
        this.statement = statement;
        this.column = column;
    }

    public ColumnDef(TableDef tableDef, String column) {
        Statement statement = new SymbolStatement.SingleMarkStatement(column);
        this.column = column;
        if (tableDef != null) {
            ListColumnStatement listColumnStatement = new ListColumnStatement(".");
            listColumnStatement.add(new LazyFunctionStatement(() -> {
                AliasStatement tableStatement = tableDef.getStatement();
                Statement aliasStatement = tableStatement.getAlias();
                if (aliasStatement == null) {
                    aliasStatement = tableStatement.getColumn();
                }
                return aliasStatement;
            }));
            listColumnStatement.add(statement);
            statement = listColumnStatement;
        }
        this.statement = statement;
    }

    public Statement getStatement() {
        if (alias != null && !alias.isEmpty()) {
            AliasStatement aliasStatement = new AliasStatement();
            aliasStatement.setColumn(statement);
            aliasStatement.setAlias(new SymbolStatement.SingleMarkStatement(alias));
            return aliasStatement;
        } else {
            return statement;
        }
    }

    public ColumnDef as(String alias) {
        ColumnDef columnDef = new ColumnDef(statement);
        columnDef.alias = alias;
        return columnDef;
    }

    public ConditionDef eq(Object value) {
        return conditionDef(new OperStatement.EQStatement(), value);
    }

    public ConditionDef eq(ColumnDef columnDef) {
        return conditionDef(new OperStatement.EQStatement(), columnDef.getStatement());
    }

    public  ConditionDef eq(T value, Predicate fn) {
        return eq(value).when(fn.test(value));
    }

    public ConditionDef neq(Object value) {
        return conditionDef(new OperStatement.NEQStatement(), value);
    }

    public  ConditionDef neq(T value, Predicate fn) {
        return neq(value).when(fn.test(value));
    }

    public ConditionDef leq(Object value) {
        return conditionDef(new OperStatement.LEQStatement(), value);
    }

    public  ConditionDef leq(T value, Predicate fn) {
        return leq(value).when(fn.test(value));
    }

    public ConditionDef lt(Object value) {
        return conditionDef(new OperStatement.LTStatement(), value);
    }

    public  ConditionDef lt(T value, Predicate fn) {
        return lt(value).when(fn.test(value));
    }

    public ConditionDef geq(Object value) {
        return conditionDef(new OperStatement.GEQStatement(), value);
    }

    public  ConditionDef geq(T value, Predicate fn) {
        return geq(value).when(fn.test(value));
    }

    public ConditionDef gt(Object value) {
        return conditionDef(new OperStatement.GTStatement(), value);
    }

    public  ConditionDef gt(T value, Predicate fn) {
        return gt(value).when(fn.test(value));
    }

    public ConditionDef in(Object... values) {
        return in(Arrays.asList(values));
    }

    public ConditionDef in(Object[] values, Predicate fn) {
        return in(values).when(fn.test(values));
    }

    public ConditionDef in(Collection values) {
        ListColumnStatement listColumnStatement = new ListColumnStatement(",");
        listColumnStatement.add(values.stream().map(value -> new TakeMarkInvokerStatement(this.column(), value)).toArray(Statement[]::new));
        BraceStatement braceStatement = new BraceStatement(listColumnStatement);
        return conditionDef(new OperStatement.INStatement(), braceStatement);
    }

    public ConditionDef in(Collection values, Predicate> fn) {
        return in(values).when(fn.test(values));
    }

    public ConditionDef in(QueryDef queryDef) {
        BraceStatement braceStatement = new BraceStatement(queryDef.statement());
        return conditionDef(new OperStatement.INStatement(), braceStatement);
    }

    public ConditionDef notIn(Object... values) {
        return not(in(Arrays.asList(values)));
    }

    public ConditionDef notIn(Object[] values, Predicate fn) {
        return notIn(values).when(fn.test(values));
    }

    public ConditionDef notIn(Collection values) {
        return not(in(values));
    }

    public ConditionDef notIn(Collection values, Predicate> fn) {
        return not(in(values)).when(fn.test(values));
    }

    public ConditionDef notIn(QueryDef queryDef) {
        return not(in(queryDef));
    }

    public ConditionDef like(Object value) {
        FunctionStatement.ConcatStatement concatStatement = new FunctionStatement.ConcatStatement();
        ListColumnStatement listColumnStatement = new ListColumnStatement(",");
        listColumnStatement.add(new Statement[]{new SymbolStatement.StrStatement("%"), new TakeMarkInvokerStatement(this.column(), value), new SymbolStatement.StrStatement("%")});
        concatStatement.setParamsStatement(listColumnStatement);
        return conditionDef(new OperStatement.LIKEStatement(), concatStatement);
    }

    public  ConditionDef like(T value, Predicate fn) {
        return like(value).when(fn.test(value));
    }

    public ConditionDef likeLeft(Object value) {
        FunctionStatement.ConcatStatement concatStatement = new FunctionStatement.ConcatStatement();
        ListColumnStatement listColumnStatement = new ListColumnStatement(",");
        listColumnStatement.add(new Statement[]{new TakeMarkInvokerStatement(this.column(), value), new SymbolStatement.StrStatement("%")});
        concatStatement.setParamsStatement(listColumnStatement);
        return conditionDef(new OperStatement.LIKEStatement(), concatStatement);
    }

    public  ConditionDef likeLeft(T value, Predicate fn) {
        return likeLeft(value).when(fn.test(value));
    }

    public ConditionDef likeRight(Object value) {
        FunctionStatement.ConcatStatement concatStatement = new FunctionStatement.ConcatStatement();
        ListColumnStatement listColumnStatement = new ListColumnStatement(",");
        listColumnStatement.add(new Statement[]{new SymbolStatement.StrStatement("%"), new TakeMarkInvokerStatement(this.column(), value)});
        concatStatement.setParamsStatement(listColumnStatement);
        return conditionDef(new OperStatement.LIKEStatement(), concatStatement);
    }

    public  ConditionDef likeRight(T value, Predicate fn) {
        return likeRight(value).when(fn.test(value));
    }

    public ConditionDef notLike(Object value) {
        return not(like(value));
    }

    public  ConditionDef notLike(T value, Predicate fn) {
        return notLike(value).when(fn.test(value));
    }

    public ConditionDef notLikeLeft(Object value) {
        return not(likeLeft(value));
    }

    public  ConditionDef notLikeLeft(T value, Predicate fn) {
        return notLikeLeft(value).when(fn.test(value));
    }

    public ConditionDef notLikeRight(Object value) {
        return not(likeRight(value));
    }

    public  ConditionDef notLikeRight(T value, Predicate fn) {
        return notLikeRight(value).when(fn.test(value));
    }

    public ConditionDef between(Object start, Object end) {
        ConditionStatement conditionStatement = new ConditionStatement();
        conditionStatement.setLeft(this.getStatement());
        conditionStatement.setOper(new OperStatement.BETWEENStatement());
        ConditionStatement rightConditionStatement = new ConditionStatement();
        rightConditionStatement.setLeft(new TakeMarkInvokerStatement(this.column(), start));
        rightConditionStatement.setOper(new OperStatement.ANDStatement());
        rightConditionStatement.setRight(new TakeMarkInvokerStatement(this.column(), end));
        conditionStatement.setRight(rightConditionStatement);
        return new ConditionDef(conditionStatement);
    }

    public ConditionDef between(Object start, Object end, Predicate fn) {
        return between(start, end).when(fn.test(new Object[]{start, end}));
    }

    public ConditionDef notBetween(Object start, Object end) {
        return not(between(start, end));
    }

    public ConditionDef notBetween(Object start, Object end, Predicate fn) {
        return notBetween(start, end).when(fn.test(new Object[]{start, end}));
    }

    public ConditionDef isNull() {
        return conditionDef(new OperStatement.ISStatement(), new SymbolStatement.LetterStatement("NULL"));
    }

    public ConditionDef isNotNull() {
        ConditionStatement conditionStatement = new ConditionStatement();
        conditionStatement.setLeft(this.getStatement());
        conditionStatement.setOper(new OperStatement.ISStatement());
        ConditionStatement rightConditionStatement = new ConditionStatement();
        rightConditionStatement.setOper(new OperStatement.NOTStatement());
        rightConditionStatement.setRight(new SymbolStatement.LetterStatement("NULL"));
        conditionStatement.setRight(rightConditionStatement);
        return new ConditionDef(conditionStatement);
    }

    protected ConditionDef not(ConditionDef conditionDef) {
        return FunctionDef.not(conditionDef);
    }

    public ColumnDef add(ColumnDef columnDef) {
        return new ColumnDef(conditionDef(new OperStatement.ADDStatement(), columnDef.getStatement()).getStatement());
    }

    public ColumnDef add(Number number) {
        return add(new ColumnDef(new SymbolStatement.LetterStatement(number.toString())));
    }

    public ColumnDef sub(ColumnDef columnDef) {
        return new ColumnDef(conditionDef(new OperStatement.SUBStatement(), columnDef.getStatement()).getStatement());
    }

    public ColumnDef sub(Number number) {
        return sub(new ColumnDef(new SymbolStatement.LetterStatement(number.toString())));
    }

    public ColumnDef multiply(ColumnDef columnDef) {
        return new ColumnDef(conditionDef(new OperStatement.STARStatement(), columnDef.getStatement()).getStatement());
    }

    public ColumnDef multiply(Number number) {
        return multiply(new ColumnDef(new SymbolStatement.LetterStatement(number.toString())));
    }

    public ColumnDef divide(ColumnDef columnDef) {
        return new ColumnDef(conditionDef(new OperStatement.DIVIDEStatement(), columnDef.getStatement()).getStatement());
    }

    public ColumnDef divide(Number number) {
        return divide(new ColumnDef(new SymbolStatement.LetterStatement(number.toString())));
    }

    public SortDef asc() {
        OrderStatement.AscStatement ascStatement = new OrderStatement.AscStatement(this.getStatement());
        return new SortDef(ascStatement);
    }

    public SortDef desc() {
        OrderStatement.DescStatement descStatement = new OrderStatement.DescStatement(this.getStatement());
        return new SortDef(descStatement);
    }

    protected ConditionDef conditionDef(OperStatement operStatement, Object value) {
        return conditionDef(operStatement, new TakeMarkInvokerStatement(this.column(), value));
    }

    protected ConditionDef conditionDef(OperStatement operStatement, Statement statement) {
        ConditionStatement conditionStatement = new ConditionStatement();
        conditionStatement.setLeft(this.getStatement());
        conditionStatement.setOper(operStatement);
        conditionStatement.setRight(statement);
        return new ConditionDef(conditionStatement);
    }

    protected String column() {
        return column;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy