clickhouse

package
v0.3.19 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultDatabase         = "default"
	MaxExecutionTimeStr     = "max_execution_time"
	DefaultMaxExecutionTime = 60

	DefaultDialTimeout               = 30 * time.Second
	DefaultMaxConnectionsPerConn     = 2
	DefaultMaxIdleConnectionsPerConn = 1
	DefaultMaxLifetime               = time.Hour
	DefaultBlockBufferSize           = 10

	DefaultCheckInstanceStatusSQL = "select 1 as ok;"
	DefaultGetTimeZoneSQL         = "select timezone();"
)
View Source
const (
	DefaultMaxConnections     = 20
	DefaultInitConnections    = 5
	DefaultMaxIdleConnections = 10
	DefaultMaxIdleTime        = 1800 // seconds
	DefaultMaxWaitTime        = 1    // seconds
	DefaultMaxRetryCount      = -1
	DefaultKeepAliveInterval  = 300 // seconds
	DefaultKeepAliveChunkSize = 5
	DefaultSleepTime          = 1 // seconds

	DefaultUnlimitedWaitTime   = -1 // seconds
	DefaultUnlimitedRetryCount = -1
	DefaultDelayTime           = 5 // milliseconds
)

Variables

This section is empty.

Functions

func Close

func Close() error

Close closes global pool, it sets global pool to nil pointer

func Execute

func Execute(command string, args ...interface{}) (middleware.Result, error)

Execute executes given command

func ExecuteContext added in v0.3.0

func ExecuteContext(ctx context.Context, command string, args ...interface{}) (middleware.Result, error)

ExecuteContext executes given command with context

func InitGlobalPool added in v0.3.0

func InitGlobalPool(addr, dbName, dbUser, dbPass string, debug bool,
	maxConnections, initConnections, maxIdleConnections, maxIdleTime, maxWaitTime, maxRetryCount, keepAliveInterval int, altHosts ...string) error

InitGlobalPool returns a new *Pool and replaces it as global pool

func InitGlobalPoolWithConfig added in v0.3.0

func InitGlobalPoolWithConfig(config Config, maxConnections, initConnections, maxIdleConnections, maxIdleTime,
	maxWaitTime, maxRetryCount, keepAliveInterval int) error

InitGlobalPoolWithConfig returns a new *Pool with a Config object and replaces it as global pool

func InitGlobalPoolWithDefault added in v0.3.0

func InitGlobalPoolWithDefault(addr, dbName, dbUser, dbPass string) error

InitGlobalPoolWithDefault returns a new *Pool with default configuration and replaces it as global pool

func InitGlobalPoolWithPoolConfig added in v0.3.0

func InitGlobalPoolWithPoolConfig(config PoolConfig) error

InitGlobalPoolWithPoolConfig returns a new *Pool with a PoolConfig object and replaces it as global pool

func IsClosed

func IsClosed() bool

IsClosed returns if global pool had been closed

func NewStatement added in v0.3.0

func NewStatement(stmt *sql.Stmt, tx *sql.Tx, query string) middleware.Statement

func Release

func Release(num int) error

Release releases given number of connections of global pool, each connection will disconnect with database

func ReplaceGlobalPool

func ReplaceGlobalPool(pool *Pool) error

ReplaceGlobalPool replaces given pool as global pool

func Supply

func Supply(num int) error

Supply creates given number of connections and add them to free connection channel of global pool

Types

type Config

type Config struct {
	Addr     string
	DBName   string
	DBUser   string
	DBPass   string
	Debug    bool
	AltHosts []string
}

func NewConfig added in v0.3.0

func NewConfig(addr, dbName, dbUser, dbPass string, debug bool, altHosts ...string) Config

NewConfig returns a new Config

func NewConfigWithDefault added in v0.3.0

func NewConfigWithDefault(addr, dbName, dbUser, dbPass string, altHosts ...string) Config

NewConfigWithDefault returns a new Config with default value

func (*Config) AltHostsExist

func (c *Config) AltHostsExist() bool

AltHostsExist checks if alternative hosts is empty

func (*Config) AltHostsString

func (c *Config) AltHostsString() string

AltHostsString converts AltHosts to string

func (*Config) GetAddrs added in v0.3.17

func (c *Config) GetAddrs() []string

GetAddrs returns a slice of addresses

