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

torand / FasterSQL / 15346133148

30 May 2025 11:50AM UTC coverage: 70.397% (+0.1%) from 70.298%
15346133148

push

github

torand
chore: test javadoc publish

235 of 420 branches covered (55.95%)

Branch coverage included in aggregate %.

1237 of 1671 relevant lines covered (74.03%)

3.9 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 <a href="https://www.h2database.com/html/grammar.html">H2</a> SQL dialect.
35
 */
36
public class H2Dialect implements Dialect {
37
    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✔
38

39
    /**
40
     * Creates a H2 {@link Dialect} implementation.
41
     */
42
    public H2Dialect() {}
3✔
43

44
    @Override
45
    public String getProductName() {
46
        return "H2";
2✔
47
    }
48

49
    @Override
50
    public boolean offsetBeforeLimit() {
51
        return false;
2✔
52
    }
53

54
    @Override
55
    public Optional<String> formatRowOffsetClause() {
56
        return Optional.of("offset ?");
3✔
57
    }
58

59
    @Override
60
    public Optional<String> formatRowLimitClause() {
61
        return Optional.of("limit ?");
3✔
62
    }
63

64
    @Override
65
    public Optional<String> formatRowNumLiteral() {
66
        return Optional.of("rownum()");
×
67
    }
68

69
    @Override
70
    public String formatToNumberFunction(String operand, int precision, int scale) {
71
        return "to_number(" + operand + ")";
3✔
72
    }
73

74
    @Override
75
    public String formatToCharFunction(String operand, String format) {
76
        return "to_char(" + operand + ", " + format + ")";
4✔
77
    }
78

79
    @Override
80
    public String formatSubstringFunction(String operand, int startPos, int length) {
81
        return "substring(" + operand + ", " + startPos + ", " + length + ")";
5✔
82
    }
83

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

89
    @Override
90
    public String formatLengthFunction(String operand) {
91
        return "char_length(" + operand + ")";
3✔
92
    }
93

94
    @Override
95
    public String formatCeilFunction(String operand) {
96
        return "ceiling(" + operand + ")";
3✔
97
    }
98

99
    @Override
100
    public String formatRoundFunction(String operand) {
101
        return "round(" + operand + ")";
3✔
102
    }
103

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

109
    @Override
110
    public String formatCurrentDateFunction() {
111
        return "current_date";
2✔
112
    }
113

114
    @Override
115
    public Optional<String> getConcatOperator() {
116
        return Optional.of("||");
3✔
117
    }
118

119
    @Override
120
    public boolean supports(Capability capability) {
121
        return SUPPORTED_CAPS.contains(capability);
4✔
122
    }
123

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

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