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

torand / FasterSQL / 13431077458

20 Feb 2025 08:34AM UTC coverage: 62.338% (-0.06%) from 62.396%
13431077458

push

github

torand
feat: convert statement params of type OffsetDateTime and ZonedDateTime into Timestamp

168 of 350 branches covered (48.0%)

Branch coverage included in aggregate %.

2 of 4 new or added lines in 1 file covered. (50.0%)

888 of 1344 relevant lines covered (66.07%)

3.5 hits per line

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

77.19
/src/main/java/io/github/torand/fastersql/statement/PreparedStatementBuilder.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.dialect.Dialect;
20
import org.slf4j.Logger;
21
import org.slf4j.LoggerFactory;
22

23
import java.io.InputStream;
24
import java.net.URI;
25
import java.sql.Connection;
26
import java.sql.Date;
27
import java.sql.PreparedStatement;
28
import java.sql.SQLException;
29
import java.sql.Timestamp;
30
import java.time.Instant;
31
import java.time.LocalDate;
32
import java.time.LocalDateTime;
33
import java.time.OffsetDateTime;
34
import java.time.ZoneOffset;
35
import java.time.ZonedDateTime;
36
import java.util.List;
37
import java.util.UUID;
38

39
/**
40
 * Builder for creating PreparedStatement with a Connection.
41
 */
42
public class PreparedStatementBuilder {
43
    private static final Logger LOGGER = LoggerFactory.getLogger(PreparedStatementBuilder.class);
4✔
44
    private final Connection connection;
45
    private final Dialect dialect;
46

47
    public static PreparedStatementBuilder using(Connection connection) {
48
        return new PreparedStatementBuilder(connection, Dialect.fromConnection(connection));
×
49
    }
50

51
    public static PreparedStatementBuilder using(Connection connection, Dialect dialect) {
52
        return new PreparedStatementBuilder(connection, dialect);
6✔
53
    }
54

55
    private PreparedStatementBuilder(Connection connection, Dialect dialect) {
2✔
56
        this.connection = connection;
3✔
57
        this.dialect = dialect;
3✔
58
    }
1✔
59

60
    public PreparedStatement prepare(PreparableStatement statement) throws SQLException {
61
        LOGGER.debug("Preparing SQL statement (ANSI/ISO SQL): {}", statement);
4✔
62

63
        Context context = Context.of(dialect);
4✔
64

65
        String sql = statement.sql(context);
4✔
66
        LOGGER.debug("Generated {} SQL statement: {}", context.getDialect().getProductName(), sql);
7✔
67
        List<Object> params = statement.params(context);
4✔
68

69
        PreparedStatement stmt = connection.prepareStatement(sql);
5✔
70
        int i = 1;
2✔
71
        for (Object param : params) {
9✔
72
            if (param instanceof Instant instant) {
3!
73
                stmt.setTimestamp(i, Timestamp.from(instant));
×
74
            } else if (param instanceof LocalDateTime localDateTime) {
6✔
75
                stmt.setTimestamp(i, Timestamp.valueOf(localDateTime));
6✔
76
            } else if (param instanceof LocalDate localDate) {
3!
77
                stmt.setDate(i, Date.valueOf(localDate));
×
78
            } else if (param instanceof OffsetDateTime offsetDateTime) {
3!
NEW
79
                stmt.setTimestamp(i, Timestamp.valueOf(offsetDateTime.atZoneSameInstant(ZoneOffset.UTC).toLocalDateTime()));
×
80
            } else if (param instanceof ZonedDateTime zonedDateTime) {
3!
NEW
81
                stmt.setTimestamp(i, Timestamp.valueOf(zonedDateTime.toLocalDateTime()));
×
82
            } else if (param instanceof UUID uuid) {
6✔
83
                stmt.setObject(i, uuid.toString());
6✔
84
            } else if (param instanceof URI uri) {
3!
85
                stmt.setObject(i, uri.toString());
×
86
            } else if (param instanceof Enum enumValue) {
6✔
87
                stmt.setObject(i, enumValue.name());
6✔
88
            } else if (param instanceof InputStream inputStream) {
3!
89
                stmt.setBinaryStream(i, inputStream);
×
90
            } else {
91
                stmt.setObject(i, param);
4✔
92
            }
93

94
            i++;
1✔
95
        }
1✔
96

97
        return stmt;
2✔
98
    }
99
}
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