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

torand / FasterSQL / 16990059345

15 Aug 2025 12:34PM UTC coverage: 66.782% (-6.6%) from 73.399%
16990059345

push

github

torand
chore: access central snapshots

294 of 598 branches covered (49.16%)

Branch coverage included in aggregate %.

1638 of 2295 relevant lines covered (71.37%)

3.83 hits per line

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

44.64
/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 io.github.torand.fastersql.function.singlerow.cast.DataType;
19

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

27
import static io.github.torand.fastersql.dialect.Capability.CONCAT_OPERATOR;
28
import static io.github.torand.fastersql.dialect.Capability.CURRENT_TIME;
29
import static io.github.torand.fastersql.dialect.Capability.LIMIT_OFFSET;
30
import static io.github.torand.fastersql.dialect.Capability.MODULO_OPERATOR;
31
import static io.github.torand.fastersql.dialect.Capability.NULL_ORDERING;
32
import static io.github.torand.fastersql.dialect.Capability.SELECT_FOR_UPDATE;
33
import static io.github.torand.fastersql.dialect.Capability.SET_OPERATION_PARENTHESES;
34
import static io.github.torand.fastersql.dialect.Capability.TRUNCATE_TABLE;
35

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

42
    /**
43
     * Creates a H2 {@link Dialect} implementation.
44
     */
45
    public H2Dialect() {}
3✔
46

47
    @Override
48
    public String getProductName() {
49
        return "H2";
2✔
50
    }
51

52
    @Override
53
    public boolean offsetBeforeLimit() {
54
        return false;
2✔
55
    }
56

57
    @Override
58
    public Optional<String> formatRowOffsetClause() {
59
        return Optional.of("offset ?");
3✔
60
    }
61

62
    @Override
63
    public Optional<String> formatRowLimitClause() {
64
        return Optional.of("limit ?");
3✔
65
    }
66

67
    @Override
68
    public Optional<String> formatRowNumLiteral() {
69
        return Optional.of("rownum()");
×
70
    }
71

72
    @Override
73
    public String formatToNumberFunction(String operand, int precision, int scale) {
74
        return "to_number(" + operand + ")";
3✔
75
    }
76

77
    @Override
78
    public String formatToCharFunction(String operand, String format) {
79
        return "to_char(" + operand + ", " + format + ")";
4✔
80
    }
81

82
    @Override
83
    public String formatSubstringFunction(String operand, int startPos, int length) {
84
        return "substring(" + operand + ", " + startPos + ", " + length + ")";
5✔
85
    }
86

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

92
    @Override
93
    public String formatLengthFunction(String operand) {
94
        return "char_length(" + operand + ")";
3✔
95
    }
96

97
    @Override
98
    public String formatCeilFunction(String operand) {
99
        return "ceiling(" + operand + ")";
3✔
100
    }
101

102
    @Override
103
    public String formatLnFunction(String operand) {
104
        return "ln(" + operand + ")";
3✔
105
    }
106

107
    @Override
108
    public String formatRoundFunction(String operand) {
109
        return "round(" + operand + ")";
3✔
110
    }
111

112
    @Override
113
    public String formatModuloFunction(String divisor, String dividend) {
114
        throw new UnsupportedOperationException("H2 does not support the mod() function (use the modulo infix operator instead)");
×
115
    }
116

117
    @Override
118
    public String formatCurrentDateFunction() {
119
        return "current_date";
2✔
120
    }
121

122
    @Override
123
    public Optional<String> getDataType(DataType dataType) {
124
        // https://www.h2database.com/html/datatypes.html
125
        return Optional.ofNullable(switch(dataType.getIsoDataType()) {
8!
126
            case BOOLEAN -> "boolean";
×
127
            case CHAR -> "char";
×
128
            case VARCHAR -> "varchar";
2✔
129
            case BIT, BIT_VARYING -> null;
×
130
            case NUMERIC -> "numeric";
×
131
            case DECIMAL -> "decimal";
2✔
132
            case INTEGER -> "integer";
×
133
            case SMALLINT -> "smallint";
×
134
            case FLOAT, DOUBLE_PRECISION -> "float";
×
135
            case REAL -> "real";
×
136
            case DATE -> "date";
×
137
            case TIME -> "time";
×
138
            case INTERVAL -> "interval";
×
139
            case CHARACTER_LARGE_OBJECT -> "clob";
×
140
            case BINARY_LARGE_OBJECT -> "blob";
×
141
        });
142
    }
143

144
    @Override
145
    public Optional<String> getConcatOperator() {
146
        return Optional.of("||");
3✔
147
    }
148

149
    @Override
150
    public boolean supports(Capability capability) {
151
        return SUPPORTED_CAPS.contains(capability);
4✔
152
    }
153

154
    /**
155
     * Adds some user defined functions emulating common SQL constructs not supported by default.
156
     * @param connection a live H2 connection.
157
     * @return the modified dialect.
158
     */
159
    public H2Dialect withCustomizations(Connection connection) {
160
        String source = """
2✔
161
            import java.lang.*;
162
            @CODE
163
            Double toNumber(String value) throws Exception {
164
                return value == null ? null : Double.valueOf(value);
165
            }
166
            """;
167

168
        try (PreparedStatement preparedStatement = connection.prepareStatement("CREATE ALIAS IF NOT EXISTS TO_NUMBER AS '%s'".formatted(source))) {
11✔
169
            preparedStatement.execute();
3✔
170
            return this;
4✔
171
        } catch (SQLException e) {
×
172
            throw new RuntimeException("Failed to setup H2 customizations", e);
×
173
        }
174
    }
175
}
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