Coder Social home page Coder Social logo

r2dbc-h2's Introduction

Reactive Relational Database Connectivity H2 Implementation

Build Status

Maven Central

This project contains the H2 implementation of the R2DBC SPI. This implementation is not intended to be used directly, but rather to be used as the backing implementation for a humane client library.

This driver provides the following features:

  • Filesystem or in-memory instances

  • Explict transactions

  • Execution of prepared statements with bindings

  • Execution of batch statements without bindings

  • Read and write support for all data types except LOB types (e.g. BLOB, CLOB)

Warning
Since this driver runs on top of the internals of H2, there is risk of change. r2dbc-h2 does not guarantee compatibility except against the version of H2 found in the build file. Because various parts of H2 are blocking, like file and network access, the only non-blocking assurances are in the layers above H2. Nevertheless, r2dbc-h2 is a great way to warm up to the usage of R2DBC with a small footprint.

Code of Conduct

This project is governed by the [Spring Code of Conduct](CODE_OF_CONDUCT.adoc). By participating, you are expected to uphold this code of conduct. Please report unacceptable behavior to [[email protected]](mailto:[email protected]).

Getting Started

Here is a quick teaser of how to use R2DBC H2 in Java:

URL Connection Factory Discovery for In-Memory Databases

ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:mem:///testdb");

Publisher<? extends Connection> connectionPublisher = connectionFactory.create();

URL Connection Factory Discovery for File Databases

ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:file//my/relative/path");

Publisher<? extends Connection> connectionPublisher = connectionFactory.create();

Programmatic Connection Factory Discovery

ConnectionFactoryOptions options = builder()
    .option(DRIVER, "h2")
    .option(PROTOCOL, "...")  // file, mem
    .option(HOST, "…")
    .option(USER, "…")
    .option(PASSWORD, "…")
    .option(DATABASE, "…")
    .build();

ConnectionFactory connectionFactory = ConnectionFactories.get(options);

Publisher<? extends Connection> connectionPublisher = connectionFactory.create();

// Alternative: Creating a Mono using Project Reactor
Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

Supported ConnectionFactory Discovery Options

Option Description

driver

Must be h2.

protocol

Must be file, mem, or tcp. Requires database if set (Optional)

host

Only for tcp protocol: Server hostname to connect to. (Optional)

port

Only for tcp protocol: Server port to connect to. (Optional)

username

Login username.

password

Login password.

database

Database to use. For file protocol: Relative (r2dbc:h2:file//../relative/file/name) or absolute (r2dbc:h2:file///absolute/file/name) file name. For mem protocol: In-memory database name (r2dbc:h2:mem:///testdb).

<well-known-h2-option>

Pass-thru of well-known H2 options such as DB_CLOSE_DELAY=10&MODE=DB2. See io.r2dbc.h2.H2ConnectionOption for all options. (Optional)

options

A semicolon-delimited list of H2 configuration options(options=DB_CLOSE_DELAY=10;DB_CLOSE_ON_EXIT=true;…). (Optional)

Programmatic Configuration

H2ConnectionFactory connectionFactory = new H2ConnectionFactory(H2ConnectionConfiguration.builder()
    .inMemory("...")
    .option(H2ConnectionOption.DB_CLOSE_DELAY, "-1")
    .build());

Mono<Connection> connection = connectionFactory.create();

Programmatic In-Memory Database Configuration

CloseableConnectionFactory connectionFactory = H2ConnectionFactory.inMemory("testdb");

Mono<Connection> connection = connectionFactory.create();

Maven

Artifacts can be found on Maven Central.

<dependency>
  <groupId>io.r2dbc</groupId>
  <artifactId>r2dbc-h2</artifactId>
  <version>${version}</version>
</dependency>

If you’d rather like the latest snapshots of the upcoming major version, use our Maven snapshot repository and declare the appropriate dependency version.

<dependency>
  <groupId>io.r2dbc</groupId>
  <artifactId>r2dbc-h2</artifactId>
  <version>${version}.BUILD-SNAPSHOT</version>
</dependency>

<repository>
  <id>spring-libs-snapshot</id>
  <name>Spring Snapshot Repository</name>
  <url>https://repo.spring.io/libs-snapshot</url>
</repository>

Setting query params

H2 uses index parameters that are prefixed with $. The following SQL statement makes use of parameters:

INSERT INTO person (id, first_name, last_name) VALUES ($1, $2, $3)

Parameters are referenced using the same identifiers when binding these:

connection
    .createStatement("INSERT INTO person (id, first_name, last_name) VALUES ($1, $2, $3)")
    .bind("$1", 1)
    .bind("$2", "Walter")
    .bind("$3", "White")
    .execute()

Geometry support

r2dbc-h2 will automatically register support for JTS Toplogy Suite and handle it’s Geometry types if org.locationtech.jts:jts-core is on the classpath.

To enable, add this to your build:

<dependency>
    <groupId>org.locationtech.jts</groupId>
    <artifactId>jts-core</artifactId>
    <version>${jts.version}</version>
</dependency>
Important
Be sure to plug in your version of JTS!

Also read H2’s reference documentation on GEOMETRY types.

We also support params binding as

  • index bind(1, "Walter"). Notice that passing an integer means index (zero-based) references.

  • $ symbol bind("$2", "Walter"). H2 supports postgres params notation.

  • Object (Integer) bind(yourIntegerAsObject, "Walter"). If you index (int) was converted into object by a framework

Running JMH Benchmarks

Running the JMH benchmarks builds and runs the benchmarks without running tests.

 $ ./mvnw clean install -Pjmh

License

This project is released under version 2.0 of the Apache License.

r2dbc-h2's People

Contributors

gregturn avatar nebhale avatar mp911de avatar dimone-kun avatar gmullerb avatar uaihebert avatar mirromutth avatar michael-simons avatar peterjurkovic avatar snicoll avatar sdeleuze avatar boojongmin avatar pull-vert avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.