Skip to content
/ go-sqldb Public

Support for multiple DB Engines over a simple abstraction

License

Notifications You must be signed in to change notification settings

cdleo/go-sqldb

Repository files navigation

go-sqldb

Go Reference license Build Status Code Coverage Scrutinizer Code Quality

go-sqlDB it's a muti DB Engine proxy for the GO database/sql package. It provides a set of standard error codes, providing abstraction from the implemented DB engine and allowing to switch it without modify the source code, just modifying configuration (almost). Besides that, provides a VERY LIMITED cross-engine sql syntax translator.

General

The sqlProxy it's created by the sqlProxyBuilder. The Open() function returns an standard *sql.DB implementation, but using a proxy connector to the selected engine.

Supported Engines Currently, the next set of engines are supported:

Usage This example program shows initialization and usage at basic level:

package sqldb

import (
	"database/sql"
	"fmt"
	"os"
	"strconv"

	"github.com/cdleo/go-commons/logger"
	"github.com/cdleo/go-sqldb/adapter"
	"github.com/cdleo/go-sqldb/connector"
)

type People struct {
	Id       int    `db:"id"`
	Nombre   string `db:"firstname"`
	Apellido string `db:"lastname"`
}

func Example_sqlDBBuilder() {

	sqlProxy := NewSQLProxyBuilder(connector.NewSqlite3Connector(":memory:")).
		WithAdapter(adapter.NewSQLite3Adapter()).
		WithLogger(logger.NewNoLogLogger()).
		Build()

	var sqlDB *sql.DB
	var err error

	if sqlDB, err = sqlProxy.Open(); err != nil {
		fmt.Println("Unable to connect to DB")
		os.Exit(1)
	}
	defer sqlProxy.Close()

	statement, err := sqlDB.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT)")
	if err != nil {
		fmt.Printf("Unable to prepare statement %v\n", err)
		os.Exit(1)
	}
	_, err = statement.Exec()
	if err != nil {
		fmt.Printf("Unable to exec statement %v\n", err)
		os.Exit(1)
	}

	statement, err = sqlDB.Prepare("INSERT INTO people (firstname, lastname) VALUES (?, ?)")
	if err != nil {
		fmt.Printf("Unable to prepare statement %v\n", err)
		os.Exit(1)
	}
	_, err = statement.Exec("Gene", "Kranz")
	if err != nil {
		fmt.Printf("Unable to exec statement %v\n", err)
		os.Exit(1)
	}

	rows, err := sqlDB.Query("SELECT id, firstname, lastname FROM people")
	if err != nil {
		fmt.Printf("Unable to query data %v\n", err)
		os.Exit(1)
	}

	var p People
	for rows.Next() {
		_ = rows.Scan(&p.Id, &p.Nombre, &p.Apellido)
		fmt.Println(strconv.Itoa(p.Id) + ": " + p.Nombre + " " + p.Apellido)
	}

	// Output:
	// 1: Gene Kranz
}

Sample

You can find a sample of the use of go-sqldb project HERE

Contributing

Comments, suggestions and/or recommendations are always welcomed. Please check the Contributing Guide to learn how to get started contributing.

About

Support for multiple DB Engines over a simple abstraction

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages