mysql

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DataBaseName name = "DataBaseName_MYSQL_URL"

	DataBaseName_RO name = "DataBaseName_RO_MYSQL_URL"
)

Variables

View Source
var ConfigDefault = Config{
	ConnMaxIdleTime: 0,
	ConnMaxLifetime: 0,
	MaxOpenConns:    10,
	MaxIdleConns:    2,
}

Functions

func GetActiveList

func GetActiveList() (nameList []name)

func GetDSN

func GetDSN(n name) string

func IsTx

func IsTx(mx Mx) bool

func JSON

func JSON(rows *sql.Rows) (j []byte, err error)

JSON :

func Map

func Map(rows *sql.Rows) (res []map[string]interface{}, err error)

Map : 直接把取得的rows轉成萬用map

func MapV1

func MapV1(rows *sql.Rows) (data []map[string]interface{}, err error)

func MySQLErrorIs

func MySQLErrorIs(err error, code int) bool

func New

func New(c ...Config) (err error)

func Rommit

func Rommit(_log *ilog.Data, err error, txs ...*sql.Tx) error

Rommit :

使用條件有兩個:

	[1] 需賦予 func 返回值變數名
	   ✔ 正確:
			func example() (err error) { }
	   ✘ 錯誤:
			func example() (error) {}

	[2] 需要用在 defer 的 func 內,不能直接接在 defer 後面
	   ✔ 正確:
	   		defer func() { Rommit(_log, err, tx) }()
	   ✘ 錯誤:
	   		defer Rommit(_log, err, tx)

 [3] 要使用追蹤 err 範圍(在有 [1] 的前提下)
	   ✔ 支援:
	   		bar, err = foo()
	   		bar, err := foo()
	   		switch bar, err = foo(); {}
	   		if bar, err = foo(); err != nil {}
	   		if err = foo(); err != nil {}

	   ✘ 不支援:
	   		switch bar, err := foo(); {}
	   		if bar, err := foo(); err != nil {}
	   		if err := foo(); err != nil {}

運作邏輯:

if err != nil { rollback() } else { commit() }

Types

type Config

type Config struct {
	Conn            name
	ConnMaxIdleTime time.Duration
	ConnMaxLifetime time.Duration
	MaxOpenConns    int
	MaxIdleConns    int
}

type Mx

type Mx interface {
	QueryRow(query string, args ...any) *sql.Row
	Query(query string, args ...any) (*sql.Rows, error)
	Exec(query string, args ...any) (sql.Result, error)
	Prepare(query string) (*sql.Stmt, error)

	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

type Sec

type Sec time.Duration

func (Sec) Duration

func (sec Sec) Duration() time.Duration

func (Sec) MarshalJSON

func (sec Sec) MarshalJSON() ([]byte, error)

func (*Sec) Scan

func (sec *Sec) Scan(src interface{}) error

func (*Sec) UnmarshalJSON

func (sec *Sec) UnmarshalJSON(s []byte) (err error)

func (Sec) Value

func (duration Sec) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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