sqldb

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

sqldb

sqldb is a helper to connect to SQL database server.

It follows the design pattern where write operation will be directed to master DB while the read one to follower DB. This pattern will improve the read scalability because follower DB can be scaled horizontally in easy way while still maintaining data consistency which written to the master.

Write to Master, Read from Follower

In sqldb, all select, get and query will come to follower database. All exec is coming to master. Or equivalent to DDL and DML is going to master and data retrieval is going to follower.

If follower DSN is not specified, then follower will be the same as master.

Single Master - Follower model

sqldb only recognize one master database. This means sqldb is not suitable for multi-master database model.

Usage example

For initializing

dbclient, err := sqldb.Connect(context.Background(), sqldb.DBConfig{
    Driver:                "mysql", // depends on the engine, currently support postgresql and
    MasterDSN:             "username:password@tcp(host:port)/database_name",
    SlaveDSN:              "username:password@tcp(host:port)/database_name",
    MaxOpenConnections:    100, // if 0, it will use default config
    MaxIdleConnections:    10, // if 0, it will use default config
    ConnectionMaxLifetime: 10 * time.Second,
    Retry:                 3,
})
if err != nil {
    log.Println("error initialize db client, err : ", err)
    return
}
log.Println("database connected !!")

for any other example, you can check on example/sqlexample folder

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	Master
	Slave
}

func Connect

func Connect(ctx context.Context, cfg DBConfig) (db *DB, err error)

type DBConfig

type DBConfig struct {
	Driver                DBEngine      `json:"driver" yaml:"driver"`
	MasterDSN             string        `json:"master" yaml:"master"`
	SlaveDSN              string        `json:"slave" yaml:"slave"`
	MaxOpenConnections    int           `json:"max_open_conns" yaml:"max_open_conns"`
	MaxIdleConnections    int           `json:"max_idle_conns" yaml:"max_idle_conns"`
	ConnectionMaxLifetime time.Duration `json:"conn_max_lifetime" yaml:"conn_max_lifetime"`
	Retry                 int           `json:"retry" yaml:"retry"`
}

type DBEngine

type DBEngine string
const (
	Postgres DBEngine = "postgres"
	Mysql    DBEngine = "mysql"
	Mariadb  DBEngine = "mysql"
)

type Master

type Master interface {
	Exec(query string, args ...interface{}) (sql.Result, error)

	// ExecContext use master database to exec query
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

	// Begin transaction on master DB
	Begin() (*sql.Tx, error)

	// BeginTx begins transaction on master DB
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

	// Rebind a query from the default bindtype (QUESTION) to the target bindtype.
	Rebind(sql string) string

	// NamedExec do named exec on master DB
	NamedExec(query string, arg interface{}) (sql.Result, error)

	// NamedExecContext do named exec on master DB
	NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)

	// BindNamed do BindNamed on master DB
	BindNamed(query string, arg interface{}) (string, []interface{}, error)
}

Master defines operation that will be executed to master DB

type Slave

type Slave interface {
	// Get from slave database
	Get(dest interface{}, query string, args ...interface{}) error

	// Select from slave database
	Select(dest interface{}, query string, args ...interface{}) error

	// Query from slave database
	Query(query string, args ...interface{}) (*sql.Rows, error)

	// QueryRow executes QueryRow against slave DB
	QueryRow(query string, args ...interface{}) *sql.Row

	// NamedQuery do named query on slave DB
	NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)

	// GetContext from sql database
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

	// SelectContext from sql database
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

	// QueryContext from sql database
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

	// QueryRowContext from sql database
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

	// QueryxContext queries the database and returns an *sqlx.Rows. Any placeholder parameters are replaced with supplied args.
	QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)

	// QueryRowxContext queries the database and returns an *sqlx.Row. Any placeholder parameters are replaced with supplied args.
	QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row

	// NamedQueryContext do named query on slave DB
	NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)
}

Slave defines operation that will be executed to slave DB

Jump to

Keyboard shortcuts

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