Documentation
¶
Overview ¶
Package db supplies some assistant functions about the database.
Index ¶
- Variables
- func InitMysqlDB(connURL string, configs ...Config) *sqlx.DB
- func RegisterSQLDBOpts()
- func SetMySQLLocation(mysqlConnURL string, loc *time.Location) string
- type Config
- type DB
- type Pool
- func (p *Pool) AddDB(index int, writer DB, reader ...DB)
- func (p *Pool) AddReaderDB(index int, reader DB)
- func (p *Pool) GetAllDBs() []Wrapper
- func (p *Pool) GetDB(key string) DB
- func (p *Pool) GetReaderDB(key string) DB
- func (p *Pool) GetWriterDB(key string) DB
- func (p *Pool) SetIndexer(index func(key string) int)
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = []Config{OnExit(), Ping(), MaxOpenConns(0)}
DefaultConfig is the default config.
var Location = time.UTC
Location sets the location in sql connection url if missing.
var SQLDBOptGroup = gconf.NewGroup("database.sql")
SQLDBOptGroup is the group of the sql database config options.
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 ¶
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.
Types ¶
type Config ¶
Config is used to set the sqlx.DB.
func ConnMaxIdleTime ¶
ConnMaxIdleTime returns a Config to set the maximum idle time of the connection.
func ConnMaxLifetime ¶
ConnMaxLifetime returns a Config to set the maximum lifetime of the connection.
func LogInterceptor ¶
LogInterceptor returns a Config to set the log interceptor for sqlx.DB.
func MaxIdleConns ¶
MaxIdleConns returns a Config to set the maximum number of the idle connection.
func MaxOpenConns ¶
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.
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 (*Pool) AddDB ¶
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 ¶
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) GetReaderDB ¶
GetReaderDB returns the reader DB by the key.
If no reader, return the writer.
func (*Pool) SetIndexer ¶
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".