Documentation ¶
Index ¶
- Constants
- Variables
- func AssembleOrders(db *gorm.DB, orders []*Order) *gorm.DB
- func AssembleWheres(db *gorm.DB, wheres []*Where) *gorm.DB
- func BatchInsert(db *gorm.DB, repo BatchInsertRepo, opts ...BatchInsertOption) error
- func BatchInsertWithContext(ctx context.Context, db *gorm.DB, repo BatchInsertRepo, ...) error
- func Deleted(dbConn *gorm.DB, value interface{}, conditions ...interface{}) *gorm.DB
- func ExecWithTransaction(dbConn *gorm.DB, fc func(tx *gorm.DB) error, opts ...*sql.TxOptions) (err error)
- func ForShareOfTable(db *gorm.DB) *gorm.DB
- func ForUpdate(db *gorm.DB) *gorm.DB
- func ForUpdateNowait(db *gorm.DB) *gorm.DB
- func ForceIndex(db *gorm.DB, indexNameList ...string) *gorm.DB
- func IgnoreIndex(db *gorm.DB, indexNameList ...string) *gorm.DB
- func IsErrDuplicatedKey(err error) bool
- func IsErrRecordNotFound(err error) bool
- func IsValidColumnName(field string) bool
- func NewDB(dialect gorm.Dialector, connOption *ConnOption) (db *gorm.DB, err error)
- func NewDummyWriter() logger.Writer
- func NewJSONWriter(w io.Writer) logger.Writer
- func NewLogger(conf *logger.Config, writers ...logger.Writer) logger.Interface
- func NewLoggerForConn(opt *ConnOption) logger.Interface
- func NewStdWriter() logger.Writer
- func NewWriter(w io.Writer) logger.Writer
- func Paginator(db *gorm.DB, pageOption *pagepkg.PageOption) *gorm.DB
- func ParseLoggerLevel(lv string) logger.LogLevel
- func ParseOrderDirection(orderDirection string) string
- func QueryDeletedData(dbConn *gorm.DB) *gorm.DB
- func QueryUndeletedData(dbConn *gorm.DB) *gorm.DB
- func RemoveCallback(db *gorm.DB) error
- func SetCreateTime(updates map[string]interface{})
- func SetOption(db *gorm.DB, key, value string) *gorm.DB
- func SetUpdateTime(updates map[string]interface{})
- func SoftDelete(dbConn *gorm.DB) *gorm.DB
- func UnsafeAssembleOrders(db *gorm.DB, orders []*Order) *gorm.DB
- func UnsafeAssembleWheres(db *gorm.DB, wheres []*Where) *gorm.DB
- func UseIndex(db *gorm.DB, indexNameList ...string) *gorm.DB
- type BatchInsertConflictActionReq
- type BatchInsertOption
- type BatchInsertRepo
- type BatchInsertValueArgs
- type ConnOption
- type Model
- type ModelForMysql
- type ModelForPostgres
- type Option
- type Order
- type PaginatorArgs
- type TransactionInterface
- type Where
Constants ¶
const ( FieldID = "id" FieldCreatedTime = "created_time" FieldUpdatedTime = "updated_time" FieldDeletedTime = "deleted_time" FieldIsDeleted = "is_deleted" )
const ( OptionKeyTableOptions = "gorm:table_options" OptionKeyCreate = "gorm:create" OptionKeyUpdate = "gorm:update" OptionKeyQuery = "gorm:query" OptionKeyDelete = "gorm:delete" OptionValueEngineInnoDB = "ENGINE=InnoDB CHARSET=utf8mb4" )
const ( DefaultOrderColumn = "id" // default order column DefaultOrderAsc = "asc" // order direction : asc DefaultOrderDesc = "desc" // order direction : desc )
const (
DefaultBatchInsertConflictAlias = "excluded"
)
const (
DefaultPlaceholder = "?" // param placeholder
)
Variables ¶
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 ¶
AssembleOrders 组装排序
func AssembleWheres ¶
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 ExecWithTransaction ¶ added in v0.3.7
func ExecWithTransaction(dbConn *gorm.DB, fc func(tx *gorm.DB) error, opts ...*sql.TxOptions) (err error)
ExecWithTransaction 在事务中执行一系列操作; 无需手动开启事务 DOCS: https://gorm.io/zh_CN/docs/transactions.html
func ForShareOfTable ¶
ForShareOfTable FOR SHARE OF `table_name`
func IgnoreIndex ¶
IgnoreIndex 忽略使用索引
func IsErrDuplicatedKey ¶ added in v0.3.2
IsErrDuplicatedKey ...
func IsErrRecordNotFound ¶ added in v0.3.1
IsErrRecordNotFound ...
func IsValidColumnName ¶ added in v0.3.7
IsValidColumnName 判断是否为有效的字段名
func NewLoggerForConn ¶
func NewLoggerForConn(opt *ConnOption) logger.Interface
NewLoggerForConn 数据库链接日志
func ParseOrderDirection ¶
ParseOrderDirection 排序方向
func QueryDeletedData ¶ added in v0.2.18
QueryDeletedData 删除的数据
func QueryUndeletedData ¶ added in v0.2.18
QueryUndeletedData 未删除的数据
func RemoveCallback ¶
RemoveCallback 移除回调 参考文档: https://gorm.io/docs/write_plugins.html#Callbacks 参考func: callbacks.RegisterDefaultCallbacks
func SetCreateTime ¶ added in v0.2.18
func SetCreateTime(updates map[string]interface{})
func SetUpdateTime ¶ added in v0.2.18
func SetUpdateTime(updates map[string]interface{})
func UnsafeAssembleOrders ¶
UnsafeAssembleOrders 不安全的组装排序
func UnsafeAssembleWheres ¶
UnsafeAssembleWheres 不安全的组装条件
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 Model ¶ added in v0.2.18
type Model struct { Id uint64 `gorm:"column:id;type:uint;autoIncrement;comment:ID" json:"id"` CreatedTime time.Time `gorm:"column:created_time;type:time;not null;comment:创建时间" json:"created_time"` UpdatedTime time.Time `gorm:"column:updated_time;type:time;not null;comment:更新时间" json:"updated_time"` IsDeleted bool `gorm:"column:is_deleted;type:uint;default:0;comment:是否已删除" json:"is_deleted"` DeletedTime time.Time `gorm:"column:deleted_time;type:time;comment:删除时间" json:"deleted_time"` }
type ModelForMysql ¶ added in v0.3.1
type ModelForMysql struct { Id uint64 `gorm:"column:id;type:uint;autoIncrement;default:current_timestamp();comment:ID" json:"id"` CreatedTime time.Time `gorm:"column:created_time;type:time;not null;default:current_timestamp();comment:创建时间" json:"created_time"` UpdatedTime time.Time `gorm:"column:updated_time;type:time;not null;default:current_timestamp();comment:更新时间" json:"updated_time"` IsDeleted bool `gorm:"column:is_deleted;type:uint;default:0;comment:是否已删除" json:"is_deleted"` DeletedTime time.Time `gorm:"column:deleted_time;type:time;comment:删除时间" json:"deleted_time"` }
type ModelForPostgres ¶ added in v0.3.1
type ModelForPostgres struct { Id uint64 `gorm:"column:id;type:uint;autoIncrement;default:current_timestamp;comment:ID" json:"id"` CreatedTime time.Time `gorm:"column:created_time;type:time;not null;default:current_timestamp;comment:创建时间" json:"created_time"` UpdatedTime time.Time `gorm:"column:updated_time;type:time;not null;default:current_timestamp;comment:更新时间" json:"updated_time"` IsDeleted bool `gorm:"column:is_deleted;type:uint;default:0;comment:是否已删除" json:"is_deleted"` DeletedTime time.Time `gorm:"column:deleted_time;type:time;comment:删除时间" json:"deleted_time"` }
type Option ¶
type Option func(*ConnOption)
Option is config option.
func WithWriters ¶
WithWriters with config writers.
type PaginatorArgs ¶
type PaginatorArgs struct { // PageRequest 分页 PageRequest *pagepkg.PageRequest // PageOption 分页 PageOption *pagepkg.PageOption // PageOrders 排序 PageOrders []*Order // PageWheres 条件 PageWheres []*Where }
PaginatorArgs 列表参数
func InitPaginatorArgs ¶ added in v0.2.19
func InitPaginatorArgs(page, pageSize uint32) *PaginatorArgs
func InitPaginatorArgsByPageRequest ¶ added in v0.2.19
func InitPaginatorArgsByPageRequest(pageRequest *pagepkg.PageRequest) *PaginatorArgs
type TransactionInterface ¶ added in v0.3.7
type TransactionInterface interface { Do(ctx context.Context, fc func(ctx context.Context, tx *gorm.DB) error) error Commit(ctx context.Context) error Rollback(ctx context.Context) error CommitAndErrRollback(ctx context.Context, resultErr error) (err error) }
func NewTransaction ¶ added in v0.2.38
Source Files ¶
- gorm.kit.go
- gorm_batch_insert.kit.go
- gorm_batch_insert_option.kit.go
- gorm_callback.kit.go
- gorm_conn.kit.go
- gorm_conn_option.kit.go
- gorm_errors.kit.go
- gorm_helper.kit.go
- gorm_hint.kit.go
- gorm_locking.kit.go
- gorm_logger.kit.go
- gorm_logger_writer.kit.go
- gorm_option.kit.go
- gorm_order.kit.go
- gorm_page.kit.go
- gorm_where.kit.go