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)
}
// Driver is the name that the Go driver registered, which is pgx here.
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 DialectProtocols(name string) []string
- func FileTypes() []string
- func GenArangoDB(u *URL) (string, string, error)
- func GenAthena(u *URL) (string, string, error)
- func GenAvatica(u *URL) (string, string, error)
- func GenCassandra(u *URL) (string, string, error)
- func GenClickhouse(u *URL) (string, string, error)
- func GenCockroachDB(u *URL) (string, string, error)
- func GenCosmos(u *URL) (string, string, error)
- func GenCouchbase(u *URL) (string, string, error)
- func GenCrateDB(u *URL) (string, string, error)
- func GenDatabend(u *URL) (string, string, error)
- func GenDatabricks(u *URL) (string, string, error)
- func GenDrill(u *URL) (string, string, error)
- func GenDruid(u *URL) (string, string, error)
- func GenDuckDB(u *URL) (string, string, error)
- func GenDynamo(u *URL) (string, string, error)
- func GenElasticsearch(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 GenGizmoSQL(u *URL) (string, string, error)
- func GenHive(u *URL) (string, string, error)
- func GenInfluxDB(u *URL) (string, string, error)
- func GenInfluxQL(u *URL) (string, string, error)
- func GenLibsql(u *URL) (string, string, error)
- func GenMaxCompute(u *URL) (string, string, error)
- func GenModernCSqlite(u *URL) (string, string, error)
- func GenMysql(u *URL) (string, string, error)
- func GenNeo4j(u *URL) (string, string, error)
- func GenOdbc(u *URL) (string, string, error)
- func GenOpaque(u *URL) (string, string, error)
- func GenOpensearch(u *URL) (string, string, error)
- func GenPgx(u *URL) (string, string, error)
- func GenPgxFromURL(urlstr string) func(*URL) (string, string, error)
- func GenPinot(u *URL) (string, string, error)
- func GenPostgres(u *URL) (string, string, error)
- func GenPq(u *URL) (string, string, error)
- func GenPresto(u *URL) (string, string, error)
- func GenQuestDB(u *URL) (string, string, error)
- func GenRqlite(u *URL) (string, string, error)
- func GenScheme(scheme string) func(*URL) (string, string, error)
- func GenSchemeHost(scheme string) func(*URL) (string, string, error)
- func GenSnowflake(u *URL) (string, string, error)
- func GenSolr(u *URL) (string, string, error)
- func GenSpanner(u *URL) (string, string, error)
- func GenSqlserver(u *URL) (string, string, error)
- func GenSurrealDB(u *URL) (string, string, error)
- func GenTableStore(u *URL) (string, string, error)
- func GenTiDB(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 SchemeNameAndAliases(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 DialectProtocols ¶ added in v0.32.0
DialectProtocols returns every protocol name and alias of the registered schemes that share the Dialect of the named scheme, sorted. A scheme with no Dialect returns its own Protocols.
For postgres it returns the names of postgres, pgx, pq and redshift. cockroachdb and cratedb speak PostgreSQL, and are products of their own, so each has a Dialect and a family of its own.
func FileTypes ¶ added in v0.18.0
func FileTypes() []string
FileTypes returns the registered file types.
func GenArangoDB ¶ added in v0.36.0
GenArangoDB generates an arangodb DSN from the passed URL.
Targets the driver planned in xo/dbimp/arangodb, which is to read an arangodb:// URL and refuse any other scheme. The user information, the path and the query pass through as they were written. It adds no port, because the driver defaults to 8529 (D34).
func GenAthena ¶ added in v0.50.0
GenAthena generates an athena DSN from the passed URL.
Targets the driver xo/dbimp/athena, which reads an athena:// URL. The host is the endpoint, such as athena.us-east-1.amazonaws.com, and the driver reads the region from it. A host that is only a region, such as us-east-1, is the short form, and GenAthena writes the endpoint (D62 and D63). The path is the database, and the query takes workgroup, output, token and catalog. The user is the access key and the password is the secret key. The driver accepts a URL with neither, so GenAthena checks neither. A URL with no host returns ErrMissingHost. Everything else passes through as it was written (D59).
func GenAvatica ¶ added in v0.41.0
GenAvatica generates an avatica DSN from the passed URL.
Targets xo/dbimp/avatica, which reads an avatica:// URL and refuses any other scheme, and refuses a path. The user information and the query pass through as they were written. It adds no port, because the driver defaults to 8765, the port of an Avatica server (D34 and D47). The alias phoenix reaches the same driver.
func GenCassandra ¶
GenCassandra generates a cassandra DSN from the passed URL.
Targets xo/cassandra, which reads a cassandra:// URL with net/url. The scheme is always cassandra, whichever alias was parsed, so cql:// and scylla:// reach the driver as cassandra://, which is the only scheme it reads. The user information and the path, which is the keyspace, pass through, and so does the query, as it was written, because the driver takes a host key that can repeat. It adds no port, because gocql defaults to 9042 (D34 and D56).
func GenClickhouse ¶
GenClickhouse generates a clickhouse DSN from the passed URL.
Targets xo/dbimp/clickhouse, which reads a clickhouse:// URL with net/url and speaks HTTP only. The user information, the path, which is the database, and the query pass through as they were written. The driver reads the tls key and refuses any other key. It adds no port, because the driver defaults to 8123, or 8443 with tls (D34). The transports +http and +https are gone, and so is the native protocol on 9000 (D53).
func GenCockroachDB ¶ added in v0.35.0
GenCockroachDB generates a cockroachdb DSN from the passed URL.
CockroachDB speaks the wire protocol of PostgreSQL, so the DSN is the one GenPgxFromURL writes, with the default port 26257 and sslmode=disable. It returns pgx as the driver. The scheme has a Dialect of its own, because it is a product of its own.
func GenCosmos ¶ added in v0.2.0
GenCosmos generates a cosmos DSN from the passed URL.
Targets the driver xo/dbimp/cosmos, which reads a cosmos:// URL. The host is the account, such as account.documents.azure.com, and a name with no dot gets that suffix (D62). The user is any text, and the password is the master key of the account as base64 text, so a URL with no host returns ErrMissingHost, and one with no password returns ErrMissingUser. The path is the database and the container, and the query takes tls, insecure, pagesize and partitionkey. Everything else passes through as it was written (D59).
func GenCouchbase ¶ added in v0.33.0
GenCouchbase generates a couchbase DSN from the passed URL.
Targets xo/dbimp/couchbase, which reads a couchbase:// URL with net/url and refuses any other scheme. The user information, the path and the query pass through as they were written, and the driver refuses an unknown or repeated key, and a path other than "/". It adds no port, because the driver defaults to 8093, or 18093 with tls (D34).
func GenCrateDB ¶ added in v0.35.0
GenCrateDB generates a cratedb DSN from the passed URL.
CrateDB speaks the wire protocol of PostgreSQL, so the DSN is the one GenPgxFromURL writes. It adds no port, because pgx defaults to 5432, which is the port of the PostgreSQL interface of CrateDB (D34). It returns pgx as the Go driver, as GenCockroachDB does.
func GenDatabend ¶ added in v0.12.5
GenDatabend generates a databend DSN from the passed URL.
Targets the driver planned in xo/dbimp/databend, which replaces databend-go and reads a databend:// URL and refuses any other scheme, so the scheme is always databend, whichever alias was parsed. The user information, the path, which names the database, and the query pass through as they were written. It adds no port, because the driver defaults to 8000, with TLS or without (D34 and D39).
func GenDatabricks ¶ added in v0.19.0
GenDatabricks generates a databricks DSN from the passed URL.
Targets the driver xo/dbimp/databricks, which reads a databricks://token:<access token>@host/<warehouse id> URL. The host is the workspace, and a name with no dot gets the suffix .cloud.databricks.com (D62). The password is the access token, and the driver accepts only the user token or no user, so a URL with no host returns ErrMissingHost and one with no password returns ErrMissingUser. The path is the id of the SQL warehouse, so a URL with no path returns ErrMissingPath. The query takes catalog, schema, timeout and tls, and everything else passes through as it was written (D61).
func GenDrill ¶ added in v0.45.0
GenDrill generates a drill DSN from the passed URL.
Targets the driver xo/dbimp/drill, which reads a drill:// URL and refuses any other scheme. The URL has no path, and the schema is the key schema. The user information and the query pass through as they were written, and the driver refuses a key it does not know. It adds no port, because the driver has its own default, which tls=true does not change (D34 and D54).
func GenDruid ¶ added in v0.42.0
GenDruid generates a druid DSN from the passed URL.
Targets the driver xo/dbimp/druid, which reads a druid:// URL, refuses any other scheme, and refuses a path, because Druid has no database to choose. The user information and the query pass through as they were written. It adds no port, because the driver defaults to 8888, the port of the Druid Router (D34 and D48).
func GenDynamo ¶ added in v0.19.0
GenDynamo generates a dynamodb DSN from the passed URL.
Targets the driver xo/dbimp/dynamodb, which reads a dynamodb:// URL. The host is the endpoint, such as dynamodb.us-east-1.amazonaws.com, and the region is the key region, which the driver requires. A host that is only a region, such as us-east-1, is the short form: GenDynamo writes the endpoint of the region, and names the region in the key when the URL has none (D62 and D63). The user is the access key and the password is the secret key, and the signature needs both, so a URL without one returns ErrMissingUser. The driver speaks HTTPS unless the URL names tls=false, and refuses a path and any other key. The user information and the query pass through. It adds no port, because the driver uses the port of the scheme (D34 and D55).
func GenElasticsearch ¶ added in v0.45.0
GenElasticsearch generates a elasticsearch DSN from the passed URL.
Targets the driver xo/dbimp/elasticsearch, which reads a elasticsearch:// URL and refuses any other scheme. The URL has no path, because Elasticsearch has no database to choose. The user information and the query pass through as they were written, and the driver refuses a key it does not know. It adds no port, because the driver has its own default, which tls=true does not change (D34 and D54).
func GenExasol ¶ added in v0.8.5
GenExasol generates an exasol DSN from the passed URL.
Targets the driver xo/dbimp/exasol, which reads an exasol://user:password@host:port/schema URL. The user is required, so a URL with no user returns ErrMissingUser. The password and the schema, which is the path, are optional. The query takes tls, validateservercertificate, certificatefingerprint, compression, autocommit, fetchsize, querytimeout, clientname and protocol, and the driver refuses any other key and a repeated one. The user information, the path and the query pass through as they were written. It adds no port, because the driver defaults to 8563 (D34 and D66).
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.
The query is rebuilt with net/url.Values.Encode, which writes a space as a +, and a key that appears more than once is joined into one value with spaces. Do not use it for a driver that reads a + as itself, such as pgx, which has GenPgxFromURL, or for a driver that takes a repeated key.
func GenGizmoSQL ¶ added in v0.36.0
GenGizmoSQL generates a flightsql DSN for GizmoSQL from the passed URL.
GizmoSQL serves Flight SQL, so the DSN is a flightsql:// URL, with the default port 31337, which is the port of GizmoSQL. The Flight SQL driver has no default port (D34). It returns flightsql as the Go driver.
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 GenInfluxDB ¶ added in v0.35.0
GenInfluxDB generates an influxdb DSN from the passed URL.
Targets the driver in xo/dbimp/influxdb, which reads an influxdb:// URL and refuses any other scheme. The user information, the path and the query pass through as they were written. It adds no driver option, so the driver reads sqlmode and version with its own defaults. See GenInfluxQL.
It adds no port, because the driver defaults to 8086 for version 1 and 2, and 8181 otherwise (D34).
func GenInfluxQL ¶ added in v0.35.0
GenInfluxQL generates an influxdb DSN for InfluxQL from the passed URL.
It writes the DSN as GenInfluxDB does, and adds sqlmode=disable when the URL does not name sqlmode, so that the driver speaks InfluxQL. It returns influxdb as the Go driver, because one driver serves both schemes.
func GenLibsql ¶ added in v0.40.0
GenLibsql generates a libsql DSN from the passed URL.
Targets xo/dbimp/libsql, which serves libSQL and Turso, reads a libsql:// URL and refuses any other scheme, and refuses a path. The token is the password of the URL. The user information and the query pass through as they were written. It adds no port, because the driver defaults to 443 with TLS, which is on by default, and needs an explicit port with tls=false (D34 and D45).
func GenMaxCompute ¶ added in v0.31.0
GenMaxCompute generates a maxcompute DSN from the passed URL.
Targets the driver xo/dbimp/maxcompute, which reads a maxcompute://id:key@host/project URL. The user is the access key id and the password is the access key secret, so a URL with no password returns ErrMissingUser. The path is the project, so a URL with no path returns ErrMissingPath, and a URL with no host returns ErrMissingHost. The host is service.<region>.maxcompute.aliyun.com, and the driver reads the region from it, or from the region option when the host has another form. The tls option passes through to the driver, and the endpoint path /api is the driver's (D68).
func GenModernCSqlite ¶ added in v0.53.0
GenModernCSqlite generates a modernc.org/sqlite DSN from the passed URL. It is GenOpaque, and it names the driver sqlite, which is the name that modernc.org/sqlite registers with database/sql (D67).
func GenMysql ¶ added in v0.9.0
GenMysql generates a mysql DSN from the passed URL.
It adds no port, because go-sql-driver/mysql defaults to 3306 (D34).
func GenNeo4j ¶ added in v0.35.0
GenNeo4j generates a neo4j DSN from the passed URL.
Targets xo/dbimp/neo4j, which reads a neo4j:// URL with net/url and refuses any other scheme. The path names the database, and the driver reads an empty path as the database neo4j. The user information, the path and the query pass through as they were written. It adds no port, because the driver defaults to 7474, or 7473 with tls (D34).
func GenOdbc ¶ added in v0.9.0
GenOdbc generates an odbc DSN from the passed URL.
Targets xo/odbc, which reads an odbc+<driver>:// URL, so the URL passes through with its user information, host, port, path and query. The driver writes the connection string from it, and quotes a value that holds a ; or a brace (D52). GenOdbc adds no host, no port and no option, because the ODBC driver that the user installed owns each default (D34).
A query key that starts with a prefix in OdbcIgnoreQueryPrefixes is dropped, so a client can keep its own options out of the DSN. A URL with no driver, such as odbc://host/db, returns ErrMissingDriver.
func GenOpensearch ¶ added in v0.45.0
GenOpensearch generates a opensearch DSN from the passed URL.
Targets the driver xo/dbimp/opensearch, which reads a opensearch:// URL and refuses any other scheme. The URL has no path, because OpenSearch has no database to choose. The user information and the query pass through as they were written, and the driver refuses a key it does not know. It adds no port, because the driver has its own default, which tls=true does not change (D34 and D54).
func GenPgx ¶ added in v0.32.0
GenPgx generates a pgx DSN from the passed URL.
Targets jackc/pgx/v5/stdlib, which reads a postgres:// URL with the same rules as libpq. Every component is percent-encoded, a space as %20, because the driver rejects a raw space and reads a + as itself. A unix socket directory cannot be a URL host, so it is passed as the host query option.
GenPgx supplies no default host and no default port, so the driver reads PGHOST and then its socket default. See GenPgxFromURL for one that does.
See GenPostgres, which lib/pq uses instead.
func GenPgxFromURL ¶ added in v0.32.0
GenPgxFromURL returns a func that generates a pgx DSN, taking the default host, port and query options from the passed URL. The parsed URL overrides each one it carries, as with GenFromURL.
Use it in place of GenFromURL for a scheme whose driver is pgx, because GenFromURL writes a space in a query option as a +, which pgx reads as itself.
func GenPinot ¶ added in v0.36.0
GenPinot generates a pinot DSN from the passed URL.
Targets xo/dbimp/pinot, which reads a pinot:// URL and refuses any other scheme, and refuses a path. The user information, the path and the query pass through as they were written. It adds no port, because the driver defaults to 8099, the port of a Pinot broker (D34 and D43).
func GenPostgres ¶
GenPostgres generates a postgres DSN from the passed URL.
Targets lib/pq, which reads the keyword/value form. A value holding a space, a quote or a backslash is quoted, as the parser requires.
See GenPgx, which postgres uses instead, and GenPq, which names the driver for the pq scheme.
func GenPq ¶ added in v0.36.0
GenPq generates a pq DSN from the passed URL.
It is the DSN of GenPostgres, and it names postgres as the driver, which is the name that lib/pq registers.
func GenPresto ¶
GenPresto generates a trino DSN for Presto from the passed URL.
Presto is a flavor of the driver that GenTrino targets. The DSN is the one GenTrino writes, with the key flavor set to presto, and the Dialect is presto. It returns trino as the name of the driver, because the driver registers one name (D50).
func GenQuestDB ¶ added in v0.36.0
GenQuestDB generates a questdb DSN from the passed URL.
QuestDB speaks the wire protocol of PostgreSQL, so the DSN is the one GenPgxFromURL writes, with the default port 8812, which is the port of its PostgreSQL interface. pgx would use 5432 (D34). It returns pgx as the Go driver, as GenCrateDB does.
func GenRqlite ¶ added in v0.36.0
GenRqlite generates a rqlite DSN from the passed URL.
Targets xo/dbimp/rqlite, which reads a rqlite:// URL and refuses any other scheme, and refuses a path. The user information, the path and the query pass through as they were written. It adds no port, because the driver defaults to 4001, the port of the HTTP API of rqlite (D34 and D44).
func GenScheme ¶
GenScheme returns a generator that will generate a scheme based on the passed scheme DSN.
func GenSchemeHost ¶ added in v0.36.0
GenSchemeHost returns a generator that rewrites only the scheme, as GenScheme does, and that returns ErrMissingHost when the URL names no host. It is for a scheme whose host is not a server, such as the project of bigquery, where localhost is never right (D38).
func GenSnowflake ¶
GenSnowflake generates a snowflake DSN from the passed URL.
Targets xo/dbimp/snowflake, which reads a snowflake:// URL. The host is the full host of the account, such as org-account.snowflakecomputing.com, and the driver refuses a host that does not end that way. The user is the login user, and the password is the private key of the user, as the base64url text of its PKCS8 DER bytes. The key is the only way to sign in, so a URL with no host, no user or no password returns ErrMissingHost or ErrMissingUser. A host that does not end that way, has no port and is not an IP address is an account name, such as org-account, and GenSnowflake adds the suffix (D58). The path holds the database and the schema, and the query takes role, warehouse, timeout and timezone. Each one passes through as it was written, and the driver refuses any other key. It adds no port, because the driver uses 443 (D34 and D58).
func GenSolr ¶ added in v0.45.0
GenSolr generates a solr DSN from the passed URL.
Targets the driver xo/dbimp/solr, which reads a solr:// URL and refuses any other scheme. The path is the name of one collection, and it is optional. The user information and the query pass through as they were written, and the driver refuses a key it does not know. It adds no port, because the driver has its own default, which tls=true does not change (D34 and D54).
func GenSpanner ¶ added in v0.2.0
GenSpanner generates a spanner DSN from the passed URL.
Targets xo/dbimp/spanner. The URL is spanner://host:port/project/instance/database?credential_file=/key.json, and it passes through with its user information and its whole query as they were written (D60 and D63). An empty host leaves the endpoint to the driver, as spanner:///p/i/d does, so GenSpanner adds no host. Parse reads a URL with no host and a path as a unix socket, so the scheme accepts that transport, and a spanner+unix URL is refused here. The path must name all three parts (D35).
func GenSqlserver ¶ added in v0.9.0
GenSqlserver generates a sqlserver DSN from the passed URL.
A URL for azuresql, by its scheme or by the key fedauth, can name a server with no dot, such as myserver, and GenSqlserver adds .database.windows.net to it. A host with a port is left as it is (D62 and D63).
func GenSurrealDB ¶ added in v0.34.0
GenSurrealDB generates a surrealdb DSN from the passed URL.
Targets xo/dbimp/surrealdb, which reads a surrealdb:// URL with net/url and refuses any other scheme. The path names the namespace and the database, as /namespace/database, and passes through with its escaping. The user information and the query pass through as they were written. It adds no port, because the driver defaults to 8000 (D34). A path that does not name both returns ErrMissingPath (D38).
func GenTableStore ¶ added in v0.11.0
GenTableStore generates a tablestore DSN from the passed URL.
Targets the driver xo/dbimp/tablestore, which reads a tablestore://id:secret@host/instance URL. The user is the access key id and the password is the access key secret, so a URL with no password returns ErrMissingUser. The path is the instance, so a URL with no path returns ErrMissingPath, and a URL with no host returns ErrMissingHost. A host that is <instance>.<region> gets the suffix .ots.aliyuncs.com, and a host with no dot, such as localhost, does not (D68). The tls option passes through to the driver.
func GenTiDB ¶ added in v0.36.0
GenTiDB generates a tidb DSN from the passed URL.
It is the DSN of GenMysql, with the default port 4000, which is the port of TiDB. go-sql-driver/mysql would use 3306 (D34).
func GenTrino ¶ added in v0.27.0
GenTrino generates a trino DSN from the passed URL.
Targets xo/dbimp/trino, which serves Trino and Presto, registers the name trino, reads a trino:// URL and refuses any other scheme. The path names the catalog and the schema, and each is optional. The path and the query pass through as they were written. It adds no port, because the driver defaults to 8080, and to 8443 with tls (D34).
Two things are written for the scheme (D50):
- The driver requires a user, and a server that does no authentication accepts any name, so a URL with no user gets the user "user", as it always did. This is an exception to rule 10.
- The scheme chooses the flavor: the key flavor is trino unless the URL names it, so the driver does not ask the server, and presto:// against a Trino server fails and does not act as Trino. This is an exception to rule 7, as sqlmode=disable is for influxql (D29).
The driver reads TLS from the tls key and never from the scheme, as every dbimp driver does, so no alias of the scheme means HTTPS.
func GenYDB ¶ added in v0.19.0
GenYDB generates a ydb dsn from the passed URL.
Targets ydb-platform/ydb-go-sdk/v3, which reads TLS from the scheme: grpcs is secure and grpc is not. The tls option chooses it, and is false by default. With tls=true the scheme is grpcs and the default port is 2135, and otherwise they are grpc and 2136. The aliases yds and ydbs are gone (D51).
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 SchemeNameAndAliases ¶ added in v0.36.0
SchemeNameAndAliases returns the Name and the aliases of the registered scheme that name or an alias names. It was SchemeDriverAndAliases (D38).
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" // ErrMissingDriver is the missing ODBC driver error. ErrMissingDriver Error = "missing driver" // ErrInvalidQuery is the invalid query error. ErrInvalidQuery Error = "invalid query" )
Error values.
type Scheme ¶
type Scheme struct {
// Name is the name of the scheme. [Parse] sets it as the SchemeName of the
// returned URL, and as its Driver when the generator names no driver.
//
// Note: a 2 letter alias is registered automatically, taken from the
// first 2 characters of the Name. This does not happen when one of the
// Aliases is already 2 characters.
Name string
// Generator is the func responsible for generating a DSN based on parsed
// URL information. It returns the DSN, and the name to pass to sql.Open
// when that is not the Name, as mysql for tidb or pgx for postgres.
//
// 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
// Defaults are query options that [Parse] adds to the URL before it runs
// the Generator, when the URL does not name the key or gives it an empty
// value. A value the URL gives always wins, so the caller can change any of
// them. They are options that the driver needs to behave as the xo
// projects expect, such as parseTime for mysql (D65).
Defaults map[string]string
// Dialect is the Name of the scheme that is canonical for the database
// product, which is this scheme's own Name when it is the canonical one.
//
// A product reached by more than one Go driver has a scheme per driver.
// They share a Dialect: pgx, pq and postgres are all PostgreSQL, as
// moderncsqlite and sqlite3 are both SQLite3. A product that speaks the
// wire protocol of another has a Dialect of its own, because its catalog
// differs: tidb opens the mysql driver, and its Dialect is tidb.
Dialect 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.
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.
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
// SchemeName is the [Scheme.Name] of the parsed scheme, with no alias, as
// "tidb" for ti:// and tidb://.
SchemeName string
// Driver is the name to pass to [sql.Open], which the Go driver
// registered. It is always set, and it is the SchemeName unless the
// scheme opens a driver of another name: tidb opens mysql, postgres and
// cockroachdb open pgx, pq opens postgres, and influxql opens influxdb.
Driver string
// Dialect is the [Scheme.Dialect] of the parsed scheme: the Name of the
// scheme that is canonical for the database product.
//
// Use it, and not Driver, to learn which product a URL connects to.
// Driver names the Go driver, so postgres:// and pgx:// both open pgx,
// and pq:// opens postgres. All three have the Dialect postgres.
Dialect 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 the OriginalScheme, Transport, SchemeName, Driver, Dialect 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 the OriginalScheme, Transport, SchemeName, Driver, Dialect 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. When cut is more than 0, it drops the empty fields at the end, and keeps the last field that is not empty and at least cut fields. It drops a field only when empty is the empty string.