dbrepo

package
v0.0.0-...-cc9a93f Latest Latest
Warning

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

Go to latest
Published: May 27, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound          = errors.New("记录不存在")
	ErrISBNAlreadyExists = errors.New("isbn 已经存在")
	ErrUpdateFailed      = errors.New("更新失败")
	ErrDelFailed         = errors.New("删除失败")
)

Functions

func IsCustomDBError

func IsCustomDBError(err error) bool

func NewBookRepo

func NewBookRepo(db Queryable) *bookRepo

Types

type BookRepo

type BookRepo interface {
	Insert(b *models.Book) (uint, error)
	Get(id uint) (*models.Book, error)
	List(page Pager) ([]*models.Book, error)
	Update(id uint, book *models.Book) error
	Del(id uint) error
	BulkInsert(books []*models.Book) (int64, error)
	TestTx(id uint) error
}

type Pager

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

func NewPager

func NewPager(page int, size int) Pager

NewPager 创建一个分页器 page >= 1, <=10 size <= 100

func (*Pager) Limit

func (p *Pager) Limit() int

func (*Pager) Offset

func (p *Pager) Offset() int

type Queryable

type Queryable interface {
	Exec(query string, args ...any) (sql.Result, error)
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	Query(query string, args ...any) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRow(query string, args ...any) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
	Prepare(query string) (*sql.Stmt, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

Queryable 提取 sql.DB 和 sql.Tx 公共的方法当作一个接口

type Repository

type Repository struct {
	Book BookRepo
}

func NewRepository

func NewRepository(db Queryable) *Repository

NOTE: 采用这这种方式,是为了在执行事务上可以复用代码,更加灵活性 NewRepository 创建一个Repository仓库,使用 Queryable 接口,同时兼容 sql.DB 和 sql.Tx 接口

Jump to

Keyboard shortcuts

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