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

torand / FasterSQL / 13367709795

17 Feb 2025 10:06AM UTC coverage: 58.785% (+2.0%) from 56.762%
13367709795

push

github

torand
feat: support length function

148 of 338 branches covered (43.79%)

Branch coverage included in aggregate %.

10 of 17 new or added lines in 7 files covered. (58.82%)

66 existing lines in 9 files now uncovered.

829 of 1324 relevant lines covered (62.61%)

3.33 hits per line

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

82.0
/src/main/java/io/github/torand/fastersql/statement/InsertStatement.java
1
/*
2
 * Copyright (c) 2024 Tore Eide Andersen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.torand.fastersql.statement;
17

18
import io.github.torand.fastersql.Context;
19
import io.github.torand.fastersql.Field;
20
import io.github.torand.fastersql.Table;
21
import io.github.torand.fastersql.expression.Expression;
22

23
import java.util.Collection;
24
import java.util.List;
25
import java.util.Optional;
26
import java.util.stream.Stream;
27

28
import static io.github.torand.fastersql.Command.INSERT;
29
import static io.github.torand.fastersql.constant.Constants.$;
30
import static io.github.torand.fastersql.util.collection.CollectionHelper.asList;
31
import static io.github.torand.fastersql.util.collection.CollectionHelper.concat;
32
import static io.github.torand.fastersql.util.collection.CollectionHelper.isEmpty;
33
import static io.github.torand.fastersql.util.collection.CollectionHelper.streamSafely;
34
import static java.util.Objects.isNull;
35
import static java.util.Objects.requireNonNull;
36
import static java.util.stream.Collectors.joining;
37

38
public class InsertStatement extends PreparableStatement {
39
    private final Table<?> table;
40
    private final List<FieldValue> fieldValues;
41

42
    InsertStatement(Table<?> table, Collection<FieldValue> fieldValues) {
2✔
43
        this.table = requireNonNull(table, "No table specified");
6✔
44
        this.fieldValues = asList(fieldValues);
4✔
45
    }
1✔
46

47
    public InsertStatement value(Field field, Expression expression) {
48
        requireNonNull(field, "No field specified");
4✔
49
        requireNonNull(expression, "No expression specified");
4✔
50

51
        List<FieldValue> concatenated = concat(fieldValues, new FieldValue(field, expression));
14✔
52
        return new InsertStatement(table, concatenated);
7✔
53
    }
54

55
    public InsertStatement value(Field field, Object value) {
56
        requireNonNull(field, "No field specified");
4✔
57

58
        List<FieldValue> concatenated = concat(fieldValues, new FieldValue(field, $(value)));
15✔
59
        return new InsertStatement(table, concatenated);
7✔
60
    }
61

62
    public InsertStatement value(Field field, Optional<?> maybeValue) {
63
        requireNonNull(field, "No field specified");
4✔
64
        requireNonNull(maybeValue, "No value specified");
4✔
65

66
        if (maybeValue.isPresent()) {
3!
UNCOV
67
            List<FieldValue> concatenated = concat(fieldValues, new FieldValue(field, $(maybeValue.get())));
×
UNCOV
68
            return new InsertStatement(table, concatenated);
×
69
        } else {
70
            return this;
2✔
71
        }
72
    }
73

74
    @Override
75
    String sql(Context context) {
76
        final Context localContext = context.withCommand(INSERT);
4✔
77
        validate();
2✔
78

79
        StringBuilder sb = new StringBuilder();
4✔
80
        sb.append("insert into ").append(table.sql(context));
9✔
81
        sb.append(" (");
4✔
82
        sb.append(streamSafely(fieldValues).map(fv -> fv.field().sql(localContext)).collect(joining(", ")));
18✔
83
        sb.append(") values (").append(streamSafely(fieldValues).map(fv -> fv.valueSql(localContext)).collect(joining(", ")));
19✔
84
        sb.append(")");
4✔
85

86
        return sb.toString();
3✔
87
    }
88

89
    @Override
90
    List<Object> params(Context context) {
91
        final Context localContext = context.withCommand(INSERT);
4✔
92
        return streamSafely(fieldValues)
6✔
93
            .flatMap(fv -> fv.params(localContext))
5✔
94
            .toList();
1✔
95
    }
96

97
    private void validate() {
98
        if (isNull(table)) {
4!
UNCOV
99
            throw new IllegalStateException("No table specified");
×
100
        }
101
        if (isEmpty(fieldValues)) {
4!
102
            throw new IllegalStateException("No values specified");
×
103
        }
104
        validateFieldTableRelations(streamSafely(fieldValues).map(FieldValue::field));
7✔
105
    }
1✔
106

107
    private void validateFieldTableRelations(Stream<Field> fields) {
108
        fields
3✔
109
            .filter(f -> !table.name().equals(f.table().name()))
11!
110
            .findFirst()
2✔
111
            .ifPresent(f -> {
1✔
UNCOV
112
                throw new IllegalStateException("Field " + f.name() + " belongs to table " + f.table().name() + ", not the table specified by the INTO clause");
×
113
            });
114
    }
1✔
115
}
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