Skip to content

Repository files navigation

h2-compatibility

Library of functions to extend H2 database with functions missing in compatibility modes.

Mainly for simplified integration testing in cases where running a real target database in container or standalone could be considered an overkill.

Aim is to provide missing functions, aggregations and types as compatible as possible, with minimal additional dependencies and targeting same minimal JDK version as H2 project, which is currently Java 11. Minimum H2 version that is compatible is 2.2.x.

Functions are split into modules to minimize dependency footprint and are dynamically registered using the Java ServiceLoader mechanism.

Disclaimer

Though the functions have a suite of tests, there might be differences to original DB functions and these are not optimized performance-wise - that means this library is intended for test usage and is not recommended for any production or mission-critical deployment.

Usage

1. Include required modules

Gradle

testRuntimeOnly("cz.miou.h2:h2-[DB]-[FUNCTIONS]:1.1.0")

Maven

<dependency>
    <groupId>cz.miou.h2</groupId>
    <artifactId>h2-[DB]-[FUNCTIONS]</artifactId>
    <version>1.1.0</version>
    <scope>test</scope>
</dependency>

2. Load functions to database

From SQL code

CREATE ALIAS IF NOT EXISTS H2_COMPATIBILITY FOR "cz.miou.h2.loader.Loader.load";
CALL H2_COMPATIBILITY();

From Java code

import java.sql.Connection;
import cz.miou.h2.Loader;

...

Connection connection = ... // JDBC connection for H2 database
Loader.load(connection);

Using Flyway migration

As a convenience option for projects using Flyway for handling database migrates, it's possible to add a dependency to h2-loader-flyway module, that will automatically provide a migration with version 0 targeting a H2 database environment, that would load all currently present functions.

Gradle
testRuntimeOnly("cz.miou.h2:h2-loader-flyway:1.1.0")
Maven
<dependency>
    <groupId>cz.miou.h2</groupId>
    <artifactId>h2-loader-flyway</artifactId>
    <version>1.1.0</version>
    <scope>test</scope>
</dependency>

Troubleshooting

If for some reason some of the functions aren't compatible with H2 anymore, it's possible to exclude them by specifying cz.miou.h2.loader.exclude system property with a coma separated list of function names.

Supported Functions

MariaDB / MySQL

Gradle

testRuntimeOnly("cz.miou.h2:h2-mariadb-all:1.1.0")

Maven

<dependency>
    <groupId>cz.miou.h2</groupId>
    <artifactId>h2-mariadb-all</artifactId>
    <version>1.1.0</version>
    <scope>test</scope>
</dependency>

In case a stricter control over dependencies is needed, it's possible to include only as specific module.

String functions

Module h2-mariadb-functions

List of supported functions

Numeric, binary and misc. functions

Module h2-mariadb-functions

List of supported functions

Date / Time functions

Module h2-mariadb-functions

List of supported functions

GET_FORMAT is intentionally left out because MariaDB requires a keyword first argument, for example GET_FORMAT(DATE, 'EUR') which is not possible to replicate with H2.

IP address functions

Module h2-mariadb-inet

List of supported functions

Hashing and cryptography functions

Module h2-mariadb-crypto

List of supported functions

JSON functions

Module h2-mariadb-json

List of supported functions

UUID functions

Module h2-mariadb-uuid

List of supported functions

Spatial functions

Module h2-mariadb-spatial

List of supported types
List of supported functions

ENVELOPE is not supported because it collides with H2's built-in ENVELOPE aggregate function.

Notes

Due to how DATE, TIME and TIMESTAMP types in H2 works, the functions behave as if MariaDB had ALLOW_INVALID_DATES, NO_ZERO_DATE and NO_ZERO_IN_DATE SQL modes disabled.

License

MIT

About

Library of functions to extend H2 database with functions missing in compatibility modes.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages