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

torand / FasterSQL / 17073502945

19 Aug 2025 02:56PM UTC coverage: 68.2%. Remained the same
17073502945

push

github

torand
chore: adjust sonar cloud config to avoid noise

299 of 598 branches covered (50.0%)

Branch coverage included in aggregate %.

1629 of 2229 relevant lines covered (73.08%)

3.97 hits per line

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

86.21
/src/main/java/io/github/torand/fastersql/expression/cases/SearchedCase.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.streamSafely;
33
import static java.util.Objects.nonNull;
34

35
/**
36
 * Implements a searched CASE expression.
37
 */
38
public class SearchedCase implements Expression {
39
    private final List<SearchedWhenThen> whenThenExpressions;
40
    private final Expression elseExpression;
41
    private final ColumnAlias alias;
42

43
    SearchedCase(List<SearchedWhenThen> whenThenExpressions, Expression elseExpression, String alias) {
2✔
44
        this.whenThenExpressions = requireNonEmpty(whenThenExpressions, "No when-then expressions specified");
8✔
45
        this.elseExpression = elseExpression;
3✔
46
        this.alias = nonBlank(alias) ? new ColumnAlias(alias) : defaultAlias();
12✔
47
    }
1✔
48

49
    // Sql
50

51
    @Override
52
    public String sql(Context context) {
53
        StringBuilder sql = new StringBuilder();
4✔
54
        sql.append("case ");
4✔
55
        streamSafely(whenThenExpressions).forEach(wt -> sql.append(wt.sql(context)).append(" "));
16✔
56
        if (nonNull(elseExpression)) {
4!
57
            sql.append("else ").append(elseExpression.sql(context)).append(" ");
11✔
58
        }
59
        sql.append("end");
4✔
60
        return sql.toString();
3✔
61
    }
62

63
    @Override
64
    public Stream<Object> params(Context context) {
65
        return Stream.concat(
4✔
66
            streamSafely(whenThenExpressions).flatMap(wh -> wh.params(context)),
10✔
67
            Stream.ofNullable(elseExpression).flatMap(e -> e.params(context))
8✔
68
        );
69
    }
70

71
    @Override
72
    public Stream<Column> columnRefs() {
73
        return Stream.concat(
4✔
74
            streamSafely(whenThenExpressions).flatMap(SearchedWhenThen::columnRefs),
5✔
75
            Stream.ofNullable(elseExpression).flatMap(Sql::columnRefs)
3✔
76
        );
77
    }
78

79
    @Override
80
    public Stream<ColumnAlias> aliasRefs() {
81
        return Stream.concat(
×
82
            streamSafely(whenThenExpressions).flatMap(SearchedWhenThen::aliasRefs),
×
83
            Stream.ofNullable(elseExpression).flatMap(Sql::aliasRefs)
×
84
        );
85
    }
86

87
    // Projection
88

89
    @Override
90
    public Projection as(String alias) {
91
        requireNonBlank(alias, "No alias specified");
6✔
92
        return new SearchedCase(whenThenExpressions, elseExpression, alias);
9✔
93
    }
94

95
    @Override
96
    public Optional<ColumnAlias> alias() {
97
        return Optional.ofNullable(alias);
4✔
98
    }
99

100
    private ColumnAlias defaultAlias() {
101
        return ColumnAlias.generate("CASE_");
3✔
102
    }
103
}
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