Documentation ¶
Overview ¶
Package dburl provides a standard, URL style mechanism for parsing and opening SQL database connection strings for Go. Provides standardized way to parse and open URLs for popular databases PostgreSQL, MySQL, SQLite3, Oracle Database, Microsoft SQL Server, in addition to most other SQL databases with a publicly available Go driver.
See the package documentation README section for more details.
Example (Open) ¶
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) } 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) } 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 GenAdodb(u *URL) (string, string, error)
- func GenCassandra(u *URL) (string, string, error)
- func GenCosmos(u *URL) (string, string, error)
- func GenDatabend(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 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 GenSchemeTruncate(u *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 GenVoltdb(u *URL) (string, string, error)
- func Open(urlstr string) (*sql.DB, error)
- func Protocols(name string) []string
- func Register(scheme Scheme)
- func RegisterAlias(name, alias string)
- func SchemeDriverAndAliases(name string) (string, []string)
- func ShortAlias(name string) string
- type Error
- type Scheme
- type Transport
- type URL
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 within the package to stat files, which is used when generating the DSNs for postgres, mysql, and sqlite3 URL schemes.
Functions ¶
func GenCassandra ¶
GenCassandra generates a cassandra DSN from the passed URL.
func GenDatabend ¶
GenDatabend generates a databend 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 GenMymysql ¶
GenMymysql generates a mymysql 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 GenSchemeTruncate ¶
GenSchemeTruncate generates a DSN by truncating the scheme://.
func GenSnowflake ¶
GenSnowflake generates a snowflake DSN from the passed URL.
func GenSpanner ¶
GenSpanner generates a spanner DSN from the passed URL.
func GenSqlserver ¶
GenSqlserver generates a sqlserver DSN from the passed URL.
func GenTableStore ¶
GenTableStore generates a tablestore DSN from the passed URL.
func Open ¶
Open takes a URL like "protocol+transport://user:pass@host/dbname?option1=a&option2=b" and opens 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 an additional alias for a registered scheme.
func SchemeDriverAndAliases ¶
SchemeDriverAndAliases returns the registered driver and aliases for a database scheme.
func ShortAlias ¶
ShortAlias returns the short alias for the scheme name.
Types ¶
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" // 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" )
Error values.
type Scheme ¶
type Scheme struct { // Driver is the name of the SQL driver that is 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, 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 }
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 ¶
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 that should be used 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 Parse ¶
Parse parses a URL, similar to the standard net/url.Parse.
Handles parsing OriginalScheme, Transport, Driver, Unaliased, and DSN fields.
Note: if the URL has a Opaque component (ie, URLs not specified as "scheme://" but "scheme:"), and the database scheme does not support opaque components, Parse will attempt to re-process the URL as "scheme://<opaque>".
func (*URL) Normalize ¶
Normalize returns the driver, host, port, database, and user name of a URL, joined with sep, populating blank fields with empty.