Skip to content

Commit

Permalink
Adding transport support for Clickhouse DSNs
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed May 11, 2024
1 parent 95c81b1 commit c0a0e19
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
18 changes: 18 additions & 0 deletions dburl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,24 @@ func TestParse(t *testing.T) {
`grpcs://user:pass@localhost:8888/?opt1=a&opt2=b`,
``,
},
{
`clickhouse://user:pass@localhost/?opt1=a&opt2=b`,
`clickhouse`,
`clickhouse://user:pass@localhost:9000/?opt1=a&opt2=b`,
``,
},
{
`clickhouse+http://user:pass@localhost/?opt1=a&opt2=b`,
`clickhouse`,
`http://user:pass@localhost/?opt1=a&opt2=b`,
``,
},
{
`clickhouse+https://user:pass@host/?opt1=a&opt2=b`,
`clickhouse`,
`https://user:pass@host/?opt1=a&opt2=b`,
``,
},
}
m := make(map[string]bool)
for i, tt := range tests {
Expand Down
20 changes: 20 additions & 0 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ func GenCassandra(u *URL) (string, string, error) {
return host + ":" + port + genQueryOptions(q), "", nil
}

// GenClickhouse generates a clickhouse DSN from the passed URL.
func GenClickhouse(u *URL) (string, string, error) {
switch strings.ToLower(u.Transport) {
case "", "tcp":
return clickhouseTCP(u)
case "http":
return clickhouseHTTP(u)
case "https":
return clickhouseHTTPS(u)
}
return "", "", ErrInvalidTransportProtocol
}

// clickhouse generators.
var (
clickhouseTCP = GenFromURL("clickhouse://localhost:9000/")
clickhouseHTTP = GenFromURL("http://localhost/")
clickhouseHTTPS = GenFromURL("https://localhost/")
)

// GenCosmos generates a cosmos DSN from the passed URL.
func GenCosmos(u *URL) (string, string, error) {
host, port, dbname := u.Hostname(), u.Port(), strings.TrimPrefix(u.Path, "/")
Expand Down
2 changes: 1 addition & 1 deletion scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func BaseSchemes() []Scheme {
},
{
"clickhouse",
GenFromURL("clickhouse://localhost:9000/"), 0, false,
GenClickhouse, TransportAny, false,
[]string{"ch"},
"",
},
Expand Down

0 comments on commit c0a0e19

Please sign in to comment.