func (*Config) GetOptions added in v0.3.17

func (c *Config) GetOptions() *clickhouse.Options

GetOptions gets *clickhouse.Options

type Conn

type Conn struct {
	Config
	Conn *sql.DB
}

func NewConn added in v0.3.0

func NewConn(addr, dbName, dbUser, dbPass string, debug bool, altHosts ...string) (*Conn, error)

NewConn returns connection to Clickhouse database, be aware that addr is host:port style

func NewConnWithConfig added in v0.3.0

func NewConnWithConfig(config Config) (*Conn, error)

NewConnWithConfig returns connection to mysql database with given Config

func NewConnWithDefault added in v0.3.0

func NewConnWithDefault(addr, dbName, dbUser, dbPass string, altHosts ...string) (*Conn, error)

func (*Conn) CheckInstanceStatus

func (conn *Conn) CheckInstanceStatus() bool

CheckInstanceStatus returns if instance is ok

func (*Conn) Close added in v0.3.17

func (conn *Conn) Close() error

Close closes the connection

func (*Conn) Execute

func (conn *Conn) Execute(command string, args ...interface{}) (middleware.Result, error)

Execute executes given sql with arguments and return a result

func (*Conn) ExecuteContext added in v0.3.0

func (conn *Conn) ExecuteContext(ctx context.Context, command string, args ...interface{}) (middleware.Result, error)

ExecuteContext executes given sql with arguments and context then return a result

func (*Conn) GetTimeZone added in v0.3.16

func (conn *Conn) GetTimeZone() (*time.Location, error)

func (*Conn) Ping added in v0.3.17

func (conn *Conn) Ping() error

Ping checks if the connection is alive

func (*Conn) PingContext added in v0.3.17

func (conn *Conn) PingContext(ctx context.Context) error

PingContext checks if the connection is alive with context

func (*Conn) Prepare added in v0.3.0

func (conn *Conn) Prepare(command string) (middleware.Statement, error)

Prepare prepares given sql with arguments and return a statement

func (*Conn) PrepareContext added in v0.3.0

func (conn *Conn) PrepareContext(ctx context.Context, command string) (middleware.Statement, error)

PrepareContext prepares given sql with arguments and return a statement

type Pool

type Pool struct {
	sync.Mutex
	PoolConfig
	// contains filtered or unexported fields
}

func NewPool added in v0.3.0

func NewPool(addr, dbName, dbUser, dbPass string, debug bool,
	maxConnections, initConnections, maxIdleConnections, maxIdleTime, maxWaitTime, maxRetryCount, keepAliveInterval int, altHosts ...string) (*Pool, error)

NewPool returns a new *Pool

func NewPoolWithConfig added in v0.3.0

func NewPoolWithConfig(config Config, maxConnections, initConnections, maxIdleConnections, maxIdleTime, maxWaitTime,
	maxRetryCount, keepAliveInterval int) (*Pool, error)

NewPoolWithConfig returns a new *Pool with a Config object

func NewPoolWithDefault added in v0.3.0

func NewPoolWithDefault(addr, dbName, dbUser, dbPass string) (*Pool, error)

NewPoolWithDefault returns a new *Pool with default configuration

func NewPoolWithPoolConfig added in v0.3.0

func NewPoolWithPoolConfig(config PoolConfig) (*Pool, error)

NewPoolWithPoolConfig returns a new *Pool with a PoolConfig object

func (*Pool) Close

func (p *Pool) Close() error

Close releases each connection in the pool

func (*Pool) Get

func (p *Pool) Get() (middleware.PoolConn, error)

Get is an exported alias of get() function with routine safe

func (*Pool) IsClosed

func (p *Pool) IsClosed() bool

IsClosed returns if pool had been closed

func (*Pool) Release

func (p *Pool) Release(num int) error

Release is an exported alias of release() function

func (*Pool) Supply

func (p *Pool) Supply(num int) error

Supply is an exported alias of supply() function with routine safe

func (*Pool) Transaction

func (p *Pool) Transaction() (middleware.Transaction, error)

Transaction is used to implement the interface, but it is not supported in prometheus, never call this function

func (*Pool) UsedConnections

func (p *Pool) UsedConnections() int

UsedConnections returns used connection number

type PoolConfig

