util

package
v0.0.36 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2024 License: MulanPSL-2.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMysqlWrongSql      = errors.New("wrong sql")
	ErrMysqlEmptyData     = errors.New("empty data")
	ErrMysqlWrongDsn      = errors.New("wrong mysql dsn")
	ErrMysqlWrongOption   = errors.New("wrong option")
	ErrMysqlPageTooLarge  = errors.New("page too large")
	ErrMysqlColWithBQuote = errors.New("column name with back quote")
)

Functions

func AnyToBool

func AnyToBool(v any) bool

func AnyToFloat64

func AnyToFloat64(v any) (ret float64)

func AnyToInt64

func AnyToInt64(v any) (ret int64)

func AnyToStr

func AnyToStr(v any) (ret string)

func InArray

func InArray(needle any, haystack any) bool

func NumCPU

func NumCPU() int

func RandIntN

func RandIntN(i int) int

RandIntN 获得随机数

func RandRange

func RandRange(min, max int) int

RandRange 按范围取随机值 [min, max)

Types

type MysqlClient

type MysqlClient struct {
	Listener func(ctx context.Context, query string, err error, args ...any)

	ConnOptions   []*MysqlConnOption
	TimeoutInsert time.Duration
	TimeoutSelect time.Duration
	TimeoutUpdate time.Duration
	TimeoutTrans  time.Duration
	// contains filtered or unexported fields
}

func NewMysqlClient

func NewMysqlClient(connOptions ...*MysqlConnOption) (*MysqlClient, error)

func NewMysqlClientEasy

func NewMysqlClientEasy(dsn ...string) (*MysqlClient, error)

func (*MysqlClient) CloseDB

func (c *MysqlClient) CloseDB()

func (*MysqlClient) Count

func (c *MysqlClient) Count(ctx context.Context, query string, params ...any) (cnt int64, err error)

func (*MysqlClient) Delete

func (c *MysqlClient) Delete(ctx context.Context, query string, params ...any) (int64, error)

func (*MysqlClient) DeleteByIds

func (c *MysqlClient) DeleteByIds(ctx context.Context, table string, ids ...any) (int64, error)

func (*MysqlClient) GetDB

func (c *MysqlClient) GetDB(ctx context.Context, useSlave bool) (*sql.DB, error)

func (*MysqlClient) Insert

func (c *MysqlClient) Insert(ctx context.Context, table string, row *MysqlRow) (ret int64, err error)

func (*MysqlClient) InsertBatch

func (c *MysqlClient) InsertBatch(ctx context.Context, table string, rows []*MysqlRow) (ret int64, err error)

func (*MysqlClient) InsertDuplicate

func (c *MysqlClient) InsertDuplicate(ctx context.Context, table string, row *MysqlRow) (ret int64, err error)

func (*MysqlClient) InsertIgnore

func (c *MysqlClient) InsertIgnore(ctx context.Context, table string, row *MysqlRow) (ret int64, err error)

func (*MysqlClient) InsertReplace

func (c *MysqlClient) InsertReplace(ctx context.Context, table string, row *MysqlRow) (ret int64, err error)

func (*MysqlClient) Select

func (c *MysqlClient) Select(ctx context.Context, query string, params ...any) (rows []*MysqlRow, err error)

func (*MysqlClient) SelectByIds

func (c *MysqlClient) SelectByIds(
	ctx context.Context,
	fn func(ctx context.Context, row *MysqlRow) error,
	table, cols string,
	ids ...any,
) error

func (*MysqlClient) SelectPage

func (c *MysqlClient) SelectPage(
	ctx context.Context,
	fn func(ctx context.Context, row *MysqlRow) error,
	page, size int64,
	table, where, order, cols string,
	params ...any,
) (totalRows, totalPages, currentPage int64, err error)

func (*MysqlClient) SelectWalk

func (c *MysqlClient) SelectWalk(
	ctx context.Context,
	fn func(ctx context.Context, row *MysqlRow) error,
	query string,
	params ...any,
) error

func (*MysqlClient) ShowColumns

func (c *MysqlClient) ShowColumns(ctx context.Context, table string) (map[string]string, error)

func (*MysqlClient) Transaction

func (c *MysqlClient) Transaction(
	ctx context.Context,
	fn func(ctx context.Context, tx *sql.Tx) error,
	opts *sql.TxOptions,
) error

func (*MysqlClient) Truncate

func (c *MysqlClient) Truncate(ctx context.Context, table string) error

func (*MysqlClient) Update

func (c *MysqlClient) Update(ctx context.Context, query string, params ...any) (ret int64, err error)

func (*MysqlClient) UpdateById

func (c *MysqlClient) UpdateById(ctx context.Context, table string, row *MysqlRow, id any) (int64, error)

func (*MysqlClient) Version added in v0.0.32

func (c *MysqlClient) Version(ctx context.Context) (string, error)

type MysqlConnOption

