Apache H2 Database Go Driver

Apache H2 Database Go Driver

This driver is VERY experimental state

NOT use for production yet

Introduction

Apache H2 Database is a very-low footprint database with in-memory capabilities.

It's written in Java and it's fully ACID compliant.

You can use H2 as embedded database or via TCP/IP.

It has interfaces for Postgres protocol and native TCP server.

Motivation

Until now, using H2 in your Go projects could only be done through the Postgres driver.

This approach has several cons. The poor error messagens or not being able to use native data types are some of them.

This pure Go driver uses the native TCP interface.

Pre-requesites

In "contrib" folder you can find the scripts to download and launch the H2 database server. You need to have any Java Runtime installed.

cd contrib
./downloadH2.sh
./runStandalone.sh

Usage

First make sure the H2 server is running in TCP server mode. You can launch using the runStandalone.sh or with a command similar to the following:

java -classpath h2.jar org.h2.tools.Server -tcp -tcpAllowOthers -ifNotExists

This starts the server at the defaulr port (9092)

The following example connect to H2 and creates an in-memory database.

package main

import (
	"database/sql"
	"log"
	_ "github.com/jmrobles/h2go"
)

func main() {
	conn, err := sql.Open("h2", "h2://sa@localhost/testdb?mem=true")
	if err != nil {
		log.Fatalf("Can't connet to H2 Database: %s", err)
	}
    err = conn.Ping()
    if err != nil {
        log.Fatalf("Can't ping to H2 Database: %s", err)
    }
    log.Printf("H2 Database connected")
    conn.Close()
}

In the folder examples you can find more examples.

Connection string

In the connection string you must specify:

  • Database driver: h2 literal
  • Username (optional)
  • Password (optinal)
  • Host: format (:)?
  • Database name
  • Other connection options

Options

You can use the following options:

  • mem=(true|false): to use in-memory or in-disk database
  • logging=(none|info|debug|error|warn|panic|trace): the common logging level

Parameters

For the use of parameters in SQL statement you need to use the ? placeholder symbol.

For example:

    conn.Exec("INSERT INTO employees VALUES (?,?,?)", name, age, salary)

Data types

The following H2 datatypes are implemented:

H2 Data type Go mapping
String string
StringIgnoreCase string
StringFixed string
Bool bool
Short int16
Int int32
Long int64
Float float32
Double float64
Byte byte
Bytes []byte
Time time.Time
Time with timezone time.Time
Date time.Time
Timestamp time.Time
Timestamp with timezone time.Time

H2 Supported version

This driver supports H2 database version 1.4.200 or above.

ToDo

  • Rest of native data types (UUID, JSON, Decimal, ...)
  • NamedValue interface
  • Multiple result sets
  • Improve context usage (timeouts, ...)
  • Submit your issue

Contributors

jmrobles

Pull Requests are welcome

License

MIT License

Similar Resources

The MongoDB driver for Go

The MongoDB driver for Go This fork has had a few improvements by ourselves as well as several PR's merged from the original mgo repo that are current

Dec 25, 2022

go mysql driver, support distributed transaction

Go-MySQL-Driver A MySQL-Driver for Go's database/sql package Features Requirements Installation Usage DSN (Data Source Name) Password Protocol Address

Jul 23, 2022

sqlcomment is an ent driver that adds SQL comments following sqlcommenter specification.

sqlcomment sqlcomment is an ent driver that adds SQL comments following sqlcommenter specification. sqlcomment includes support for OpenTelemetry and

Nov 14, 2022

🏋️ dbbench is a simple database benchmarking tool which supports several databases and own scripts

dbbench Table of Contents Description Example Installation Supported Databases Usage Custom Scripts Troubeshooting Development Acknowledgements Descri

Dec 30, 2022

Database wrapper that manage read write connections

rwdb Database wrapper that manage read write connections Install go get github.com/andizzle/rwdb Create connections package main import "github.com/

Dec 10, 2022

Vitess is a database clustering system for horizontal scaling of MySQL.

Vitess Vitess is a database clustering system for horizontal scaling of MySQL through generalized sharding. By encapsulating shard-routing logic, Vite

Jan 3, 2023

sqlx is a library which provides a set of extensions on go's standard database/sql library

sqlx is a library which provides a set of extensions on go's standard database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, so that their interfaces are a superset on the standard ones. This makes it relatively painless to integrate existing codebases using database/sql with sqlx.

Jan 7, 2023

Convert data exports from various services to a single SQLite database

Convert data exports from various services to a single SQLite database

Bionic Bionic is a tool to convert data exports from web apps to a single SQLite database. Bionic currently supports data exports from Google, Apple H

Dec 9, 2022

Dumpling is a fast, easy-to-use tool written by Go for dumping data from the database(MySQL, TiDB...) to local/cloud(S3, GCP...) in multifarious formats(SQL, CSV...).

🥟 Dumpling Dumpling is a tool and a Go library for creating SQL dump from a MySQL-compatible database. It is intended to replace mysqldump and mydump

Nov 9, 2022
Comments
  • Fix issues with string reading from buffer

    Fix issues with string reading from buffer

    Also increate the max fetch size to java Integer.MAX_VALUE (like in https://github.com/h2database/h2database/blob/45b609dec0e45125e6a93f85c9018d34551332a1/h2/src/main/org/h2/command/CommandRemote.java#L165 when scrollable)

  • Fix issues with string reading from buffer

    Fix issues with string reading from buffer

    Also increate the max fetch size to java Integer.MAX_VALUE (like in https://github.com/h2database/h2database/blob/45b609dec0e45125e6a93f85c9018d34551332a1/h2/src/main/org/h2/command/CommandRemote.java#L165 when scrollable)

  • What's the URL for this JDBC connection?

    What's the URL for this JDBC connection?

    I am able to connect with a test H2 database with the following URL:

    • jdbc:h2:tcp://localhost/./test

    using the dBeaver database workbench.

    The URL example from the README gives an error whe I use that as my model with usql:

    • error: h2: driver: bad connection

    for this command:

     $ usql  h2://sa@localhost/test?mem=true
    error: h2: driver: bad connection
     $ usql  h2://sa@localhost/./test?mem=true
    error: h2: driver: bad connection
     $  usql  h2://sa@localhost:9092/./test
    

    The server is started with this command.

    • java -jar h2/bin/h2-1.4.200.jar -webAllowOthers -tcpAllowOthers -ifNotExists

    I think more explaination is needed to format a URL effectively.

Go-driver-benchmark - Driver benchmark with golang

We use ClickkHouse for time-series databases, and the driver's performance is ve

Sep 5, 2022
A fully-featured AWS Athena database driver (+ athenareader https://github.com/uber/athenadriver/tree/master/athenareader)
A fully-featured AWS Athena database driver (+ athenareader https://github.com/uber/athenadriver/tree/master/athenareader)

?? athenadriver - A fully-featured AWS Athena database driver for Go ?? athenareader - A moneywise command line utililty to query athena in command li

Jan 3, 2023
Attach hooks to any database/sql driver

sqlhooks Attach hooks to any database/sql driver. The purpose of sqlhooks is to provide a way to instrument your sql statements, making really easy to

Jan 6, 2023
Golang database driver for SQLite

go-sqlite Golang database driver for SQLite. Does not use cgo. This driver is based on pure-Go SQLite implementation (https://gitlab.com/cznic/sqlite)

Dec 30, 2022
BigQuery database/sql golang driver

BigQuery SQL Driver This library is compatible with Go 1.17+ Please refer to CHA

Dec 7, 2022
Otelsql - OpenTelemetry SQL database driver wrapper for Go
Otelsql - OpenTelemetry SQL database driver wrapper for Go

OpenTelemetry SQL database driver wrapper for Go Add a OpenTelemetry wrapper to

Dec 15, 2022
Google Cloud Spanner driver for Go's database/sql package.

go-sql-spanner Google Cloud Spanner driver for Go's database/sql package. This support is currently in the Preview release status. import _ "github.co

Dec 11, 2022
[mirror] the database client and tools for the Go vulnerability database

The Go Vulnerability Database golang.org/x/vulndb This repository is a prototype of the Go Vulnerability Database. Read the Draft Design. Neither the

Dec 29, 2022
Database - Example project of database realization using drivers and models

database Golang based database realization Description Example project of databa

Feb 10, 2022