dbv1

package
v0.0.0-...-c9c6fa8 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 23 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, timeout ...time.Duration) *BatchTx

func NewBatchTxWithContext

func NewBatchTxWithContext(ctx context.Context, conn bun.IDB, timeout ...time.Duration) *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

GetSelect returns a select query with a hardcoded "status>0" filter. NOTE: This assumes all models have a "status" column. For models without this column, use c.GetDB().NewSelect().Model(model) directly.

func (*Client) GetSoftDelete

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

GetSoftDelete returns an update query that sets "status=-1" for soft deletion. NOTE: This assumes all models have a "status" column. For models without this column, use c.GetTx(tx...).NewUpdate().Model(model) directly.

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) Order

func (c *Client) Order() int

Order returns the close order for graceful shutdown. DB clients should close after Kafka consumers (lower order) to ensure consumers can finish processing messages that may need DB access.

func (*Client) ParseErr

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

func (*Client) QueryAll

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

QueryAll queries all records with default ordering by "id desc". NOTE: This assumes all models have an "id" column. For models without this column, use GetSelect(result) or a custom query directly.

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 {
	Name                  string        `yaml:"name" json:"name"`
	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       time.Duration `yaml:"connMaxLifetime" json:"connMaxLifetime" default:"28800s"`
	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" default:"true"`
	TransactionTimeout    time.Duration `yaml:"transactionTimeout" json:"transactionTimeout" default:"30s"`
}

func (*Config) Validate

func (c *Config) Validate() error

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