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

jhannes / fluent-jdbc / #184

12 Aug 2024 07:40AM UTC coverage: 93.009% (+0.02%) from 92.992%
#184

push

jhannes
[maven-release-plugin] prepare for next development iteration

1184 of 1273 relevant lines covered (93.01%)

0.93 hits per line

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

90.0
/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<Integer> listInt(String fieldName) {
67
        return list(row -> row.getInt(fieldName));
1✔
68
    }
69

70
    /**
71
     * Executes <code>SELECT fieldName FROM ...</code> on the query and returns the result as a list
72
     */
73
    @CheckReturnValue
74
    default List<Long> listLongs(String fieldName) {
75
        return list(row -> row.getLong(fieldName));
1✔
76
    }
77

78
    /**
79
     * If the query returns no rows, returns {@link SingleRow#absent}, if exactly one row is returned, maps it and return it,
80
     * if more than one is returned, throws `IllegalStateException`
81
     *
82
     * @param mapper Function object to map a single returned row to a object
83
     * @return the mapped row if one row is returned, {@link SingleRow#absent} otherwise
84
     * @throws MultipleRowsReturnedException if more than one row was matched the query
85
     */
86
    @Nonnull
87
    @CheckReturnValue
88
    <OBJECT> SingleRow<OBJECT> singleObject(DatabaseResult.RowMapper<OBJECT> mapper);
89

90
    /**
91
     * Returns a string from the specified column name
92
     *
93
     * @return the mapped row if one row is returned, {@link SingleRow#absent} otherwise
94
     * @throws MultipleRowsReturnedException if more than one row was matched the query
95
     */
96
    @Nonnull
97
    @CheckReturnValue
98
    default SingleRow<String> singleString(String fieldName) {
99
        return singleObject(row -> row.getString(fieldName));
×
100
    }
101

102
    /**
103
     * Returns an integer from the specified column name
104
     *
105
     * @return the mapped row if one row is returned, {@link SingleRow#absent} otherwise
106
     * @throws MultipleRowsReturnedException if more than one row was matched the query
107
     */
108
    @Nonnull
109
    @CheckReturnValue
110
    default SingleRow<Integer> singleInt(String fieldName) {
111
        return singleObject(row -> row.getInt(fieldName));
1✔
112
    }
113

114

115
    /**
116
     * Returns a long from the specified column name
117
     *
118
     * @return the mapped row if one row is returned, {@link SingleRow#absent} otherwise
119
     * @throws MultipleRowsReturnedException if more than one row was matched the query
120
     */
121
    @Nonnull
122
    @CheckReturnValue
123
    default SingleRow<Long> singleLong(String fieldName) {
124
        return singleObject(row -> row.getLong(fieldName));
1✔
125
    }
126

127
    /**
128
     * Executes <code>SELECT fieldName FROM ...</code> on the query.
129
     * If there is no rows, returns {@link SingleRow#absent}, if there is one result, returns it, if there
130
     * are more than one result, throws {@link IllegalStateException}
131
     */
132
    @Nonnull
133
    @CheckReturnValue
134
    default SingleRow<Instant> singleInstant(String fieldName) {
135
        return singleObject(row -> row.getInstant(fieldName));
1✔
136
    }
137

138
    /**
139
     * Executes <code>SELECT fieldName FROM ...</code> on the query.
140
     * If there is no rows, returns {@link SingleRow#absent}, if there is one result, returns the
141
     * binary stream of the object. Used with BLOB (Binary Large Objects) and bytea (PostgreSQL) data types
142
     */
143
    @Nonnull
144
    @CheckReturnValue
145
    default SingleRow<InputStream> singleInputStream(String fieldName) {
146
        return singleObject(row -> row.getInputStream(fieldName));
1✔
147
    }
148

149
    /**
150
     * Executes <code>SELECT fieldName FROM ...</code> on the query.
151
     * If there is no rows, returns {@link SingleRow#absent}, if there is one result, returns the
152
     * character reader of the object. Used with CLOB (Character Large Objects) and text (PostgreSQL) data types
153
     */
154
    @Nonnull
155
    @CheckReturnValue
156
    default SingleRow<Reader> singleReader(String fieldName) {
157
        return singleObject(row -> row.getReader(fieldName));
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
    void forEach(DatabaseResult.RowConsumer consumer);
165

166
    /**
167
     * Adds <code>FETCH ... ROWS ONLY</code> clause to the <code>SELECT</code> statement.
168
     * FETCH FIRST was introduced in
169
     * <a href="https://en.wikipedia.org/wiki/Select_%28SQL%29#Limiting_result_rows">SQL:2008</a>
170
     * and is supported by Postgresql 8.4, Oracle 12c, IBM DB2, HSQLDB, H2, and SQL Server 2012.
171
     */
172
    @CheckReturnValue
173
    default T limit(int rowCount) {
174
        return skipAndLimit(0, rowCount);
1✔
175
    }
176

177
    /**
178
     * Adds <code>OFFSET ... ROWS FETCH ... ROWS ONLY</code> clause to the <code>SELECT</code>
179
     * statement. FETCH FIRST was introduced in
180
     * <a href="https://en.wikipedia.org/wiki/Select_%28SQL%29#Limiting_result_rows">SQL:2008</a>
181
     * and is supported by Postgresql 8.4, Oracle 12c, IBM DB2, HSQLDB, H2, and SQL Server 2012.
182
     */
183
    @CheckReturnValue
184
    T skipAndLimit(int offset, int rowCount);
185
}
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

© 2025 Coveralls, Inc