1

I'd like to know if there is an approach or projection pattern to be able to choose SQL driver at runtime in Golang when both of these drivers have the same name. I want to switch between HTTP ClickHouse driver (https://github.com/mailru/go-clickhouse) and native TCP ClickHouse driver (https://github.com/ClickHouse/clickhouse-go) using an environment variable.

import(
//HTTP driver
_ "github.com/mailru/go-clickhouse"
)

func getHttpCHConnection() (*sql.DB, error) {
   ...
   db, err := sql.Open("clickhouse", clkConnUrl)
import(
//Native driver
_ "github.com/ClickHouse/clickhouse-go"
)

func getNativeCHConnection() (*sql.DB, error) {
    ...
    db, err := sql.Open("clickhouse", clkConnUrl)

Normally, it causes "panic: sql: Register called twice for driver clickhouse". Is it possible to avoid that?

Denis Shebut
  • 11
  • 1
  • 3
  • 1
    sure, when you open the connection you are literally providing which driver to use `sql.Open("mysql", dsn)` –  Jan 12 '22 at 14:51
  • I've edited the question. Forgot to mention, the main problem is that both of these drivers have the same name – Denis Shebut Jan 12 '22 at 15:05
  • 3
    That's a huge mistake on the part of the author of `mailru/go-clickhouse` unfortunately; they shouldn't have used the same exact name as the official driver. – Adrian Jan 12 '22 at 15:10
  • well, it does not make sense to toggle between two drivers that handle the same database. so i would not say the problem is with the libraries but rather your approach/design. –  Jan 12 '22 at 15:15
  • Personally, I'd open an issue on the unofficial driver's GH repo for the driver name collision. – Adrian Jan 12 '22 at 15:16
  • @glen sure it does. Depending on the operating context, one might prefer HTTP or socket, or only be able to use one or the other. Depends on how clickhouse is deployed and how security is set up between your app and the DB. – Adrian Jan 12 '22 at 15:18
  • 1
    I think your best bet here is to fork one of the repos and change the driver name. – Bracken Jan 17 '22 at 18:16

1 Answers1

0

Since version 2 of mailru/go-clickhouse it is possible to use both of them, authors have changed driver name to chhttp: https://github.com/mailru/go-clickhouse/issues/151

Denis Shebut
  • 11
  • 1
  • 3