dbv1

package
v0.0.0-...-ed6b86b Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: Apache-2.0 Imports: 20 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetContextWithTraceID

func GetContextWithTraceID(ctx context.Context) context.Context

GetContext getContext with traceId

func SplitIndex

func SplitIndex(pageNumber, pageSize, total int) (canSplit bool, start int, end int)

Types

type BatchTx

type BatchTx struct {
	// contains filtered or unexported fields
}

func NewBatchTx

func NewBatchTx(conn bun.IDB) *BatchTx

func (*BatchTx) Add

func (b *BatchTx) Add(txFunc func(tx *bun.Tx) error) *BatchTx

func (*BatchTx) Submit

func (b *BatchTx) Submit() (err error)

type Client

type Client struct {
	DB *bun.DB
	// contains filtered or unexported fields
}

func NewDBClient

func NewDBClient(config Config) (c *Client, err error)

func (*Client) AfterQuery

func (c *Client) AfterQuery(ctx context.Context, event *bun.QueryEvent)

func (*Client) BeforeQuery

func (c *Client) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context

func (*Client) Begin

func (c *Client) Begin(ctx context.Context) (*bun.Tx, context.CancelFunc, error)

func (*Client) Close

func (c *Client) Close()

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, model interface{}, id interface{}, tx ...*bun.Tx) (int64, error)

func (*Client) GetDB

func (c *Client) GetDB() *bun.DB

func (*Client) GetDelete

func (c *Client) GetDelete(model interface{}, tx ...*bun.Tx) *bun.DeleteQuery

func (*Client) GetInsert

func (c *Client) GetInsert(model interface{}, tx ...*bun.Tx) *bun.InsertQuery

func (*Client) GetRowsAffected

func (c *Client) GetRowsAffected(result sql.Result, err error) (int64, error)

func (*Client) GetSelect

func (c *Client) GetSelect(model interface{}) *bun.SelectQuery

func (*Client) GetSoftDelete

func (c *Client) GetSoftDelete(model interface{}, tx ...*bun.Tx) *bun.UpdateQuery

func (*Client) GetTx

func (c *Client) GetTx(tx ...*bun.Tx) bun.IDB

func (*Client) GetUpdate

func (c *Client) GetUpdate(model interface{}, tx ...*bun.Tx) *bun.UpdateQuery

func (*Client) Insert

func (c *Client) Insert(ctx context.Context, data interface{}, tx ...*bun.Tx) (int64, error)

func (*Client) ParseErr

func (c *Client) ParseErr(err error) error

func (*Client) QueryAll

func (c *Client) QueryAll(ctx context.Context, result interface{}) (int, error)

func (*Client) QueryPage

func (c *Client) QueryPage(ctx context.Context, result interface{}, filter Filter) (int, error)

func (*Client) SoftDelete

func (c *Client) SoftDelete(ctx context.Context, model interface{}, id interface{}, tx ...*bun.Tx) (int64, error)

type Config

type Config struct {
	Dialect               string        `yaml:"dialect" default:"mysql" json:"dialect"`
	Url                   string        `yaml:"url" default:"127.0.0.1:3306" json:"url"`
	DbName                string        `yaml:"dbName" json:"dbName"`
	User                  string        `yaml:"user" json:"user"`
	Password              string        `yaml:"password" json:"password"`
	EnablePasswordEncrypt bool          `yaml:"enablePasswordEncrypt" json:"enablePasswordEncrypt"`
	Charset               string        `yaml:"charset" default:"utf8mb4" json:"charset"`
	MaxOpen               int           `yaml:"maxOpen" default:"200" json:"maxOpen"`
	MaxIdle               int           `yaml:"maxIdle" default:"20" json:"maxIdle"`
	ConnMaxLifetime       int           `yaml:"connMaxLifetime" default:"28800" json:"connMaxLifetime"`
	ConnMaxIdleTime       time.Duration `yaml:"connMaxIdleTime" json:"connMaxIdleTime" default:"60s"`
	Plural                bool          `yaml:"plural" json:"plural"`
	Debug                 bool          `yaml:"debug" json:"debug"`
	EnableMetric          bool          `yaml:"enableMetric" json:"enableMetric"`
	TransactionTimeout    time.Duration `yaml:"transactionTimeout" json:"transactionTimeout" default:"30s"`
}

type DB

type DB interface {
	GetDB() *bun.DB                                                                                  // 获取数据库连接,无事物
	GetTx(tx ...*bun.Tx) bun.IDB                                                                     // 获取数据库连接,如果tx=nil,那么获取的是无事物的连接,否者返回tx。
	Begin(ctx context.Context) (*bun.Tx, context.CancelFunc, error)                                  // 获取一个连接,并且开始事务
	Close()                                                                                          // 关闭DB
	GetSelect(model interface{}) *bun.SelectQuery                                                    // 获得通用处理器:查询
	GetInsert(model interface{}, tx ...*bun.Tx) *bun.InsertQuery                                     // 获得通用处理器:写入
	GetUpdate(model interface{}, tx ...*bun.Tx) *bun.UpdateQuery                                     // 获得通用处理器:更新
	GetDelete(model interface{}, tx ...*bun.Tx) *bun.DeleteQuery                                     // 获得通用处理器:删除
	GetSoftDelete(model interface{}, tx ...*bun.Tx) *bun.UpdateQuery                                 // 获得通用处理器:逻辑删除
	Insert(ctx context.Context, data interface{}, tx ...*bun.Tx) (int64, error)                      // 通用处理:插入数据(单条及批量处理,批量太大时不要使用)
	SoftDelete(ctx context.Context, model interface{}, id interface{}, tx ...*bun.Tx) (int64, error) // 通用处理:逻辑删除(id可以是单个也可以是数组)
	Delete(ctx context.Context, model interface{}, id interface{}, tx ...*bun.Tx) (int64, error)     // 通用处理:物理删除(id可以是单个也可以是数组)
	QueryPage(ctx context.Context, result interface{}, filter Filter) (int, error)                   // 通用处理:根据条件查询
	QueryAll(ctx context.Context, result interface{}) (int, error)                                   // 通用处理:查询全量
	GetRowsAffected(result sql.Result, err error) (int64, error)                                     // 通用处理:获取执行结果影响的记录数量
	ParseErr(err error) error                                                                        // 单个数据操作,消化ErrNoRows
}

type Filter

type Filter interface {
	GetPage() (offset int, limit int, disable bool)
	Filter(db bun.IDB) *bun.SelectQuery
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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