|
1 | | -// Copyright 2025 The DBQ Authors |
| 1 | +// Copyright 2026 The DBQ Authors |
2 | 2 | // |
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | // you may not use this file except in compliance with the License. |
|
15 | 15 | package cnn |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "crypto/tls" |
| 19 | + |
18 | 20 | "github.com/ClickHouse/clickhouse-go/v2" |
19 | 21 | "github.com/ClickHouse/clickhouse-go/v2/lib/driver" |
20 | 22 | "github.com/DataBridgeTech/dbqcore" |
21 | 23 | ) |
22 | 24 |
|
23 | 25 | func NewClickhouseConnection(connectionCfg dbqcore.ConnectionConfig, poolSize int) (driver.Conn, error) { |
| 26 | + proto, tlsCfg := clickhouseProtocol(connectionCfg.Protocol) |
| 27 | + |
24 | 28 | cnn, err := clickhouse.Open(&clickhouse.Options{ |
25 | 29 | Addr: []string{connectionCfg.Host}, |
26 | 30 | Auth: clickhouse.Auth{ |
27 | 31 | Database: connectionCfg.Database, |
28 | 32 | Username: connectionCfg.Username, |
29 | 33 | Password: connectionCfg.Password, |
30 | 34 | }, |
| 35 | + Protocol: proto, |
| 36 | + TLS: tlsCfg, |
31 | 37 | MaxOpenConns: poolSize, |
32 | 38 | MaxIdleConns: poolSize, |
33 | | - //TLS: &tls.Config{ |
34 | | - // InsecureSkipVerify: true, |
35 | | - //}, |
36 | 39 | }) |
37 | 40 | return cnn, err |
38 | 41 | } |
| 42 | + |
| 43 | +func clickhouseProtocol(protocol string) (clickhouse.Protocol, *tls.Config) { |
| 44 | + switch protocol { |
| 45 | + case "http": |
| 46 | + return clickhouse.HTTP, nil |
| 47 | + case "https": |
| 48 | + return clickhouse.HTTP, &tls.Config{} |
| 49 | + case "clickhouse-secure": |
| 50 | + return clickhouse.Native, &tls.Config{} |
| 51 | + default: |
| 52 | + // "clickhouse", "" or any unrecognised value → native TCP, no TLS |
| 53 | + return clickhouse.Native, nil |
| 54 | + } |
| 55 | +} |
0 commit comments