db

package
v1.1.14 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrDuplicatedName   utils.Error = "duplicated database name"
	ErrDatabaseNotFound utils.Error = "not found database to use"
)

Variables

This section is empty.

Functions

func AppName

func AppName(name string) utils.OptionFunc[useOption]

func CheckGormErrorFn

func CheckGormErrorFn(tx *gorm.DB) func() error

func Clauses

func Clauses(clauses ...clause.Expression) utils.OptionFunc[mysqlDALOption]

func Construct

func Construct(ctx context.Context, confs map[string]*Conf, opts ...utils.OptionExtender) func()

func JsonMarshalFn

func JsonMarshalFn(tx *gorm.DB, obj, field any) func() error

func JsonUnmarshalFn

func JsonUnmarshalFn(tx *gorm.DB, obj, field any) func() error

func Scan

func Scan[T any, TS ~[]*T](ctx context.Context, cb func(TS) bool, opts ...utils.OptionExtender) (err error)

func ScanBatch

func ScanBatch(batch int) utils.OptionFunc[scanOption]

func ScanCursor

func ScanCursor(cursorWhere any, cursorColumns []string, cursors ...any) utils.OptionFunc[scanOption]

func ScanDAL

func ScanDAL[T any, TS ~[]*T](dal DalInterface[T, TS]) utils.OptionFunc[scanOptionGeneric[T, TS]]

func ScanLimit

func ScanLimit(limit int) utils.OptionFunc[scanOption]

func ScanLog

func ScanLog(log log.Loggable) utils.OptionFunc[scanOption]

func ScanOrder

func ScanOrder(order any) utils.OptionFunc[scanOption]

func ScanUse

func ScanUse(dbName string) utils.OptionFunc[scanOption]

func ScanWhere

func ScanWhere(where any, sqlAndArguments ...any) utils.OptionFunc[scanOption]

func SetCtxGormDB

func SetCtxGormDB(ctx context.Context, db *DB) context.Context

func TxUse

func TxUse(name string) utils.OptionFunc[txOption]

func Unscoped

func Unscoped() utils.OptionFunc[mysqlDALOption]

func WithinTx

func WithinTx(ctx context.Context, cb func(ctx context.Context) (err error), opts ...utils.OptionExtender) error

WithinTx 事务内执行 DAL 操作

func WriteDB

func WriteDB() utils.OptionFunc[mysqlDALOption]

Types

type Business

type Business struct {
	Data

	UUID            uuid.UUID `gorm:"column:uuid;type:varchar(36);uniqueIndex" json:"uuid"`
	BizCreateTimeMs int64     `gorm:"column:biz_create_time;type:bigint" json:"bizCreateTime"`
	BizModifyTimeMs int64     `gorm:"column:biz_modify_time;type:bigint" json:"bizModifyTime"`
}

func (*Business) BeforeCreate

func (b *Business) BeforeCreate(tx *gorm.DB) (err error)

func (*Business) BeforeCreateFn

func (b *Business) BeforeCreateFn(tx *gorm.DB) func() error

func (*Business) BizCreateTime

func (b *Business) BizCreateTime() time.Time

func (*Business) BizModifyTime

func (b *Business) BizModifyTime() time.Time

func (*Business) Clone

func (b *Business) Clone() *Business

func (*Business) Equals

func (b *Business) Equals(o *Business) bool

type BusinessSoftDeleted

type BusinessSoftDeleted struct {
	DataSoftDeleted

	UUID            uuid.UUID `gorm:"column:uuid;type:varchar(36);uniqueIndex:uniq_uuid" json:"uuid"`
	BizCreateTimeMs int64     `gorm:"column:biz_create_time;type:bigint" json:"bizCreateTime"`
	BizModifyTimeMs int64     `gorm:"column:biz_modify_time;type:bigint" json:"bizModifyTime"`
}

func (*BusinessSoftDeleted) BeforeCreate

func (b *BusinessSoftDeleted) BeforeCreate(tx *gorm.DB) (err error)

func (*BusinessSoftDeleted) BeforeCreateFn

func (b *BusinessSoftDeleted) BeforeCreateFn(tx *gorm.DB) func() error

func (*BusinessSoftDeleted) BizCreateTime

func (b *BusinessSoftDeleted) BizCreateTime() time.Time

func (*BusinessSoftDeleted) BizModifyTime

func (b *BusinessSoftDeleted) BizModifyTime() time.Time

func (*BusinessSoftDeleted) Clone

func (*BusinessSoftDeleted) Equals

type Conf

type Conf struct {
	orm.Option             `yaml:",inline" json:",inline" toml:",inline"`
	AutoIncrementIncrement int64          `yaml:"auto_increment_increment" json:"auto_increment_increment" toml:"auto_increment_increment"`
	Sharding               []shardingConf `yaml:"sharding" json:"sharding" toml:"sharding"`
	EnableLogger           bool           `yaml:"enable_logger" json:"enable_logger" toml:"enable_logger" default:"false"`
	LoggerConfig           struct {
		Logger        string `yaml:"logger" json:"logger" toml:"logger" default:"github.com/wfusion/gofusion/log/customlogger.gormLogger"`
		LogInstance   string `yaml:"log_instance" json:"log_instance" toml:"log_instance" default:"default"`
		LogLevel      string `yaml:"log_level" json:"log_level" toml:"log_level"`
		SlowThreshold string `yaml:"slow_threshold" json:"slow_threshold" toml:"slow_threshold"`
	} `yaml:"logger_config" json:"logger_config" toml:"logger_config"`
}

Conf nolint: revive // struct tag too long issue

type DB

type DB struct {
	*orm.DB
	Name string
	// contains filtered or unexported fields
}

func GetCtxGormDB

func GetCtxGormDB(ctx context.Context) *DB

func GetCtxGormDBByName

func GetCtxGormDBByName(ctx context.Context, name string) (db *DB)

func GetCtxGormDBByNameList added in v1.1.13

func GetCtxGormDBByNameList(ctx context.Context, nameList []string) (db *DB)

func Use

func Use(ctx context.Context, name string, opts ...utils.OptionExtender) *DB

type DalInterface

type DalInterface[T any, TS ~[]*T] interface {
	Query(ctx context.Context, query any, args ...any) (TS, error)
	QueryFirst(ctx context.Context, query any, args ...any) (*T, error)
	QueryLast(ctx context.Context, query any, args ...any) (*T, error)
	QueryInBatches(ctx context.Context, batchSize int, fc func(tx *DB, batch int, found TS) error, query any, args ...any) error
	Count(ctx context.Context, query any, args ...any) (int64, error)
	Pluck(ctx context.Context, column string, dest any, query any, args ...any) error
	Take(ctx context.Context, dest any, conds ...any) error
	InsertOne(ctx context.Context, mod *T, opts ...utils.OptionExtender) error
	InsertInBatches(ctx context.Context, modList TS, batchSize int, opts ...utils.OptionExtender) error
	Save(ctx context.Context, mod any, opts ...utils.OptionExtender) error
	Update(ctx context.Context, column string, value any, query any, args ...any) (int64, error)
	Updates(ctx context.Context, columns map[string]any, query any, args ...any) (int64, error)
	Delete(ctx context.Context, query any, args ...any) (int64, error)
	FirstOrCreate(ctx context.Context, mod *T, conds ...any) (int64, error)
	Transaction(ctx context.Context, fc func(tx context.Context) error, opts ...utils.OptionExtender) error
	ReadDB(ctx context.Context) *gorm.DB
	WriteDB(ctx context.Context) *gorm.DB
	SetCtxReadDB(src context.Context) (dst context.Context)
	SetCtxWriteDB(src context.Context) (dst context.Context)
	Model() *T
	ModelSlice() TS
	IgnoreErr(err error) error
	CanIgnore(err error) bool
	ShardingByValues(ctx context.Context, src []map[string]any) (dst map[string][]map[string]any, err error)
	ShardingIDGen(ctx context.Context) (id uint64, err error)
	ShardingIDListGen(ctx context.Context, amount int) (idList []uint64, err error)
	ShardingByModelList(ctx context.Context, src TS) (dst map[string]TS, err error)
}

DalInterface nolint: revive // interface issue

func NewDAL

func NewDAL[T any, TS ~[]*T](readDBName, writeDBName string, opts ...utils.OptionExtender) DalInterface[T, TS]

type Data

type Data struct {
	ID           uint64 `gorm:"column:id;primary_key;autoIncrement" json:"id"`
	CreateTimeMs int64  `gorm:"column:create_time;type:bigint;autoCreateTime:milli" json:"createTime"`
	ModifyTimeMs int64  `gorm:"column:modify_time;type:bigint;autoUpdateTime:milli" json:"modifyTime"`
}

func (*Data) Clone

func (d *Data) Clone() *Data

func (*Data) CreateTime

func (d *Data) CreateTime() time.Time

func (*Data) Equals

func (d *Data) Equals(o *Data) bool

func (*Data) ModifyTime

func (d *Data) ModifyTime() time.Time

type DataSoftDeleted

type DataSoftDeleted struct {
	ID           uint64               `gorm:"column:id;primary_key;autoIncrement" json:"id"`
	CreateTimeMs int64                `gorm:"column:create_time;type:bigint;autoCreateTime:milli" json:"createTime"`
	ModifyTimeMs int64                `gorm:"column:modify_time;type:bigint;autoUpdateTime:milli" json:"modifyTime"`
	DeleteTimeMs softdelete.Timestamp `gorm:"column:delete_time;type:bigint;index:,composite:soft_delete" json:"deleteTime"`
	Deleted      softdelete.Deleted   `gorm:"column:deleted;index:,composite:soft_delete;default:false" json:"deleted"`
}

DataSoftDeleted nolint: revive // struct tag too long issue

func (*DataSoftDeleted) Clone

func (d *DataSoftDeleted) Clone() *DataSoftDeleted

func (*DataSoftDeleted) CreateTime

func (d *DataSoftDeleted) CreateTime() time.Time

func (*DataSoftDeleted) DeleteTime

func (d *DataSoftDeleted) DeleteTime() *time.Time

func (*DataSoftDeleted) Equals

func (d *DataSoftDeleted) Equals(o *DataSoftDeleted) bool

func (*DataSoftDeleted) ModifyTime

func (d *DataSoftDeleted) ModifyTime() time.Time

type Instance

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

func (*Instance) GetProxy

func (d *Instance) GetProxy() *gorm.DB

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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