clickhouse

package
v0.2.26 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultDatabase     = "default"
	DefaultReadTimeout  = 10
	DefaultWriteTimeout = 10
)
View Source
const (
	DefaultMaxConnections     = 20
	DefaultInitConnections    = 5
	DefaultMaxIdleConnections = 10
	DefaultMaxIdleTime        = 1800 // seconds
	DefaultKeepAliveInterval  = 300  // seconds
	DefaultKeepAliveChunkSize = 5
	DefaultSleepTime          = 1 // seconds
)

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(sql string, args ...interface{}) (middleware.Result, error)

Execute execute given sql statement

func InitClickhouseGlobalPool

func InitClickhouseGlobalPool(addr, dbName, dbUser, dbPass string, debug bool, readTimeout, writeTimeout int,
	maxConnections, initConnections, maxIdleConnections, maxIdleTime, keepAliveInterval int, altHosts ...string) error

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

func InitClickhouseGlobalPoolWithConfig

func InitClickhouseGlobalPoolWithConfig(config Config, maxConnections, initConnections, maxIdleConnections, maxIdleTime, keepAliveInterval int) error

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

func InitClickhouseGlobalPoolWithDefault

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

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

func InitClickhouseGlobalPoolWithPoolConfig

func InitClickhouseGlobalPoolWithPoolConfig(config PoolConfig) error

InitClickhouseGlobalPoolWithPoolConfig 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 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
	ReadTimeout  int
	WriteTimeout int
	AltHosts     []string
}

func NewClickhouseConfig

func NewClickhouseConfig(addr, dbName, dbUser, dbPass string, debug bool, readTimeout, writeTimeout int, altHosts ...string) Config

NewClickhouseConfig returns a new Config

func NewClickhouseConfigWithDefault

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

NewClickhouseConfig returns a new Config with default value

func (*Config) AltHostsExist

func (c *Config) AltHostsExist() bool

AltHostExists checks if alternative hosts is empty

func (*Config) AltHostsString

func (c *Config) AltHostsString() string

AltHostsString converts AltHosts to string

func (*Config) GetConnectionString

func (c *Config) GetConnectionString() string

GetConnectString generates connection string to clickhouse

type Conn

type Conn struct {
	Config
	clickhouse.Clickhouse
}

func NewClickhouseConn

func NewClickhouseConn(addr, dbName, dbUser, dbPass string, debug bool, readTimeout, writeTimeout int, altHosts ...string) (*Conn, error)

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

func NewClickhouseConnWithConfig

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

NewMySQLConn returns connection to mysql database with given Config

func NewClickhouseConnWithDefault

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

func (*Conn) Begin

func (conn *Conn) Begin() error

func (*Conn) CheckInstanceStatus

func (conn *Conn) CheckInstanceStatus() bool

func (*Conn) Commit

func (conn *Conn) Commit() error

func (*Conn) Execute

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

Execute executes given sql with arguments and return a result

func (*Conn) Rollback

func (conn *Conn) Rollback() error

type Pool

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

func NewClickhousePool

func NewClickhousePool(addr, dbName, dbUser, dbPass string, debug bool, readTimeout, writeTimeout int,
	maxConnections, initConnections, maxIdleConnections, maxIdleTime, keepAliveInterval int, altHosts ...string) (*Pool, error)

NewClickhousePool returns a new *Pool

func NewClickhousePoolWithConfig

func NewClickhousePoolWithConfig(config Config, maxConnections, initConnections, maxIdleConnections, maxIdleTime, keepAliveInterval int) (*Pool, error)

NewClickhousePoolWithConfig returns a new *Pool with a Config object

func NewClickhousePoolWithDefault

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

NewClickhousePoolWithDefault returns a new *Pool with default configuration

func NewClickhousePoolWithPoolConfig

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

NewClickhousePoolWithPoolConfig 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 simply returns *PoolConn, because it had implemented Transaction interface

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
	KeepAliveInterval  int
}

func NewPoolConfig

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

NewPoolConfig returns a new PoolConfig

func NewPoolConfigWithConfig

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

NewPoolConfigWithConfig returns a new PoolConfig

func (*PoolConfig) Validate

func (cfg *PoolConfig) Validate() (bool, error)

Validate validates pool config

type PoolConn

type PoolConn struct {
	*Conn
	Pool *Pool
}

func Get

func Get() (*PoolConn, error)

Get 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, readTimeout, writeTimeout int, alterHosts ...string) (*PoolConn, error)

NewPoolConn returns a new *PoolConn

func NewPoolConnWithPool

func NewPoolConnWithPool(pool *Pool, addr, dbName, dbUser, dbPass string, debug bool, readTimeout, writeTimeout int, 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) IsValid

func (pc *PoolConn) IsValid() bool

IsValid validates if connection is valid

type Result

type Result struct {
	middleware.MapResult
	FieldSlice []string
	FieldMap   map[string]int
	Values     [][]driver.Value
}

func NewEmptyResult

func NewEmptyResult() *Result

NewEmptyResult returns an empty *Result

func NewResult

func NewResult(rows driver.Rows) *Result

NewResult returns *Result, it builds from given rows

func (*Result) ColumnExists

