package main

import (
	"context"
	"database/sql"
	"fmt"
	"net"
	"time"

	"github.com/ClickHouse/clickhouse-go/v2"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	var dialCount int
	conn, err := clickhouse.Open(&clickhouse.Options{
		Addr: []string{"node-stable-1.db.cluster.zerops:9000"},
		Auth: clickhouse.Auth{
			Database: "cluster",
			Username: "zerops",
			Password: "uvKq9EnwUnXqKQEYcVTYzhoQ",
		},
		DialContext: func(ctx context.Context, addr string) (net.Conn, error) {
			dialCount++
			var d net.Dialer
			return d.DialContext(ctx, "tcp", addr)
		},
		Debug: true,
		Debugf: func(format string, v ...any) {
			fmt.Printf(format+"\n", v...)
		},
		Settings: clickhouse.Settings{
			"max_execution_time": 60,
		},
		Compression: &clickhouse.Compression{
			Method: clickhouse.CompressionLZ4,
		},
		DialTimeout:          time.Second * 30,
		MaxOpenConns:         5,
		MaxIdleConns:         5,
		ConnMaxLifetime:      time.Duration(10) * time.Minute,
		ConnOpenStrategy:     clickhouse.ConnOpenInOrder,
		BlockBufferSize:      10,
		MaxCompressionBuffer: 10240,
		ClientInfo: clickhouse.ClientInfo{ // optional, please see Client info section in the README.md
			Products: []struct {
				Name    string
				Version string
			}{
				{Name: "my-app", Version: "0.1"},
			},
		},
	})
	if err != nil {
		panic(err)
	}
	err = conn.Ping(context.Background())
	if err != nil {
		panic(err)
	}
	conn2, err := sql.Open("mysql", "zerops:uvKq9EnwUnXqKQEYcVTYzhoQ@tcp(node-stable-1.db.cluster.zerops:9004)/")
	if err != nil {
		panic(err)
	}
	err = conn2.Ping()
	if err != nil {
		panic(err)
	}
}
