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

com.cadenzauk.siesta.grammar.expression.CaseExpression Maven / Gradle / Ivy

/*
 * Copyright (c) 2017 Cadenza United Kingdom Limited
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.cadenzauk.siesta.grammar.expression;

import com.cadenzauk.core.function.Function1;
import com.cadenzauk.core.function.FunctionOptional1;
import com.cadenzauk.core.sql.RowMapper;
import com.cadenzauk.core.stream.StreamUtil;
import com.cadenzauk.core.tuple.Tuple;
import com.cadenzauk.core.tuple.Tuple2;
import com.cadenzauk.siesta.Alias;
import com.cadenzauk.siesta.Scope;
import com.cadenzauk.siesta.grammar.LabelGenerator;
import com.google.common.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;

public class CaseExpression implements TypedExpression {
    private final LabelGenerator labelGenerator = new LabelGenerator("case_");
    private final List>> cases = new ArrayList<>();
    private Optional> orElse = Optional.empty();

    public CaseExpression(BooleanExpression booleanExpression, TypedExpression value) {
        cases.add(Tuple.of(booleanExpression, value));
    }

    @Override
    public String sql(Scope scope) {
        return String.format("case %s%s end",
            cases.stream().map(c -> String.format("when %s then %s", c.item1().sql(scope), c.item2().sql(scope))).collect(joining(" ")),
            orElse.map(e -> " else " + e.sql(scope)).orElse(""));
    }

    @Override
    public Stream args(Scope scope) {
        return Stream.concat(
            cases.stream().flatMap(c -> Stream.concat(c.item1().args(scope), c.item2().args(scope))),
            StreamUtil.of(orElse).flatMap(e -> e.args(scope))
        );
    }

    @Override
    public Precedence precedence() {
        return Precedence.UNARY;
    }

    @Override
    public String label(Scope scope) {
        return labelGenerator.label();
    }

    @Override
    public RowMapper rowMapper(Scope scope, String label) {
        return cases.get(0).item2().rowMapper(scope, label);
    }

    @Override
    public TypeToken type() {
        return cases.get(0).item2().type();
    }

    public TypedExpression orElse(T value) {
        orElse = Optional.of(LiteralExpression.of(value));
        return this;
    }

    public TypedExpression orElse(TypedExpression value) {
        orElse = Optional.of(value);
        return this;
    }

    public  TypedExpression orElse(Function1 method) {
        orElse = Optional.of(UnresolvedColumn.of(method));
        return this;
    }

    public  TypedExpression orElse(FunctionOptional1 method) {
        orElse = Optional.of(UnresolvedColumn.of(method));
        return this;
    }

    public  TypedExpression orElse(String alias, Function1 method) {
        orElse = Optional.of(UnresolvedColumn.of(alias, method));
        return this;
    }

    public  TypedExpression orElse(String alias, FunctionOptional1 method) {
        orElse = Optional.of(UnresolvedColumn.of(alias, method));
        return this;
    }

    public  TypedExpression orElse(Alias alias, Function1 method) {
        orElse = Optional.of(ResolvedColumn.of(alias, method));
        return this;
    }

    public  TypedExpression orElse(Alias alias, FunctionOptional1 method) {
        orElse = Optional.of(ResolvedColumn.of(alias, method));
        return this;
    }

    public  ExpressionBuilder when(X val) {
        return ExpressionBuilder.of(ValueExpression.of(val), InSubsequentWhenExpectingThen::new);
    }

    public  ExpressionBuilder when(TypedExpression expression) {
        return ExpressionBuilder.of(expression, InSubsequentWhenExpectingThen::new);
    }

    public  ExpressionBuilder when(Function1 method) {
        return ExpressionBuilder.of(UnresolvedColumn.of(method), InSubsequentWhenExpectingThen::new);
    }

    public  ExpressionBuilder when(FunctionOptional1 method) {
        return ExpressionBuilder.of(UnresolvedColumn.of(method), InSubsequentWhenExpectingThen::new);
    }

    public  ExpressionBuilder when(String alias, Function1 method) {
        return ExpressionBuilder.of(UnresolvedColumn.of(alias, method), InSubsequentWhenExpectingThen::new);
    }

    public  ExpressionBuilder when(String alias, FunctionOptional1 method) {
        return ExpressionBuilder.of(UnresolvedColumn.of(alias, method), InSubsequentWhenExpectingThen::new);
    }

    public  ExpressionBuilder when(Alias alias, Function1 method) {
        return ExpressionBuilder.of(ResolvedColumn.of(alias, method), InSubsequentWhenExpectingThen::new);
    }

    public  ExpressionBuilder when(Alias alias, FunctionOptional1 method) {
        return ExpressionBuilder.of(ResolvedColumn.of(alias, method), InSubsequentWhenExpectingThen::new);
    }

    public static class InFirstWhenExpectingThen {
        private final BooleanExpression booleanExpression;

        public InFirstWhenExpectingThen(BooleanExpression booleanExpression) {
            this.booleanExpression = booleanExpression;
        }

        public  CaseExpression then(T value) {
            return new CaseExpression<>(booleanExpression, LiteralExpression.of(value));
        }

        public  CaseExpression then(TypedExpression value) {
            return new CaseExpression<>(booleanExpression, value);
        }

        public  CaseExpression then(Function1 method) {
            return new CaseExpression<>(booleanExpression, UnresolvedColumn.of(method));
        }

        public  CaseExpression then(FunctionOptional1 method) {
            return new CaseExpression<>(booleanExpression, UnresolvedColumn.of(method));
        }

        public  CaseExpression then(String alias, Function1 method) {
            return new CaseExpression<>(booleanExpression, UnresolvedColumn.of(alias, method));
        }

        public  CaseExpression then(String alias, FunctionOptional1 method) {
            return new CaseExpression<>(booleanExpression, UnresolvedColumn.of(alias, method));
        }

        public  CaseExpression then(Alias alias, Function1 method) {
            return new CaseExpression<>(booleanExpression, ResolvedColumn.of(alias, method));
        }

        public  CaseExpression then(Alias alias, FunctionOptional1 method) {
            return new CaseExpression<>(booleanExpression, ResolvedColumn.of(alias, method));
        }
    }

    public class InSubsequentWhenExpectingThen {
        private final BooleanExpression booleanExpression;

        public InSubsequentWhenExpectingThen(BooleanExpression booleanExpression) {
            this.booleanExpression = booleanExpression;
        }

        public CaseExpression then(T value) {
            cases.add(Tuple.of(booleanExpression, LiteralExpression.of(value)));
            return CaseExpression.this;
        }

        public CaseExpression then(TypedExpression value) {
            cases.add(Tuple.of(booleanExpression, value));
            return CaseExpression.this;
        }

        public  CaseExpression then(Function1 method) {
            cases.add(Tuple.of(booleanExpression, UnresolvedColumn.of(method)));
            return CaseExpression.this;
        }

        public  CaseExpression then(FunctionOptional1 method) {
            cases.add(Tuple.of(booleanExpression, UnresolvedColumn.of(method)));
            return CaseExpression.this;
        }

        public  CaseExpression then(String alias, Function1 method) {
            cases.add(Tuple.of(booleanExpression, UnresolvedColumn.of(alias, method)));
            return CaseExpression.this;
        }

        public  CaseExpression then(String alias, FunctionOptional1 method) {
            cases.add(Tuple.of(booleanExpression, UnresolvedColumn.of(alias, method)));
            return CaseExpression.this;
        }

        public  CaseExpression then(Alias alias, Function1 method) {
            cases.add(Tuple.of(booleanExpression, ResolvedColumn.of(alias, method)));
            return CaseExpression.this;
        }

        public  CaseExpression then(Alias alias, FunctionOptional1 method) {
            cases.add(Tuple.of(booleanExpression, ResolvedColumn.of(alias, method)));
            return CaseExpression.this;
        }
    }

}