Documentation
¶
Overview ¶
Package dburl parses and opens SQL database connection strings for Go, in the style of net/url.URL.
It handles the URL formats of PostgreSQL, MySQL, SQLite3, Oracle Database and Microsoft SQL Server. It also handles most other SQL databases that have a public Go driver.
See the package documentation README section for more details.
Example ¶
package main
import (
"log"
"github.com/xo/dburl"
)
func main() {
db, err := dburl.Open("my://user:pass@host:1234/dbname")
if err != nil {
log.Fatal(err)
}
defer db.Close()
res, err := db.Query("SELECT ...")
if err != nil {
log.Fatal(err)
}
for res.Next() {
/* ... */
}
if err := res.Err(); err != nil {
log.Fatal(err)
}
}
Output:
Example (Parse) ¶
package main
import (
"database/sql"
"log"
"github.com/xo/dburl"
)
func main() {
u, err := dburl.Parse("pg://user:pass@host:1234/dbname")
if err != nil {
log.Fatal(err)
}
db, err := sql.Open(u.Driver, u.DSN)
if err != nil {
log.Fatal(err)
}
defer db.Close()
res, err := db.Query("SELECT ...")
if err != nil {
log.Fatal(err)
}
for res.Next() {
/* ... */
}
if err := res.Err(); err != nil {
log.Fatal(err)
}
}
Output:
Index ¶
- Variables
- func BuildURL(components map[string]any) (string, error)
- func FileTypes() []string
- func GenAdodb(u *URL) (string, string, error)
- func GenCassandra(u *URL) (string, string, error)
- func GenClickhouse(u *URL) (string, string, error)
- func GenCosmos(u *URL) (string, string, error)
- func GenDatabend(u *URL) (string, string, error)
- func GenDatabricks(u *URL) (string, string, error)
- func GenDuckDB(u *URL) (string, string, error)
- func GenDynamo(u *URL) (string, string, error)
- func GenExasol(u *URL) (string, string, error)
- func GenFirebird(u *URL) (string, string, error)
- func GenFromURL(urlstr string) func(*URL) (string, string, error)
- func GenGodror(u *URL) (string, string, error)
- func GenHive(u *URL) (string, string, error)
- func GenIgnite(u *URL) (string, string, error)
- func GenMymysql(u *URL) (string, string, error)
- func GenMysql(u *URL) (string, string, error)
- func GenOdbc(u *URL) (string, string, error)
- func GenOleodbc(u *URL) (string, string, error)
- func GenOpaque(u *URL) (string, string, error)
- func GenPostgres(u *URL) (string, string, error)
- func GenPresto(u *URL) (string, string, error)
- func GenScheme(scheme string) func(*URL) (string, string, error)
- func GenSnowflake(u *URL) (string, string, error)
- func GenSpanner(u *URL) (string, string, error)
- func GenSqlserver(u *URL) (string, string, error)
- func GenTableStore(u *URL) (string, string, error)
- func GenTrino(u *URL) (string, string, error)
- func GenVoltdb(u *URL) (string, string, error)
- func GenYDB(u *URL) (string, string, error)
- func Open(urlstr string) (*sql.DB, error)
- func OpenMap(components map[string]any) (*sql.DB, error)
- func Protocols(name string) []string
- func Register(scheme Scheme)
- func RegisterAlias(name, alias string)
- func RegisterFileType(driver string, f func([]byte) bool, ext string)
- func SchemeDriverAndAliases(name string) (string, []string)
- func SchemeType(name string) (string, error)
- func ShortAlias(name string) string
- type Deployment
- type Error
- type Scheme
- type Transport
- type URL
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var OdbcIgnoreQueryPrefixes []string
OdbcIgnoreQueryPrefixes are the query prefixes to ignore when generating the odbc DSN. Used by GenOdbc.
var OpenFile = func(name string) (fs.File, error) { return os.OpenFile(name, os.O_RDONLY, 0) }
OpenFile is the default open file func.
Used internally to read file headers.
var ResolveSchemeType = true
ResolveSchemeType is a configuration setting to open paths on disk using SchemeType, Stat, and OpenFile. Set this to false in an `init()` func to disable this behavior.
var Stat = func(name string) (fs.FileInfo, error) { return fs.Stat(os.DirFS(filepath.Dir(name)), filepath.Base(name)) }
Stat is the default stat func.
Used internally to stat files, and used when generating the DSNs for postgres://, mysql://, file:// schemes, and opaque URL's.
Functions ¶
func BuildURL ¶ added in v0.23.0
BuildURL creates a dsn using the mapped components.
Recognized components are:
protocol, proto, scheme transport username, user password, pass hostname, host port path, file, opaque database, dbname, db instance parameters, params, options, opts, query, q
See BuildURL for more information.
func FileTypes ¶ added in v0.18.0
func FileTypes() []string
FileTypes returns the registered file types.
func GenCassandra ¶
GenCassandra generates a cassandra DSN from the passed URL.
func GenClickhouse ¶
GenClickhouse generates a clickhouse DSN from the passed URL.
func GenDatabend ¶ added in v0.12.5
GenDatabend generates a databend DSN from the passed URL.
func GenDatabricks ¶ added in v0.19.0
GenDatabricks generates a databricks DSN from the passed URL.
func GenFirebird ¶
GenFirebird generates a firebird DSN from the passed URL.
func GenFromURL ¶
GenFromURL returns a func that generates a DSN based on parameters of the passed URL.
func GenHive ¶ added in v0.28.1
GenHive generates a hive DSN from the passed URL.
Targets beltran/gohive/v2. Its ParseDSN rejects any string that does not start with "hive://", and reads the database name from the path.
The database name is required. An empty path is an error and not a default, so an absent name becomes "default".
The driver also cannot start without auth. Its connect path tests the accepted values in turn, and anything else reaches panic("Unrecognized auth"). The empty string is one of those, so an absent or empty auth becomes NONE. Any other value passes through untouched. See D16.
func GenMymysql ¶ added in v0.9.0
GenMymysql generates a mymysql DSN from the passed URL.
func GenOleodbc ¶ added in v0.9.0
GenOleodbc generates a oleodbc DSN from the passed URL.
func GenPostgres ¶
GenPostgres generates a postgres DSN from the passed URL.
func GenPresto ¶
GenPresto generates a presto DSN from the passed URL.
Targets prestodb/presto-go-client/v2, which accepts only the presto and trino schemes, and reads the catalog and the schema from the path.
The driver sends any query option it does not recognize to the server as a session property. The catalog and the schema cannot go there.
The driver selects TLS from the ssl_ca, ssl_cert, ssl_key and ssl_skip_verify options, and not from the scheme, so presto has no "s" suffixed alias. Setting any of those options also moves the default port to 8443.
See GenTrino, which Trino uses instead. The two drivers want different DSNs.
func GenScheme ¶
GenScheme returns a generator that will generate a scheme based on the passed scheme DSN.
func GenSnowflake ¶
GenSnowflake generates a snowflake DSN from the passed URL.
func GenSpanner ¶ added in v0.2.0
GenSpanner generates a spanner DSN from the passed URL.
func GenSqlserver ¶ added in v0.9.0
GenSqlserver generates a sqlserver DSN from the passed URL.
func GenTableStore ¶ added in v0.11.0
GenTableStore generates a tablestore DSN from the passed URL.
func GenTrino ¶ added in v0.27.0
GenTrino generates a trino DSN from the passed URL.
Targets trinodb/trino-go-client, which takes an http or https URL and reads the catalog and schema from the query.
See GenPresto. Trino and Presto share a wire protocol, but their drivers disagree about the scheme and about where the catalog and schema belong.
func Open ¶
Open takes a URL string, also known as a DSN, in the form of "protocol+transport://user:pass@host/dbname?option1=a&option2=b" and opens a standard sql.DB connection.
See Parse for information on formatting URL strings to work properly with Open.
func OpenMap ¶ added in v0.23.0
OpenMap takes a map of URL components and opens a standard sql.DB connection.
See BuildURL for information on the recognized map components.
func Protocols ¶ added in v0.12.0
Protocols returns list of all valid protocol aliases for a registered Scheme name.
func RegisterAlias ¶
func RegisterAlias(name, alias string)
RegisterAlias registers an additional alias for a registered scheme.
func RegisterFileType ¶ added in v0.18.0
RegisterFileType registers a file header recognition func, and extension regexp.
func SchemeDriverAndAliases ¶
SchemeDriverAndAliases returns the registered driver and aliases for a database scheme.
func SchemeType ¶ added in v0.17.0
SchemeType returns the scheme type for a path.
func ShortAlias ¶ added in v0.12.4
ShortAlias returns the short alias for the scheme name.
Types ¶
type Deployment ¶ added in v0.29.0
type Deployment uint
Deployment is the deployment kinds a database offers.
const ( // DeploymentEmbedded is a database with no server, used as a library. DeploymentEmbedded Deployment = 1 // DeploymentServer is a database with a server that anyone can run. DeploymentServer Deployment = 2 // DeploymentHosted is a database offered only as a service, with no // edition anyone can run. DeploymentHosted Deployment = 4 )
Deployment kinds.
A database can offer more than one. CockroachDB is a server anyone can run and a service, so it is DeploymentServer|DeploymentHosted.
type Error ¶
type Error string
Error is an error.
const ( // ErrInvalidDatabaseScheme is the invalid database scheme error. ErrInvalidDatabaseScheme Error = "invalid database scheme" // ErrUnknownDatabaseScheme is the unknown database type error. ErrUnknownDatabaseScheme Error = "unknown database scheme" // ErrUnknownFileHeader is the unknown file header error. ErrUnknownFileHeader Error = "unknown file header" // ErrUnknownFileExtension is the unknown file extension error. ErrUnknownFileExtension Error = "unknown file extension" // ErrInvalidTransportProtocol is the invalid transport protocol error. ErrInvalidTransportProtocol Error = "invalid transport protocol" // ErrRelativePathNotSupported is the relative paths not supported error. ErrRelativePathNotSupported Error = "relative path not supported" // ErrMissingHost is the missing host error. ErrMissingHost Error = "missing host" // ErrMissingPath is the missing path error. ErrMissingPath Error = "missing path" // ErrMissingUser is the missing user error. ErrMissingUser Error = "missing user" // ErrInvalidQuery is the invalid query error. ErrInvalidQuery Error = "invalid query" )
Error values.
type Scheme ¶
type Scheme struct {
// Driver is the name of the SQL driver. [Parse] sets it as the Scheme on
// the returned URL, and the standard sql.Open calls expect it.
//
// Note: a 2 letter alias is registered automatically, taken from the
// first 2 characters of the Driver. This does not happen when one of the
// Aliases is already 2 characters.
Driver string
// Generator is the func responsible for generating a DSN based on parsed
// URL information.
//
// Note: this func must not modify the passed URL.
Generator func(*URL) (string, string, error)
// Transport are allowed protocol transport types for the scheme.
Transport Transport
// 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.
//
// Used for "wire compatible" driver schemes.
Override string
// Desc is the database display name, as "Apache Hive".
Desc string
// Home is the home page of the database provider. It can be blank.
Home string
// DriverURL is the home page of the Go database driver.
//
// Blank for a wire compatible scheme, which reaches its driver through
// Override.
DriverURL string
// GoPackage is the import path of the Go database driver, including the
// major version when the module carries one.
//
// This is the import path and not the module path. The two differ: for
// pgx the module is github.com/jackc/pgx/v5 and the import path that
// registers the driver is github.com/jackc/pgx/v5/stdlib.
//
// Blank for a wire compatible scheme.
GoPackage string
// RequiresCGO reports whether the Go database driver needs cgo.
RequiresCGO bool
// Deployment is the deployment kinds the database offers.
Deployment Deployment
}
Scheme wraps information used for registering a database URL scheme for use with Parse/Open.
func Unregister ¶
Unregister unregisters a scheme and all associated aliases, returning the removed Scheme.
type Transport ¶ added in v0.3.0
type Transport uint
Transport is the allowed transport protocol types in a database URL scheme.
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
// Transport is the specified transport protocol (ie, "tcp", "udp",
// "unix", ...), if provided.
Transport string
// Driver is the non-aliased SQL driver name to use in a call to
// [sql.Open].
Driver string
// GoDriver is the Go SQL driver name to use when opening a connection to
// the database. Used by Microsoft SQL Server's azuresql:// URLs, as the
// wire-compatible alias style uses a different syntax style.
GoDriver string
// UnaliasedDriver is the unaliased driver name.
UnaliasedDriver string
// DSN is the built connection "data source name" that can be used in a
// call to [sql.Open].
DSN string
// contains filtered or unexported fields
}
URL wraps the standard net/url.URL type, adding OriginalScheme, Transport, Driver, Unaliased, and DSN strings.
func FromMap ¶ added in v0.23.0
FromMap creates a URL using the mapped components.
Recognized components are:
protocol, proto, scheme transport username, user password, pass hostname, host port path, file, opaque database, dbname, db instance parameters, params, options, opts, query, q
See BuildURL for more information.
func Parse ¶
Parse parses a URL string, similar to the standard net/url.Parse.
Handles parsing OriginalScheme, Transport, Driver, Unaliased, and DSN fields.
Note: an opaque URL is written "scheme:" and not "scheme://". When the database scheme does not support an opaque component, Parse rebuilds the URL as "scheme://<opaque>" and parses it again.
func (*URL) Normalize ¶
Normalize returns the driver, host, port, database, and user name of a URL, joined with sep, populating blank fields with empty.