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

torand / FasterSQL / 13591799490

28 Feb 2025 03:59PM UTC coverage: 65.237% (-1.3%) from 66.563%
13591799490

push

github

web-flow
Merge pull request #14 from torand/having-support

feat: supporting the HAVING clause + IS NULL operator now supports an…

214 of 408 branches covered (52.45%)

Branch coverage included in aggregate %.

273 of 389 new or added lines in 69 files covered. (70.18%)

3 existing lines in 3 files now uncovered.

1079 of 1574 relevant lines covered (68.55%)

3.68 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.Column;
19
import io.github.torand.fastersql.Context;
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<ColumnValue> columnValues;
41

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

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

51
        List<ColumnValue> concatenated = concat(columnValues, new ColumnValue(column, expression));
14✔
52
        return new InsertStatement(table, concatenated);
7✔
53
    }
54

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

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

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

66
        if (maybeValue.isPresent()) {
3!
NEW
67
            List<ColumnValue> concatenated = concat(columnValues, new ColumnValue(column, $(maybeValue.get())));
×
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(columnValues).map(cv -> cv.column().sql(localContext)).collect(joining(", ")));
18✔
83
        sb.append(") values (").append(streamSafely(columnValues).map(cv -> cv.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(columnValues)
6✔
93
            .flatMap(cv -> cv.params(localContext))
5✔
94
            .toList();
1✔
95
    }
96

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

107
    private void validateColumnTableRelations(Stream<Column> columns) {
108
        columns
3✔
109
            .filter(c -> !table.name().equals(c.table().name()))
11!
110
            .findFirst()
2✔
111
            .ifPresent(c -> {
1✔
NEW
112
                throw new IllegalStateException("Column " + c.name() + " belongs to table " + c.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