Apache H2 Database driver for Go

TL;DR: I’ve release a pure-go Apache H2 Database SQL driver for Golang. You can checkout here: h2go

Image for post
Image for post
Oh yeah! The H2 SQL Driver for Go is here at last!

Introduction

Apache H2 is a light weight and high performance SQL Database written in Java.

It’s (almost) 100% ANSI SQL-99 compatible and it can store the data in files or in-memory. You can embed in your Java project or using with client/server pattern.

Motivation

Some months ago, I worked in a “tech debt” project where a lot of business logic was coded as SQL sentences.

The goal was significantly improve the performance and scalability.

We chosen Go as programming language for its performance and flexibility balance. It’s ideal for time-to-market adn/or deadline projects.

The project has a lot of SQL sentences that we need keep them and use a very fast database a local “SQL cache” and “business logic processador”.

After we studied the state-of-art of in-memory databases we chosen H2. Its ANSI SQL compatibility and its very high speed were the key factors.

But until now, the only H2 interface for Go was the Postgres interface (pq driver).

We had to face a number of challenges when using the Postgres driver. The main one was not being able to get the correct error messages when executing some SQL statements. Its debugging process were tricky.

We had the need to have a “native” Go SQL Driver for the H2 Server protocol.

Let’s do it

It have been my first Go SQL driver.

I have developed for Go version 1.15 and I implemented the new recommended database/sql/driver interfaces.

For its development I had need to study the Java source code and some research work, inspecting traffic between a JDBC client and the H2 server using Wireshark.

To develop and to serve the package, I’ll use GitHub. I also use the new GitHub feature, Actions, for CI. Testing stage by the moment.

The result is a “MVP” Go SQL driver tagging as version 0.5.0 (in the halfway) that support the H2 1.4.200 (current stable) or above version.

Hello world, H2!

The following example demonstrate how to use the H2 SQL Go driver.

The repository

Here is the respository: https://github.com/jmrobles/h2go

Features

  • Support of basic types: string, float, double, short, long, date, time, timestamp, timezones, …
  • Basic testing with CI (GitHub Action)
  • Prepared Statements
  • Parametrized DML statements(parameter symbol: ?)
  • Transaction support.
  • Logging with different levels.

Roadmap

  • Implementation of complex types: UUID, JSON, Blob, etc.
  • MultiResultSet support.
  • Older Go versions compatibility.
  • Other H2 version support.
  • More tests.
  • Benchmark tests.

Conclusion

The development of the driver has been very didactic. The most difficult part was analyzing and implementing the protocol due to the lack of internal documentation.

I hope this driver helps other colleagues that we need to use H2 in theirs Go projects.

If you want to contribute it, PRs are welcome! :)

Related posts

Please, if you liked it, give it a round of applause. And if you want to know more about DevOps, Kubernetes, Docker, etc … follow me :)

Image for post
Image for post
Thanks so much! =D

Written by

CTO @ Digitalilusion.com & DigitalSecured.net Beyond-Full-stack developer #go #python #kubernetes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store