type MysqlConnOption struct {
	Dsn         string
	Addr        string
	DbName      string
	MaxOpen     int
	MaxIdle     int
	MaxIdleTime time.Duration
	MaxLifetime time.Duration
}

func NewMysqlConnOption

func NewMysqlConnOption(
	dsn string,
	maxOpen, maxIdle int,
	maxIdleTime, maxLifeTime time.Duration,
) (*MysqlConnOption, error)

type MysqlRow

type MysqlRow map[string]any

func NewMysqlRow

func NewMysqlRow() *MysqlRow

func NewMysqlRowFrom added in v0.0.35

func NewMysqlRowFrom(x any) (*MysqlRow, error)

func (*MysqlRow) Drop

func (row *MysqlRow) Drop(keys ...string)

func (*MysqlRow) DropAll

func (row *MysqlRow) DropAll()

func (*MysqlRow) Get

func (row *MysqlRow) Get(key string) any

func (*MysqlRow) Has

func (row *MysqlRow) Has(key string) bool

func (*MysqlRow) IsEmpty

func (row *MysqlRow) IsEmpty() bool

func (*MysqlRow) Set

func (row *MysqlRow) Set(key string, val any)

func (*MysqlRow) ToBool

func (row *MysqlRow) ToBool(key string) bool

func (*MysqlRow) ToInt64

func (row *MysqlRow) ToInt64(key string) int64

func (*MysqlRow) ToMap

func (row *MysqlRow) ToMap(key string) map[string]any

func (*MysqlRow) ToNum

func (row *MysqlRow) ToNum(key string) float64

func (*MysqlRow) ToStr

func (row *MysqlRow) ToStr(key string) string

func (*MysqlRow) ToStrings

func (row *MysqlRow) ToStrings(key string) []string

type MysqlTable

type MysqlTable struct {
	Client *MysqlClient
	Name   string
}

func NewMysqlTable

func NewMysqlTable(client *MysqlClient, name string) *MysqlTable

func (*MysqlTable) AllWalk

func (mt *MysqlTable) AllWalk(ctx context.Context, fn func(ctx context.Context, row *MysqlRow) error, cols string) error

func (*MysqlTable) Count

func (mt *MysqlTable) Count(ctx context.Context, query string, params ...any) (cnt int64, err error)

func (*MysqlTable) Delete

func (mt *MysqlTable) Delete(ctx context.Context, query string, params ...any) (int64, error)

func (*MysqlTable) DeleteByIds

func (mt *MysqlTable) DeleteByIds(ctx context.Context, ids ...any) (int64, error)

func (*MysqlTable) GetOne

func (mt *MysqlTable) GetOne(ctx context.Context, id any) (*MysqlRow, error)

func (*MysqlTable) IdsWalk

func (mt *MysqlTable) IdsWalk(
	ctx context.Context,
	fn func(ctx context.Context, row *MysqlRow) error,
	cols string,
	ids ...any,
) error

func (*MysqlTable) Insert

func (mt *MysqlTable) Insert(ctx context.Context, row *MysqlRow) (int64, error)

func (*MysqlTable) InsertBatch

func (mt *MysqlTable) InsertBatch(ctx context.Context, rows []*MysqlRow) (int64, error)

func (*MysqlTable) InsertDuplicate

func (mt *MysqlTable) InsertDuplicate(ctx context.Context, row *MysqlRow) (int64, error)

func (*MysqlTable) InsertIgnore

func (mt *MysqlTable) InsertIgnore(ctx context.Context, row *MysqlRow) (int64, error)

func (*MysqlTable) InsertReplace

func (mt *MysqlTable) InsertReplace(ctx context.Context, row *MysqlRow) (int64, error)

func (*MysqlTable) Select

func (mt *MysqlTable) Select(ctx context.Context, query string, params ...any) ([]*MysqlRow, error)

func (*MysqlTable) SelectPage

func (mt *MysqlTable) SelectPage(
	ctx context.Context,
	fn func(ctx context.Context, row *MysqlRow) error,
	page, size int64,
	where, order, cols string,
	params ...any,
) (totalRows, totalPages, currentPage int64, err error)

func (*MysqlTable) SelectWalk

func (mt *MysqlTable) SelectWalk(ctx context.Context, fn func(ctx context.Context, row *MysqlRow) error, query string, params ...any) error

func (*MysqlTable) ShowColumns

func (mt *MysqlTable) ShowColumns(ctx context.Context) (map[string]string, error)

func (*MysqlTable) Transaction

func (mt *MysqlTable) Transaction(
	ctx context.Context,
	fn func(ctx context.Context, tx *sql.Tx) error,
	opts *sql.TxOptions,
) error

func (*MysqlTable) Truncate

func (mt *MysqlTable) Truncate(ctx context.Context) error

func (*MysqlTable) Update

func (mt *MysqlTable) Update(ctx context.Context, query string, params ...any) (int64, error)

func (*MysqlTable) UpdateById

func (mt *MysqlTable) UpdateById(ctx context.Context, row *MysqlRow, id any) (int64, error)

Jump to

Keyboard shortcuts

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