Skip to content

Commit

Permalink
Adding duckdb support
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Nov 3, 2023
1 parent 34fab66 commit 3c836c0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
29 changes: 15 additions & 14 deletions dburl.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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.
// Package dburl provides a standard, [net/url.URL] style mechanism for parsing
// and opening SQL database connection strings for Go. Provides standardized
// way to parse and open [URL]'s 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.
//
Expand All @@ -15,10 +15,11 @@ import (
"strings"
)

// Open takes a URL like "protocol+transport://user:pass@host/dbname?option1=a&option2=b"
// and opens a standard sql.DB connection.
// 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 URLs to work properly with Open.
// See [Parse] for information on formatting URL strings to work properly with Open.
func Open(urlstr string) (*sql.DB, error) {
u, err := Parse(urlstr)
if err != nil {
Expand All @@ -34,34 +35,34 @@ func Open(urlstr string) (*sql.DB, error) {
// URL wraps the standard [net/url.URL] type, adding OriginalScheme, Transport,
// Driver, Unaliased, and DSN strings.
type URL struct {
// URL is the base net/url/URL.
// 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.
// 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
// 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.
// call to [sql.Open].
DSN string
// hostPortDB will be set by Gen*() funcs after determining the host, port,
// database.
//
// when empty, indicates that these values are not special, and can be
// When empty, indicates that these values are not special, and can be
// retrieved as the host, port, and path[1:] as usual.
hostPortDB []string
}

// Parse parses a URL, similar to the standard [net/url.Parse].
// Parse parses a URL string, similar to the standard [net/url.Parse].
//
// Handles parsing OriginalScheme, Transport, Driver, Unaliased, and DSN
// fields.
Expand Down
2 changes: 2 additions & 0 deletions dburl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ func TestParse(t *testing.T) {
{`bend://user:pass@localhost/instance_name?sslmode=disabled&warehouse=wh`, `databend`, `bend://user:pass@localhost/instance_name?sslmode=disabled&warehouse=wh`, ``},
{`databend://user:pass@localhost/instance_name?tenant=tn&warehouse=wh`, `databend`, `databend://user:pass@localhost/instance_name?tenant=tn&warehouse=wh`, ``},
{`flightsql://user:pass@localhost?timeout=3s&token=foobar&tls=enabled`, `flightsql`, `flightsql://user:pass@localhost?timeout=3s&token=foobar&tls=enabled`, ``},
{`duckdb:/path/to/foo.db?access_mode=read_only&threads=4`, `duckdb`, `/path/to/foo.db?access_mode=read_only&threads=4`, ``},
{`dk:///path/to/foo.db?access_mode=read_only&threads=4`, `duckdb`, `/path/to/foo.db?access_mode=read_only&threads=4`, ``},
}
for i, test := range tests {
u, err := Parse(test.s)
Expand Down
8 changes: 4 additions & 4 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (

// 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.
// Used internally to stat files, and is subsequently used when generating the
// DSNs for postgres://, mysql://, and sqlite3:// [URL] schemes.
var Stat = func(name string) (fs.FileInfo, error) {
return fs.Stat(os.DirFS(filepath.Dir(name)), filepath.Base(name))
}

// GenScheme returns a func that generates a scheme:// style DSN from the
// passed URL.
// GenScheme returns a generator that will generate a scheme based on the
// passed scheme DSN.
func GenScheme(scheme string) func(*URL) (string, string, error) {
return func(u *URL) (string, string, error) {
z := &url.URL{
Expand Down
6 changes: 6 additions & 0 deletions scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ func BaseSchemes() []Scheme {
[]string{"ch"},
"",
},
{
"duckdb",
GenOpaque, 0, true,
[]string{"dk", "ddb", "duck"},
"",
},
{
"cosmos",
GenCosmos, 0, false,
Expand Down

0 comments on commit 3c836c0

Please sign in to comment.