Skip to content

Commit

Permalink
Support duckdb empty path to specify in-memory DB
Browse files Browse the repository at this point in the history
Closes #41
  • Loading branch information
murfffi authored and kenshaw committed Feb 8, 2025
1 parent 2a98895 commit da92587
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
19 changes: 18 additions & 1 deletion dburl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestBadParse(t *testing.T) {
{`file+tcp://`, ErrInvalidTransportProtocol},
{`file://`, ErrMissingPath},
{`ql://`, ErrMissingPath},
{`duckdb://`, ErrMissingPath},
{`mssql+tcp://user:pass@host/dbname`, ErrInvalidTransportProtocol},
{`mssql+foobar://`, ErrInvalidTransportProtocol},
{`mssql+unix:/var/run/mssql.sock`, ErrInvalidTransportProtocol},
Expand Down Expand Up @@ -787,6 +786,24 @@ func TestParse(t *testing.T) {
`/path/to/foo.db?access_mode=read_only&threads=4`,
``,
},
{
`duckdb://`,
`duckdb`,
``,
``,
},
{
`duckdb://:memory:`,
`duckdb`,
`:memory:`,
``,
},
{
`duckdb:?threads=4`,
`duckdb`,
`?threads=4`,
``,
},
{
`file:./testdata/test.sqlite3?a=b`,
`sqlite3`,
Expand Down
6 changes: 6 additions & 0 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,12 @@ func GenYDB(u *URL) (string, string, error) {
return s + genOptions(u.Query(), "?", "=", "&", ",", true, nil, nil), "", nil
}

// GenDuckDB generates a duckdb dsn from the passed URL.
func GenDuckDB(u *URL) (string, string, error) {
// Same as GenOpaque but accepts empty path which refers to in-memory DB
return u.Opaque + genQueryOptions(u.Query()), "", nil
}

// convertOptions converts an option value based on name, value pairs.
func convertOptions(q url.Values, pairs ...string) url.Values {
n := make(url.Values)
Expand Down
2 changes: 1 addition & 1 deletion scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func BaseSchemes() []Scheme {
},
{
"duckdb",
GenOpaque, 0, true,
GenDuckDB, 0, true,
[]string{"dk", "ddb", "duck"},
"",
},
Expand Down

0 comments on commit da92587

Please # to comment.