bzgorm

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy() func(db *gorm.DB) *gorm.DB

Copy 拷贝会话

func Count

func Count[T any](db *gorm.DB, fn func(tx *gorm.DB) *gorm.DB) (int64, error)

func Create

func Create(value any, batchSize ...int) func(db *gorm.DB) (int64, error)

func DB

func DB(db *gorm.DB) (*sql.DB, error)

func DBClose

func DBClose(db *gorm.DB) error

func DBPing

func DBPing(db *gorm.DB) error

func DBStats

func DBStats(db *gorm.DB) (sql.DBStats, error)

func DefaultLogger

func DefaultLogger() func(conf *gorm.Config) *gorm.Config

func Delete

func Delete[T any](fn func(tx *gorm.DB) *gorm.DB) func(db *gorm.DB) (int64, error)

func DeleteAndReturn

func DeleteAndReturn[T any](fn func(tx *gorm.DB) *gorm.DB, field ...string) func(db *gorm.DB) ([]T, int64, error)

func DeleteById

func DeleteById[T any](ids ...uint) func(db *gorm.DB) (int64, error)

func Exec

func Exec(db *gorm.DB, sql string, values ...any) (int64, error)

func Find

func Find[T any](fn func(tx *gorm.DB) *gorm.DB) func(db *gorm.DB) ([]T, error)

func FindById

func FindById[T any](ids ...uint) func(db *gorm.DB) ([]T, error)

func FindByWhere

func FindByWhere[T any](where string, args ...any) func(db *gorm.DB) ([]T, error)

func First

func First[T any](fn func(tx *gorm.DB) *gorm.DB) func(db *gorm.DB) (T, error)

func FirstById

func FirstById[T any](ids ...uint) func(db *gorm.DB) (T, error)

FirstById 根据主键检索

func FirstByWhere

func FirstByWhere[T any](where string, args ...any) func(db *gorm.DB) (T, error)

func IgnoreErrRecordNotFound

func IgnoreErrRecordNotFound[T any](v T, err error) (T, error)

IgnoreErrRecordNotFound 忽略 ErrRecordNotFound 错误 当 First、Last、Take 方法找不到记录时,GORM 会返回 ErrRecordNotFound 错误

func Last

func Last[T any](fn func(tx *gorm.DB) *gorm.DB) func(db *gorm.DB) (T, error)

func LastById

func LastById[T any](ids ...uint) func(db *gorm.DB) (T, error)

LastById 根据主键检索

func LastByWhere

func LastByWhere[T any](where string, args ...any) func(db *gorm.DB) (T, error)

func LogLevel

func LogLevel(logLevel string) logger.LogLevel

func LogWriter

func LogWriter(isStdout bool, logFile string, logLevel logger.LogLevel, slowSqlThreshold time.Duration) (logger.Interface, error)

func Logger

func Logger(out io.Writer, logConf ...logger.Config) func(conf *gorm.Config) *gorm.Config

func New

func New() func(db *gorm.DB) *gorm.DB

New 创建一个不带之前条件的新 DB

func NewMySQL

func NewMySQL(dsn string, logWriter logger.Interface) (*gorm.DB, error)

func One

func One[T any](fn func(tx *gorm.DB) *gorm.DB) func(db *gorm.DB) (T, error)

func OneById

func OneById[T any](ids ...uint) func(db *gorm.DB) (T, error)

func OneByWhere

func OneByWhere[T any](where string, args ...any) func(db *gorm.DB) (T, error)

func Paginate

func Paginate[T any](page int, pageSize int, fn func(tx *gorm.DB) *gorm.DB) func(db *gorm.DB) (*Pages, error)

func Pipeline

func Pipeline(db *gorm.DB, fn func(tx *gorm.DB) error) error

Pipeline 在一条 tcp DB 连接中运行多条 SQL

func Pluck

func Pluck[T any, F FieldType](db *gorm.DB, fn func(tx *gorm.DB) *gorm.DB, field string) ([]F, error)

func PrepareStmt

func PrepareStmt(b bool) func(conf *gorm.Config) *gorm.Config

func Query

func Query[T any](db *gorm.DB, sql string, values ...any) (T, error)

func SelectModelField

func SelectModelField(b bool) func(conf *gorm.Config) *gorm.Config

SelectModelField QueryFields 模式会根据当前 model 的所有字段名称进行 select

func SkipTx

func SkipTx(b bool) func(conf *gorm.Config) *gorm.Config

SkipTx 禁用默认事务

func ToSQL

func ToSQL(db *gorm.DB, fn func(tx *gorm.DB) *gorm.DB) string

func Tx

func Tx(db *gorm.DB, fn func(tx *gorm.DB) error) error

func Update

