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

torand / FasterSQL / 15294773771

28 May 2025 08:01AM UTC coverage: 70.078% (+0.2%) from 69.877%
15294773771

Pull #31

github

web-flow
Merge 9f1c324c0 into 6be86da23
Pull Request #31: feat: add support for SQLite dialect

233 of 418 branches covered (55.74%)

Branch coverage included in aggregate %.

28 of 33 new or added lines in 12 files covered. (84.85%)

45 existing lines in 10 files now uncovered.

1212 of 1644 relevant lines covered (73.72%)

3.91 hits per line

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

79.17
/src/main/java/io/github/torand/fastersql/dialect/H2Dialect.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.dialect;
17

18
import java.sql.Connection;
19
import java.sql.PreparedStatement;
20
import java.sql.SQLException;
21
import java.util.EnumSet;
22
import java.util.List;
23
import java.util.Optional;
24

25
import static io.github.torand.fastersql.dialect.Capability.CONCAT_OPERATOR;
26
import static io.github.torand.fastersql.dialect.Capability.CURRENT_TIME;
27
import static io.github.torand.fastersql.dialect.Capability.LIMIT_OFFSET;
28
import static io.github.torand.fastersql.dialect.Capability.MODULO_OPERATOR;
29
import static io.github.torand.fastersql.dialect.Capability.NULL_ORDERING;
30
import static io.github.torand.fastersql.dialect.Capability.SELECT_FOR_UPDATE;
31
import static io.github.torand.fastersql.dialect.Capability.TRUNCATE_TABLE;
32

33
/**
34
 * Defines the H2 SQL dialect.
35
 *
36
 * <a href="https://www.h2database.com/html/grammar.html" />
37
 */
38
public class H2Dialect implements Dialect {
3✔
39
    private static final EnumSet<Capability> SUPPORTED_CAPS = EnumSet.of(LIMIT_OFFSET, CONCAT_OPERATOR, MODULO_OPERATOR, CURRENT_TIME, NULL_ORDERING, SELECT_FOR_UPDATE, TRUNCATE_TABLE);
30✔
40

41
    @Override
42
    public String getProductName() {
43
        return "H2";
2✔
44
    }
45

46
    @Override
47
    public boolean offsetBeforeLimit() {
48
        return false;
2✔
49
    }
50

51
    @Override
52
    public Optional<String> formatRowOffsetClause() {
53
        return Optional.of("offset ?");
3✔
54
    }
55

56
    @Override
57
    public Optional<String> formatRowLimitClause() {
58
        return Optional.of("limit ?");
3✔
59
    }
60

61
    @Override
62
    public Optional<String> formatRowNumLiteral() {
UNCOV
63
        return Optional.of("rownum()");
×
64
    }
65

66
    @Override
67
    public String formatToNumberFunction(String operand, int precision, int scale) {
68
        return "to_number(" + operand + ")";
3✔
69
    }
70

71
    @Override
72
    public String formatToCharFunction(String operand, String format) {
73
        return "to_char(" + operand + ", " + format + ")";
4✔
74
    }
75

76
    @Override
77
    public String formatSubstringFunction(String operand, int startPos, int length) {
78
        return "substring(" + operand + ", " + startPos + ", " + length + ")";
5✔
79
    }
80

81
    @Override
82
    public String formatConcatFunction(List<String> operands) {
UNCOV
83
        throw new UnsupportedOperationException("H2 does not support the concat() function (use the concat infix operator instead)");
×
84
    }
85

86
    @Override
87
    public String formatLengthFunction(String operand) {
88
        return "char_length(" + operand + ")";
3✔
89
    }
90

91
    @Override
92
    public String formatCeilFunction(String operand) {
93
        return "ceiling(" + operand + ")";
3✔
94
    }
95

96
    @Override
97
    public String formatRoundFunction(String operand) {
98
        return "round(" + operand + ")";
3✔
99
    }
100

101
    @Override
102
    public String formatModuloFunction(String divisor, String dividend) {
UNCOV
103
        throw new UnsupportedOperationException("H2 does not support the mod() function (use the modulo infix operator instead)");
×
104
    }
105

106
    @Override
107
    public String formatCurrentDateFunction() {
108
        return "current_date";
2✔
109
    }
110

111
    @Override
112
    public Optional<String> getConcatOperator() {
113
        return Optional.of("||");
3✔
114
    }
115

116
    @Override
117
    public boolean supports(Capability capability) {
118
        return SUPPORTED_CAPS.contains(capability);
4✔
119
    }
120

121
    /**
122
     * Adds some user defined functions emulating common SQL constructs not supported by default.
123
     * @param connection a live H2 connection.
124
     * @return the modified dialect.
125
     */
126
    public H2Dialect withCustomizations(Connection connection) {
127
        String source = """
2✔
128
            import java.lang.*;
129
            @CODE
130
            Double toNumber(String value) throws Exception {
131
                return value == null ? null : Double.valueOf(value);
132
            }
133
            """;
134

135
        try (PreparedStatement preparedStatement = connection.prepareStatement("CREATE ALIAS IF NOT EXISTS TO_NUMBER AS '%s'".formatted(source))) {
11✔
136
            preparedStatement.execute();
3✔
137
            return this;
4✔
138
        } catch (SQLException e) {
×
UNCOV
139
            throw new RuntimeException("Failed to setup H2 customizations", e);
×
140
        }
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