gormpkg

package
v0.2.17 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: Apache-2.0 Imports: 17 Imported by: 4

Documentation

Index

Constants

View Source
const (
	OptionKeyTableOptions = "gorm:table_options"
	OptionKeyCreate       = "gorm:create"
	OptionKeyUpdate       = "gorm:update"
	OptionKeyQuery        = "gorm:query"
	OptionKeyDelete       = "gorm:delete"

	OptionValueEngineInnoDB = "ENGINE=InnoDB CHARSET=utf8mb4"
)
View Source
const (
	DefaultOrderColumn = "id"   // default order column
	DefaultOrderAsc    = "asc"  // order direction : asc
	DefaultOrderDesc   = "desc" // order direction : desc
)
View Source
const (
	DefaultBatchInsertConflictAlias = "excluded"
)
View Source
const (
	DefaultPlaceholder = "?" // param placeholder
)

Variables

View Source
var (
	DefaultBatchInsertConflictActionForMySQL = BatchInsertConflictActionReq{
		OnConflictValueAlias:  "AS " + DefaultBatchInsertConflictAlias,
		OnConflictTarget:      "ON DUPLICATE KEY",
		OnConflictAction:      "UPDATE",
		OnConflictPrepareData: nil,
	}
	DefaultBatchInsertConflictActionPostgres = BatchInsertConflictActionReq{
		OnConflictValueAlias:  "",
		OnConflictTarget:      "ON CONFLICT",
		OnConflictAction:      "DO UPDATE SET",
		OnConflictPrepareData: nil,
	}
)

Functions

func AssembleOrders

func AssembleOrders(db *gorm.DB, orders []*Order) *gorm.DB

AssembleOrders 组装排序

func AssembleWheres

func AssembleWheres(db *gorm.DB, wheres []*Where) *gorm.DB

AssembleWheres 组装条件

func BatchInsert

func BatchInsert(db *gorm.DB, repo BatchInsertRepo, opts ...BatchInsertOption) error

BatchInsert 批量插入

func BatchInsertWithContext

func BatchInsertWithContext(ctx context.Context, db *gorm.DB, repo BatchInsertRepo, opts ...BatchInsertOption) error

BatchInsertWithContext 批量插入

func ForShareOfTable

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

ForShareOfTable FOR SHARE OF `table_name`

func ForUpdate

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

ForUpdate FOR UPDATE

func ForUpdateNowait

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

ForUpdateNowait FOR UPDATE NOWAIT

func ForceIndex

func ForceIndex(db *gorm.DB, indexNameList ...string) *gorm.DB

ForceIndex 强制使用索引

func IgnoreIndex

func IgnoreIndex(db *gorm.DB, indexNameList ...string) *gorm.DB

IgnoreIndex 忽略使用索引

func IsValidField

func IsValidField(field string) bool

IsValidField 判断是否为有效的字段名

func NewDB

func NewDB(dialect gorm.Dialector, connOption *ConnOption) (db *gorm.DB, err error)

NewDB creates a new DB instance

func NewDummyWriter

func NewDummyWriter() logger.Writer

NewDummyWriter .

func NewJSONWriter

func NewJSONWriter(w io.Writer) logger.Writer

NewJSONWriter .

func NewLogger

func NewLogger(conf *logger.Config, writers ...logger.Writer) logger.Interface

NewLogger 数据库日志

func NewLoggerForConn

func NewLoggerForConn(opt *ConnOption) logger.Interface

NewLoggerForConn 数据库链接日志

func NewStdWriter

func NewStdWriter() logger.Writer

NewStdWriter .

func NewWriter

func NewWriter(w io.Writer) logger.Writer

NewWriter .

func Paginator

func Paginator(db *gorm.DB, pageOption *pagepkg.PageOption) *gorm.DB

Paginator 分页

func ParseLoggerLevel

func ParseLoggerLevel(lv string) logger.LogLevel

ParseLoggerLevel 日志级别

func ParseOrderDirection

func ParseOrderDirection(orderDirection string) string

ParseOrderDirection 排序方向

func RemoveCallback

func RemoveCallback(db *gorm.DB) error

RemoveCallback 移除回调 参考文档: https://gorm.io/docs/write_plugins.html#Callbacks 参考func: callbacks.RegisterDefaultCallbacks

func SetOption

func SetOption(db *gorm.DB, key, value string) *gorm.DB

SetOption set option

func UnsafeAssembleOrders

func UnsafeAssembleOrders(db *gorm.DB, orders []*Order) *gorm.DB

UnsafeAssembleOrders 不安全的组装排序

func UnsafeAssembleWheres

func UnsafeAssembleWheres(db *gorm.DB, wheres []*Where) *gorm.DB

UnsafeAssembleWheres 不安全的组装条件

func UseIndex

func UseIndex(db *gorm.DB, indexNameList ...string) *gorm.DB

UseIndex 使用索引

Types

type BatchInsertConflictActionReq