func Update[T any](db *gorm.DB, fn func(tx *gorm.DB) *gorm.DB, data map[string]any) (int64, error)

func UpdateAndReturn

func UpdateAndReturn[T any](db *gorm.DB, fn func(tx *gorm.DB) *gorm.DB, data map[string]any, field ...string) ([]T, int64, error)

func WithAllowGlobalUpdate

func WithAllowGlobalUpdate(b bool) func(db *gorm.DB) *gorm.DB

WithAllowGlobalUpdate 不允许进行全局 update/execFunc,操作会返回 ErrMissingWhereClause 错误

func WithContext

func WithContext(ctx context.Context) func(db *gorm.DB) *gorm.DB

WithContext 持续会话模式

func WithCreateBatchSize

func WithCreateBatchSize(i int) func(db *gorm.DB) *gorm.DB

func WithDefaultLogger

func WithDefaultLogger() func(db *gorm.DB) *gorm.DB

func WithError

func WithError(err error) func(db *gorm.DB) *gorm.DB

WithError 添加错误

func WithJoin

func WithJoin(typ, table, tableJoinField, mainTableField string) string

func WithLeftJoin

func WithLeftJoin(table, tableJoinField, mainTableField string) string

func WithLogger

func WithLogger(out io.Writer, logConf ...logger.Config) func(db *gorm.DB) *gorm.DB

func WithPrepareStmt

func WithPrepareStmt(b bool) func(db *gorm.DB) *gorm.DB

WithPrepareStmt 所有 DB 操作都会创建并缓存预编译语句

func WithRightJoin

func WithRightJoin(table, tableJoinField, mainTableField string) string

func WithSelectModeField

func WithSelectModeField(b bool) func(db *gorm.DB) *gorm.DB

func WithSkipTx

func WithSkipTx(b bool) func(db *gorm.DB) *gorm.DB

WithSkipTx 禁用默认事务

func WithTestRun

func WithTestRun(b bool) func(db *gorm.DB) *gorm.DB

WithTestRun 生成 SQL 但不执行。 它可以用于准备或测试生成的 SQL

func WithTime

func WithTime(f func() time.Time) func(db *gorm.DB) *gorm.DB

func WithUnscoped

func WithUnscoped(db *gorm.DB) *gorm.DB

WithUnscoped 取消过滤软删除的数据

Types

type Day

type Day struct {
	time.Time
}

Day format json time field by myself

func (Day) MarshalJSON

func (t Day) MarshalJSON() ([]byte, error)

MarshalJSON on JSONTime format Time field with %Y-%m-%d %H:%M:%S

func (*Day) Scan

func (t *Day) Scan(v interface{}) error

Scan valueof time.Time

func (Day) Value

func (t Day) Value() (driver.Value, error)

Value insert timestamp into mysql need this function.

type FieldType

type FieldType interface {
	uint | uint32 | uint64 | int | int32 | int64 | string
}

type JSON

type JSON json.RawMessage

func (*JSON) Scan

func (j *JSON) Scan(value interface{}) error

实现 sql.Scanner 接口,Scan 将 value 扫描至 Jsonb

func (JSON) Value

func (j JSON) Value() (driver.Value, error)

实现 driver.Valuer 接口,Value 返回 json value

type Model

type Model struct {
	Id        uint           `gorm:"column:id;primarykey" json:"id"`
	CreatedAt Time           `gorm:"column:created_at" json:"createdAt"`
	UpdatedAt Time           `gorm:"column:updated_at" json:"updatedAt"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index" json:"deletedAt"`
}

type Page

type Page struct {
	Page     *int   `json:"page" form:"page" bind:"numeric"`         // 当前页,默认为1
	PageSize *int   `json:"pageSize",form:"pageSize" bind:"numeric"` // 分页条目数据,默认10
	Total    *int64 `json:"total"`                                   // 查询总数量
}

Page 通用分页 @Description 通用分页查条件

func (*Page) Data

func (p *Page) Data(data any) *Pages

type Pages

type Pages struct {
	Page Page `json:"pages"` // 分页信息
	Data any  `json:"data"`  // 返回数据
}

Pages 分页查询返回 @Description 分页查询返回

type SQLStmt

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

func GetSQLStmt

func GetSQLStmt(db *gorm.DB, fn func(tx *gorm.DB) *gorm.DB) *SQLStmt

func (*SQLStmt) String

func (s *SQLStmt) String()

生成最终的 SQL

type Time

type Time struct {
	time.Time
}

Time format json time field by myself

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON on JSONTime format Time field with %Y-%m-%d %H:%M:%S

func (*Time) Scan

func (t *Time) Scan(v interface{}) error

Scan valueof time.Time

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value insert timestamp into mysql need this function.

Jump to

Keyboard shortcuts

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