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

torand / FasterSQL / 12893661530

21 Jan 2025 06:40PM UTC coverage: 50.829%. Remained the same
12893661530

push

github

torand
refactor: rename expression -> condition

104 of 304 branches covered (34.21%)

Branch coverage included in aggregate %.

81 of 211 new or added lines in 24 files covered. (38.39%)

3 existing lines in 2 files now uncovered.

601 of 1083 relevant lines covered (55.49%)

2.91 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.condition.Condition;
22
import io.github.torand.fastersql.condition.OptionalCondition;
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<Condition> conditions;
40

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

46
    public DeleteStatement where(Condition... conditions) {
47
        requireNonEmpty(conditions, "No conditions specified");
6✔
48
        List<Condition> concatenated = concat(this.conditions, conditions);
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 conditions that are present.
54
     * @param maybeConditions the conditions that may be present or not
55
     * @return updated statement, for method chaining
56
     */
57
    public final DeleteStatement where(OptionalCondition... maybeConditions) {
NEW
58
        requireNonEmpty(maybeConditions, "No optional conditions specified");
×
NEW
59
        List<Condition> concatenated = concat(this.conditions, OptionalCondition.unwrap(maybeConditions));
×
UNCOV
60
        return new DeleteStatement(fromTable, concatenated);
×
61
    }
62

63
    /**
64
     * Adds one or more conditions to the where clause, if a predicate is true.
65
     * @param predicate the predicate that must be true for conditions to be added
66
     * @param conditionSuppliers the suppliers providing conditions to add
67
     * @return updated statement, for method chaining
68
     */
69
    @SafeVarargs
70
    public final DeleteStatement whereIf(boolean predicate, Supplier<Condition>... conditionSuppliers) {
NEW
71
        requireNonEmpty(conditionSuppliers, "No condition suppliers specified");
×
NEW
72
        if (predicate) {
×
NEW
73
            List<Condition> concatenated = concat(this.conditions, unwrapSuppliers(conditionSuppliers));
×
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(conditions)) {
4!
89
            sb.append(" where ");
4✔
90
            sb.append(streamSafely(conditions)
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(conditions)
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(conditions).flatMap(Condition::fields));
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