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

torand / FasterSQL / 15071216480

16 May 2025 02:49PM UTC coverage: 69.877% (+2.4%) from 67.475%
15071216480

push

github

web-flow
Merge pull request #30 from torand/access-support

Access support

229 of 414 branches covered (55.31%)

Branch coverage included in aggregate %.

105 of 152 new or added lines in 26 files covered. (69.08%)

3 existing lines in 3 files now uncovered.

1193 of 1621 relevant lines covered (73.6%)

3.92 hits per line

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

80.0
/src/main/java/io/github/torand/fastersql/statement/InsertStatement.java
1
/*
2
 * Copyright (c) 2024-2025 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.dialect.AnsiIsoDialect;
22
import io.github.torand.fastersql.expression.Expression;
23

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

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

39
/**
40
 * Implements an INSERT statement.
41
 */
42
public class InsertStatement implements PreparableStatement {
43
    private final Table<?> table;
44
    private final List<ColumnValue> columnValues;
45

46
    InsertStatement(Table<?> table, Collection<ColumnValue> columnValues) {
2✔
47
        this.table = requireNonNull(table, "No table specified");
6✔
48
        this.columnValues = asList(columnValues);
4✔
49
    }
1✔
50

51
    /**
52
     * Adds an expression value to be inserted into a column.
53
     * @param column the column.
54
     * @param value the value.
55
     * @return the modified statement.
56
     */
57
    public InsertStatement value(Column column, Expression value) {
58
        requireNonNull(column, "No column specified");
4✔
59
        requireNonNull(value, "No expression specified");
4✔
60

61
        List<ColumnValue> concatenated = concat(columnValues, new ColumnValue(column, value));
14✔
62
        return new InsertStatement(table, concatenated);
7✔
63
    }
64

65
    /**
66
     * Adds a constant value to be inserted into a column.
67
     * @param column the column.
68
     * @param value the value.
69
     * @return the modified statement.
70
     */
71
    public InsertStatement value(Column column, Object value) {
72
        requireNonNull(column, "No column specified");
4✔
73

74
        List<ColumnValue> concatenated = concat(columnValues, new ColumnValue(column, $(value)));
15✔
75
        return new InsertStatement(table, concatenated);
7✔
76
    }
77

78
    /**
79
     * Adds an optional value to be inserted into a column.
80
     * The value is inserted only if the optional value is present.
81
     * @param column the column.
82
     * @param maybeValue the optional value.
83
     * @return the modified statement.
84
     */
85
    public InsertStatement value(Column column, Optional<?> maybeValue) {
86
        requireNonNull(column, "No column specified");
4✔
87
        requireNonNull(maybeValue, "No value specified");
4✔
88

89
        if (maybeValue.isPresent()) {
3!
90
            List<ColumnValue> concatenated = concat(columnValues, new ColumnValue(column, $(maybeValue.get())));
×
91
            return new InsertStatement(table, concatenated);
×
92
        } else {
93
            return this;
2✔
94
        }
95
    }
96

97
    @Override
98
    public String sql(Context context) {
99
        final Context localContext = context.withCommand(INSERT);
4✔
100
        validate();
2✔
101

102
        StringBuilder sb = new StringBuilder();
4✔
103
        sb.append("insert into ").append(table.sql(context));
9✔
104
        sb.append(" (");
4✔
105
        sb.append(streamSafely(columnValues).map(cv -> cv.column().sql(localContext)).collect(joining(", ")));
18✔
106
        sb.append(") values (").append(streamSafely(columnValues).map(cv -> cv.valueSql(localContext)).collect(joining(", ")));
19✔
107
        sb.append(")");
4✔
108

109
        return sb.toString();
3✔
110
    }
111

112
    @Override
113
    public Stream<Object> params(Context context) {
114
        final Context localContext = context.withCommand(INSERT);
4✔
115
        return streamSafely(columnValues)
6✔
116
            .flatMap(cv -> cv.valueParams(localContext));
5✔
117
    }
118

119
    private void validate() {
120
        if (isNull(table)) {
4!
121
            throw new IllegalStateException("No table specified");
×
122
        }
123
        if (isEmpty(columnValues)) {
4!
124
            throw new IllegalStateException("No values specified");
×
125
        }
126
        validateColumnTableRelations(streamSafely(columnValues).map(ColumnValue::column));
7✔
127
    }
1✔
128

129
    private void validateColumnTableRelations(Stream<Column> columns) {
130
        columns
3✔
131
            .filter(c -> !table.name().equals(c.table().name()))
11!
132
            .findFirst()
2✔
133
            .ifPresent(c -> {
1✔
134
                throw new IllegalStateException("Column " + c.name() + " belongs to table " + c.table().name() + ", not the table specified by the INTO clause");
×
135
            });
136
    }
1✔
137

138
    @Override
139
    public String toString() {
NEW
140
        return toString(new AnsiIsoDialect());
×
141
    }
142
}
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