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

jhannes / fluent-jdbc / #207

10 Feb 2026 01:24PM UTC coverage: 91.533% (-0.06%) from 91.59%
#207

push

jhannes-test
[maven-release-plugin] prepare release fluent-jdbc-0.7.0

1200 of 1311 relevant lines covered (91.53%)

0.92 hits per line

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

96.15
/src/main/java/org/fluentjdbc/DbContextTableQueryBuilder.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.List;
7
import java.util.stream.Stream;
8

9
/**
10
 * Generate <code>SELECT</code> statements by collecting <code>WHERE</code> expressions and parameters.Example:
11
 *
12
 * <pre>
13
 * {@link DbContextTable} table = context.table("database_test_table");
14
 * try (DbContextConnection ignored = context.startConnection(dataSource)) {
15
 *      List&lt;Person&gt; result = table
16
 *          .where("firstName", firstName)
17
 *          .whereExpression("lastName like ?", "joh%")
18
 *          .whereIn("status", statuses)
19
 *          .orderBy("lastName")
20
 *          .list(row -&gt; new Person(row));
21
 * }
22
 * </pre>
23
 *
24
 * @see org.fluentjdbc.DatabaseTableQueryBuilder
25
 */
26
public class DbContextTableQueryBuilder implements DbContextListableSelect<DbContextTableQueryBuilder> {
27

28
    private final DbContextTable dbContextTable;
29
    private final DatabaseTableQueryBuilder builder;
30

31
    public DbContextTableQueryBuilder(DbContextTable dbContextTable) {
32
        this(dbContextTable, new DatabaseTableQueryBuilder(dbContextTable.getTable()));
1✔
33
    }
1✔
34

35
    public DbContextTableQueryBuilder(DbContextTable dbContextTable, DatabaseTableQueryBuilder builder) {
1✔
36
        this.dbContextTable = dbContextTable;
1✔
37
        this.builder = builder;
1✔
38
    }
1✔
39

40
    /**
41
     * Returns this. Needed to make {@link DbContextTableQueryBuilder} interchangeable with {@link DbContextTable}
42
     */
43
    @Override
44
    public DbContextTableQueryBuilder query() {
45
        return this;
1✔
46
    }
47

48

49
    @CheckReturnValue
50
    private DbContextTableQueryBuilder query(@SuppressWarnings("unused") DatabaseTableQueryBuilder builder) {
51
        return this;
1✔
52
    }
53

54
    /**
55
     * Add the arguments to the column list for the <code>SELECT column, column...</code> statement
56
     */
57
    @CheckReturnValue
58
    public DbContextSelectBuilder select(String... columns) {
59
        return new DbContextSelectBuilder(dbContextTable.getDbContext(), builder.select(columns));
×
60
    }
61

62
    /**
63
     * Adds the parameter to the WHERE-clause and all the parameter list.
64
     * E.g. <code>where(new DatabaseQueryParameter("created_at between ? and ?", List.of(earliestDate, latestDate)))</code>
65
     */
66
    @Override
67
    public DbContextTableQueryBuilder where(DatabaseQueryParameter parameter) {
68
        return query(builder.where(parameter));
1✔
69
    }
70

71
    /**
72
     * Adds <code>WHERE column in (SELECT ....)</code> with the provided selectBuilder
73
     * and adds the parameters from the builder
74
     */
75
    public DbContextTableQueryBuilder whereSubselect(String column, DbContextSelectBuilder selectBuilder) {
76
        return whereSubselect(column, selectBuilder.getBuilder());
1✔
77
    }
78

79
    /**
80
     * Adds <code>ORDER BY ...</code> clause to the <code>SELECT</code> statement
81
     */
82
    @Override
83
    public DbContextTableQueryBuilder orderBy(String orderByClause) {
84
        return query(builder.orderBy(orderByClause));
1✔
85
    }
86

87
    /**
88
     * If you haven't called {@link #orderBy}, the results of {@link #list}
89
     * will be unpredictable. Call <code>unordered()</code> if you are okay with this.
90
     */
91
    @CheckReturnValue
92
    public DbContextTableQueryBuilder unordered() {
93
        return query(builder.unordered());
1✔
94
    }
95

96
    /**
97
     * Adds <code>FETCH ... ROWS ONLY</code> clause to the <code>SELECT</code> statement.
98
     * FETCH FIRST was introduced in
99
     * <a href="https://en.wikipedia.org/wiki/Select_%28SQL%29#Limiting_result_rows">SQL:2008</a>
100
     * and is supported by Postgresql 8.4, Oracle 12c, IBM DB2, HSQLDB, H2, and SQL Server 2012.
101
     */
102
    @CheckReturnValue
103
    public DbContextSelectBuilder limit(int rowCount) {
104
        return skipAndLimit(0, rowCount);
1✔
105
    }
106

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

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

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

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

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

162
    /**
163
     * Returns a string from the specified column name
164
     *
165
     * @return the mapped row if one row is returned, {@link SingleRow#absent} otherwise
166
     * @throws MultipleRowsReturnedException if more than one row was matched the query
167
     */
168
    @Nonnull
169
    @Override
170
    public SingleRow<String> singleString(String fieldName) {
171
        return builder.singleString(getConnection(), fieldName);
1✔
172
    }
173

174
    /**
175
     * Executes the <code>SELECT * FROM ...</code> statement and calls back to
176
     * {@link DatabaseResult.RowConsumer} for each returned row
177
     */
178
    @Override
179
    public void forEach(DatabaseResult.RowConsumer consumer) {
180
        builder.forEach(getConnection(), consumer);
1✔
181
    }
1✔
182

183
    /**
184
     * Executes <code>DELETE FROM tableName WHERE ....</code>
185
     */
186
    public int executeDelete() {
187
        return builder.delete(getConnection());
1✔
188
    }
189

190
    /**
191
     * Creates a {@link DbContextUpdateBuilder} object to fluently generate a <code>UPDATE ...</code> statement
192
     */
193
    @CheckReturnValue
194
    public DbContextUpdateBuilder update() {
195
        return new DbContextUpdateBuilder(this.dbContextTable, builder.update());
1✔
196
    }
197

198
    @CheckReturnValue
199
    public DbContextInsertOrUpdateBuilder insertOrUpdate() {
200
        return new DbContextInsertOrUpdateBuilder(this.dbContextTable, builder.insertOrUpdate());
1✔
201
    }
202

203
    @CheckReturnValue
204
    private Connection getConnection() {
205
        return dbContextTable.getConnection();
1✔
206
    }
207
}
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