clickhouse

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package clickhouse provides a ClickHouse client implementation

Package clickhouse provides test mocks for the ClickHouse client. This file should only be imported in test files.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDestMustBePointerToSlice = errors.New("dest must be a pointer to a slice")
	ErrDataMustBeSlice          = errors.New("data must be a slice")
	ErrClickHouseResponse       = errors.New("clickhouse error")
)

Define static errors

View Source
var (
	ErrBothMustBeSlices = errors.New("both dest and data must be slices")
)

Define static errors

View Source
var (
	ErrURLRequired = errors.New("URL is required")
)

Static errors for configuration validation

Functions

func EnsureDatabase

func EnsureDatabase(ctx context.Context, client ClientInterface, database, cluster string) error

EnsureDatabase creates a database if it doesn't exist

func TableExists

func TableExists(ctx context.Context, client ClientInterface, database, table string) (bool, error)

TableExists checks if a table exists in the given database

Types

type ClientInterface

type ClientInterface interface {
	// QueryOne executes a query and returns a single result
	QueryOne(ctx context.Context, query string, dest interface{}) error
	// QueryMany executes a query and returns multiple results
	QueryMany(ctx context.Context, query string, dest interface{}) error
	// Execute runs a query without expecting results
	Execute(ctx context.Context, query string) error
	// BulkInsert performs a bulk insert operation
	BulkInsert(ctx context.Context, table string, data interface{}) error
	// IsStorageEmpty checks if a table has any records matching the given conditions
	IsStorageEmpty(ctx context.Context, table string, conditions map[string]interface{}) (bool, error)
	// Start initializes the client
	Start() error
	// Stop closes the client
	Stop() error
}

ClientInterface defines the methods for interacting with ClickHouse

func NewClient

func NewClient(logger *logrus.Logger, cfg *Config) (ClientInterface, error)

NewClient creates a new HTTP-based ClickHouse client

type Config

type Config struct {
	URL           string        `yaml:"url" validate:"required,url"`
	Cluster       string        `yaml:"cluster"`
	LocalSuffix   string        `yaml:"local_suffix"`
	QueryTimeout  time.Duration `yaml:"query_timeout"`
	InsertTimeout time.Duration `yaml:"insert_timeout"`
	Debug         bool          `yaml:"debug"`
	KeepAlive     time.Duration `yaml:"keep_alive"`
	AdminDatabase string        `yaml:"admin_database"`
	AdminTable    string        `yaml:"admin_table"`
}

Config contains ClickHouse connection and cluster settings

func (*Config) SetDefaults

func (c *Config) SetDefaults()

SetDefaults sets default values for the configuration

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the configuration is valid

type MockCall

type MockCall struct {
	Method string
	Args   []interface{}
}

MockCall represents a method call made to the mock

type MockClient

type MockClient struct {
	// Function fields that can be set by tests
	QueryOneFunc   func(ctx context.Context, query string, dest interface{}) error
	QueryManyFunc  func(ctx context.Context, query string, dest interface{}) error
	ExecuteFunc    func(ctx context.Context, query string) error
	BulkInsertFunc func(ctx context.Context, table string, data interface{}) error
	StartFunc      func() error
	StopFunc       func() error

	// Track calls for assertions
	Calls []MockCall
}

MockClient is a mock implementation of ClientInterface for testing. It should only be used in test files, not in production code.

func NewMockClient

func NewMockClient() *MockClient

NewMockClient creates a new mock client with default implementations

func (*MockClient) BulkInsert

func (m *MockClient) BulkInsert(ctx context.Context, table string, data interface{}) error

BulkInsert implements ClientInterface

func (*MockClient) Execute

func (m *MockClient) Execute(ctx context.Context, query string) error

Execute implements ClientInterface

func (*MockClient) GetCallCount

func (m *MockClient) GetCallCount(method string) int

GetCallCount returns the number of times a method was called

func (*MockClient) QueryMany

func (m *MockClient) QueryMany(ctx context.Context, query string, dest interface{}) error

QueryMany implements ClientInterface

func (*MockClient) QueryOne

func (m *MockClient) QueryOne(ctx context.Context, query string, dest interface{}) error

QueryOne implements ClientInterface

func (*MockClient) Reset

func (m *MockClient) Reset()

Reset clears all recorded calls

func (*MockClient) SetError

func (m *MockClient) SetError(err error)

SetError sets all functions to return the specified error

func (*MockClient) SetQueryManyResponse

func (m *MockClient) SetQueryManyResponse(data interface{})

SetQueryManyResponse sets up the mock to return specific data for QueryMany

func (*MockClient) SetQueryOneResponse

func (m *MockClient) SetQueryOneResponse(data interface{})

SetQueryOneResponse sets up the mock to return specific data for QueryOne

func (*MockClient) Start

func (m *MockClient) Start() error

Start implements ClientInterface

func (*MockClient) Stop

func (m *MockClient) Stop() error

Stop implements ClientInterface

func (*MockClient) WasCalled

func (m *MockClient) WasCalled(method string) bool

WasCalled returns true if the specified method was called

Jump to

Keyboard shortcuts

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