Documentation
¶
Index ¶
- Constants
- Variables
- func GormColumn(record interface{}) (string, error)
- func NewBadGorm(err error) *gorm.DB
- func ParsePrimaryKey(in any) (string, bool)
- type Accessor
- type DbClient
- func (c *DbClient) Count(count *int64) Accessor
- func (c *DbClient) DB() *gorm.DB
- func (c *DbClient) Delete(records any) error
- func (c *DbClient) DeleteByPrimaryKeys(dest any, keys any) error
- func (c *DbClient) DeleteExpired(filter any) error
- func (c *DbClient) FindUniq(filterAndDest any) error
- func (c *DbClient) First(dest any, conds ...any) error
- func (c *DbClient) FirstInJoinQuery(table, fields string, joins []string, dest any, conds ...any) error
- func (c *DbClient) FirstOrCreate(dest any, cond any) error
- func (c *DbClient) Get(filterAndDest any) error
- func (c *DbClient) GetArrayCondition(records any, whereConditions []WhereCondition, ...) (int64, error)
- func (c *DbClient) GetByPrimary(dest any, id any) error
- func (c *DbClient) GetCount(count *int64, filter any) error
- func (c *DbClient) InitCompleted()
- func (c *DbClient) IsInitCompleted() bool
- func (c *DbClient) Joins(query string, args ...any) Accessor
- func (c *DbClient) Last(dest any, conds ...any) error
- func (c *DbClient) Limit(count int) Accessor
- func (c *DbClient) List(dest any, cond any) error
- func (c *DbClient) ListByPrimaryKeys(dest any, keys any) error
- func (c *DbClient) ListWithKeyPrefix(dest any, filter any, keyFieldName, keyPrefix string) error
- func (c *DbClient) ListWithOneAttr(dest any, modelWithOneColumn any, attrValue any) error
- func (c *DbClient) Model(value any) Accessor
- func (c *DbClient) Offset(offset int) Accessor
- func (c *DbClient) Order(value any) Accessor
- func (c *DbClient) RawCmd(sql string) ([]map[string]any, error)
- func (c *DbClient) ReActivate(in any) error
- func (c *DbClient) Save(records any) error
- func (c *DbClient) Select(query any, args ...any) Accessor
- func (c *DbClient) Set(input any) error
- func (c *DbClient) Table(name string, args ...any) Accessor
- func (c *DbClient) Update(dest any, column string, value any) error
- func (c *DbClient) Updates(dest any, values any, conds ...any) error
- func (c *DbClient) UpdatesOmitZero(cond any, newAttrs any) error
- func (c *DbClient) UpdatesWithZero(cond any, newAttrs any) error
- func (c *DbClient) Where(query any, args ...any) *DbClient
- type LogConfig
- type LogContent
- type MultipleDb
- func (d *MultipleDb) Add(id uint64, conf SqlConfig) error
- func (d *MultipleDb) AddTestDbClient(id uint64, db *gorm.DB) error
- func (d *MultipleDb) AllDb() map[uint64]*DbClient
- func (d *MultipleDb) AllReadyDb() map[uint64]*DbClient
- func (d *MultipleDb) Db() map[uint64]*DbClient
- func (d *MultipleDb) DbById(id uint64) *DbClient
- func (d *MultipleDb) ExtDb() *MultipleDb
- func (d *MultipleDb) Remove(id uint64)
- func (d *MultipleDb) SelectExtDbByPath(path string) *MultipleDb
- func (d *MultipleDb) SelectExtDbByPathWhenNotFoundUseInnerDb(path string) *MultipleDb
- func (d *MultipleDb) SelectExtDbByPathWithInnerDb(path string) *MultipleDb
- func (d *MultipleDb) Walk(f func(uint64, *DbClient) error) error
- func (d *MultipleDb) WalkAllDb(f func(uint64, *DbClient) error) error
- func (d *MultipleDb) WalkExtDb(f func(uint64, *DbClient) error) error
- func (d *MultipleDb) WalkRawDb_DO_NOT_USE(f func(uint64, *gorm.DB) error) error
- type OrCondition
- type OrderCondition
- type SqlConfig
- type SqlStructIf
- type UpgradeTable
- type WhereCondition
Constants ¶
const ( ConstDbTypePsql string = "psql" ConstDbTypeMysql string = "mysql" ConstDbTypeSqlLite string = "sqllite" )
Variables ¶
var ErrStopWalk = errors.New("stop walk")
Functions ¶
func GormColumn ¶
func NewBadGorm ¶
func ParsePrimaryKey ¶
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 NewDbClient ¶
func NewTestDbClient ¶
func (*DbClient) DeleteByPrimaryKeys ¶
func (*DbClient) DeleteExpired ¶
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) 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) InitCompleted ¶
func (c *DbClient) InitCompleted()
func (*DbClient) IsInitCompleted ¶
func (*DbClient) ListWithKeyPrefix ¶
func (*DbClient) ListWithOneAttr ¶
获取filterWithAttr中的非零成员作为
func (*DbClient) ReActivate ¶
func (*DbClient) UpdatesOmitZero ¶
注意: 如果newAttrs为结构体,会忽略0值, 比如EnableSamba: false
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 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) 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 ¶
WalkRawDb_DO_NOT_USE go through all DBs and execute f
type OrCondition ¶
type OrCondition struct {
Wheres []WhereCondition
}
func (*OrCondition) Or ¶
func (o *OrCondition) Or(w WhereCondition)
func (*OrCondition) ToWhere ¶
func (o *OrCondition) ToWhere() *WhereCondition
type OrderCondition ¶
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"`
}
type SqlStructIf ¶
type SqlStructIf interface {
TableName() string
}
type UpgradeTable ¶
type WhereCondition ¶
func (*WhereCondition) IsOr ¶
func (w *WhereCondition) IsOr() bool
func (*WhereCondition) Valid ¶
func (w *WhereCondition) Valid() bool