dbo

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2022 License: MIT Imports: 12 Imported by: 2

README

Database Operator

Database Operator 简称 dbo,是方便操作数据库的 helper

安装

go get -u github.com/nzai/dbo

使用

详见 示例测试

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRecordNotFound record not found
	ErrRecordNotFound = errors.New("record not found")
	// ErrDuplicateRecord duplicate record
	ErrDuplicateRecord = errors.New("duplicate record")
	// ErrExceededLimit exceeded limit
	ErrExceededLimit = errors.New("exceeded limit")
)
View Source
var NoPager = Pager{Page: 0, PageSize: 0}

NoPager do not paging

Functions

func GetTrans

func GetTrans(ctx context.Context, fn func(ctx context.Context, tx *DBContext) error) error

GetTrans begin a transaction

func GetTransResult

func GetTransResult(ctx context.Context, fn func(ctx context.Context, tx *DBContext) (interface{}, error)) (interface{}, error)

GetTransResult begin a transaction, get result of callback

func ReplaceGlobal

func ReplaceGlobal(dbo *DBO)

ReplaceGlobal replace global dbo instance

Types

type BaseDA

type BaseDA struct{}

func (BaseDA) Count

func (s BaseDA) Count(ctx context.Context, condition Conditions, values interface{}) (int, error)

func (BaseDA) CountTx

func (s BaseDA) CountTx(ctx context.Context, db *DBContext, condition Conditions, value interface{}) (int, error)

func (BaseDA) Get

func (s BaseDA) Get(ctx context.Context, id interface{}, value interface{}) error

func (BaseDA) GetTx

func (s BaseDA) GetTx(ctx context.Context, db *DBContext, id interface{}, value interface{}) error

func (BaseDA) Insert

func (s BaseDA) Insert(ctx context.Context, value interface{}) (interface{}, error)

func (BaseDA) InsertInBatches

func (s BaseDA) InsertInBatches(ctx context.Context, value interface{}, batchSize int) (interface{}, error)

InsertInBatches Insert records in batch. visit https://gorm.io/docs/create.html for detail

func (BaseDA) InsertInBatchesTx

func (s BaseDA) InsertInBatchesTx(ctx context.Context, db *DBContext, value interface{}, batchSize int) (interface{}, error)

InsertInBatchesTx Insert records in batch with context. visit https://gorm.io/docs/create.html for detail

func (BaseDA) InsertTx

func (s BaseDA) InsertTx(ctx context.Context, db *DBContext, value interface{}) (interface{}, error)

func (BaseDA) Page

func (s BaseDA) Page(ctx context.Context, condition Conditions, values interface{}) (int, error)

func (BaseDA) PageTx

func (s BaseDA) PageTx(ctx context.Context, db *DBContext, condition Conditions, values interface{}) (int, error)

func (BaseDA) Query

func (s BaseDA) Query(ctx context.Context, condition Conditions, values interface{}) error

func (BaseDA) QueryRawSQL

func (s BaseDA) QueryRawSQL(ctx context.Context, values interface{}, sql string, parameters ...interface{}) error

func (BaseDA) QueryRawSQLTx

func (s BaseDA) QueryRawSQLTx(ctx context.Context, db *DBContext, values interface{}, sql string, parameters ...interface{}) error

func (BaseDA) QueryTx

func (s BaseDA) QueryTx(ctx context.Context, db *DBContext, condition Conditions, values interface{}) error

func (BaseDA) Save

func (s BaseDA) Save(ctx context.Context, value interface{}) error

func (BaseDA) SaveTx

func (s BaseDA) SaveTx(ctx context.Context, db *DBContext, value interface{}) error

func (BaseDA) Update

func (s BaseDA) Update(ctx context.Context, value interface{}) (int64, error)

func (BaseDA) UpdateTx

func (s BaseDA) UpdateTx(ctx context.Context, db *DBContext, value interface{}) (int64, error)

type Conditions

type Conditions interface {
	GetConditions() ([]string, []interface{})
	GetPager() *Pager
	GetOrderBy() string
}

type Config

type Config struct {
	ConnectionString   string
	MaxOpenConns       int
	MaxIdleConns       int
	ConnMaxLifetime    time.Duration
	ConnMaxIdleTime    time.Duration
	DBType             DBType
	TransactionTimeout time.Duration
	LogLevel           LogLevel
	SlowThreshold      time.Duration
}

Config dbo config

type DBContext

type DBContext struct {
	*gorm.DB
}

DBContext db with context

func GetDB

func GetDB(ctx context.Context) (*DBContext, error)

GetDB get db context

func MustGetDB

func MustGetDB(ctx context.Context) *DBContext

MustGetDB get db context otherwise panic

func (*DBContext) GetTableName

func (s *DBContext) GetTableName(value interface{}) string

GetTableName get database table name of value

func (*DBContext) Printf

func (s *DBContext) Printf(format string, v ...interface{})

Print print sql log

func (*DBContext) ResetCondition

func (s *DBContext) ResetCondition() *DBContext

ResetCondition reset session query conditions

type DBO

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

DBO database operator

func GetGlobal

func GetGlobal() (*DBO, error)

GetGlobal get global dbo

func New

func New(options ...Option) (*DBO, error)

New create new database operator

func NewWithConfig

func NewWithConfig(options ...Option) (*DBO, error)

NewWithConfig create new database operator

func (DBO) GetDB

func (s DBO) GetDB(ctx context.Context) *DBContext

type DBType

type DBType string
const (
	MySQL DBType = "mysql"
)

func (DBType) DriverName

func (t DBType) DriverName() string

func (DBType) String

func (t DBType) String() string

type DataAccesser

type DataAccesser interface {
	Inserter
	Updater
	Saver
	Geter
	Querier
}

DataAccesser data access contract

type Geter

type Geter interface {
	Get(context.Context, interface{}, interface{}) error
	GetTx(context.Context, *DBContext, interface{}, interface{}) error
}

type Inserter

type Inserter interface {
	Insert(context.Context, interface{}) (interface{}, error)
	InsertTx(context.Context, *DBContext, interface{}) (interface{}, error)
	InsertInBatches(context.Context, interface{}, int) (interface{}, error)
	InsertInBatchesTx(context.Context, *DBContext, interface{}, int) (interface{}, error)
}

type LogLevel

type LogLevel string
const (
	Silent LogLevel = "Silent"
	Error  LogLevel = "Error"
	Warn   LogLevel = "Warn"
	Info   LogLevel = "Info"
)

func (LogLevel) GormLogLevel

func (l LogLevel) GormLogLevel() logger.LogLevel

func (LogLevel) String

func (l LogLevel) String() string

type NullInts

type NullInts struct {
	Ints  []int
	Valid bool
}

NullInts nullable ints

func (NullInts) SQLPlaceHolder

func (s NullInts) SQLPlaceHolder() string

SQLPlaceHolder generate sql place holder

func (NullInts) ToInterfaceSlice

func (s NullInts) ToInterfaceSlice() []interface{}

ToInterfaceSlice values to interface() slice

type NullStrings

type NullStrings struct {
	Strings []string
	Valid   bool
}

NullStrings nullable strings

func (NullStrings) SQLPlaceHolder

func (s NullStrings) SQLPlaceHolder() string

SQLPlaceHolder generate sql place holder

func (NullStrings) ToInterfaceSlice

func (s NullStrings) ToInterfaceSlice() []interface{}

ToInterfaceSlice values to interface() slice

type Option

type Option func(*Config)

Option dbo option

func WithConnectionString

func WithConnectionString(connectionString string) Option

func WithDBType

func WithDBType(dbType DBType) Option

func WithLogLevel

func WithLogLevel(logLevel LogLevel) Option

func WithMaxIdleConns

func WithMaxIdleConns(maxIdleConns int) Option

func WithMaxOpenConns

func WithMaxOpenConns(maxOpenConns int) Option

func WithTransactionTimeout

func WithTransactionTimeout(timeout time.Duration) Option

type Pager

type Pager struct {
	Page     int
	PageSize int
}

Pager paging setting

func (Pager) Enable

func (p Pager) Enable() bool

Enable enable paging if page and pageSize is not zero

func (Pager) Offset

func (p Pager) Offset() (int, int)

Offset pager to offset

func (Pager) Range

func (p Pager) Range() (int, int)

Range pager to [start, end)

type Querier

type Querier interface {
	Query(context.Context, Conditions, interface{}) error
	QueryTx(context.Context, *DBContext, Conditions, interface{}) error
	Count(context.Context, Conditions, interface{}) (int, error)
	CountTx(context.Context, *DBContext, Conditions, interface{}) (int, error)
	Page(context.Context, Conditions, interface{}) (int, error)
	PageTx(context.Context, *DBContext, Conditions, interface{}) (int, error)
	QueryRawSQL(context.Context, interface{}, string, ...interface{}) error
	QueryRawSQLTx(context.Context, *DBContext, interface{}, string, ...interface{}) error
}

type Saver

type Saver interface {
	Save(context.Context, interface{}) error
	SaveTx(context.Context, *DBContext, interface{}) error
}

type Updater

type Updater interface {
	Update(context.Context, interface{}) (int64, error)
	UpdateTx(context.Context, *DBContext, interface{}) (int64, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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