Giter VIP home page Giter VIP logo

h2go's Introduction

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

h2go's People

Contributors

jmrobles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

h2go's Issues

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.

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.