dbc

package
v1.3.90 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ConstDbTypePsql    string = "psql"
	ConstDbTypeMysql   string = "mysql"
	ConstDbTypeSqlLite string = "sqllite"
)

Variables

View Source
var ErrStopWalk = errors.New("stop walk")

Functions

func GormColumn

func GormColumn(record interface{}) (string, error)

func NewBadGorm

func NewBadGorm(err error) *gorm.DB

func ParsePrimaryKey

func ParsePrimaryKey(in any) (string, bool)

Types

type Accessor

type Accessor interface {
	Count(count *int64) *gorm.DB
	Model(value any) *gorm.DB
	Joins(query string, args ...any) *gorm.DB
	Table(name string, args ...any) *gorm.DB
	Where(query any, args ...any) *gorm.DB
	Limit(limit int) *gorm.DB
	Offset(offset int) *gorm.DB
	Find(dest any, conds ...any) *gorm.DB
	Select(query any, args ...any) *gorm.DB
	Order(value any) *gorm.DB
	First(dest any, conds ...any) *gorm.DB
}

enable chainning operations

type DbClient

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

func NewBadDbclient

func NewBadDbclient(err error) *DbClient

func NewDbClient

func NewDbClient(pCtx context.Context, conf SqlConfig, tables ...any) (*DbClient, error)

func NewTestDbClient

func NewTestDbClient(db *gorm.DB) *DbClient

func (*DbClient) Count

func (c *DbClient) Count(count *int64) Accessor

func (*DbClient) DB

func (c *DbClient) DB() *gorm.DB

func (*DbClient) Delete

func (c *DbClient) Delete(records any) error

func (*DbClient) DeleteByPrimaryKeys

func (c *DbClient) DeleteByPrimaryKeys(dest any, keys any) error

func (*DbClient) DeleteExpired

func (c *DbClient) DeleteExpired(filter any) error

func (*DbClient) FindUniq

func (c *DbClient) FindUniq(filterAndDest any) error

WARMING: zero in filterAndDest is omited

func (*DbClient) First

func (c *DbClient) First(dest any, conds ...any) error

func (*DbClient) FirstInJoinQuery

func (c *DbClient) FirstInJoinQuery(table, fields string,
	joins []string, dest any, conds ...any) error

db.Table("users").Select("users.*, profiles.*") FirstInJoinQuery supports join ops like .Joins("LEFT JOIN profiles ON users.id = profiles.user_id") .Where("users.username = ?", "john") .Find(&usersWithProfiles) or .Take(&usersWithProfiles)

func (*DbClient) FirstOrCreate

func (c *DbClient) FirstOrCreate(dest any, cond any) error

func (*DbClient) Get

func (c *DbClient) Get(filterAndDest any) error

func (*DbClient) GetArrayCondition

func (c *DbClient) GetArrayCondition(records any,
	whereConditions []WhereCondition, orderConditions []OrderCondition,
	pageSize int, pageNum int,
) (int64, error)

GetArrayCondition retrieves a paginated list of records from the database and also returns the total count of records matching the conditions. The conditions parameter can be a string with arguments, or a map or struct used to build the WHERE clause. It returns the total record count and an error if the operation fails.

func (*DbClient) GetByPrimary

func (c *DbClient) GetByPrimary(dest any, id any) error

func (*DbClient) GetCount

func (c *DbClient) GetCount(count *int64, filter any) error

func (*DbClient) InitCompleted

func (c *DbClient) InitCompleted()

func (*DbClient) IsInitCompleted

func (c *DbClient) IsInitCompleted() bool

func (*DbClient) Joins

func (c *DbClient) Joins(query string, args ...any) Accessor

func (*DbClient) Last

func (c *DbClient) Last(dest any, conds ...any) error

func (*DbClient) Limit

func (c *DbClient) Limit(count int) Accessor

func (*DbClient) List

func (c *DbClient) List(dest any, cond any) error

func (*DbClient) ListByPrimaryKeys

func (c *DbClient) ListByPrimaryKeys(dest any, keys any) error

func (*DbClient) ListWithKeyPrefix

func (c *DbClient) ListWithKeyPrefix(dest any, filter any, keyFieldName, keyPrefix string) error

func (*DbClient) ListWithOneAttr

func (c *DbClient) ListWithOneAttr(dest any, modelWithOneColumn any, attrValue any) error

获取filterWithAttr中的非零成员作为

func (*DbClient) Model

func (c *DbClient) Model(value any) Accessor

func (*DbClient) Offset

func (c *DbClient) Offset(offset int) Accessor

func (*DbClient) Order

func (c *DbClient) Order(value any) Accessor

func (*DbClient) RawCmd

func (c *DbClient) RawCmd(sql string) ([]map[string]any, error)

func (*DbClient) ReActivate

func (c *DbClient) ReActivate(in any) error

func (*DbClient) Save

func (c *DbClient) Save(records any) error

func (*DbClient) Select

func (c *DbClient) Select(query any, args ...any) Accessor

func (*DbClient) Set

func (c *DbClient) Set(input any) error

func (*DbClient) Table

func (c *DbClient) Table(name string, args ...any) Accessor

func (*DbClient) Update

func (c *DbClient) Update(dest any, column string, value any) error

func (*DbClient) Updates

func (c *DbClient) Updates(dest any, values any, conds ...any) error

func (*DbClient) UpdatesOmitZero

func (c *DbClient) UpdatesOmitZero(cond any, newAttrs any) error

注意: 如果newAttrs为结构体,会忽略0值, 比如EnableSamba: false

func (*DbClient) UpdatesWithZero

func (c *DbClient) UpdatesWithZero(cond any, newAttrs any) error

func (*DbClient) Where

