chclient

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package chclient is the ClickHouse transport layer. It wraps the official clickhouse-go driver and knows nothing about how credentials were acquired or how results are rendered.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthError

type AuthError struct{ Err error }

AuthError reports that credentials could not be obtained or were rejected.

func (*AuthError) Error

func (e *AuthError) Error() string

func (*AuthError) Unwrap

func (e *AuthError) Unwrap() error

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a session with one ClickHouse server. All statements of a session run on a single pooled connection, so session state set by statements the client does not track itself (SET ROLE, temporary tables) survives until that connection is re-established. The current database and plain settings are tracked by the client and survive reconnects.

func New

func New(opts Options, provider auth.Provider) *Client

New creates a client. No connection is made until Connect or the first query.

func (*Client) Close

func (c *Client) Close() error

Close releases the connection.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

Connect authenticates and verifies that the server accepts the session.

func (*Client) Database

func (c *Client) Database() string

Database returns the session's current database.

func (*Client) Exec

func (c *Client) Exec(ctx context.Context, sql string) (Stats, error)

Exec runs a statement that returns no result set.

func (*Client) Info

func (c *Client) Info() ServerInfo

Info returns the server information gathered by Connect.

func (*Client) Options

func (c *Client) Options() Options

Options returns the options the client was created with.

func (*Client) Query

func (c *Client) Query(ctx context.Context, sql string) (*Rows, error)

Query runs a statement and returns a streaming cursor over its result. The caller must Close the cursor before running another statement.

func (*Client) QueryStrings

func (c *Client) QueryStrings(ctx context.Context, sql string) ([][]string, error)

QueryStrings runs a query and returns every value rendered as a string. It buffers the whole result and is meant for small metadata queries.

func (*Client) Set

func (c *Client) Set(ctx context.Context, settings map[string]string) error

Set applies session settings to all subsequent statements. The settings are validated against the server first; on failure nothing changes.

func (*Client) UseDatabase

func (c *Client) UseDatabase(ctx context.Context, database string) error

UseDatabase switches the session to another database. The switch is verified against the server first; on failure the session is unchanged.

type Column

type Column struct {
	Name string
	Type string // ClickHouse type name, for example Nullable(DateTime64(3))
}

Column describes a result column.

type ConnError

type ConnError struct {
	Addr string
	Err  error
}

ConnError reports that the server could not be reached or the connection broke.

func (*ConnError) Error

func (e *ConnError) Error() string

func (*ConnError) Unwrap

func (e *ConnError) Unwrap() error

type Options

type Options struct {
	Host               string
	Port               int
	Database           string
	Protocol           string // config.ProtocolNative or config.ProtocolHTTP
	Secure             bool
	InsecureSkipVerify bool
	CACert             string
	// ClientVersion is reported to the server (visible in system.query_log).
	ClientVersion string
}

Options describe the server to connect to.

func OptionsFrom

func OptionsFrom(r *config.Resolved, clientVersion string) Options

OptionsFrom maps a resolved configuration to transport options.

func (Options) Addr

func (o Options) Addr() string

Addr returns host:port.

func (Options) TLSConfig

func (o Options) TLSConfig() (*tls.Config, error)

TLSConfig builds the TLS configuration, or nil for plaintext connections. Certificates are verified unless InsecureSkipVerify was explicitly set.

type Rows

type Rows struct {
	// contains filtered or unexported fields
}

Rows is a forward-only cursor over a query result. Rows are streamed from the server; nothing is buffered beyond the driver's current block.

func (*Rows) Close

func (r *Rows) Close() error

Close releases the cursor and its connection. If the query was cancelled it is also stopped on the server.

func (*Rows) Columns

func (r *Rows) Columns() []Column

Columns returns the result columns. It is empty for statements that return no result set (DDL and the like).

func (*Rows) Err

func (r *Rows) Err() error

Err returns the error that ended iteration, if any.

func (*Rows) Next

func (r *Rows) Next() bool

Next advances to the next row.

func (*Rows) Stats

func (r *Rows) Stats() Stats

Stats returns the read statistics reported by the server so far.

func (*Rows) Totals

func (r *Rows) Totals() (values []any, ok bool, err error)

Totals returns the extra row produced by WITH TOTALS, if the query has one. It is available once all rows have been read.

func (*Rows) Values

func (r *Rows) Values() ([]any, error)

Values returns the current row. The slice is reused by the next call. NULLs are returned as nil.

type ServerInfo

type ServerInfo struct {
	Version string
	User    string // the ClickHouse user the session is authenticated as
}

ServerInfo is what the server reported about itself and the session.

type Stats

type Stats struct {
	Rows  uint64
	Bytes uint64
}

Stats are the server-reported read statistics of a statement.

Jump to

Keyboard shortcuts

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