type BatchInsertConflictActionReq struct {
	// OnConflictValueAlias 值的别名
	// MySQL : INSERT INTO ... VALUES (...) AS alias ON DUPLICATE KEY UPDATE a = alias.a
	// Postgres : 默认:excluded
	OnConflictValueAlias string
	// OnConflictTarget 条件
	// MySQL : ON DUPLICATE KEY
	// Postgres : ON CONFLICT (targetColumn, ...)
	OnConflictTarget string
	// OnConflictAction 执行冲突解决方案
	// MySQL : DO NOTHING
	OnConflictAction string
	// OnConflictPrepareData 冲突解决方案数据;DO UPDATE SET column_2 = ?
	OnConflictPrepareData []interface{}
}

BatchInsertConflictActionReq 批量插入冲突解决请求

type BatchInsertOption

type BatchInsertOption func(*batchInsertOptions)

BatchInsertOption 批量插入选项

func WithBatchInsertConflictAction

func WithBatchInsertConflictAction(req *BatchInsertConflictActionReq) BatchInsertOption

WithBatchInsertConflictAction 执行冲突解决 MySQL : INSERT INTO ... VALUES (...) AS alias ON DUPLICATE KEY UPDATE a = alias.a Postgres : ON CONFLICT(id) DO UPDATE SET column_2= CONCAT(test_table.column_2, excluded.column_2);

func WithBatchInsertIgnore

func WithBatchInsertIgnore() BatchInsertOption

WithBatchInsertIgnore 忽略重复插入 INSERT IGNORE INTO ...

type BatchInsertRepo

type BatchInsertRepo interface {
	// TableName 表名
	TableName() string
	// Len 数据长度
	// 例子:length := len([]*User{})
	Len() int
	// InsertColumns 插入的列
	// @param columnList 插入的列名数组;例:[]string{"id","name","age"}
	// @param placeholder 列的占位符;例:"?, ?, ?"
	// 在实现此方法时:需要自行拼接占位符;
	InsertColumns() (columnList []string, placeholder string)
	// InsertValues 插入的值
	// @result prepareData 插入的值;例:[]interface{}{1, "张三", 18, 2, "李四", 20, 3, "小明", 30}
	// @result prepareDataLen 插入的占位符;例:[]string{"(?, ?, ?)", "(?, ?, ?)", "(?, ?, ?)"}
	InsertValues(args *BatchInsertValueArgs) (prepareData []interface{}, placeholderSlice []string)
}

BatchInsertRepo 批量插入

type BatchInsertValueArgs

type BatchInsertValueArgs struct {
	// StepStart 开始步长:索引
	StepStart int
	// StepEnd 结束步长:索引
	StepEnd int

	// InsertPlaceholder 列的占位符;例:"?, ?, ?"
	InsertPlaceholder string
	// InsertSQL 入库的SQL;INSERT INTO ... VALUES (...) AS alias
	InsertSQL string
	// ConflictActionSQL 存在冲突,执行冲突动作
	//insertSQL += "ON DUPLICATE KEY UPDATE " + strings.Join(s.UpdateColumnFromMtdReportData(), ",")
	//insertSQL += "ON CONFLICT (id) DO UPDATE SET column_2= CONCAT(test_table.column_2, excluded.column_2)"
	ConflictActionSQL   string
	ConflictPrepareData []interface{}
}

BatchInsertValueArgs 批量插入值的参数

type ConnOption

type ConnOption struct {
	LoggerEnable              bool
	LoggerLevel               logger.LogLevel
	LoggerWriters             []logger.Writer
	LoggerColorful            bool
	SlowThreshold             time.Duration
	IgnoreRecordNotFoundError bool

	ConnMaxActive   int
	ConnMaxLifetime time.Duration
	ConnMaxIdle     int
	ConnMaxIdleTime time.Duration
}

ConnOption 连接配置

type Option

type Option func(*ConnOption)

Option is config option.

func WithWriters

func WithWriters(writers ...logger.Writer) Option

WithWriters with config writers.

type Order

type Order struct {
	// Field 排序的字段(例子:id)
	Field string
	// Order 排序的方向(例子:desc)
	Order string
}

Order 排序(例子:order by id desc)

func NewOrder

func NewOrder(field, orderDirection string) *Order

NewOrder order

type PaginatorArgs

type PaginatorArgs struct {
	// PageOption 分页
	PageOption *pagepkg.PageOption
	// PageOrders 排序
	PageOrders []*Order
	// PageWheres 条件
	PageWheres []*Where
}

PaginatorArgs 列表参数

type Where

type Where struct {
	// Field 字段
	Field string
	// Operator 运算符
	Operator string
	// Placeholder 占位符
	Placeholder string
	// Value 数据
	Value interface{}
}

Where 条件;例:where id = ?(where id = 1)

func NewWhere

func NewWhere(field, operator string, value interface{}) *Where

NewWhere where

Jump to

Keyboard shortcuts

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