• 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

89.47
/src/main/java/org/fluentjdbc/DbContextInsertBuilder.java
1
package org.fluentjdbc;
2

3
import javax.annotation.CheckReturnValue;
4
import java.util.Collection;
5
import java.util.List;
6
import java.util.Map;
7

8
/**
9
 * Generate <code>INSERT</code> statements by collecting field names and parameters. Support
10
 * autogeneration of primary keys. Example:
11
 *
12
 * <pre>
13
 * {@link DbContextTable} table = context.table("database_test_table");
14
 * try (DbContextConnection ignored = context.startConnection(dataSource)) {
15
 *     Long id = table.insert()
16
 *      .setPrimaryKey("id", (Long)null)
17
 *      .setField("name", "Something")
18
 *      .setField("code", 102)
19
 *      .execute();
20
 * }
21
 * </pre>
22
 *
23
 * @see DatabaseInsertBuilder
24
 */
25
public class DbContextInsertBuilder implements DatabaseUpdatable<DbContextInsertBuilder> {
26

27
    public class DbContextInsertBuilderWithPk<T> {
28

29
        private final DatabaseInsertWithPkBuilder<T> builder;
30

31
        public DbContextInsertBuilderWithPk(DatabaseInsertWithPkBuilder<T> builder) {
1✔
32
            this.builder = builder;
1✔
33
        }
1✔
34

35
        @CheckReturnValue
36
        public DbContextInsertBuilderWithPk<T> setField(String fieldName, Object parameter) {
37
            //noinspection ResultOfMethodCallIgnored
38
            builder.setField(fieldName, parameter);
1✔
39
            return this;
1✔
40
        }
41

42
        public T execute() {
43
            return builder.execute(dbContextTable.getConnection());
1✔
44
        }
45
    }
46

47
    private DatabaseInsertBuilder builder;
48
    private final DbContextTable dbContextTable;
49

50
    public DbContextInsertBuilder(DbContextTable dbContextTable) {
1✔
51
        this.dbContextTable = dbContextTable;
1✔
52
        builder = dbContextTable.getTable().insert();
1✔
53
    }
1✔
54

55
    /**
56
     * Adds primary key to the <code>INSERT</code> statement if idValue is not null. If idValue is null
57
     * this will {@link java.sql.PreparedStatement#execute(String, String[])} to generate the primary
58
     * key using the underlying table autogeneration mechanism
59
     */
60
    @CheckReturnValue
61
    public <T> DbContextInsertBuilderWithPk<T> setPrimaryKey(String idField, T idValue) {
62
        DatabaseInsertWithPkBuilder<T> setPrimaryKey = builder.setPrimaryKey(idField, idValue);
1✔
63
        return new DbContextInsertBuilderWithPk<>(setPrimaryKey);
1✔
64
    }
65

66
    /**
67
     * Adds fieldName to the <code>INSERT (field) VALUES (?)</code> and value to the list of parameters
68
     */
69
    @Override
70
    public DbContextInsertBuilder setField(String field, Object value) {
71
        return build(builder.setField(field, value));
1✔
72
    }
73

74
    /**
75
     * Adds fieldName to the <code>INSERT (field) VALUES (expression)</code> and value to the list of parameters
76
     */
77
    @Override
78
    public DbContextInsertBuilder setField(String field, String expression, Collection<?> values) {
NEW
79
        return build(builder.setField(field, expression, values));
×
80
    }
81

82
    /**
83
     * Calls {@link #setField(String, Object)} for each fieldName and parameter
84
     */
85
    @Override
86
    public DbContextInsertBuilder setFields(List<String> fields, List<?> values) {
87
        return build(builder.setFields(fields, values));
1✔
88
    }
89

90
    /**
91
     * Calls {@link #setField(String, Object)} for each key and value in the parameter map
92
     */
93
    @Override
94
    public DbContextInsertBuilder setFields(Map<String, ?> fields) {
UNCOV
95
        return build(builder.setFields(fields));
×
96
    }
97

98
    /**
99
     * Executes the insert statement and returns the number of rows inserted
100
     */
101
    public int execute() {
102
        return builder.execute(dbContextTable.getConnection());
1✔
103
    }
104

105
    private DbContextInsertBuilder build(DatabaseInsertBuilder builder) {
106
        this.builder = builder;
1✔
107
        return this;
1✔
108
    }
109
}
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