cDB

package module
v0.4.28 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

View Source
const (
	Context_GormDBTransaction = "gorm.db.transaction"
)
View Source
const (
	DatabaseConfigName = "DatabaseConf"
)

Variables

View Source
var Component = &DBComponent{}

Functions

func AddConnection added in v0.4.0

func AddConnection(ctx *gin.Context, name string, connection drivers.DriverInterface) error

func Client added in v0.4.6

func Client[T DBClient](ctx *gin.Context, name string) T

func StartTransaction added in v0.4.19

func StartTransaction(ctx *gin.Context, name string, f func(ctx *gin.Context, tx *gorm.DB) error) error

func StartTransactionWithModel added in v0.4.27

func StartTransactionWithModel(ctx *gin.Context, model ModelInterface, f func(ctx *gin.Context, tx *gorm.DB) error) error

func WithContextModel added in v0.0.52

func WithContextModel(ctx *gin.Context, model ModelInterface) *gorm.DB

WithContextModel 仅用于 gorm,使用model实体进行查询

func WithContextTable added in v0.4.16

func WithContextTable(ctx *gin.Context, model ModelInterface) *gorm.DB

WithContextTable 仅用于 gorm,使用table进行查询

Types

type DBClient added in v0.4.8

type DBClient interface {
	*gorm.DB | *redis.Client | *mgo.Database
}

type DBComponent added in v0.3.2

type DBComponent struct{}

func (*DBComponent) Inject added in v0.3.2

func (i *DBComponent) Inject(instance any) bool

func (*DBComponent) InjectConf added in v0.4.0

func (i *DBComponent) InjectConf(config cComponents.ConfigInterface) bool

func (*DBComponent) Listen added in v0.4.0

func (i *DBComponent) Listen() []*cComponents.ConfigListener

func (*DBComponent) Load added in v0.3.2

func (i *DBComponent) Load()

type DBContainer added in v0.4.0

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

func (*DBContainer) Get added in v0.4.0

func (i *DBContainer) Get(name string) any

func (*DBContainer) GetConnector added in v0.4.0

func (i *DBContainer) GetConnector(name string) drivers.ConnectorInterface

func (*DBContainer) Is added in v0.4.0

func (i *DBContainer) Is(instance any) bool

func (*DBContainer) Range added in v0.4.0

func (i *DBContainer) Range(f func(instance any))

func (*DBContainer) Remove added in v0.4.0

func (i *DBContainer) Remove(name string) bool

func (*DBContainer) Save added in v0.4.0

func (i *DBContainer) Save(instance any) bool

type DatabaseConf

type DatabaseConf struct {
	Connections map[string]drivers.DriverInterface `json:"connections"`
}

func (*DatabaseConf) ConfigName

func (i *DatabaseConf) ConfigName() string

func (*DatabaseConf) Decode added in v0.4.8

func (i *DatabaseConf) Decode(data string) error

type GormBuilder added in v0.4.28

type GormBuilder struct {
	Conditions map[string]any
	Page       int64
	Size       int64
	OrderBy    string
	GroupBy    string
}

type Model added in v0.4.20

type Model[T ModelInterface] struct{}

func (Model[T]) CountByConditions added in v0.4.20

func (i Model[T]) CountByConditions(ctx *gin.Context, conditions map[string]any) (total int64, err error)

CountByConditions 根据条件统计数量

func (Model[T]) Create added in v0.4.20

func (i Model[T]) Create(ctx *gin.Context, data T) (err error)

Create 添加单条数据

func (Model[T]) CreateOrIgnore added in v0.4.21

func (i Model[T]) CreateOrIgnore(ctx *gin.Context, data T, fields []string) (err error)

CreateOrIgnore 新建单条数据或忽略,必须要有唯一索引才生效

func (Model[T]) DeleteByConditions added in v0.4.20

func (i Model[T]) DeleteByConditions(ctx *gin.Context, conditions map[string]any) (err error)

DeleteByConditions 根据条件删除数据

func (Model[T]) FindByConditions added in v0.4.20

func (i Model[T]) FindByConditions(ctx *gin.Context, conditions map[string]any) (result T, err error)

FindByConditions 根据条件查询单条数据

func (Model[T]) FirstOrCreate added in v0.4.20

func (i Model[T]) FirstOrCreate(ctx *gin.Context, data T, conditions map[string]any) (created int64, err error)

FirstOrCreate 查找或新增单条数据

func (Model[T]) GetByBuilder added in v0.4.28

func (i Model[T]) GetByBuilder(ctx *gin.Context, builder GormBuilder) (result []T, err error)

GetByConditions 根据条件查询多条数据,条件不存在则查询所有数据

func (Model[T]) GetByConditions added in v0.4.20

func (i Model[T]) GetByConditions(ctx *gin.Context, conditions map[string]any) (result []T, err error)

GetByConditions 根据条件查询多条数据,条件不存在则查询所有数据

func (Model[T]) Insert added in v0.4.20

func (i Model[T]) Insert(ctx *gin.Context, data []T) (err error)

Insert 批量添加数据

func (Model[T]) InsertOrIgnore added in v0.4.21

func (i Model[T]) InsertOrIgnore(ctx *gin.Context, data []T, fields []string) (err error)

InsertOrIgnore 批量添加数据,存在则忽略

func (Model[T]) InsertOrUpdate added in v0.4.21

func (i Model[T]) InsertOrUpdate(ctx *gin.Context, data []T, fields []string) (err error)

InsertOrUpdate 批量添加数据,存在则更新

func (Model[T]) ListByBuilder added in v0.4.28

func (i Model[T]) ListByBuilder(ctx *gin.Context, builder GormBuilder) (result []T, err error)

ListByBuilder 根据构造器查询

func (Model[T]) ListByConditions added in v0.4.20

func (i Model[T]) ListByConditions(ctx *gin.Context, conditions map[string]any, page, size int64, order string) (result []T, err error)

ListByConditions 根据条件分页查询

func (Model[T]) Save added in v0.4.20

func (i Model[T]) Save(ctx *gin.Context, data T) (err error)

Save 根据主键更新或新增数据

func (Model[T]) StartTransaction added in v0.4.27

func (i Model[T]) StartTransaction(ctx *gin.Context, f func(ctx *gin.Context, tx *gorm.DB) (err error)) (err error)

func (Model[T]) UpdateByConditions added in v0.4.20

func (i Model[T]) UpdateByConditions(ctx *gin.Context, conditions map[string]any, fields map[string]any) (err error)

UpdateByConditions 根据条件更新数据

func (Model[T]) UpdateOrCreate added in v0.4.20

func (i Model[T]) UpdateOrCreate(ctx *gin.Context, data T, fields []string) (err error)

UpdateOrCreate 更新或新建单条数据,必须要有唯一索引才生效

type ModelInterface

type ModelInterface interface {
	Connection(ctx *gin.Context) string
	TableName() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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