func (c *DbClient) Where(query any, args ...any) *DbClient

type LogConfig

type LogConfig struct {
	LogFile log.FileConfig
	Level   string `vx_default:"info"`
}

func (LogConfig) ToSqlLogLevel

func (lc LogConfig) ToSqlLogLevel() (gormlogger.LogLevel, error)

type LogContent

type LogContent struct {
	SlowThreshold             int64 `vx_default:"10000"` //in ns
	Colorful                  bool  `vx_default:"true"`
	IgnoreRecordNotFoundError bool  `vx_default:"false"`
	ParameterizedQueries      bool  `vx_default:"false"`
}

type MultipleDb

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

func NewMultipleDb

func NewMultipleDb(pctx context.Context, tables ...interface{}) *MultipleDb

func NewTestMultipleDbClient

func NewTestMultipleDbClient(db *gorm.DB) *MultipleDb

func (*MultipleDb) Add

func (d *MultipleDb) Add(id uint64, conf SqlConfig) error

func (*MultipleDb) AddTestDbClient

func (d *MultipleDb) AddTestDbClient(id uint64, db *gorm.DB) error

For test

func (*MultipleDb) AllDb

func (d *MultipleDb) AllDb() map[uint64]*DbClient

func (*MultipleDb) AllReadyDb

func (d *MultipleDb) AllReadyDb() map[uint64]*DbClient

AllReadyDb 只返回所有已经初始化完成的 db

func (*MultipleDb) Db

func (d *MultipleDb) Db() map[uint64]*DbClient

func (*MultipleDb) DbById

func (d *MultipleDb) DbById(id uint64) *DbClient

DbById 根据 id 来拿 db 在 scan 设备的时候,堆积了很多事件在 chan 里面,但此时将设备拔出,对应的 db 会删除 此时事件还在处理中,通过 id 只会拿到 nil 的 db 对象,会导致整个程序 panic 掉 解决方法: 1. 所有用到 DbById 的地方,先 check nil 2. DbById 函数要返回一个 error 3. DbById 返回一个带有错误的 gorm.DB 对象(选择这个,改动最小)

func (*MultipleDb) ExtDb

func (d *MultipleDb) ExtDb() *MultipleDb

func (*MultipleDb) Remove

func (d *MultipleDb) Remove(id uint64)

func (*MultipleDb) SelectExtDbByPath

func (d *MultipleDb) SelectExtDbByPath(path string) *MultipleDb

SelectExtDbByPath 根据路径选择 ext db

func (*MultipleDb) SelectExtDbByPathWhenNotFoundUseInnerDb

func (d *MultipleDb) SelectExtDbByPathWhenNotFoundUseInnerDb(path string) *MultipleDb

SelectExtDbByPathWhenNotFoundUseInnerDb 根据路径选择 ext db,如果 ext db 不存在,则使用内部 db

func (*MultipleDb) SelectExtDbByPathWithInnerDb

func (d *MultipleDb) SelectExtDbByPathWithInnerDb(path string) *MultipleDb

SelectExtDbByPathWithInnerDb 根据路径选择 ext db,并且附带内部磁盘的 db

func (*MultipleDb) Walk

func (d *MultipleDb) Walk(f func(uint64, *DbClient) error) error

Walk stop walking when f returns an error, if f returns ErrStopWalk, Walk will return nil, otherwise, it will return the error returned by f

func (*MultipleDb) WalkAllDb

func (d *MultipleDb) WalkAllDb(f func(uint64, *DbClient) error) error

func (*MultipleDb) WalkExtDb

func (d *MultipleDb) WalkExtDb(f func(uint64, *DbClient) error) error

func (*MultipleDb) WalkRawDb_DO_NOT_USE

func (d *MultipleDb) WalkRawDb_DO_NOT_USE(f func(uint64, *gorm.DB) error) error

WalkRawDb_DO_NOT_USE go through all DBs and execute f

type OrCondition

type OrCondition struct {
	Wheres []WhereCondition
}

func (*OrCondition) Apply

func (o *OrCondition) Apply(db *gorm.DB) *gorm.DB

func (*OrCondition) Or

func (o *OrCondition) Or(w WhereCondition)

func (*OrCondition) ToWhere

func (o *OrCondition) ToWhere() *WhereCondition

type OrderCondition

type OrderCondition struct {
	Field string
	Order string
}

func (*OrderCondition) ToString

func (o *OrderCondition) ToString() string

func (*OrderCondition) Valid

func (o *OrderCondition) Valid() bool

type SqlConfig

type SqlConfig struct {
	Type         string `vx_range:"oneof=psql mysql sqllite" vx_must:"true"`
	Host         string `json:"-"`
	Port         string `json:"-"`
	User         string `json:"-"`
	Password     string `json:"-"`
	Dbname       string `json:"-" vx_must:"true"`
	Log          LogConfig
	LogContent   LogContent
	MaxIdleConns int    `vx_default:"10"`
	MaxOpenConns int    `vx_default:"100"`
	ExtPrefix    string `vx_default:""`
	ExtDbDir     string `vx_default:"/app/workspace/file_server/ext_db"`
}

func (*SqlConfig) IsExt

func (c *SqlConfig) IsExt() bool

type SqlStructIf

type SqlStructIf interface {
	TableName() string
}

type UpgradeTable

type UpgradeTable interface {
	OldTableName() string
	Upgrade(*DbClient) error
}

type WhereCondition

type WhereCondition struct {
	Query any
	Args  any
}

func (*WhereCondition) IsOr

func (w *WhereCondition) IsOr() bool

func (*WhereCondition) Valid

func (w *WhereCondition) Valid() bool

Jump to

Keyboard shortcuts

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