• 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

87.5
/src/main/java/org/fluentjdbc/DbContextListableSelect.java
1
package org.fluentjdbc;
2

3
import javax.annotation.CheckReturnValue;
4
import javax.annotation.Nonnull;
5
import java.io.InputStream;
6
import java.io.Reader;
7
import java.time.Instant;
8
import java.util.List;
9
import java.util.stream.Stream;
10

11

12
/**
13
 * Interface to execute terminal operations for <code>SELECT</code>-statements. Usage:
14
 *
15
 * <pre>
16
 * try (DbContextConnection ignored = context.startConnection(dataSource)) {
17
 *     table.where...().list(row -&gt; row.getUUID("column"));
18
 *     table.where...().single(row -&gt; row.getLocalDate("column"));
19
 * }
20
 * </pre>
21
 */
22
public interface DbContextListableSelect<T extends DbContextListableSelect<T>> extends DatabaseQueryable<T> {
23

24
    /**
25
     * Adds <code>ORDER BY ...</code> clause to the <code>SELECT</code> statement
26
     */
27
    @CheckReturnValue
28
    T orderBy(String orderByClause);
29

30
    /**
31
     * Executes <code>SELECT count(*) FROM ...</code> on the query and returns the result
32
     */
33
    @CheckReturnValue
34
    int getCount();
35

36
    /**
37
     * Execute the query and map each return value over the {@link DatabaseResult.RowMapper} function to return a stream. Example:
38
     * <pre>
39
     *     table.where("status", status).stream(row -&gt; row.getInstant("created_at"))
40
     * </pre>
41
     */
42
    @CheckReturnValue
43
    <OBJECT> Stream<OBJECT> stream(DatabaseResult.RowMapper<OBJECT> mapper);
44

45
    /**
46
     * Execute the query and map each return value over the {@link DatabaseResult.RowMapper} function to return a list. Example:
47
     * <pre>
48
     *     List&lt;Instant&gt; creationTimes = table.where("status", status).list(row -&gt; row.getInstant("created_at"))
49
     * </pre>
50
     */
51
    @CheckReturnValue
52
    <OBJECT> List<OBJECT> list(DatabaseResult.RowMapper<OBJECT> mapper);
53

54
    /**
55
     * Executes <code>SELECT fieldName FROM ...</code> on the query and returns the result as a list
56
     */
57
    @CheckReturnValue
58
    default List<String> listStrings(String fieldName) {
59
        return list(row -> row.getString(fieldName));
1✔
60
    }
61

62
    /**
63
     * Executes <code>SELECT fieldName FROM ...</code> on the query and returns the result as a list
64
     */
65
    @CheckReturnValue
66
    default List<Long> listLongs(String fieldName) {
67
        return list(row -> row.getLong(fieldName));
1✔
68
    }
69

70
    /**
71
     * If the query returns no rows, returns {@link SingleRow#absent}, if exactly one row is returned, maps it and return it,
72
     * if more than one is returned, throws `IllegalStateException`
73
     *
74
     * @param mapper Function object to map a single returned row to a object
75
     * @return the mapped row if one row is returned, Optional.empty otherwise
76
     * @throws IllegalStateException if more than one row was matched the the query
77
     */
78
    @Nonnull
79
    @CheckReturnValue
80
    <OBJECT> SingleRow<OBJECT> singleObject(DatabaseResult.RowMapper<OBJECT> mapper);
81

82
    /**
83
     * Returns a string from the specified column name
84
     *
85
     * @return the mapped row if one row is returned, Optional.empty otherwise
86
     * @throws IllegalStateException if more than one row was matched the the query
87
     */
88
    @Nonnull
89
    @CheckReturnValue
90
    default SingleRow<String> singleString(String fieldName) {
UNCOV
91
        return singleObject(row -> row.getString(fieldName));
×
92
    }
93

94
    /**
95
     * Returns a long from the specified column name
96
     *
97
     * @return the mapped row if one row is returned, Optional.empty otherwise
98
     * @throws IllegalStateException if more than one row was matched the the query
99
     */
100
    @Nonnull
101
    @CheckReturnValue
102
    default SingleRow<Number> singleLong(String fieldName) {
103
        return singleObject(row -> row.getLong(fieldName));
1✔
104
    }
105

106
    /**
107
     * Executes <code>SELECT fieldName FROM ...</code> on the query.
108
     * If there is no rows, returns {@link SingleRow#absent}, if there is one result, returns it, if there
109
     * are more than one result, throws {@link IllegalStateException}
110
     */
111
    @Nonnull
112
    @CheckReturnValue
113
    default SingleRow<Instant> singleInstant(String fieldName) {
114
        return singleObject(row -> row.getInstant(fieldName));
1✔
115
    }
116

117
    /**
118
     * Executes <code>SELECT fieldName FROM ...</code> on the query.
119
     * If there is no rows, returns {@link SingleRow#absent}, if there is one result, returns the
120
     * binary stream of the object. Used with BLOB (Binary Large Objects) and bytea (PostgreSQL) data types
121
     */
122
    @Nonnull
123
    @CheckReturnValue
124
    default SingleRow<InputStream> singleInputStream(String fieldName) {
125
        return singleObject(row -> row.getInputStream(fieldName));
1✔
126
    }
127

128
    /**
129
     * Executes <code>SELECT fieldName FROM ...</code> on the query.
130
     * If there is no rows, returns {@link SingleRow#absent}, if there is one result, returns the
131
     * character reader of the object. Used with CLOB (Character Large Objects) and text (PostgreSQL) data types
132
     */
133
    @Nonnull
134
    @CheckReturnValue
135
    default SingleRow<Reader> singleReader(String fieldName) {
136
        return singleObject(row -> row.getReader(fieldName));
1✔
137
    }
138

139
    /**
140
     * Executes the <code>SELECT * FROM ...</code> statement and calls back to
141
     * {@link DatabaseResult.RowConsumer} for each returned row
142
     */
143
    void forEach(DatabaseResult.RowConsumer consumer);
144

145
    /**
146
     * Adds <code>FETCH ... ROWS ONLY</code> clause to the <code>SELECT</code> statement.
147
     * FETCH FIRST was introduced in
148
     * <a href="https://en.wikipedia.org/wiki/Select_%28SQL%29#Limiting_result_rows">SQL:2008</a>
149
     * and is supported by Postgresql 8.4, Oracle 12c, IBM DB2, HSQLDB, H2, and SQL Server 2012.
150
     */
151
    @CheckReturnValue
152
    default T limit(int rowCount) {
153
        return skipAndLimit(0, rowCount);
1✔
154
    }
155

156
    /**
157
     * Adds <code>OFFSET ... ROWS FETCH ... ROWS ONLY</code> clause to the <code>SELECT</code>
158
     * statement. FETCH FIRST was introduced in
159
     * <a href="https://en.wikipedia.org/wiki/Select_%28SQL%29#Limiting_result_rows">SQL:2008</a>
160
     * and is supported by Postgresql 8.4, Oracle 12c, IBM DB2, HSQLDB, H2, and SQL Server 2012.
161
     */
162
    @CheckReturnValue
163
    T skipAndLimit(int offset, int rowCount);
164
}
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