clickhouse

package
v0.1.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default() driver.Conn

Default returns the default singleton instance of the ClickHouse client.

func HealthChecker

func HealthChecker() health.Checker

HealthChecker returns a health.Checker that verifies ClickHouse connectivity by pinging the default client. The returned Checker reports health.StatusUnhealthy when no client has been initialised or when the ping fails; health.StatusHealthy when reachable.

Example

ExampleHealthChecker shows how to wire HealthChecker into a health.Check call for a /health HTTP endpoint. Default() is nil in this example so the Checker reports StatusUnhealthy.

package main

import (
	"context"
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/clickhouse"
	"github.com/phcp-tech/common-library-golang/health"
)

func main() {
	results := health.Check(context.Background(), clickhouse.HealthChecker())
	fmt.Println(results[0].Name)
	fmt.Println(results[0].Status == health.StatusUnhealthy) // true — Default() is nil
}
Output:
clickhouse
true

func InitDefault

func InitDefault(conf *Config) error

InitDefault initializes the default singleton ClickHouse client. If you want to use any other instance, please call NewClickHouse.

Example

ExampleInitDefault shows the singleton pattern: call InitDefault once at application startup. Subsequent calls are silently ignored (sync.Once). After a successful call, Default returns the initialised driver.Conn which can be passed directly to sqlc-generated Queries.

package main

import (
	"github.com/phcp-tech/common-library-golang/dbsqlc/clickhouse"
)

func main() {
	if err := clickhouse.InitDefault(&clickhouse.Config{
		Host:     "127.0.0.1",
		Port:     "9000",
		Database: "default",
		Username: "default",
		Password: "",
	}); err != nil {
		// handle error
		return
	}
	_ = clickhouse.Default() // driver.Conn, pass to sqlc-generated Queries
}

func NewClickHouse

func NewClickHouse(conf *Config) (driver.Conn, error)
Example

ExampleNewClickHouse shows how to open a ClickHouse connection with the default pool settings. Zero-value pool fields fall back to the dbsqlc package defaults (MaxOpenConns=100, MaxIdleConns=25, ConnMaxLifetime=60min). Note: unlike MySQL/PostgreSQL, ClickHouse establishes a real TCP connection at Open time, so a live server is required.

package main

import (
	"github.com/phcp-tech/common-library-golang/dbsqlc/clickhouse"
)

func main() {
	conn, err := clickhouse.NewClickHouse(&clickhouse.Config{
		Host:     "127.0.0.1",
		Port:     "9000",
		Database: "default",
		Username: "default",
		Password: "",
	})
	if err != nil {
		// handle error
		return
	}
	defer conn.Close()
}
Example (CustomPool)

ExampleNewClickHouse_customPool shows how to override the default connection pool settings.

package main

import (
	"github.com/phcp-tech/common-library-golang/dbsqlc/clickhouse"
)

func main() {
	conn, err := clickhouse.NewClickHouse(&clickhouse.Config{
		Host:            "127.0.0.1",
		Port:            "9000",
		Database:        "default",
		Username:        "default",
		Password:        "",
		MaxOpenConns:    50,
		MaxIdleConns:    10,
		ConnMaxLifetime: 30, // minutes
	})
	if err != nil {
		// handle error
		return
	}
	defer conn.Close()
}

Types

type Config

type Config struct {
	// Database connection parameters
	Host     string
	Port     string
	Database string
	Username string
	Password string

	// Connection pool parameters
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime int
}

Directories

Path Synopsis
Package component provides ClickHouse lifecycle integration for bootstrap.
Package component provides ClickHouse lifecycle integration for bootstrap.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL