• 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/DbContextStatement.java
1
package org.fluentjdbc;
2

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

11
/**
12
 * Used to execute an arbitrary statement. Create with {@link DbContext#statement(String, List)}.
13
 *
14
 * <h2>Example</h2>
15
 *
16
 * <pre>
17
 * dbContext.statement(
18
 *      "update orders set quantity = quantity + 1 WHERE customer_id = ?",
19
 *      Arrays.asList(customerId)
20
 * ).executeUpdate();
21
 * </pre>
22
 */
23
public class DbContextStatement {
24
    private final DbContext dbContext;
25
    private final DatabaseStatement statement;
26

27
    public DbContextStatement(DbContext dbContext, String statement, List<Object> parameters) {
1✔
28
        this.dbContext = dbContext;
1✔
29
        this.statement = dbContext.getStatementFactory().newStatement("*", "*", statement, parameters);
1✔
30
    }
1✔
31

32
    /**
33
     * If the query returns no rows, returns {@link SingleRow#absent}, if exactly one row is returned, maps it and return it,
34
     * if more than one is returned, throws `IllegalStateException`
35
     *
36
     * @param mapper Function object to map a single returned row to an object
37
     * @return the mapped row if one row is returned, {@link SingleRow#absent} otherwise
38
     * @throws IllegalStateException if more than one row was matched the query
39
     */
40
    @Nonnull
41
    @CheckReturnValue
42
    public <OBJECT> SingleRow<OBJECT> singleObject(DatabaseResult.RowMapper<OBJECT> mapper) {
43
        return statement.singleObject(dbContext.getThreadConnection(), mapper);
1✔
44
    }
45

46
    /**
47
     * Execute the query and map each return value over the {@link DatabaseResult.RowMapper} function to return a stream. Example:
48
     * <pre>
49
     *     table.where("status", status).stream(row -&gt; row.getInstant("created_at"))
50
     * </pre>
51
     */
52
    @CheckReturnValue
53
    public <OBJECT> Stream<OBJECT> stream(DatabaseResult.RowMapper<OBJECT> mapper) {
54
        return statement.stream(dbContext.getThreadConnection(), mapper);
1✔
55
    }
56

57
    /**
58
     * Execute the query and map each return value over the {@link DatabaseResult.RowMapper} function to return a list. Example:
59
     * <pre>
60
     *     List&lt;Instant&gt; creationTimes = table.where("status", status).list(row -&gt; row.getInstant("created_at"))
61
     * </pre>
62
     */
63
    @CheckReturnValue
64
    public <OBJECT> List<OBJECT> list(DatabaseResult.RowMapper<OBJECT> mapper) {
65
        return stream(mapper).collect(Collectors.toList());
1✔
66
    }
67

68
    /**
69
     * Calls {@link Connection#prepareStatement(String)} with the statement,
70
     * {@link DatabaseStatement#bindParameters(PreparedStatement, List)}, converting each parameter in the process
71
     * and executes the statement
72
     */
73
    public int executeUpdate(Connection connection) {
UNCOV
74
        return statement.executeUpdate(connection);
×
75
    }
76
}
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