• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

torand / FasterSQL / 17194256933

24 Aug 2025 09:56PM UTC coverage: 68.417% (+0.3%) from 68.15%
17194256933

push

github

torand
refactor: sonar cloud issues

299 of 598 branches covered (50.0%)

Branch coverage included in aggregate %.

35 of 40 new or added lines in 6 files covered. (87.5%)

1 existing line in 1 file now uncovered.

1616 of 2201 relevant lines covered (73.42%)

4.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.85
/src/main/java/io/github/torand/fastersql/expression/cases/SimpleCase.java
1
/*
2
 * Copyright (c) 2024-2025 Tore Eide Andersen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.torand.fastersql.expression.cases;
17

18
import io.github.torand.fastersql.alias.ColumnAlias;
19
import io.github.torand.fastersql.expression.Expression;
20
import io.github.torand.fastersql.model.Column;
21
import io.github.torand.fastersql.projection.Projection;
22
import io.github.torand.fastersql.sql.Context;
23
import io.github.torand.fastersql.sql.Sql;
24

25
import java.util.List;
26
import java.util.Optional;
27
import java.util.stream.Stream;
28

29
import static io.github.torand.javacommons.contract.Requires.requireNonBlank;
30
import static io.github.torand.javacommons.contract.Requires.requireNonEmpty;
31
import static io.github.torand.javacommons.lang.StringHelper.nonBlank;
32
import static io.github.torand.javacommons.stream.StreamHelper.concatStreams;
33
import static io.github.torand.javacommons.stream.StreamHelper.streamSafely;
34
import static java.util.Objects.nonNull;
35
import static java.util.Objects.requireNonNull;
36

37
/**
38
 * Implements a simple CASE expression.
39
 */
40
public class SimpleCase implements Expression {
41
    private final Expression caseExpression;
42
    private final List<SimpleWhenThen> whenThenExpressions;
43
    private final Expression elseExpression;
44
    private final ColumnAlias alias;
45

46
    SimpleCase(Expression caseExpression, List<SimpleWhenThen> whenThenExpressions, Expression elseExpression, String alias) {
2✔
47
        this.caseExpression = requireNonNull(caseExpression, "No case expression specified");
6✔
48
        this.whenThenExpressions = requireNonEmpty(whenThenExpressions, "No when-then expressions specified");
8✔
49
        this.elseExpression = elseExpression;
3✔
50
        this.alias = nonBlank(alias) ? new ColumnAlias(alias) : defaultAlias();
12✔
51
    }
1✔
52

53
    // Sql
54

55
    @Override
56
    public String sql(Context context) {
57
        StringBuilder sql = new StringBuilder();
4✔
58
        sql.append("case %s ".formatted(caseExpression.sql(context)));
14✔
59
        streamSafely(whenThenExpressions).forEach(wt -> sql.append(wt.sql(context)).append(" "));
16✔
60
        if (nonNull(elseExpression)) {
4!
61
            sql.append("else ").append(elseExpression.sql(context)).append(" ");
11✔
62
        }
63
        sql.append("end");
4✔
64
        return sql.toString();
3✔
65
    }
66

67
    @Override
68
    public Stream<Object> params(Context context) {
69
        return concatStreams(
9✔
70
            caseExpression.params(context),
6✔
71
            streamSafely(whenThenExpressions).flatMap(wh -> wh.params(context)),
13✔
72
            Stream.ofNullable(elseExpression).flatMap(e -> e.params(context))
9✔
73
        );
74
    }
75

76
    @Override
77
    public Stream<Column> columnRefs() {
78
        return concatStreams(
8✔
79
            caseExpression.columnRefs(),
6✔
80
            streamSafely(whenThenExpressions).flatMap(SimpleWhenThen::columnRefs),
8✔
81
            Stream.ofNullable(elseExpression).flatMap(Sql::columnRefs)
4✔
82
        );
83
    }
84

85
    @Override
86
    public Stream<ColumnAlias> aliasRefs() {
87
        return concatStreams(
×
88
            caseExpression.aliasRefs(),
×
NEW
89
            streamSafely(whenThenExpressions).flatMap(SimpleWhenThen::aliasRefs),
×
90
            Stream.ofNullable(elseExpression).flatMap(Sql::aliasRefs)
×
91
        );
92
    }
93

94
    // Projection
95

96
    @Override
97
    public Projection as(String alias) {
98
        requireNonBlank(alias, "No alias specified");
6✔
99
        return new SimpleCase(caseExpression, whenThenExpressions, elseExpression, alias);
11✔
100
    }
101

102
    @Override
103
    public Optional<ColumnAlias> alias() {
104
        return Optional.ofNullable(alias);
4✔
105
    }
106

107
    private ColumnAlias defaultAlias() {
108
        return ColumnAlias.generate("CASE_");
3✔
109
    }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc