Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HealthChecker ¶
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 ¶
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
}
Output:
func NewClickHouse ¶
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()
}
Output:
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()
}
Output: