H2go
Apache H2 Go SQL Driver
Install / Use
/learn @jmrobles/H2goREADME
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:
h2literal - Username (optional)
- Password (optinal)
- Host: format <host>(:<port>)?
- 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, ...)
NamedValueinterface- Multiple result sets
- Improve
contextusage (timeouts, ...) - Submit your issue
Contributors
Pull Requests are welcome
License
MIT License
Related Skills
oracle
337.4kBest practices for using the oracle CLI (prompt + file bundling, engines, sessions, and file attachment patterns).
xurl
337.4kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
prose
337.4kOpenProse VM skill pack. Activate on any `prose` command, .prose files, or OpenProse mentions; orchestrates multi-agent workflows.
Command Development
83.2kThis skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
