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

torand / FasterSQL / 13059422121

30 Jan 2025 07:06PM UTC coverage: 45.739% (-0.03%) from 45.768%
13059422121

push

github

torand
doc: more details in README

108 of 334 branches covered (32.34%)

Branch coverage included in aggregate %.

622 of 1262 relevant lines covered (49.29%)

2.6 hits per line

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

0.0
/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.*;
26
import java.time.Instant;
27
import java.time.LocalDate;
28
import java.time.LocalDateTime;
29
import java.util.List;
30
import java.util.UUID;
31

32
/**
33
 * Builder for creating PreparedStatement with a Connection.
34
 */
35
public class PreparedStatementBuilder {
36
    private static final Logger LOGGER = LoggerFactory.getLogger(PreparedStatementBuilder.class);
×
37
    private final Connection connection;
38

39
    public static PreparedStatementBuilder using(Connection connection) {
40
        return new PreparedStatementBuilder(connection);
×
41
    }
42

43
    private PreparedStatementBuilder(Connection connection) {
×
44
        this.connection = connection;
×
45
    }
×
46

47
    public PreparedStatement prepare(PreparableStatement statement) throws SQLException {
48
        LOGGER.debug("Preparing SQL statement (ANSI/ISO SQL): {}", statement);
×
49

50
        Context context = Context.of(Dialect.fromConnection(connection));
×
51
        String sql = statement.sql(context);
×
52
        LOGGER.debug("Generated {} SQL statement: {}", context.getDialect().getProductName(), sql);
×
53
        List<Object> params = statement.params(context);
×
54

55
        PreparedStatement stmt = connection.prepareStatement(sql);
×
56
        int i = 1;
×
57
        for (Object param : params) {
×
58
            if (param instanceof Instant instant) {
×
59
                stmt.setTimestamp(i, Timestamp.from(instant));
×
60
            } else if (param instanceof LocalDateTime localDateTime) {
×
61
                stmt.setTimestamp(i, Timestamp.valueOf(localDateTime));
×
62
            } else if (param instanceof LocalDate localDate) {
×
63
                stmt.setDate(i, Date.valueOf(localDate));
×
64
            } else if (param instanceof UUID uuid) {
×
65
                stmt.setObject(i, uuid.toString());
×
66
            } else if (param instanceof URI uri) {
×
67
                stmt.setObject(i, uri.toString());
×
68
            } else if (param instanceof Enum enumValue) {
×
69
                stmt.setObject(i, enumValue.name());
×
70
            } else if (param instanceof InputStream inputStream) {
×
71
                stmt.setBinaryStream(i, inputStream);
×
72
            } else {
73
                stmt.setObject(i, param);
×
74
            }
75

76
            i++;
×
77
        }
×
78

79
        return stmt;
×
80
    }
81
}
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