• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

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

85.71
/src/main/java/org/fluentjdbc/DbContextSqlBuilder.java
1
package org.fluentjdbc;
2

3
import javax.annotation.CheckReturnValue;
4
import javax.annotation.Nonnull;
5
import java.sql.Connection;
6
import java.util.Collection;
7
import java.util.List;
8
import java.util.stream.Stream;
9

10
/**
11
 * Used to construct SQL SELECT statements in a flexible way with {@link #where(String, Object)}
12
 * clauses, {@link #select(String...)} column names, {@link #from(String)} table statement,
13
 * {@link #groupBy(String...)}, {@link #orderBy(String)} and {@link #skipAndLimit(int, int)}.
14
 *
15
 * <h2>Example:</h2>
16
 *
17
 * <pre>
18
 * new DbContextSqlBuilder(dbContext)
19
 *      .select("first_name", "last_name")
20
 *      .select("p.id")
21
 *      .from("person p full join organizations o")
22
 *      .where("organization_sector", sector)
23
 *      .limit(100)
24
 *      .list(row -&gt; List.of(row.getString("first_name"), row.getString("last_name"), row.getUUID("p.id"));
25
 * </pre>
26
 *
27
 */
28
public class DbContextSqlBuilder implements DbContextListableSelect<DbContextSqlBuilder> {
29

30
    private final DatabaseSqlBuilder builder;
31
    private final DbContext dbContext;
32

33
    public DbContextSqlBuilder(DbContext dbContext) {
1✔
34
        this.dbContext = dbContext;
1✔
35
        builder = new DatabaseSqlBuilder(dbContext.getStatementFactory());
1✔
36
    }
1✔
37

38
    /**
39
     * Implemented as <code>return this</code> for compatibility purposes
40
     */
41
    @Override
42
    public DbContextSqlBuilder query() {
43
        return this;
1✔
44
    }
45

46
    /**
47
     * Add the arguments to the column list for the <code>SELECT column, column...</code> statement
48
     */
49
    @CheckReturnValue
50
    public DbContextSqlBuilder select(String... columns) {
51
        return query(builder.select(columns));
1✔
52
    }
53

54
    /**
55
     * Replace the "from" part of the <code>SELECT ... FROM fromStatement</code> in the select statement
56
     */
57
    @CheckReturnValue
58
    public DbContextSqlBuilder from(String fromStatement) {
59
        return query(builder.from(fromStatement));
1✔
60
    }
61

62
    /**
63
     * Adds the expression to the WHERE-clause and all the values to the parameter list.
64
     * E.g. <code>whereExpressionWithParameterList("created_at between ? and ?", List.of(earliestDate, latestDate))</code>
65
     */
66
    @Override
67
    public DbContextSqlBuilder whereExpressionWithParameterList(String expression, Collection<?> parameters) {
UNCOV
68
        return query(builder.whereExpressionWithParameterList(expression, parameters));
×
69
    }
70

71
    /**
72
     * Adds the expression to the WHERE-clause and all the values to the parameter list.
73
     * E.g. <code>whereColumnValues("json_column", "?::json", jsonString)</code>
74
     */
75
    @CheckReturnValue
76
    public DbContextSqlBuilder whereColumnValuesEqual(String column, String expression, Collection<?> parameters) {
77
        return query(builder.whereColumnValuesEqual(column, expression, parameters));
1✔
78
    }
79

80
    /**
81
     * Add the arguments to the column list for the <code>SELECT ... FROM ... ... GROUP BY groupByStatement</code> statement
82
     */
83
    @CheckReturnValue
84
    public DbContextSqlBuilder groupBy(String... groupByStatement) {
85
        return query(builder.groupBy(groupByStatement));
1✔
86
    }
87

88
    /**
89
     * Adds <code>ORDER BY ...</code> clause to the <code>SELECT</code> statement
90
     */
91
    @Override
92
    public DbContextSqlBuilder orderBy(String orderByClause) {
93
        return query(builder.orderBy(orderByClause));
1✔
94
    }
95

96
    /**
97
     * If you haven't called {@link #orderBy}, the results of {@link DatabaseListableQueryBuilder#list}
98
     * will be unpredictable. Call <code>unordered()</code> if you are okay with this.
99
     */
100
    @CheckReturnValue
101
    public DbContextSqlBuilder unordered() {
102
        return query(builder.unordered());
1✔
103
    }
104

105
    /**
106
     * Adds <code>OFFSET ... ROWS FETCH ... ROWS ONLY</code> clause to the <code>SELECT</code>
107
     * statement. FETCH FIRST was introduced in
108
     * <a href="https://en.wikipedia.org/wiki/Select_%28SQL%29#Limiting_result_rows">SQL:2008</a>
109
     * and is supported by Postgresql 8.4, Oracle 12c, IBM DB2, HSQLDB, H2, and SQL Server 2012.
110
     */
111
    @Override
112
    public DbContextSqlBuilder skipAndLimit(int offset, int rowCount) {
113
        return query(builder.skipAndLimit(offset, rowCount));
1✔
114
    }
115

116
    /**
117
     * Execute the query and map each return value over the {@link DatabaseResult.RowMapper} function to return a stream. Example:
118
     * <pre>
119
     *     table.where("status", status).stream(row -&gt; row.getInstant("created_at"))
120
     * </pre>
121
     */
122
    @Override
123
    public <OBJECT> Stream<OBJECT> stream(DatabaseResult.RowMapper<OBJECT> mapper) {
124
        return builder.stream(getConnection(), mapper);
1✔
125
    }
126

127
    /**
128
     * Execute the query and map each return value over the {@link DatabaseResult.RowMapper} function to return a list. Example:
129
     * <pre>
130
     *     List&lt;Instant&gt; creationTimes = table.where("status", status).list(row -&gt; row.getInstant("created_at"))
131
     * </pre>
132
     */
133
    @Override
134
    public <OBJECT> List<OBJECT> list(DatabaseResult.RowMapper<OBJECT> mapper) {
135
        return builder.list(getConnection(), mapper);
1✔
136
    }
137

138
    /**
139
     * Executes <code>SELECT count(*) FROM ...</code> on the query and returns the result
140
     */
141
    @Override
142
    public int getCount() {
143
        return builder.getCount(getConnection());
1✔
144
    }
145

146
    /**
147
     * If the query returns no rows, returns {@link SingleRow#absent}, if exactly one row is returned, maps it and return it,
148
     * if more than one is returned, throws `IllegalStateException`
149
     *
150
     * @param mapper Function object to map a single returned row to an object
151
     * @return the mapped row if one row is returned, {@link SingleRow#absent} otherwise
152
     * @throws IllegalStateException if more than one row was matched the query
153
     */
154
    @Nonnull
155
    @Override
156
    public <OBJECT> SingleRow<OBJECT> singleObject(DatabaseResult.RowMapper<OBJECT> mapper) {
157
        return builder.singleObject(getConnection(), mapper);
1✔
158
    }
159

160
    /**
161
     * Executes the <code>SELECT * FROM ...</code> statement and calls back to
162
     * {@link DatabaseResult.RowConsumer} for each returned row
163
     */
164
    @Override
165
    public void forEach(DatabaseResult.RowConsumer consumer) {
UNCOV
166
        builder.forEach(getConnection(), consumer);
×
UNCOV
167
    }
×
168

169
    @SuppressWarnings("unused")
170
    private DbContextSqlBuilder query(DatabaseSqlBuilder builder) {
171
        return this;
1✔
172
    }
173

174
    @Nonnull
175
    private Connection getConnection() {
176
        return dbContext.getThreadConnection();
1✔
177
    }
178
}
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