ch

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2022 License: BSD-2-Clause Imports: 20 Imported by: 29

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SafeQuery

func SafeQuery(query string, args ...any) chschema.QueryWithArgs

Types

type AfterScanRowHook

type AfterScanRowHook = chschema.AfterScanRowHook

type CHModel

type CHModel = chschema.CHModel

type Config

type Config struct {
	chpool.Config

	Network  string
	Addr     string
	User     string
	Password string
	Database string

	DialTimeout   time.Duration
	TLSConfig     *tls.Config
	QuerySettings map[string]any

	ReadTimeout  time.Duration
	WriteTimeout time.Duration

	MaxRetries      int
	MinRetryBackoff time.Duration
	MaxRetryBackoff time.Duration
}

type CreateTableQuery

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

func NewCreateTableQuery

func NewCreateTableQuery(db *DB) *CreateTableQuery

func (*CreateTableQuery) AppendNamedArg

func (q *CreateTableQuery) AppendNamedArg(fmter chschema.Formatter, b []byte, name string) ([]byte, bool)

func (*CreateTableQuery) AppendQuery

func (q *CreateTableQuery) AppendQuery(fmter chschema.Formatter, b []byte) (_ []byte, err error)

func (*CreateTableQuery) ColumnExpr

func (q *CreateTableQuery) ColumnExpr(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) DB

func (q *CreateTableQuery) DB() *DB

func (*CreateTableQuery) Engine

func (q *CreateTableQuery) Engine(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) Exec

func (q *CreateTableQuery) Exec(ctx context.Context) (sql.Result, error)

func (*CreateTableQuery) GetModel

func (q *CreateTableQuery) GetModel() Model

func (*CreateTableQuery) GetTableName

func (q *CreateTableQuery) GetTableName() string

func (*CreateTableQuery) IfNotExists

func (q *CreateTableQuery) IfNotExists() *CreateTableQuery

func (*CreateTableQuery) Model

func (q *CreateTableQuery) Model(model any) *CreateTableQuery

func (*CreateTableQuery) ModelTableExpr

func (q *CreateTableQuery) ModelTableExpr(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) Operation

func (q *CreateTableQuery) Operation() string

func (*CreateTableQuery) Order

func (q *CreateTableQuery) Order(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) Partition

func (q *CreateTableQuery) Partition(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) Setting

func (q *CreateTableQuery) Setting(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) TTL

func (q *CreateTableQuery) TTL(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) Table

func (q *CreateTableQuery) Table(tables ...string) *CreateTableQuery

func (*CreateTableQuery) TableExpr

func (q *CreateTableQuery) TableExpr(query string, args ...any) *CreateTableQuery

type DB

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

func Connect

func Connect(opts ...Option) *DB

func (*DB) AddQueryHook

func (db *DB) AddQueryHook(hook QueryHook)

AddQueryHook adds a hook into query processing.

func (*DB) Close

func (db *DB) Close() error

Close closes the database client, releasing any open resources.

It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.

func (*DB) Config

func (db *DB) Config() *Config

func (*DB) Exec

func (db *DB) Exec(query string, args ...any) (sql.Result, error)

func (*DB) ExecContext

func (db *DB) ExecContext(
	ctx context.Context, query string, args ...any,
) (sql.Result, error)

func (*DB) FormatQuery

func (db *DB) FormatQuery(query string, args ...any) string

func (*DB) Formatter

func (db *DB) Formatter() chschema.Formatter

func (*DB) NewCreateTable

func (db *DB) NewCreateTable() *CreateTableQuery

func (*DB) NewDropTable

func (db *DB) NewDropTable() *DropTableQuery

func (*DB) NewInsert

func (db *DB) NewInsert() *InsertQuery

func (*DB) NewSelect

func (db *DB) NewSelect() *SelectQuery

func (*DB) NewTruncateTable

func (db *DB) NewTruncateTable() *TruncateTableQuery

func (*DB) Ping

func (db *DB) Ping(ctx context.Context) error

func (*DB) Query

func (db *DB) Query(query string, args ...any) (*Rows, error)

func (*DB) QueryContext

func (db *DB) QueryContext(
	ctx context.Context, query string, args ...any,
) (*Rows, error)

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...any) *Row

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *Row

func (*DB) ResetModel

func (db *DB) ResetModel(ctx context.Context, models ...any) error

func (*DB) Stats

func (db *DB) Stats() DBStats

func (*DB) String

func (db *DB) String() string

func (*DB) WithFormatter

func (db *DB) WithFormatter(fmter chschema.Formatter) *DB

func (*DB) WithTimeout

func (db *DB) WithTimeout(d time.Duration) *DB

type DBStats

type DBStats struct {
	Queries uint64
	Errors  uint64
}

type DropTableQuery

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

func NewDropTableQuery

