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

torand / FasterSQL / 15071216480

16 May 2025 02:49PM UTC coverage: 69.877% (+2.4%) from 67.475%
15071216480

push

github

web-flow
Merge pull request #30 from torand/access-support

Access support

229 of 414 branches covered (55.31%)

Branch coverage included in aggregate %.

105 of 152 new or added lines in 26 files covered. (69.08%)

3 existing lines in 3 files now uncovered.

1193 of 1621 relevant lines covered (73.6%)

3.92 hits per line

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

84.75
/src/main/java/io/github/torand/fastersql/statement/UpdateStatement.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.statement;
17

18
import io.github.torand.fastersql.Column;
19
import io.github.torand.fastersql.Context;
20
import io.github.torand.fastersql.Table;
21
import io.github.torand.fastersql.dialect.AnsiIsoDialect;
22
import io.github.torand.fastersql.expression.Expression;
23
import io.github.torand.fastersql.predicate.OptionalPredicate;
24
import io.github.torand.fastersql.predicate.Predicate;
25

26
import java.util.Collection;
27
import java.util.List;
28
import java.util.Optional;
29
import java.util.stream.Stream;
30

31
import static io.github.torand.fastersql.Command.UPDATE;
32
import static io.github.torand.fastersql.constant.Constants.$;
33
import static io.github.torand.fastersql.util.collection.CollectionHelper.asList;
34
import static io.github.torand.fastersql.util.collection.CollectionHelper.concat;
35
import static io.github.torand.fastersql.util.collection.CollectionHelper.isEmpty;
36
import static io.github.torand.fastersql.util.collection.CollectionHelper.nonEmpty;
37
import static io.github.torand.fastersql.util.collection.CollectionHelper.streamSafely;
38
import static io.github.torand.fastersql.util.contract.Requires.requireNonEmpty;
39
import static java.util.Objects.requireNonNull;
40
import static java.util.stream.Collectors.joining;
41

42
/**
43
 * Implements an UPDATE statement.
44
 */
45
public class UpdateStatement implements PreparableStatement {
46
    private final Table<?> table;
47
    private final List<ColumnValue> columnValues;
48
    private final List<Predicate> predicates;
49

50
    UpdateStatement(Table<?> table, Collection<ColumnValue> columnValues, Collection<Predicate> predicates) {
2✔
51
        this.table = requireNonNull(table, "No table specified");
6✔
52
        this.columnValues = asList(columnValues);
4✔
53
        this.predicates = asList(predicates);
4✔
54
    }
1✔
55

56
    /**
57
     * Adds an expression value to be updated for a column.
58
     * @param column the column.
59
     * @param value the value.
60
     * @return the modified statement.
61
     */
62
    public UpdateStatement set(Column column, Expression value) {
63
        requireNonNull(column, "No column specified");
4✔
64
        requireNonNull(value, "No expression specified");
4✔
65

66
        List<ColumnValue> concatenated = concat(this.columnValues, new ColumnValue(column, value));
14✔
67
        return new UpdateStatement(table, concatenated, predicates);
9✔
68
    }
69

70
    /**
71
     * Adds a constant value to be updated for a column.
72
     * @param column the column.
73
     * @param value the value.
74
     * @return the modified statement.
75
     */
76
    public UpdateStatement set(Column column, Object value) {
77
        requireNonNull(column, "No column specified");
4✔
78

79
        List<ColumnValue> concatenated = concat(this.columnValues, new ColumnValue(column, $(value)));
15✔
80
        return new UpdateStatement(table, concatenated, predicates);
9✔
81
    }
82

83
    /**
84
     * Adds an optional value to be updated for a column.
85
     * The column is updated only if the optional value is present.
86
     * @param column the column.
87
     * @param maybeValue the optional value.
88
     * @return the modified statement.
89
     */
90
    public UpdateStatement set(Column column, Optional<?> maybeValue) {
91
        requireNonNull(column, "No column specified");
4✔
92
        requireNonNull(maybeValue, "No value specified");
4✔
93

94
        if (maybeValue.isPresent()) {
3✔
95
            List<ColumnValue> concatenated = concat(this.columnValues, new ColumnValue(column, $(maybeValue.get())));
16✔
96
            return new UpdateStatement(table, concatenated, predicates);
9✔
97
        } else {
98
            return this;
2✔
99
        }
100
    }
101

102
    /**
103
     * Adds one or more predicates to the WHERE clause.
104
     * @param predicates the predicates.
105
     * @return the modified statement.
106
     */
107
    public UpdateStatement where(Predicate... predicates) {
108
        requireNonEmpty(predicates, "No predicates specified");
6✔
109

110
        List<Predicate> concatenated = concat(this.predicates, predicates);
5✔
111
        return new UpdateStatement(table, columnValues, concatenated);
9✔
112
    }
113

114
    /**
115
     * Adds optional predicates to the WHERE clause if the wrapped predicates are present.
116
     * @param maybePredicates the optional predicates.
117
     * @return the modified statement.
118
     */
119
    public final UpdateStatement where(OptionalPredicate... maybePredicates) {
120
        requireNonEmpty(maybePredicates, "No optional predicates specified");
×
121

122
        List<Predicate> concatenated = concat(this.predicates, OptionalPredicate.unwrap(maybePredicates));
×
123
        return new UpdateStatement(table, columnValues, concatenated);
×
124
    }
125

126
    @Override
127
    public String sql(Context context) {
128
        final Context localContext = context.withCommand(UPDATE);
4✔
129
        validate();
2✔
130

131
        StringBuilder sb = new StringBuilder();
4✔
132
        sb.append("update ");
4✔
133
        sb.append(table.sql(context));
7✔
134
        sb.append(" set ");
4✔
135
        sb.append(streamSafely(columnValues).map(cv -> cv.column().sql(localContext) + " = " + cv.valueSql(localContext)).collect(joining(", ")));
22✔
136
        if (nonEmpty(predicates)) {
4!
137
            sb.append(" where ");
4✔
138
            sb.append(streamSafely(predicates).map(p -> p.sql(localContext)).collect(joining(" and ")));
17✔
139
        }
140

141
        return sb.toString();
3✔
142
    }
143

144
    @Override
145
    public Stream<Object> params(Context context) {
146
        final Context localContext = context.withCommand(UPDATE);
4✔
147
        return Stream.concat(
4✔
148
                streamSafely(columnValues).flatMap(cv -> cv.valueParams(localContext)),
10✔
149
                streamSafely(predicates).flatMap(p -> p.params(localContext)));
8✔
150
    }
151

152
    private void validate() {
153
        if (isEmpty(columnValues)) {
4!
154
            throw new IllegalStateException("No values to set");
×
155
        }
156
        validateColumnTableRelations(streamSafely(columnValues).map(ColumnValue::column));
7✔
157
        validateColumnTableRelations(streamSafely(predicates).flatMap(Predicate::columnRefs));
7✔
158
    }
1✔
159

160
    private void validateColumnTableRelations(Stream<Column> columns) {
161
        columns
3✔
162
            .filter(c -> !table.name().equalsIgnoreCase(c.table().name()))
11!
163
            .findFirst()
2✔
164
            .ifPresent(c -> {
1✔
165
                throw new IllegalStateException("Column " + c.name() + " belongs to table " + c.table().name() + ", but is not specified in the UPDATE clause");
×
166
            });
167
    }
1✔
168

169
    @Override
170
    public String toString() {
NEW
171
        return toString(new AnsiIsoDialect());
×
172
    }
173
}
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