db

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package db supplies some assistant functions about the database.

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = []Config{OnExit(), Ping(), MaxOpenConns(0)}

DefaultConfig is the default config.

View Source
var Location = time.UTC

Location sets the location in sql connection url if missing.

View Source
var SQLDBOptGroup = gconf.NewGroup("database.sql")

SQLDBOptGroup is the group of the sql database config options.

View Source
var SQLDBOpts = []gconf.Opt{
	gconf.StrOpt("connection", "The URL connection to the alarm database, user:password@tcp(127.0.0.1:3306)/db").C(false),
	gconf.IntOpt("maxconnnum", "The maximum number of the connections.").C(false).D(0),
	gconf.BoolOpt("logsqlstmt", "Log the sql statement when executing it.").C(false),
	gconf.BoolOpt("logsqlargs", "Log the arguments of sql statement when executing it.").C(false),
}

SQLDBOpts collects the options of the SQL database.

Functions

func InitMysqlDB

func InitMysqlDB(connURL string, configs ...Config) *sqlx.DB

InitMysqlDB initializes the MySQL DB.

If configs is nil, it will use DefaultConfig as the default.

func RegisterSQLDBOpts

func RegisterSQLDBOpts()

RegisterSQLDBOpts registers the default options of the sql database.

func SetMySQLLocation

func SetMySQLLocation(mysqlConnURL string, loc *time.Location) string

SetMySQLLocation sets the loc argument in the mysql connection url if missing.

Types

type Config

type Config func(*sqlx.DB)

Config is used to set the sqlx.DB.

func ConnMaxIdleTime

func ConnMaxIdleTime(d time.Duration) Config

ConnMaxIdleTime returns a Config to set the maximum idle time of the connection.

func ConnMaxLifetime

func ConnMaxLifetime(d time.Duration) Config

ConnMaxLifetime returns a Config to set the maximum lifetime of the connection.

func LogInterceptor

func LogInterceptor(debug, logArgs bool) Config

LogInterceptor returns a Config to set the log interceptor for sqlx.DB.

func MaxIdleConns

func MaxIdleConns(n int) Config

MaxIdleConns returns a Config to set the maximum number of the idle connection.

func MaxOpenConns

func MaxOpenConns(maxnum int) Config

MaxOpenConns returns a Config to set the maximum number of the open connection.

If maxnum is equal to or less than 0, it is runtime.NumCPU()*2 by default.

func OnExit

func OnExit() Config

OnExit returns a Config to register a close callback which will be called when the program exits.

func Ping

func Ping() Config

Ping returns a Config to ping the db server, which exits the program when fails.

type DB

type DB interface {
	Begin() (*sql.Tx, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
	Close() error
	Conn(ctx context.Context) (*sql.Conn, error)
	Driver() driver.Driver
	Exec(query string, args ...interface{}) (sql.Result, error)
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	Ping() error
	PingContext(ctx context.Context) error
	Prepare(query string) (*sql.Stmt, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	SetConnMaxLifetime(d time.Duration)
	SetMaxIdleConns(n int)
	SetMaxOpenConns(n int)
	Stats() sql.DBStats
}

DB is an interface to stands for the general sql.DB.

type Pool

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

Pool is a read-write DB pool.

func NewPool

func NewPool() *Pool

NewPool returns a new DB pool.

func (*Pool) AddDB

func (p *Pool) AddDB(index int, writer DB, reader ...DB)

AddDB adds a db into the pool.

If reader is nil, the reader DB is the same as writer by default.

Noitce: the index is only used to sort and identify whether two DBs are equal.

func (*Pool) AddReaderDB

func (p *Pool) AddReaderDB(index int, reader DB)

AddReaderDB adds a reader db into the pool.

Noitce: the index is only used to sort and identify whether two DBs are equal.

func (*Pool) GetAllDBs

func (p *Pool) GetAllDBs() []Wrapper

GetAllDBs returns all the dbs.

func (*Pool) GetDB

func (p *Pool) GetDB(key string) DB

GetDB is short for GetWriterDB.

func (*Pool) GetReaderDB

func (p *Pool) GetReaderDB(key string) DB

GetReaderDB returns the reader DB by the key.

If no reader, return the writer.

func (*Pool) GetWriterDB

func (p *Pool) GetWriterDB(key string) DB

GetWriterDB returns the writer DB by the key.

If no DB, return nil.

func (*Pool) SetIndexer

func (p *Pool) SetIndexer(index func(key string) int)

SetIndexer sets the indexer to get the corresponding DB by the key, that's, it will convert the same key to a constant index forever.

The default indexer only converts the key ending with any character of "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".

type Wrapper

type Wrapper struct {
	Index  int
	Writer DB
	Reader DB
}

Wrapper is a wrapper of the read and write db.

Jump to

Keyboard shortcuts

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