func NewDropTableQuery(db *DB) *DropTableQuery

func (*DropTableQuery) AppendNamedArg

func (q *DropTableQuery) AppendNamedArg(fmter chschema.Formatter, b []byte, name string) ([]byte, bool)

func (*DropTableQuery) AppendQuery

func (q *DropTableQuery) AppendQuery(fmter chschema.Formatter, b []byte) (_ []byte, err error)

func (*DropTableQuery) DB

func (q *DropTableQuery) DB() *DB

func (*DropTableQuery) Exec

func (q *DropTableQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*DropTableQuery) GetModel

func (q *DropTableQuery) GetModel() Model

func (*DropTableQuery) GetTableName

func (q *DropTableQuery) GetTableName() string

func (*DropTableQuery) IfExists

func (q *DropTableQuery) IfExists() *DropTableQuery

func (*DropTableQuery) Model

func (q *DropTableQuery) Model(model any) *DropTableQuery

func (*DropTableQuery) ModelTableExpr

func (q *DropTableQuery) ModelTableExpr(query string, args ...any) *DropTableQuery

func (*DropTableQuery) Operation

func (q *DropTableQuery) Operation() string

func (*DropTableQuery) Table

func (q *DropTableQuery) Table(tables ...string) *DropTableQuery

func (*DropTableQuery) TableExpr

func (q *DropTableQuery) TableExpr(query string, args ...any) *DropTableQuery

type Error

type Error struct {
	Code       int32
	Name       string
	Message    string
	StackTrace string
	// contains filtered or unexported fields
}

func (*Error) Error

func (exc *Error) Error() string

type Ident

type Ident = chschema.Ident

type InValues

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

func In

func In(slice any) InValues

func (InValues) AppendQuery

func (in InValues) AppendQuery(fmter chschema.Formatter, b []byte) (_ []byte, err error)

type InsertQuery

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

func NewInsertQuery

func NewInsertQuery(db *DB) *InsertQuery

func (*InsertQuery) AppendQuery

func (q *InsertQuery) AppendQuery(fmter chschema.Formatter, b []byte) (_ []byte, err error)

func (*InsertQuery) Column

func (q *InsertQuery) Column(columns ...string) *InsertQuery

func (*InsertQuery) ColumnExpr

func (q *InsertQuery) ColumnExpr(query string, args ...any) *InsertQuery

func (*InsertQuery) ExcludeColumn

func (q *InsertQuery) ExcludeColumn(columns ...string) *InsertQuery

func (*InsertQuery) Exec

func (q *InsertQuery) Exec(ctx context.Context) (sql.Result, error)

func (*InsertQuery) Model

func (q *InsertQuery) Model(model any) *InsertQuery

func (*InsertQuery) ModelTableExpr

func (q *InsertQuery) ModelTableExpr(query string, args ...any) *InsertQuery

func (*InsertQuery) Operation

func (q *InsertQuery) Operation() string

func (*InsertQuery) Setting

func (q *InsertQuery) Setting(query string, args ...any) *InsertQuery

func (*InsertQuery) Table

func (q *InsertQuery) Table(tables ...string) *InsertQuery

func (*InsertQuery) TableExpr

func (q *InsertQuery) TableExpr(query string, args ...any) *InsertQuery

func (*InsertQuery) Where

func (q *InsertQuery) Where(query string, args ...any) *InsertQuery

func (*InsertQuery) WhereGroup

func (q *InsertQuery) WhereGroup(sep string, fn func(*WhereQuery)) *InsertQuery

func (*InsertQuery) WhereOr

func (q *InsertQuery) WhereOr(query string, args ...any) *InsertQuery

type Model

type Model = chschema.Model

type Option

type Option func(db *DB)

func WithAddr

func WithAddr(addr string) Option

WithAddr configures TCP host:port or Unix socket depending on Network.

func WithDSN

func WithDSN(dsn string) Option

func WithDatabase

func WithDatabase(database string) Option

func WithDialTimeout

func WithDialTimeout(timeout time.Duration) Option

WithDialTimeout configures dial timeout for establishing new connections. Default is 5 seconds.

func WithDiscardUnknownColumns

func WithDiscardUnknownColumns() Option

func WithInsecure

func WithInsecure(on bool) Option

func WithMaxConnAge

func WithMaxConnAge(timeout time.Duration) Option

WithMaxConnAge configures Connection age at which client retires (closes) the connection. It is useful with proxies like HAProxy. Default is to not close aged connections.

func WithMaxRetries

func WithMaxRetries(maxRetries int) Option

WithMaxRetries configures maximum number of retries before giving up. Default is to retry query 2 times.

func WithMaxRetryBackoff

func WithMaxRetryBackoff(backoff time.Duration) Option

WithMaxRetryBackoff configures maximum backoff between each retry. Default is 4 seconds; -1 disables backoff.

