Documentation
¶
Index ¶
- Variables
- func Delete(ctx context.Context, ent Entity, db DB) error
- func DeleteCache(ctx context.Context, ent Cacheable) error
- func ExecDelete(ctx context.Context, db DB, stmt *goqu.DeleteDataset) (sql.Result, error)
- func ExecInsert(ctx context.Context, db DB, stmt *goqu.InsertDataset) (sql.Result, error)
- func ExecUpdate(ctx context.Context, db DB, stmt *goqu.UpdateDataset) (sql.Result, error)
- func GetRecord(ctx context.Context, dest interface{}, db DB, stmt *goqu.SelectDataset) error
- func GetRecords(ctx context.Context, dest interface{}, db DB, stmt *goqu.SelectDataset) error
- func GetTotalCount(ctx context.Context, db DB, stmt *goqu.SelectDataset) (int, error)
- func Insert(ctx context.Context, ent Entity, db DB) (int64, error)
- func IsNotFound(err error) bool
- func Load(ctx context.Context, ent Entity, db DB) error
- func QueryBy(ctx context.Context, db DB, stmt *goqu.SelectDataset, ...) error
- func SaveCache(ctx context.Context, ent Cacheable) error
- func Transaction(db *sqlx.DB, fn func(tx *sqlx.Tx) error) (err error)
- func TryTransaction(db DB, fn func(DB) error) error
- func Update(ctx context.Context, ent Entity, db DB) error
- type AfterDeleteHook
- type AfterInsertHook
- type AfterUpdateHook
- type BeforeDeleteHook
- type BeforeInsertHook
- type BeforeUpdateHook
- type CacheOption
- type Cacheable
- type Cacher
- type Column
- type DB
- type Entity
- type Event
- type EventHook
- type Factory
- type Metadata
- type Pagination
- type PrepareInsertStatement
- type PrepareUpdateStatement
- type Repository
- func (repo *Repository[ID, E]) Create(ctx context.Context, ent E) error
- func (repo *Repository[ID, E]) Delete(ctx context.Context, ent E) error
- func (repo *Repository[ID, E]) Find(ctx context.Context, id ID) (E, error)
- func (repo *Repository[ID, E]) ForEach(ctx context.Context, stmt *goqu.SelectDataset, ...) error
- func (repo *Repository[ID, E]) NewEntity(id ID) E
- func (repo *Repository[ID, E]) PageQuery(ctx context.Context, stmt *goqu.SelectDataset, currentPage, pageSize int) (items []E, page Pagination, err error)
- func (repo *Repository[ID, E]) Save(ctx context.Context, ent E) error
- func (repo *Repository[ID, E]) Update(ctx context.Context, id ID, apply func(ent E) (bool, error)) error
- func (repo *Repository[ID, E]) UpdateByQuery(ctx context.Context, stmt *goqu.SelectDataset, apply func(ent E) (bool, error)) error
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func DeleteCache ¶
DeleteCache 删除entity缓存
func ExecDelete ¶
ExecDelete 执行删除语句
func ExecInsert ¶ added in v0.2.3
ExecInsert 执行插入语句
func ExecUpdate ¶
ExecUpdate 执行更新语句
func GetRecords ¶ added in v0.2.2
GetRecords 执行查询语句,返回多条结果
func GetTotalCount ¶
GetTotalCount 符合条件的总记录数量
func IsNotFound ¶ added in v0.6.1
IsNotFound 判断是否是未找到错误
repository在没有找到记录时返回ErrNotFound错误 GetRecord()在没有找到记录时返回sql.ErrNoRows错误 使用这个方法来统一处理错误判断
func QueryBy ¶ added in v0.5.1
func QueryBy(ctx context.Context, db DB, stmt *goqu.SelectDataset, fn func(ctx context.Context, rows *sqlx.Rows) error) error
QueryBy 查询并使用回调函数处理游标
func Transaction ¶
Transaction 执行事务过程,根据结果选择提交或回滚
func TryTransaction ¶ added in v0.5.1
TryTransaction 尝试执行事务,如果DB不是*sqlx.DB,则直接执行fn
Types ¶
type AfterDeleteHook ¶ added in v0.5.2
AfterDeleteHook 在删除后调用
type AfterInsertHook ¶ added in v0.5.2
AfterInsertHook 在插入后调用
type AfterUpdateHook ¶ added in v0.5.2
AfterUpdateHook 在更新后调用
type BeforeDeleteHook ¶ added in v0.5.2
BeforeDeleteHook 在删除前调用
type BeforeInsertHook ¶ added in v0.5.2
BeforeInsertHook 在插入前调用
type BeforeUpdateHook ¶ added in v0.5.2
BeforeUpdateHook 在更新前调用
type CacheOption ¶
type CacheOption struct {
Cacher Cacher
Key string
Expiration time.Duration
Compress bool
// 如果为true,将不会生成缓存
// 这个配置只控制缓存的生成,不控制缓存的读取
// 因为在没有读到数据之前,没有足够的信息进行判断
Disable bool
// 某些由其它地方构造的缓存,其中存在字段内容进入缓存前先被json encode过
// 这些字段缓存结果需要被decode两次才能使用
RecursiveDecode []string
}
CacheOption 缓存参数
type Cacher ¶
type Cacher interface {
Get(ctx context.Context, key string) ([]byte, error)
Put(ctx context.Context, key string, data []byte, expiration time.Duration) error
Delete(ctx context.Context, key string) error
}
Cacher 缓存数据存储接口
var DefaultCacher Cacher
DefaultCacher 默认缓存存储
type Column ¶
type Column struct {
StructField string
DBField string
PrimaryKey bool
AutoIncrement bool
RefuseUpdate bool
ReturningInsert bool
ReturningUpdate bool
}
Column 字段信息
type DB ¶
type DB interface {
sqlx.Queryer
sqlx.QueryerContext
sqlx.Execer
sqlx.ExecerContext
sqlx.Preparer
sqlx.PreparerContext
Get(dest interface{}, query string, args ...interface{}) error
GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
Select(dest interface{}, query string, args ...interface{}) error
SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
NamedExec(query string, arg interface{}) (sql.Result, error)
NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)
NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)
PrepareNamed(query string) (*sqlx.NamedStmt, error)
PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error)
Preparex(query string) (*sqlx.Stmt, error)
PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error)
DriverName() string
Rebind(string) string
BindNamed(string, interface{}) (string, []interface{}, error)
}
DB 数据库接口 sqlx.DB 和 sqlx.Tx 公共方法
type Event ¶
type Event int
Event 存储事件
const ( // EventUnknown 未定义事件 EventUnknown Event = iota // EventBeforeInsert before insert entity EventBeforeInsert // EventAfterInsert after insert entity EventAfterInsert // EventBeforeUpdate before update entity EventBeforeUpdate // EventAfterUpdate after update entity EventAfterUpdate // EventBeforeDelete before delete entity EventBeforeDelete // EventAfterDelete after delete entity EventAfterDelete )
type Metadata ¶
type Metadata struct {
Type reflect.Type
TableName string
Columns []Column
PrimaryKeys []Column
// contains filtered or unexported fields
}
Metadata 元数据
type Pagination ¶ added in v0.3.4
type Pagination struct {
First int `json:"first"`
Last int `json:"last"`
Previous int `json:"previous"`
Current int `json:"current"`
Next int `json:"next"`
Size int `json:"size"`
Items int `json:"items"`
}
Pagination 数据库分页计算
func NewPagination ¶ added in v0.3.4
func NewPagination(current, size, items int) Pagination
NewPagination 计算分页页码
type PrepareInsertStatement ¶
type PrepareInsertStatement struct {
// contains filtered or unexported fields
}
PrepareInsertStatement is a prepared insert statement for entity
func PrepareInsert ¶
PrepareInsert returns a prepared insert statement for Entity
func (*PrepareInsertStatement) Close ¶
func (pis *PrepareInsertStatement) Close() error
Close closes the prepared statement
func (*PrepareInsertStatement) ExecContext ¶
func (pis *PrepareInsertStatement) ExecContext(ctx context.Context, ent Entity) (lastID int64, err error)
ExecContext executes a prepared insert statement using the Entity passed.
type PrepareUpdateStatement ¶
type PrepareUpdateStatement struct {
// contains filtered or unexported fields
}
PrepareUpdateStatement is a prepared update statement for entity
func PrepareUpdate ¶
PrepareUpdate returns a prepared update statement for Entity
func (*PrepareUpdateStatement) Close ¶
func (pus *PrepareUpdateStatement) Close() error
Close closes the prepared statement
func (*PrepareUpdateStatement) ExecContext ¶
func (pus *PrepareUpdateStatement) ExecContext(ctx context.Context, ent Entity) error
ExecContext executes a prepared update statement using the Entity passed.
type Repository ¶ added in v0.6.0
Repository 实体仓库
func NewRepository ¶ added in v0.6.0
func NewRepository[ID any, E Entity](db DB, factory Factory[ID, E]) *Repository[ID, E]
NewRepository 创建实体仓库
func (*Repository[ID, E]) Create ¶ added in v0.6.0
func (repo *Repository[ID, E]) Create(ctx context.Context, ent E) error
Create 保存新的实体
func (*Repository[ID, E]) Delete ¶ added in v0.6.0
func (repo *Repository[ID, E]) Delete(ctx context.Context, ent E) error
Delete 删除实体
func (*Repository[ID, E]) Find ¶ added in v0.6.0
func (repo *Repository[ID, E]) Find(ctx context.Context, id ID) (E, error)
Find 根据主键查询实体
func (*Repository[ID, E]) ForEach ¶ added in v0.6.0
func (repo *Repository[ID, E]) ForEach(ctx context.Context, stmt *goqu.SelectDataset, iteratee func(ent E) (bool, error)) error
ForEach 根据查询遍历实体,iteratee return false则停止遍历
func (*Repository[ID, E]) NewEntity ¶ added in v0.6.1
func (repo *Repository[ID, E]) NewEntity(id ID) E
NewEntity 创建实体对象
func (*Repository[ID, E]) PageQuery ¶ added in v0.6.0
func (repo *Repository[ID, E]) PageQuery(ctx context.Context, stmt *goqu.SelectDataset, currentPage, pageSize int) (items []E, page Pagination, err error)
PageQuery 分页查询
func (*Repository[ID, E]) Save ¶ added in v0.6.0
func (repo *Repository[ID, E]) Save(ctx context.Context, ent E) error
Save 更新实体
func (*Repository[ID, E]) Update ¶ added in v0.6.0
func (repo *Repository[ID, E]) Update(ctx context.Context, id ID, apply func(ent E) (bool, error)) error
Update 根据ID查询实体并执行更新函数,apply return false则不保存
func (*Repository[ID, E]) UpdateByQuery ¶ added in v0.6.0
func (repo *Repository[ID, E]) UpdateByQuery(ctx context.Context, stmt *goqu.SelectDataset, apply func(ent E) (bool, error)) error
UpdateByQuery 查询并更新,apply return false则放弃那一条的更新