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

torand / FasterSQL / 12970217277

26 Jan 2025 01:31AM UTC coverage: 49.458%. Remained the same
12970217277

push

github

torand
refactor: condition -> predicate

108 of 322 branches covered (33.54%)

Branch coverage included in aggregate %.

58 of 137 new or added lines in 12 files covered. (42.34%)

3 existing lines in 2 files now uncovered.

622 of 1154 relevant lines covered (53.9%)

2.85 hits per line

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

68.09
/src/main/java/io/github/torand/fastersql/statement/DeleteStatement.java
1
/*
2
 * Copyright (c) 2024 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.statement;
17

18
import io.github.torand.fastersql.Context;
19
import io.github.torand.fastersql.Field;
20
import io.github.torand.fastersql.Table;
21
import io.github.torand.fastersql.predicate.OptionalPredicate;
22
import io.github.torand.fastersql.predicate.Predicate;
23

24
import java.util.Collection;
25
import java.util.List;
26
import java.util.function.Supplier;
27
import java.util.stream.Stream;
28

29
import static io.github.torand.fastersql.Command.DELETE;
30
import static io.github.torand.fastersql.statement.Helpers.unwrapSuppliers;
31
import static io.github.torand.fastersql.util.collection.CollectionHelper.*;
32
import static io.github.torand.fastersql.util.contract.Requires.requireNonEmpty;
33
import static java.util.Objects.isNull;
34
import static java.util.Objects.requireNonNull;
35
import static java.util.stream.Collectors.joining;
36

37
public class DeleteStatement extends PreparableStatement {
38
    private final Table<?> fromTable;
39
    private final List<Predicate> predicates;
40

41
    DeleteStatement(Table<?> table, Collection<Predicate> predicates) {
2✔
42
        this.fromTable = requireNonNull(table, "No table specified");
6✔
43
        this.predicates = asList(predicates);
4✔
44
    }
1✔
45

46
    public DeleteStatement where(Predicate... predicates) {
47
        requireNonEmpty(predicates, "No predicates specified");
6✔
48
        List<Predicate> concatenated = concat(this.predicates, predicates);
5✔
49
        return new DeleteStatement(fromTable, concatenated);
7✔
50
    }
51

52
    /**
53
     * Same as other method of same name, but only adds to the where clause predicates that are present.
54
     * @param maybePredicates the predicates that may be present or not
55
     * @return updated statement, for method chaining
56
     */
57
    public final DeleteStatement where(OptionalPredicate... maybePredicates) {
NEW
58
        requireNonEmpty(maybePredicates, "No optional predicates specified");
×
NEW
59
        List<Predicate> concatenated = concat(this.predicates, OptionalPredicate.unwrap(maybePredicates));
×
UNCOV
60
        return new DeleteStatement(fromTable, concatenated);
×
61
    }
62

63
    /**
64
     * Adds one or more predicates to the where clause, if a predicate is true.
65
     * @param condition the condition that must be true for predicates to be added
66
     * @param predicateSuppliers the suppliers providing predicates to add
67
     * @return updated statement, for method chaining
68
     */
69
    @SafeVarargs
70
    public final DeleteStatement whereIf(boolean condition, Supplier<Predicate>... predicateSuppliers) {
NEW
71
        requireNonEmpty(predicateSuppliers, "No predicate suppliers specified");
×
NEW
72
        if (condition) {
×
NEW
73
            List<Predicate> concatenated = concat(this.predicates, unwrapSuppliers(predicateSuppliers));
×
UNCOV
74
            return new DeleteStatement(fromTable, concatenated);
×
75
        }
76
        return this;
×
77
    }
78

79
    @Override
80
    String sql(Context context) {
81
        final Context localContext = context.withCommand(DELETE);
4✔
82
        validate();
2✔
83

84
        StringBuilder sb = new StringBuilder();
4✔
85
        sb.append("delete from ");
4✔
86
        sb.append(fromTable.sql(localContext));
7✔
87

88
        if (nonEmpty(predicates)) {
4!
89
            sb.append(" where ");
4✔
90
            sb.append(streamSafely(predicates)
8✔
91
                .map(e -> e.sql(localContext))
6✔
92
                .collect(joining(" and ")));
3✔
93
        }
94

95
        return sb.toString();
3✔
96
    }
97

98
    @Override
99
    List<Object> params(Context context) {
100
        return streamSafely(predicates)
6✔
101
            .flatMap(e -> e.params(context))
5✔
102
            .toList();
1✔
103
    }
104

105
    private void validate() {
106
        if (isNull(fromTable)) {
4!
107
            throw new IllegalStateException("No FROM clause specified");
×
108
        }
109

110
        // TODO: Verify that deleteTables is subset of fromTables
111

112
        validateFieldTableRelations(streamSafely(predicates).flatMap(Predicate::fieldRefs));
7✔
113
    }
1✔
114

115
    private void validateFieldTableRelations(Stream<Field> fields) {
116
        fields
3✔
117
            .filter(f -> !fromTable.name().equalsIgnoreCase(f.table().name()))
11!
118
            .findFirst()
2✔
119
            .ifPresent(f -> {
1✔
120
                throw new IllegalStateException("Field " + f.name() + " belongs to table " + f.table().name() + ", but table is not specified in the FROM clause");
×
121
            });
122
    }
1✔
123
}
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