func WithMinIdleConns

func WithMinIdleConns(minIdleConns int) Option

WithMinIdleConns configures minimum number of idle connections which is useful when establishing new connection is slow.

func WithMinRetryBackoff

func WithMinRetryBackoff(backoff time.Duration) Option

WithMinRetryBackoff configures minimum backoff between each retry. Default is 250 milliseconds; -1 disables backoff.

func WithPassword

func WithPassword(password string) Option

func WithPoolSize

func WithPoolSize(poolSize int) Option

WithPoolSize configures maximum number of socket connections. Default is 2 connections per every CPU as reported by runtime.NumCPU.

func WithPoolTimeout

func WithPoolTimeout(timeout time.Duration) Option

WithPoolTimeout configures time for which client waits for free connection if all connections are busy before returning an error. Default is 30 seconds if ReadTimeOut is not defined, otherwise, ReadTimeout + 1 second.

func WithQuerySettings

func WithQuerySettings(params map[string]any) Option

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) Option

WithReadTimeout configures timeout for socket reads. If reached, commands will fail with a timeout instead of blocking.

func WithTLSConfig

func WithTLSConfig(cfg *tls.Config) Option

WithTLSConfig configures TLS config for secure connections.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

func WithUser

func WithUser(user string) Option

func WithWriteTimeout

func WithWriteTimeout(timeout time.Duration) Option

WithWriteTimeout configures timeout for socket writes. If reached, commands will fail with a timeout instead of blocking.

type Query

type Query = chschema.Query

type QueryEvent

type QueryEvent struct {
	DB *DB

	Model     Model
	IQuery    Query
	Query     string
	QueryArgs []any

	StartTime time.Time
	Result    sql.Result
	Err       error

	Stash map[any]any
}

func (*QueryEvent) Operation

func (e *QueryEvent) Operation() string

type QueryHook

type QueryHook interface {
	BeforeQuery(context.Context, *QueryEvent) context.Context
	AfterQuery(context.Context, *QueryEvent)
}

QueryHook ...

type Row

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

func (*Row) Err

func (r *Row) Err() error

func (*Row) Scan

func (r *Row) Scan(dest ...any) error

type Rows

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

Rows is the result of a query. Its cursor starts before the first row of the result set. Use Next to advance from row to row.

func (*Rows) Close

func (rs *Rows) Close() error

func (*Rows) ColumnTypes

func (rs *Rows) ColumnTypes() ([]*sql.ColumnType, error)

func (*Rows) Columns

func (rs *Rows) Columns() ([]string, error)

func (*Rows) Err

func (rs *Rows) Err() error

func (*Rows) Next

func (rs *Rows) Next() bool

func (*Rows) NextResultSet

func (rs *Rows) NextResultSet() bool

func (*Rows) Scan

func (rs *Rows) Scan(dest ...any) error

func (*Rows) ScanBlock

func (rs *Rows) ScanBlock(block *chschema.Block) error

type Safe

type Safe = chschema.Safe

type SelectQuery

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

func NewSelectQuery

func NewSelectQuery(db *DB) *SelectQuery

func (*SelectQuery) AppendQuery

func (q *SelectQuery) AppendQuery(fmter chschema.Formatter, b []byte) (_ []byte, err error)

func (*SelectQuery) Apply

func (q *SelectQuery) Apply(fn func(*SelectQuery) *SelectQuery) *SelectQuery

func (*SelectQuery) Column

func (q *SelectQuery) Column(columns ...string) *SelectQuery

func (*SelectQuery) ColumnExpr

func (q *SelectQuery) ColumnExpr(query string, args ...any) *SelectQuery

func (*SelectQuery) Count

func (q *SelectQuery) Count(ctx context.Context) (int, error)

Count returns number of rows matching the query using count aggregate function.

func (*SelectQuery) Distinct

func (q *SelectQuery) Distinct() *SelectQuery

func (*SelectQuery) DistinctOn

func (q *SelectQuery) DistinctOn(query string, args ...any) *SelectQuery

func (*SelectQuery) Err

func (q *SelectQuery) Err(err error) *SelectQuery

func (*SelectQuery) ExcludeColumn

func (q *SelectQuery) ExcludeColumn(columns ...string) *SelectQuery

func (*SelectQuery) Final

func (q *SelectQuery) Final() *SelectQuery

func (*SelectQuery) Group

func (q *SelectQuery) Group(columns ...string) *SelectQuery

func (*SelectQuery) GroupExpr

func (q *SelectQuery) GroupExpr(group string, args ...any) *SelectQuery

func (*SelectQuery) Having

func (q *SelectQuery) Having(having string, args ...any) *SelectQuery

func (*SelectQuery) Join

func (q *SelectQuery) Join(join string, args ...any) *SelectQuery

func (*SelectQuery) JoinOn

