mysql

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: MIT Imports: 8 Imported by: 0

README

Introduction

Usages

Customized use case.

func main() {
    s, err := hrpc.NewServer(
		option.WithServerName("orderservice"),
		option.WithDatabases(mysql.New(
            mysql.WithCustomized(), // Very important, if you forget to provide this option, the HRPC will read configuration got from the configuration center to load.
            mysql.WithAddress("xxxx"),
            mysql.WithDB("aaa_db"),
            mysql.WithAuth("user", "pass"),
            mysql.WithPort(12144), // Default 3306
        )),
		option.WithEnvironment(option.Development),
		option.WithHealthCheck(),
	)
    // xxx
}

Customized use case with connection pool

func main() {
    s, err := hrpc.NewServer(
		option.WithServerName("orderservice"),
		option.WithDatabases(mysql.New(
            mysql.WithCustomized(),
            mysql.WithAddress("xxxx"),
            mysql.WithDB("aaa_db"),
            mysql.WithAuth("user", "pass"),
            mysql.WithPort(12144), // Default 3306
            mysql.WithMaxOpenConns(10), // Default 3
            mysql.WithMaxIdleConns(4), // Default 1
        )),
		option.WithEnvironment(option.Development),
		option.WithHealthCheck(),
	)
    // xxx
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBreak    = errors.New("query break")
	ErrContinue = errors.New("query continue")
	ErrNoRows   = sql.ErrNoRows
)

Functions

func New

func New(opts ...Option) *mySQL

func Valid

func Valid() bool

Valid returns a bool valud to determine whether the connection is ready to use

Types

type NextFunc

type NextFunc func(*sql.Rows) error

NextFunc is designed for scanning all rows queryed from the database If error returned, it will cancel the loop in advance, and it will return the error. If ErrBreak returned, it will also cancel the loop in advance, but it will nil. If nil returned, it represents everything is OK.

type Option

type Option func(o *Options)

func WithAddress

func WithAddress(s string) Option

func WithAuth

func WithAuth(username, password string) Option

func WithCustomized

func WithCustomized() Option

WithCustomized will use your own configurations. To be reminder that you should make sure the values of Address, DB, Auth, Port have been assigned correctly.

func WithDB

func WithDB(name string) Option

func WithMaxIdleConns

func WithMaxIdleConns(i int) Option

func WithMaxOpenConns

func WithMaxOpenConns(i int) Option

func WithPort

func WithPort(port int) Option

type Options

type Options struct {
	Address  string `json:"address"`
	DBName   string `json:"dbname"`
	Username string `json:"username"`
	Password string `json:"password"`
	Port     int    `json:"port"`

	// MaxOpenConns sets the maximum number of open connections to the database
	MaxOpenConns int `json:"max_open_conns"`
	// MaxIdleConns sets the maximum number of connections in the idle connection pool.
	MaxIdleConns int `json:"max_idle_conns"`
	// contains filtered or unexported fields
}

type Proxy

type Proxy interface {
	// Transaction will start a transaction for the database
	// Ex.
	//		p.Transaction(ctx, func(tx *sql.Tx) error {
	//			tx.Exec(xxx)
	//			return nil // it will commit the transaction automatically
	//		})
	Transaction(ctx context.Context, fn TxFunc) error
	Query(ctx context.Context, query string, next NextFunc, args ...interface{}) error
	// QueryRow will query a row from the database
	// Ex.
	// 		var v1 string
	//		if err := p.QueryRow(ctx, `SELECT xx FROM xx`, []interface{}{&v1}, args); err != nil {
	//			// error
	//		}
	QueryRow(ctx context.Context, query string, dest []interface{}, args ...interface{}) error
	Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

Proxy is a abstract layer for operating the MySQL database

func Client

func Client() Proxy

Client returns the handler to operate mysql if success

type TxFunc

type TxFunc func(tx *sql.Tx) error

TxFunc can be used for transaction operation If error returned, tx.Rollback() will be called automatically If nil returned, tx.Commit() will be called automatically, also.

Jump to

Keyboard shortcuts

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