func (r *Result) ColumnExists(name string) bool

ColumnExists check if column exists in the result

func (*Result) ColumnNumber

func (r *Result) ColumnNumber() int

ColumnNumber returns how many columns in the result

func (*Result) GetFloat

func (r *Result) GetFloat(row, column int) (float64, error)

GetFloat returns float64 type value of given row and column number

func (*Result) GetFloatByName

func (r *Result) GetFloatByName(row int, name string) (float64, error)

GetFloatByName returns float64 type value of given row number and column name

func (*Result) GetFloatSlice

func (r *Result) GetFloatSlice(row, column int) ([]float64, error)

GetFloatSlice returns []float64 type value of given row and column number

func (*Result) GetFloatSliceByName

func (r *Result) GetFloatSliceByName(row int, name string) ([]float64, error)

GetFloatSliceByName returns []float64 type value of given row number and column name

func (*Result) GetInt

func (r *Result) GetInt(row, column int) (int64, error)

GetInt returns int64 type value of given row and column number

func (*Result) GetIntByName

func (r *Result) GetIntByName(row int, name string) (int64, error)

GetIntByName returns int64 type value of given row number and column name

func (*Result) GetIntSlice

func (r *Result) GetIntSlice(row, column int) ([]int64, error)

GetIntSlice returns []uint64 type value of given row and column number

func (*Result) GetIntSliceByName

func (r *Result) GetIntSliceByName(row int, name string) ([]int64, error)

GetIntSliceByName returns []int64 type value of given row number and column name

func (*Result) GetSlice

func (r *Result) GetSlice(row, column int) ([]interface{}, error)

GetSlice returns []interface type value of given row and column number

func (*Result) GetSliceByName

func (r *Result) GetSliceByName(row int, name string) ([]interface{}, error)

GetStringByName returns []interface type value of given row number and column name

func (*Result) GetString

func (r *Result) GetString(row, column int) (string, error)

GetString returns string type value of given row and column number

func (*Result) GetStringByName

func (r *Result) GetStringByName(row int, name string) (string, error)

GetStringByName returns string type value of given row number and column name

func (*Result) GetStringSlice

func (r *Result) GetStringSlice(row, column int) ([]string, error)

GetStringSlice returns []string type value of given row and column number

func (*Result) GetStringSliceByName

func (r *Result) GetStringSliceByName(row int, name string) ([]string, error)

GetStringSliceByName returns []string type value of given row number and column name

func (*Result) GetUint

func (r *Result) GetUint(row, column int) (uint64, error)

GetUint returns uint64 type value of given row and column number

func (*Result) GetUintByName

func (r *Result) GetUintByName(row int, name string) (uint64, error)

GetUintByName returns uint64 type value of given row number and column name

func (*Result) GetUintSlice

func (r *Result) GetUintSlice(row, column int) ([]uint64, error)

GetUintSlice returns []uint64 type value of given row and column number

func (*Result) GetUintSliceByName

func (r *Result) GetUintSliceByName(row int, name string) ([]uint64, error)

GetUintSliceByName returns []uint64 type value of given row number and column name

func (*Result) GetValue

func (r *Result) GetValue(row, column int) (interface{}, error)

GetValue returns interface{} type value of given row and column number

func (*Result) GetValueByName

func (r *Result) GetValueByName(row int, name string) (interface{}, error)

GetValueByName returns interface{} type value of given row number and column name

func (*Result) IsNull

func (r *Result) IsNull(row, column int) (bool, error)

IsNull checks if value of given row and column index is nil

func (*Result) IsNullByName

func (r *Result) IsNullByName(row int, name string) (bool, error)

IsNullByName checks if value of given row number and column name is nil

func (*Result) LastInsertID

func (r *Result) LastInsertID() (uint64, error)

LastInsertID always returns error, because clickhouse does not support this, this function is only for implementing the middleware.Result interface

func (*Result) MapToStructByRowIndex

func (r *Result) MapToStructByRowIndex(in interface{}, row int, tag string) error

MapToStructByRowIndex maps row of given index result to the struct first argument must be a pointer to struct, each column in the row maps to a field of the struct, tag argument is the tag of the field, it represents the column name, if there is no such tag in the field, this field will be ignored, so set tag to each field that need to be mapped, using "middleware" as the tag is recommended.

func (*Result) MapToStructSlice

func (r *Result) MapToStructSlice(in interface{}, tag string) error

MapToStructSlice maps each row to a struct of the first argument, first argument must be a slice of pointers to structs, each row in the result maps to a struct in the slice, each column in the row maps to a field of the struct, tag argument is the tag of the field, it represents the column name, if there is no such tag in the field, this field will be ignored, so set tag to each field that need to be mapped, using "middleware" as the tag is recommended.

func (*Result) NameIndex

func (r *Result) NameIndex(name string) (int, error)

NameIndex returns index of given column

func (*Result) RowNumber

func (r *Result) RowNumber() int

RowNumber returns how many rows in the result

func (*Result) RowsAffected

func (r *Result) RowsAffected() (uint64, error)

LastInsertID always returns error, because clickhouse does not support this, this function is only for implementing the middleware.Result interface

Jump to

Keyboard shortcuts

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