clickhouse

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TimeToString

func TimeToString(t time.Time) string

TimeToString converts a time.Time object to a string representation suitable for ClickHouse queries. It formats the time in UTC, replacing the 'Z' with an empty string and 'T' with a space. The resulting string is in the format 'YYYY-MM-DD HH:MM:SS.ssssss'. The function uses a precision of 6 decimal places for the seconds, which is common in ClickHouse. This is useful for inserting or querying datetime values in ClickHouse databases.

Parameters:

  • t (time.Time): The time.Time object to be converted.

Returns:

  • A string representing the time in ClickHouse format, e.g., "toDateTime64('2023-10-01 12:34:56.789012', 6, 'UTC')".

func TimeToString32

func TimeToString32(t time.Time) string

TimeToString32 converts a time.Time object to a string representation suitable for ClickHouse queries. It formats the time in UTC, replacing the 'Z' with an empty string and 'T' with a space. The resulting string is in the format 'YYYY-MM-DD HH:MM:SS'. This function is useful for inserting or querying datetime values in ClickHouse databases without fractional seconds.

Parameters:

  • t (time.Time): The time.Time object to be converted.

Returns:

  • A string representing the time in ClickHouse format, e.g., "2023-10-01 12:34:56".

Types

type ClickHouseClient

type ClickHouseClient struct {
	logging.CustomLogger

	DoNotLogQueries bool // If true, queries will not be logged
	// contains filtered or unexported fields
}
var (
	CH ClickHouseClient
)

func (*ClickHouseClient) Client

func (dst *ClickHouseClient) Client() *driver.Conn

Client returns the underlying ClickHouse client instance. This function is useful for accessing the raw ClickHouse client methods directly, allowing for more advanced operations that may not be covered by the ClickHouseClient methods. It returns a pointer to the driver.Conn type, which is the ClickHouse client connection.

Returns:

  • *driver.Conn: A pointer to the ClickHouse client connection.

func (*ClickHouseClient) Count

func (dst *ClickHouseClient) Count(ctx context.Context, model string, query string) (uint64, error)

Count executes a count query on the ClickHouse database. It takes a context, a model name, and a query string as parameters. The function logs the execution time and the query, and returns the count of rows and any error encountered during execution. The model name is used for logging purposes to identify the operation being performed.

Parameters:

  • ctx (context.Context): The context for the operation.
  • model (string): The name of the model being counted.
  • query (string): The SQL query to be executed.

Returns:

  • uint64: The count of rows returned by the query.
  • error: An error if the execution fails, or nil if it succeeds.

func (*ClickHouseClient) Insert

func (dst *ClickHouseClient) Insert(ctx context.Context, model string, query string) error

Insert executes an insert query on the ClickHouse database. It takes a context, a model name, and a query string as parameters. The function logs the execution time and the query, and returns any error encountered during execution. The model name is used for logging purposes to identify the operation being performed.

Parameters:

  • ctx (context.Context): The context for the operation.
  • model (string): The name of the model being inserted.
  • query (string): The SQL query to be executed.

Returns:

  • error: An error if the execution fails, or nil if it succeeds.

func (*ClickHouseClient) LastQuery

func (dst *ClickHouseClient) LastQuery() string

LastQuery returns the last executed Redis query as a string. This function is useful for debugging purposes, allowing you to see the last query executed by the RedisClient.

Returns:

  • A string containing the last executed query.

func (*ClickHouseClient) LogSelect

func (dst *ClickHouseClient) LogSelect(ctx context.Context, model string, query string, start time.Time)

LogSelect puts a query to the log with a specific format. It takes a context, a model name, a query string, and the start time of the query execution. The function formats the log message to include the model name, execution time, and the query itself. This function is useful for debugging and monitoring purposes, allowing you to see the performance of your queries.

Parameters:

  • ctx (context.Context): The context for the operation.
  • model (string): The name of the model being queried.
  • query (string): The SQL query to be executed.
  • start (time.Time): The start time of the query execution.

func (*ClickHouseClient) Scan

func (dst *ClickHouseClient) Scan(ctx context.Context, model string, query string, dest ...any) error

Scan executes a query on the ClickHouse database and scans the result into the provided destination variables. It takes a context, a model name, a query string, and a variadic list of destination variables. The function logs the execution time and the query, and returns any error encountered during execution. The model name is used for logging purposes to identify the operation being performed.

Parameters:

  • ctx (context.Context): The context for the operation.
  • model (string): The name of the model being queried.
  • query (string): The SQL query to be executed.
  • dest (...any): A variadic list of destination variables to scan the result into.

Returns:

  • error: An error if the execution fails, or nil if it succeeds.

func (*ClickHouseClient) Select

func (dst *ClickHouseClient) Select(ctx context.Context, model string, query string, data any) error

Select executes a select query on the ClickHouse database. It takes a context, a model name, a query string, and a pointer to a data structure to hold the results. The function logs the execution time and the query, and returns any error encountered during execution. The model name is used for logging purposes to identify the operation being performed.

Parameters:

  • ctx (context.Context): The context for the operation.
  • model (string): The name of the model being queried.
  • query (string): The SQL query to be executed.
  • data (any): A pointer to a data structure where the results will be stored.

Returns:

  • error: An error if the execution fails, or nil if it succeeds.

func (*ClickHouseClient) Start

func (dst *ClickHouseClient) Start(ctx context.Context, hosts, username, password, db string)

Start initializes the ClickHouse client with the provided configuration. It connects to the ClickHouse server using the specified username, password, host, and database. If the connection fails, it logs an error and exits the application. The function also sets various connection settings such as maximum execution time, insert quorum, and compression method. It logs the connection details and the ClickHouse server version upon successful connection.

Parameters:

  • ctx (context.Context): The context for the connection.
  • hosts (string): The host addresses of the ClickHouse server.
  • username (string): The username for authentication.
  • password (string): The password for authentication.
  • db (string): The name of the database to connect to.

func (*ClickHouseClient) Stop

func (dst *ClickHouseClient) Stop(ctx context.Context)

Stop closes the ClickHouse client connection and logs a message indicating disconnection. It does not return any error, as the disconnection is expected to be successful. This function is typically called when the application is shutting down or when the ClickHouse client is no longer needed. It ensures that the client connection is properly closed to free up resources.

Parameters:

  • ctx (context.Context): The context for the operation.

func (*ClickHouseClient) Update

func (dst *ClickHouseClient) Update(ctx context.Context, model string, query string) (uint, error)

Update executes an update query on the ClickHouse database. It takes a context, a model name, and a query string as parameters. The function logs the execution time and the query, and returns the number of affected rows and any error encountered during execution. The model name is used for logging purposes to identify the operation being performed.

Parameters:

  • ctx (context.Context): The context for the operation.
  • model (string): The name of the model being updated.
  • query (string): The SQL query to be executed.

Returns:

  • uint: The number of affected rows (always 0 for ClickHouse).
  • error: An error if the execution fails, or nil if it succeeds.

Jump to

Keyboard shortcuts

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