Skip to content

Commit

Permalink
Adding snowflake URL support
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Shaw committed Jun 16, 2018
1 parent 1420779 commit 689b5cf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ supported out of the box:
| OLE ODBC (oleodbc) | oo, ole, oleodbc [adodb] |
| Presto (presto) | pr, prestodb, prestos, prs, prestodbs |
| SAP HANA (hdb) | sa, saphana, sap, hana |
| Snowflake (snowflake) | sf |
| VoltDB (voltdb) | vo, volt, vdb |

Any protocol scheme `alias://` can be used in place of `protocol://`, and will work
Expand Down Expand Up @@ -154,6 +155,7 @@ to be imported:
| OLE ODBC (oleodbc) | [github.com/mattn/go-adodb](https://github.com/mattn/go-adodb) |
| Presto (presto) | [github.com/prestodb/presto-go-client/presto](https://github.com/prestodb/presto-go-client) |
| SAP HANA (hdb) | [github.com/SAP/go-hdb/driver](https://github.com/SAP/go-hdb) |
| Snowflake (snowflake) | [github.com/snowflakedb/gosnowflake](https://github.com/snowflakedb/gosnowflake) |
| VoltDB (voltdb) | [github.com/VoltDB/voltdb-client-go/voltdbclient](github.com/VoltDB/voltdb-client-go]) |

Please see [the GoDoc API page](http://godoc.org/github.com/xo/dburl) for a
Expand Down
5 changes: 5 additions & 0 deletions dburl.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
// OLE ODBC (oleodbc) | oo, ole, oleodbc [adodb]
// Presto (presto) | pr, prestodb, prestos, prs, prestodbs
// SAP HANA (hdb) | sa, saphana, sap, hana
// Snowflake (snowflake) | sf
// VoltDB (voltdb) | vo, volt, vdb
//
// Any protocol scheme alias:// can be used in place of protocol://, and will
Expand Down Expand Up @@ -131,6 +132,7 @@
// OLE ODBC (oleodbc)* | github.com/mattn/go-adodb
// Presto (presto) | github.com/prestodb/presto-go-client
// SAP HANA (hdb) | github.com/SAP/go-hdb/driver
// Snowflake (snowflake) | github.com/snowflakedb/gosnowflake
// VoltDB (voltdb) | github.com/VoltDB/voltdb-client-go/voltdbclient
//
// * OLE ODBC is not an actual protocol, but instead is an alias for using the
Expand Down Expand Up @@ -179,6 +181,9 @@ const (
// 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"
)
Expand Down
8 changes: 8 additions & 0 deletions dburl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func TestBadParse(t *testing.T) {
{`cockroach+unix:./path`, ErrInvalidTransportProtocol},
{`pg:./path/to/socket`, ErrRelativePathNotSupported}, // relative paths are not possible for postgres sockets
{`pg+unix:./path/to/socket`, ErrRelativePathNotSupported},
{`snowflake://`, ErrMissingHost},
{`sf://`, ErrMissingHost},
{`snowflake://account`, ErrMissingPath},
{`sf://account`, ErrMissingPath},
}

for i, test := range tests {
Expand Down Expand Up @@ -126,6 +130,10 @@ func TestParse(t *testing.T) {
{`ig://user@host:9999?timeout=1000`, `ignite`, `tcp://host:9999?timeout=1000&username=user`},
{`ig://user:pass@localhost:9999/?timeout=1000`, `ignite`, `tcp://localhost:9999?password=pass&timeout=1000&username=user`},
{`ig://user:pass@localhost:9999/dbname?timeout=1000`, `ignite`, `tcp://localhost:9999/dbname?password=pass&timeout=1000&username=user`},

{`snowflake://host/dbname/schema`, `snowflake`, `host/dbname/schema`},
{`sf://user@host:9999/dbname/schema?timeout=1000`, `snowflake`, `user@host:9999/dbname/schema?timeout=1000`},
{`sf://user:pass@localhost:9999/dbname/schema?timeout=1000`, `snowflake`, `user:pass@localhost:9999/dbname/schema?timeout=1000`},
}

for i, test := range tests {
Expand Down
26 changes: 26 additions & 0 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,29 @@ func GenIgnite(u *URL) (string, error) {
}
return "tcp://" + host + ":" + port + dbname + genQueryOptions(q), nil
}

// GenSnowflake generates a snowflake DSN from the passed URL.
func GenSnowflake(u *URL) (string, error) {
host, port, dbname := hostname(u.Host), hostport(u.Host), strings.TrimPrefix(u.Path, "/")
if host == "" {
return "", ErrMissingHost
}
if dbname == "" {
return "", ErrMissingPath
}
if port != "" {
port = ":" + port
}

// add user/pass
var user string
if u.User != nil {
user = u.User.Username()
if pass, _ := u.User.Password(); pass != "" {
user += ":" + pass
}
user += "@"
}

return user + host + port + "/" + dbname + genQueryOptions(u.Query()), nil
}
1 change: 1 addition & 0 deletions scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func BaseSchemes() []Scheme {
{"oleodbc", GenOLEODBC, ProtoAny, false, []string{"oo", "ole"}, "adodb"},
{"presto", GenPresto, 0, false, []string{"prestodb", "prestos", "prs", "prestodbs"}, ""},
{"ql", GenOpaque, 0, true, []string{"ql", "cznic", "cznicql"}, ""},
{"snowflake", GenSnowflake, 0, false, []string{"sf"}, ""},
{"voltdb", GenVoltDB, 0, false, []string{"volt", "vdb"}, ""},
}
}
Expand Down

0 comments on commit 689b5cf

Please sign in to comment.