Documentation
¶
Overview ¶
Package dburl provides a standard, URL style mechanism for parsing and opening SQL database connection strings.
Database URL Connection Strings ¶
Supported database URLs are of the form:
protocol+transport://user:pass@host/dbname?opt1=a&opt2=b protocol:/path/to/file
Where:
protocol - driver name or alias (see below)
transport - "tcp", "udp", "unix" or driver name (odbc/oleodbc) |
user - username
pass - password
host - host
dbname* - database, instance, or service name/id to connect to
?opt1=... - additional database driver options
(see respective SQL driver for available options)
* for Microsoft SQL Server, the syntax to supply an instance and database name is /instance/dbname, where /instance is optional. For Oracle databases, /dbname is the unique database ID (SID). Please see below for examples.
Quickstart ¶
URLs in the above format can be parsed with Parse as such:
u, err := dburl.Parse("postgresql://user:pass@localhost/mydatabase/?sslmode=disable")
if err != nil { /* ... */ }
Additionally, a simple helper func, Open, is available to quickly parse, open, and return a standard SQL database connection:
db, err := dburl.Open("sqlite:mydatabase.sqlite3?loc=auto")
if err != nil { /* ... */ }
Example URLs ¶
The following are URLs that can be handled with a call to Open or Parse:
postgres://user:pass@localhost/dbname pg://user:pass@localhost/dbname?sslmode=disable mysql://user:pass@localhost/dbname mysql:/var/run/mysqld/mysqld.sock sqlserver://user:pass@remote-host.com/dbname mssql://user:pass@remote-host.com/instance/dbname ms://user:pass@remote-host.com:port/instance/dbname?keepAlive=10 oracle://user:pass@somehost.com/oracledb sap://user:pass@localhost/dbname sqlite:/path/to/file.db file:myfile.sqlite3?loc=auto odbc+postgres://user:pass@localhost:port/dbname?option1=
Protocol Schemes and Aliases ¶
The following protocols schemes (ie, driver) and their associated aliases are supported out of the box:
Database (scheme/driver) | Protocol Aliases [real driver] -----------------------------|----------------------------------------- Microsoft SQL Server (mssql) | ms, sqlserver MySQL (mysql) | my, mariadb, maria, percona, aurora Oracle (ora) | or, oracle, oci8, oci PostgreSQL (postgres) | pg, postgresql, pgsql SQLite3 (sqlite3) | sq, sqlite, file -----------------------------|----------------------------------------- CockroachDB (cockroachdb) | cr, cockroach, crdb, cdb [postgres] MemSQL (memsql) | me [mysql] TiDB (tidb) | ti [mysql] Vitess (vitess) | vt [mysql] -----------------------------|----------------------------------------- Google Spanner (spanner) | gs, google, span (not yet public) -----------------------------|----------------------------------------- MySQL (mymysql) | zm, mymy PostgreSQL (pgx) | px -----------------------------|----------------------------------------- Apache Avatica (avatica) | av, phoenix ClickHouse (clickhouse) | ch Couchbase (n1ql) | n1, couchbase Cznic QL (ql) | ql, cznic, cznicql Firebird SQL (firebirdsql) | fb, firebird Microsoft ADODB (adodb) | ad, ado ODBC (odbc) | od OLE ODBC (oleodbc) | oo, ole, oleodbc [adodb] SAP HANA (hdb) | sa, saphana, sap, hana Sybase SQL Anywhere (sqlany) | sy, sybase, any VoltDB (voltdb) | vo, volt, vdb YQL (yql) | yq
Any protocol scheme alias:// can be used in place of protocol://, and will work identically with Parse/Open.
Usage ¶
Please note that the dburl package does not import actual SQL drivers, and only provides a standard way to parse/open respective database connection URLs.
For reference, these are the following "expected" SQL drivers that would need to be imported:
Database (scheme/driver) | Package -----------------------------|------------------------------------------------- Microsoft SQL Server (mssql) | github.com/denisenkom/go-mssqldb MySQL (mysql) | github.com/go-sql-driver/mysql Oracle (ora) | gopkg.in/rana/ora.v4 PostgreSQL (postgres) | github.com/lib/pq SQLite3 (sqlite3) | github.com/mattn/go-sqlite3 -----------------------------|------------------------------------------------- CockroachDB (cockroachdb) | github.com/lib/pq MemSQL (memsql) | github.com/go-sql-driver/mysql TiDB (tidb) | github.com/go-sql-driver/mysql Vitess (vitess) | github.com/go-sql-driver/mysql -----------------------------|------------------------------------------------- Google Spanner (spanner) | github.com/knq/spanner (not yet public) -----------------------------|------------------------------------------------- MySQL (mymysql) | github.com/ziutek/mymysql/godrv PostgreSQL (pgx) | github.com/jackc/pgx/stdlib -----------------------------|------------------------------------------------- Apache Avatica (avatica) | github.com/Boostport/avatica ClickHouse (clickhouse) | github.com/kshvakov/clickhouse Couchbase (n1ql) | github.com/couchbase/go_n1ql Cznic QL (ql) | github.com/cznic/ql Firebird SQL (firebirdsql) | github.com/nakagami/firebirdsql Microsoft ADODB (adodb) | github.com/mattn/go-adodb ODBC (odbc) | github.com/alexbrainman/odbc OLE ODBC (oleodbc)* | github.com/mattn/go-adodb SAP HANA (hdb) | github.com/SAP/go-hdb/driver Sybase SQL Anywhere (sqlany) | github.com/a-palchikov/sqlago VoltDB (voltdb) | github.com/VoltDB/voltdb-client-go/voltdbclient YQL (yql) | github.com/mattn/go-yql
- OLE ODBC is not an actual protocol, but instead is an alias for using the "MSDASQL.1" OLE provider with the ADODB driver, and the DSN will be an ADODB DSN, but with "Extended Properties" for the respective ODBC parameters, including the underlying transport prootocol. As such, "oleodbc+protocol://user:pass@host/dbname" URLs are equivalent to "adodb://MSDASQL.1/?Extended+Properties=...". Please see the documentation for GenOLEODBC for information regarding how URL components are mapped/passed to ADODB's Extended Properties parameter.
URL Parsing Rules ¶
Parse and Open rely heavily on the standard net/url.URL type, as such parsing rules have the same conventions/semantics as any URL parsed by the standard library's net/url.Parse.
Related Projects ¶
This package was written mainly to support xo (https://github.com/knq/xo) and usql (https://github.com/knq/usql).
Index ¶
- Variables
- func GenADODB(u *URL) (string, error)
- func GenClickhouse(u *URL) (string, error)
- func GenFirebird(u *URL) (string, error)
- func GenFromURL(urlstr string) func(*URL) (string, error)
- func GenMyMySQL(u *URL) (string, error)
- func GenMySQL(u *URL) (string, error)
- func GenODBC(u *URL) (string, error)
- func GenOLEODBC(u *URL) (string, error)
- func GenOpaque(u *URL) (string, error)
- func GenOracle(u *URL) (string, error)
- func GenPostgres(u *URL) (string, error)
- func GenSQLServer(u *URL) (string, error)
- func GenScheme(scheme string) func(*URL) (string, error)
- func GenSybase(u *URL) (string, error)
- func GenVoltDB(u *URL) (string, error)
- func GenYQL(u *URL) (string, error)
- func Open(urlstr string) (*sql.DB, error)
- func Register(scheme Scheme)
- func RegisterAlias(name, alias string)
- func SchemeDriverAndAliases(name string) (string, []string)
- type Proto
- type Scheme
- type URL
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidDatabaseScheme is the invalid database scheme error. ErrInvalidDatabaseScheme = errors.New("invalid database scheme") // ErrInvalidTransportProtocol is the invalid transport protocol error. ErrInvalidTransportProtocol = errors.New("invalid transport protocol") // ErrUnknownDatabaseScheme is the unknown database type error. ErrUnknownDatabaseScheme = errors.New("unknown database scheme") // ErrInvalidHost is the invalid host error. ErrInvalidHost = errors.New("invalid host") // ErrPostgresDoesNotSupportRelativePath is the postgres does not support relative path error. ErrPostgresDoesNotSupportRelativePath = errors.New("postgres does not support relative path") )
Functions ¶
func GenClickhouse ¶
GenClickhouse generates a clickhouse DSN from the passed URL.
func GenFirebird ¶
GenFirebird generates a firebirdsql DSN from the passed URL.
func GenFromURL ¶
GenFromURL returns a func that generates a DSN using urlstr as the default URL parameters, overriding the values only if when in the passed URL.
func GenMyMySQL ¶
GenMyMySQL generates a MyMySQL MySQL DSN from the passed URL.
func GenOLEODBC ¶
GenOLEODBC generates a oleodbc DSN from the passed URL.
func GenPostgres ¶
GenPostgres generates a postgres DSN from the passed URL.
func GenSQLServer ¶
GenSQLServer generates a mssql DSN from the passed URL.
func Open ¶
Open takes a urlstr like "protocol+transport://user:pass@host/dbname?option1=a&option2=b" and creates a standard sql.DB connection.
See Parse for information on formatting URLs to work properly with Open.
func RegisterAlias ¶
func RegisterAlias(name, alias string)
RegisterAlias registers a alias for an already registered Scheme.h
func SchemeDriverAndAliases ¶
SchemeDriverAndAliases returns the registered driver and aliases for a database scheme.
Types ¶
type Proto ¶
type Proto uint
Proto are the allowed transport protocol types in a database URL scheme.
type Scheme ¶
type Scheme struct {
// Driver is the name of the SQL driver that will set as the Scheme in
// Parse'd URLs, and is the driver name expected by the standard sql.Open
// calls.
//
// Note: a 2 letter alias will always be registered for the Driver as the
// first 2 characters of the Driver, unless one of the Aliases includes an
// alias that is 2 characters.
Driver string
// Generator is the func responsible for generating a DSN based on parsed
// URL information.
//
// Note: this func should not modify the passed URL.
Generator func(*URL) (string, error)
// Proto are allowed protocol types for the scheme.
Proto Proto
// Opaque toggles Parse to not re-process URLs with an "opaque" component.
Opaque bool
// Aliases are any additional aliases for the scheme.
Aliases []string
// Override is the Go SQL driver to use instead of Driver.
Override string
}
Scheme wraps information used for registering a URL scheme with Parse/Open.
func Unregister ¶
Unregister unregisters a Scheme and all associated aliases.
type URL ¶
type URL struct {
// URL is the base net/url/URL.
url.URL
// OriginalScheme is the original parsed scheme (ie, "sq", "mysql+unix", "sap", etc).
OriginalScheme string
// Proto is the specified protocol (ie, "tcp", "udp", "unix"), if provided.
Proto string
// Driver is the non-aliased SQL driver name that should be used in a call
// to sql/Open.
Driver string
// DSN is the built connection "data source name" that can be used in a
// call to sql/Open.
DSN string
}
URL wraps the standard net/url.URL type, adding OriginalScheme, Proto, Driver, and DSN strings.
func Parse ¶
Parse parses urlstr, returning a URL with the OriginalScheme, Proto, Driver, and DSN fields populated.
Note: if urlstr has a Opaque component (ie, URLs not specified as "scheme://" but "scheme:"), and the database scheme does not support opaque components, then Parse will attempt to re-process the URL as "scheme://<opaque>" using the OriginalScheme.