dbo

package module
v2.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: MIT Imports: 12 Imported by: 0

README

Database Operator

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

安装

go get -u github.com/nzai/dbo/v2

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 Count

func Count[T any](ctx context.Context, condition QueryCondition) (int64, error)

func CountTx

func CountTx[T any](ctx context.Context, db *DBContext, condition QueryCondition) (int64, error)

func Get

func Get[T any](ctx context.Context, id any) (value T, err error)

func GetTrans

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

GetTrans begin a transaction

func GetTransResult

func GetTransResult[T any](ctx context.Context, fn func(ctx context.Context, tx *DBContext) (T, error)) (T, error)

GetTransResult begin a transaction, get result of callback

func GetTx

func GetTx[T any](ctx context.Context, db *DBContext, id any) (T, error)

func Insert

func Insert[T any](ctx context.Context, value T) (int64, error)

func InsertInBatches

func InsertInBatches[T any](ctx context.Context, value []T, batchSize int) (int64, error)

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

func InsertInBatchesTx

func InsertInBatchesTx[T any](ctx context.Context, db *DBContext, value []T, batchSize int) (int64, error)

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

func InsertTx

func InsertTx[T any](ctx context.Context, db *DBContext, value T) (int64, error)

func Page

func Page[T any](ctx context.Context, condition QueryCondition) (int64, []T, error)

func PageTx

func PageTx[T any](ctx context.Context, db *DBContext, condition QueryCondition) (int64, []T, error)

func Query

func Query[T any](ctx context.Context, condition QueryCondition) ([]T, error)

func QueryMap added in v2.2.1

func QueryMap[T Entity](ctx context.Context, condition QueryCondition) (map[string]T, error)

func QueryTx

func QueryTx[T any](ctx context.Context, db *DBContext, condition QueryCondition) ([]T, error)

func ReplaceGlobal

func ReplaceGlobal(dbo *DBO)

ReplaceGlobal replace global dbo instance

func Save

func Save[T any](ctx context.Context, value T) error

func SaveTx

func SaveTx[T any](ctx context.Context, db *DBContext, value T) error

func Update

func Update[T any](ctx context.Context, value T) (int64, error)

func UpdateTx

func UpdateTx[T any](ctx context.Context, db *DBContext, value T) (int64, error)

func Var added in v2.2.1

func Var[T any](v T) *T

Types

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 Entity added in v2.2.1

type Entity interface {
	GetID() string
}

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 NullBool

type NullBool struct {
	Bool  bool
	Valid bool
}

type NullBools

type NullBools struct {
	Bools []bool
	Valid bool
}

type NullInt

type NullInt struct {
	Int   int
	Valid bool
}

type NullInt64

type NullInt64 struct {
	Int64 int64
	Valid bool
}

type NullInt64s

type NullInt64s struct {
	Int64s []int64
	Valid  bool
}

type NullInts

type NullInts struct {
	Ints  []int
	Valid bool
}

type NullString

type NullString struct {
	String string
	Valid  bool
}

type NullStrings

type NullStrings struct {
	Strings []string
	Valid   bool
}

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool
}

type NullTimes

type NullTimes struct {
	Times []time.Time
	Valid bool
}

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 OrderByCondition

type OrderByCondition interface {
	GetOrderBy() string
}

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 PagerCondition

type PagerCondition interface {
	GetPager() *Pager
}

type QueryCondition

type QueryCondition interface {
	GetConditions() ([]string, []any)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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