func (q *SelectQuery) JoinOn(cond string, args ...any) *SelectQuery

func (*SelectQuery) JoinOnOr

func (q *SelectQuery) JoinOnOr(cond string, args ...any) *SelectQuery

func (*SelectQuery) Limit

func (q *SelectQuery) Limit(limit int) *SelectQuery

func (*SelectQuery) Model

func (q *SelectQuery) Model(model any) *SelectQuery

func (*SelectQuery) ModelTableExpr

func (q *SelectQuery) ModelTableExpr(query string, args ...any) *SelectQuery

func (*SelectQuery) Offset

func (q *SelectQuery) Offset(offset int) *SelectQuery

func (*SelectQuery) Operation

func (q *SelectQuery) Operation() string

func (*SelectQuery) Order

func (q *SelectQuery) Order(orders ...string) *SelectQuery

func (*SelectQuery) OrderExpr

func (q *SelectQuery) OrderExpr(order string, args ...any) *SelectQuery

Order adds sort order to the Query.

func (*SelectQuery) Sample

func (q *SelectQuery) Sample(query string, args ...any) *SelectQuery

func (*SelectQuery) Scan

func (q *SelectQuery) Scan(ctx context.Context, values ...any) error

func (*SelectQuery) ScanAndCount

func (q *SelectQuery) ScanAndCount(
	ctx context.Context, values ...any,
) (count int, firstErr error)

SelectAndCount runs Select and Count in two goroutines, waits for them to finish and returns the result. If query limit is -1 it does not select any data and only counts the results.

func (*SelectQuery) ScanColumns

func (q *SelectQuery) ScanColumns(ctx context.Context, values ...any) error

func (*SelectQuery) Setting

func (q *SelectQuery) Setting(query string, args ...any) *SelectQuery

func (*SelectQuery) String

func (q *SelectQuery) String() string

func (*SelectQuery) Table

func (q *SelectQuery) Table(tables ...string) *SelectQuery

func (*SelectQuery) TableExpr

func (q *SelectQuery) TableExpr(query string, args ...any) *SelectQuery

func (*SelectQuery) Where

func (q *SelectQuery) Where(query string, args ...any) *SelectQuery

func (*SelectQuery) WhereGroup

func (q *SelectQuery) WhereGroup(sep string, fn func(*WhereQuery)) *SelectQuery

func (*SelectQuery) WhereOr

func (q *SelectQuery) WhereOr(query string, args ...any) *SelectQuery

func (*SelectQuery) With

func (q *SelectQuery) With(name string, subq chschema.QueryAppender) *SelectQuery

func (*SelectQuery) WithAlias

func (q *SelectQuery) WithAlias(name, query string, args ...any) *SelectQuery

type TableModel

type TableModel interface {
	Model

	Table() *chschema.Table
	Block(fields []*chschema.Field) *chschema.Block
}

type TruncateTableQuery

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

func NewTruncateTableQuery

func NewTruncateTableQuery(db *DB) *TruncateTableQuery

func (*TruncateTableQuery) AppendNamedArg

func (q *TruncateTableQuery) AppendNamedArg(fmter chschema.Formatter, b []byte, name string) ([]byte, bool)

func (*TruncateTableQuery) AppendQuery

func (q *TruncateTableQuery) AppendQuery(
	fmter chschema.Formatter, b []byte,
) (_ []byte, err error)

func (*TruncateTableQuery) DB

func (q *TruncateTableQuery) DB() *DB

func (*TruncateTableQuery) Exec

func (q *TruncateTableQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*TruncateTableQuery) GetModel

func (q *TruncateTableQuery) GetModel() Model

func (*TruncateTableQuery) GetTableName

func (q *TruncateTableQuery) GetTableName() string

func (*TruncateTableQuery) IfExists

func (q *TruncateTableQuery) IfExists() *TruncateTableQuery

func (*TruncateTableQuery) Model

func (q *TruncateTableQuery) Model(model any) *TruncateTableQuery

func (*TruncateTableQuery) Operation

func (q *TruncateTableQuery) Operation() string

func (*TruncateTableQuery) Table

func (q *TruncateTableQuery) Table(tables ...string) *TruncateTableQuery

func (*TruncateTableQuery) TableExpr

func (q *TruncateTableQuery) TableExpr(query string, args ...any) *TruncateTableQuery

type WhereQuery

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

func (*WhereQuery) WhereGroup

func (q *WhereQuery) WhereGroup(sep string, fn func(*WhereQuery))

Directories

Path Synopsis
cityhash102
* COPY from https://github.com/zentures/cityhash/ NOTE: The code is modified to be compatible with CityHash128 used in ClickHouse
* COPY from https://github.com/zentures/cityhash/ NOTE: The code is modified to be compatible with CityHash128 used in ClickHouse

Jump to

Keyboard shortcuts

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