This driver is VERY experimental state
NOT use for production yet
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.
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.
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
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.
In the connection string you must specify:
h2
literalYou can use the following options:
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)
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 |
This driver supports H2 database version 1.4.200 or above.
NamedValue
interfacecontext
usage (timeouts, ...)Pull Requests are welcome
MIT License