gdbc

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2021 License: Apache-2.0 Imports: 6 Imported by: 4

README

GDBC - Go Database Connectivity

Go Report Card codecov Build Status

Because Data Source Name, known as DSN does not have any standard format, the driver libraries have their driver-specific DSN. Sometimes you might get confused about how to specify your DSN for the database you want to connect to. In order to solve this issue, GDBC provides a common format to represent the database, and an abstract layer for database connections.

With GDBC, a database is represented by a URL (Uniform Resource Locator). URL takes one of the following forms:

gdbc:driver-name:database?arg1=value1&arg2=value...
gdbc:driver-name://localhost/database?arg1=value1&arg2=value...
gdbc:driver-name://localhost:5432/database?arg1=value1&arg2=value...
gdbc:driver-name://username:password@localhost:5432/database?arg1=value1&arg2=value...
gdbc:driver-name:file:h2?arg1=value1&arg2=value...

How to use GDBC?

It's pretty easy to connect the database. You can connect to the database as shown below.

dataSource, err := gdbc.GetDataSource("gdbc:driver-name://localhost:5432/test-db", Username("username"), Password("password"))
if err != nil {
    panic(err)
}

var connection *sql.DB
connection, err = dataSource.GetConnection()
if err != nil {
    panic(err)
}
...

Register your existing driver

If you have already an existing driver written and want to support GDBC, you have to implement the following DataSourceNameAdapter interface converting GDBC format to your specific DSN format, and register your driver by using gdbc.Register function.

type DataSourceNameAdapter interface {
	GetDataSourceName(dataSource DataSource) (string, error)
}

License

GDBC is released under version 2.0 of the Apache License

Documentation

Index

Constants

View Source
const Scheme = "gdbc"

Variables

This section is empty.

Functions

func GetDriverName added in v1.0.2

func GetDriverName(driverAliasName string) (string, bool)

func Register

func Register(driverName string, driverAliasName string, dsnAdapter DataSourceNameAdapter)

Types

type DataSource

type DataSource interface {
	GetDriverName() string
	GetURL() *url.URL
	GetConnection() (*sql.DB, error)
	GetUsername() string
	SetUsername(username string)
	GetPassword() string
	SetPassword(password string)
}

func GetDataSource

func GetDataSource(url string, options ...DataSourceOption) (DataSource, error)

type DataSourceNameAdapter

type DataSourceNameAdapter interface {
	GetDataSourceName(dataSource DataSource) (string, error)
}

func GetDataSourceNameAdapter

func GetDataSourceNameAdapter(driverAliasName string) DataSourceNameAdapter

type DataSourceOption

type DataSourceOption func(dataSource DataSource)

func Password

func Password(password string) DataSourceOption

func Username

func Username(username string) DataSourceOption

type SimpleDataSource

type SimpleDataSource struct {
	// contains filtered or unexported fields
}

func (*SimpleDataSource) GetConnection

func (dataSource *SimpleDataSource) GetConnection() (*sql.DB, error)

func (*SimpleDataSource) GetDriverName

func (dataSource *SimpleDataSource) GetDriverName() string

func (*SimpleDataSource) GetPassword

func (dataSource *SimpleDataSource) GetPassword() string

func (*SimpleDataSource) GetURL

func (dataSource *SimpleDataSource) GetURL() *url.URL

func (*SimpleDataSource) GetUsername

func (dataSource *SimpleDataSource) GetUsername() string

func (*SimpleDataSource) SetPassword

func (dataSource *SimpleDataSource) SetPassword(password string)

func (*SimpleDataSource) SetUsername

func (dataSource *SimpleDataSource) SetUsername(username string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL