db

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2022 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const NanosecondPerMillisecond = 1e6

Variables

View Source
var (
	NotFound           = gorm.ErrRecordNotFound
	ErrNotRowsAffected = errors.New("0 rows affected")
	ErrDuplicate       = "1062: Duplicate"
)

Functions

func ClientClose added in v1.2.4

func ClientClose(db *DB)

func Dsn added in v1.2.4

func Dsn(opt *Option) string

func Init added in v1.2.4

func Init(ctx context.Context, opts ...func(*Option)) (err error)

Init init database

func Mock added in v1.2.4

func Mock() (sqlmock.Sqlmock, error)

Mock new a mock 解除测试对数据库等中间件的依赖

func NewLog added in v1.1.5

func NewLog(l *zap.Logger, cfg glogger.Config) glogger.Interface

Types

type Base

type Base struct {
	SnowID
	Time
}

type BuilderOption added in v1.2.6

type BuilderOption func(*builderOption)

type DB

type DB struct {
	*gorm.DB
	// contains filtered or unexported fields
}

func New added in v1.2.4

func New(ctx context.Context, opts ...func(*Option)) (*DB, error)

New with context.Context returns DB

func With added in v1.2.4

func With(ctx context.Context, opts ...func(*SessionOption)) *DB

With call db

func (*DB) Close

func (d *DB) Close() error

Close impl io.Closer to defer close db pool

func (*DB) With added in v1.1.5

func (d *DB) With(ctx context.Context, opts ...func(*SessionOption)) *DB

With options to set orm logger

type Deleted

type Deleted uint64

func (Deleted) DeleteClauses

func (d Deleted) DeleteClauses(f *schema.Field) []clause.Interface

func (Deleted) QueryClauses

func (d Deleted) QueryClauses(f *schema.Field) []clause.Interface

func (Deleted) UpdateClauses

func (d Deleted) UpdateClauses(field *schema.Field) []clause.Interface

type DeletedAt

type DeletedAt sql.NullTime

func (DeletedAt) DeleteClauses

func (DeletedAt) DeleteClauses(f *schema.Field) []clause.Interface

func (DeletedAt) MarshalJSON

func (n DeletedAt) MarshalJSON() ([]byte, error)

func (DeletedAt) QueryClauses

func (DeletedAt) QueryClauses(f *schema.Field) []clause.Interface

func (*DeletedAt) Scan

func (n *DeletedAt) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*DeletedAt) UnmarshalJSON

func (n *DeletedAt) UnmarshalJSON(b []byte) error

func (DeletedAt) Value

func (n DeletedAt) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Log added in v1.1.5

type Log struct {
	glogger.Config
	// contains filtered or unexported fields
}

func (*Log) Error added in v1.1.5

func (l *Log) Error(_ context.Context, msg string, data ...interface{})

func (*Log) Info added in v1.1.5

func (l *Log) Info(_ context.Context, msg string, data ...interface{})

func (*Log) LogMode added in v1.1.5

func (l *Log) LogMode(level glogger.LogLevel) glogger.Interface

func (*Log) Trace added in v1.1.5

func (l *Log) Trace(_ context.Context, begin time.Time, fc func() (string, int64), err error)

func (*Log) Warn added in v1.1.5

func (l *Log) Warn(_ context.Context, msg string, data ...interface{})

type Option

type Option struct {
	Debug bool

	MaxOpenConn int
	MaxIdleConn int

	User     string
	Password string
	IP       string
	Port     string
	Database string
	Charset  string

	Timeout         time.Duration
	ReadTimeout     time.Duration
	WriteTimeout    time.Duration
	ConnMaxLifetime time.Duration
}

type Pagination added in v1.2.2

type Pagination struct {
	Page   int   `form:"page" json:"page" binding:"omitempty,min=0"`
	Size   int   `form:"size" json:"size" binding:"omitempty,min=-1"`
	Offset int   `json:"-" form:"offset"`
	Limit  int   `json:"-" form:"limit"`
	Total  int64 `json:"total"`
}

Pagination 分页

func (*Pagination) Build added in v1.2.2

func (p *Pagination) Build(_ context.Context, query *gorm.DB, _ ...BuilderOption) *gorm.DB

type Primary added in v1.2.2

type Primary struct {
	Sort
	Pagination
}

func (*Primary) Build added in v1.2.2

func (p *Primary) Build(ctx context.Context, query *gorm.DB, opts ...BuilderOption) *gorm.DB

type SQLBuilder added in v1.2.6

type SQLBuilder interface {
	Build(ctx context.Context, query *gorm.DB, opts ...BuilderOption) *gorm.DB
}

SQLBuilder 将参数组装成 gorm.DB 即预处理的sql语句

type SessionOption added in v1.2.1

type SessionOption struct {
	SlowThreshold time.Duration
	Colorful      bool
	LevelFunc     func(glogger.LogLevel, bool) glogger.LogLevel
}

type SnowID

type SnowID struct{}

func (SnowID) BeforeCreate

func (SnowID) BeforeCreate(tx *gorm.DB) error

type SoftDeleteDeleteClause

type SoftDeleteDeleteClause struct {
	Field *schema.Field
}

func (SoftDeleteDeleteClause) Build

func (SoftDeleteDeleteClause) MergeClause

func (sd SoftDeleteDeleteClause) MergeClause(*clause.Clause)

func (SoftDeleteDeleteClause) ModifyStatement

func (sd SoftDeleteDeleteClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeleteDeleteClause) Name

func (sd SoftDeleteDeleteClause) Name() string

type SoftDeleteDeletedClause

type SoftDeleteDeletedClause struct {
	Field *schema.Field
}

func (SoftDeleteDeletedClause) Build

func (SoftDeleteDeletedClause) MergeClause

func (s SoftDeleteDeletedClause) MergeClause(*clause.Clause)

func (SoftDeleteDeletedClause) ModifyStatement

func (s SoftDeleteDeletedClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeleteDeletedClause) Name

type SoftDeleteQueryClause

type SoftDeleteQueryClause struct {
	Field *schema.Field
}

func (SoftDeleteQueryClause) Build

func (SoftDeleteQueryClause) MergeClause

func (sd SoftDeleteQueryClause) MergeClause(*clause.Clause)

func (SoftDeleteQueryClause) ModifyStatement

func (sd SoftDeleteQueryClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeleteQueryClause) Name

func (sd SoftDeleteQueryClause) Name() string

type SoftDeletedQueryClause

type SoftDeletedQueryClause struct {
	Field *schema.Field
}

func (SoftDeletedQueryClause) Build

func (SoftDeletedQueryClause) MergeClause

func (s SoftDeletedQueryClause) MergeClause(*clause.Clause)

func (SoftDeletedQueryClause) ModifyStatement

func (s SoftDeletedQueryClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeletedQueryClause) Name

func (s SoftDeletedQueryClause) Name() string

type SoftDeletedUpdateClause

type SoftDeletedUpdateClause struct {
	Field *schema.Field
}

func (SoftDeletedUpdateClause) Build

func (SoftDeletedUpdateClause) MergeClause

func (s SoftDeletedUpdateClause) MergeClause(*clause.Clause)

func (SoftDeletedUpdateClause) ModifyStatement

func (s SoftDeletedUpdateClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeletedUpdateClause) Name

type Sort added in v1.2.2

type Sort struct {
	// 给多个字段排序 created_at, id asc => order by created_at desc, id asc
	SortField string `form:"sort" json:"sort" binding:"omitempty,order"`
}

Sort 排序

func (*Sort) Build added in v1.2.2

func (s *Sort) Build(_ context.Context, query *gorm.DB, _ ...BuilderOption) *gorm.DB

type Time added in v1.1.5

type Time struct {
	CreatedAt time.Time `json:"created_at" gorm:"column:created_at;not null;default:current_timestamp();comment:创建时间"`
	UpdatedAt time.Time `` /* 130-byte string literal not displayed */
	DeletedAt DeletedAt `json:"-" gorm:"column:deleted_at;index;comment:删除时间"`
}

Jump to

Keyboard shortcuts

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