type PoolConfig struct {
	Config
	MaxConnections     int
	InitConnections    int
	MaxIdleConnections int
	MaxIdleTime        int
	MaxWaitTime        int
	MaxRetryCount      int
	KeepAliveInterval  int
}

func NewPoolConfig

func NewPoolConfig(addr, dbName, dbUser, dbPass string, debug bool, maxConnections,
	initConnections, maxIdleConnections, maxIdleTime, maxWaitTime, maxRetryCount, keepAliveInterval int, altHosts ...string) PoolConfig

NewPoolConfig returns a new PoolConfig

func NewPoolConfigWithConfig

func NewPoolConfigWithConfig(config Config, maxConnections, initConnections, maxIdleConnections,
	maxIdleTime, maxWaitTime, maxRetryCount, keepAliveInterval int) PoolConfig

NewPoolConfigWithConfig returns a new PoolConfig

func (*PoolConfig) Validate

func (cfg *PoolConfig) Validate() error

Validate validates pool config

type PoolConn

type PoolConn struct {
	*Conn
	Pool *Pool
}

func Get

func Get() (*PoolConn, error)

Get gets a connection from pool and validate it, if there is no valid connection in the pool, it will create a new connection

func NewPoolConn

func NewPoolConn(addr, dbName, dbUser, dbPass string, debug bool, alterHosts ...string) (*PoolConn, error)

NewPoolConn returns a new *PoolConn

func NewPoolConnWithPool

func NewPoolConnWithPool(pool *Pool, addr, dbName, dbUser, dbPass string, debug bool, alterHosts ...string) (*PoolConn, error)

NewPoolConnWithPool returns a new *PoolConn

func (*PoolConn) Close

func (pc *PoolConn) Close() error

Close returns connection back to the pool

func (*PoolConn) Disconnect added in v0.2.24

func (pc *PoolConn) Disconnect() error

Disconnect disconnects from mysql, normally when using connection pool, there is no need to disconnect manually, consider to use Close() instead.

func (*PoolConn) Execute

func (pc *PoolConn) Execute(command string, args ...interface{}) (middleware.Result, error)

Execute executes given sql and placeholders on the mysql server

func (*PoolConn) ExecuteContext added in v0.3.0

func (pc *PoolConn) ExecuteContext(ctx context.Context, command string, args ...interface{}) (middleware.Result, error)

ExecuteContext executes given sql and placeholders on the mysql server

func (*PoolConn) IsValid

func (pc *PoolConn) IsValid() bool

IsValid validates if connection is valid

func (*PoolConn) Prepare added in v0.3.0

func (pc *PoolConn) Prepare(command string) (middleware.Statement, error)

Prepare prepares a statement and returns a *Statement

func (*PoolConn) PrepareContext added in v0.3.0

func (pc *PoolConn) PrepareContext(ctx context.Context, command string) (middleware.Statement, error)

PrepareContext prepares a statement with context and returns a *Statement

type Result

type Result struct {
	Raw *sql.Rows
	*result.Rows
	result.Metadata
	result.Map
}

func NewEmptyResult

func NewEmptyResult() *Result

NewEmptyResult returns an empty *Result

func NewResult

func NewResult(rows *sql.Rows) (*Result, error)

NewResult returns *Result, it builds from given rows

func (*Result) GetRaw added in v0.3.3

func (r *Result) GetRaw() interface{}

GetRaw returns the raw data of the result

type Statement added in v0.3.0

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

func (*Statement) Close added in v0.3.17

func (stmt *Statement) Close() error

Close closes the statement

func (*Statement) Commit added in v0.3.17

func (stmt *Statement) Commit() error

Commit commits the transaction

func (*Statement) Execute added in v0.3.0

func (stmt *Statement) Execute(args ...interface{}) (middleware.Result, error)

Execute executes given sql and placeholders and returns a result

func (*Statement) ExecuteContext added in v0.3.0

func (stmt *Statement) ExecuteContext(ctx context.Context, args ...interface{}) (middleware.Result, error)

ExecuteContext executes given sql and placeholders with context and returns a result

func (*Statement) GetQuery added in v0.3.17

func (stmt *Statement) GetQuery() string

GetQuery returns the sql of the statement

func (*Statement) Rollback added in v0.3.17

func (stmt *Statement) Rollback() error

Rollback rollbacks the transaction

Jump to

Keyboard shortcuts

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