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

jhannes / fluent-jdbc / #172

01 Aug 2024 07:53PM UTC coverage: 94.146% (-1.3%) from 95.478%
#172

push

jhannes
DatabaseUpdatable.setField with expression allows to insert and update rows with calculated column values

41 of 45 new or added lines in 7 files covered. (91.11%)

32 existing lines in 12 files now uncovered.

1174 of 1247 relevant lines covered (94.15%)

0.94 hits per line

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

94.74
/src/main/java/org/fluentjdbc/DatabaseWhereBuilder.java
1
package org.fluentjdbc;
2

3
import javax.annotation.CheckReturnValue;
4
import javax.annotation.ParametersAreNonnullByDefault;
5
import java.util.ArrayList;
6
import java.util.Collection;
7
import java.util.Collections;
8
import java.util.List;
9

10
/**
11
 * Used to build the <code>WHERE ...</code> clause of SQL statements such as SELECT, UPDATE and DELETE.
12
 * Add clauses with {@link #where(String, Object)}, {@link #whereIn(String, Collection)} etc. Then
13
 * get the string for the WHERE clause with {@link #whereClause()} and the parameters with {@link #getParameters()}
14
 */
15
@ParametersAreNonnullByDefault
16
public class DatabaseWhereBuilder implements DatabaseQueryable<DatabaseWhereBuilder> {
1✔
17

18
    private final List<String> columns = new ArrayList<>();
1✔
19
    private final List<String> columnExpressions = new ArrayList<>();
1✔
20
    private final List<String> conditions = new ArrayList<>();
1✔
21
    private final List<Object> parameters = new ArrayList<>();
1✔
22

23
    /**
24
     * Adds the expression to the WHERE-clause and all the values to the parameter list.
25
     * E.g. <code>whereExpressionWithParameterList("created_at between ? and ?", List.of(earliestDate, latestDate))</code>
26
     */
27
    @Override
28
    public DatabaseWhereBuilder whereExpressionWithParameterList(String expression, Collection<?> parameters) {
29
        this.conditions.add("(" + expression + ")");
1✔
30
        this.parameters.addAll(parameters);
1✔
31
        return this;
1✔
32
    }
33

34
    /**
35
     * Adds the expression to the WHERE-clause and all the values to the parameter list.
36
     * E.g. <code>whereColumnValues("json_column", "?::json", jsonString)</code>
37
     */
38
    @Override
39
    public DatabaseWhereBuilder whereColumnValuesEqual(String column, String expression, Collection<?> parameters) {
40
        //noinspection ResultOfMethodCallIgnored
41
        whereExpressionWithParameterList(column + " = " + expression, parameters);
1✔
42
        columns.add(column);
1✔
43
        columnExpressions.add(expression);
1✔
44
        return this;
1✔
45
    }
46

47

48
    /**
49
     * Implemented as <code>return this</code> for compatibility purposes
50
     */
51
    @Override
52
    public DatabaseWhereBuilder query() {
UNCOV
53
        return this;
×
54
    }
55

56
    /**
57
     * If any conditions were added, returns <code>WHERE condition1 AND condition2 AND condition2 ...</code>.
58
     * If no conditions were added, returns the empty string
59
     */
60
    @CheckReturnValue
61
    public String whereClause() {
62
        return conditions.isEmpty() ? "" : " WHERE " + String.join(" AND ", conditions);
1✔
63
    }
64

65
    /**
66
     * Returns all parameters added with <code>.whereXXX()</code> method calls
67
     */
68
    @CheckReturnValue
69
    public List<Object> getParameters() {
70
        return parameters;
1✔
71
    }
72

73
    public List<String> getColumns() {
74
        return columns;
1✔
75
    }
76

77
    List<String> getColumnExpressions() {
78
        return columnExpressions;
1✔
79
    }
80

81
    Collection<?> getParameterAsCollection(int i) {
82
        Object parameter = parameters.get(i);
1✔
83
        return parameter instanceof Collection ? ((Collection<?>) parameter) : Collections.singleton(parameter);
1✔
84
    }
85
}
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