gormex

package
v0.3.30 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SqlLogFormat = "elapsed:%dms, affect:%d, err:%v, sql:%s" // SQL日志格式
)

Variables

View Source
var MySqlComponent = embedded.NewComponent[*gorm.DB]()

Functions

func BaseGroup

func BaseGroup[T dependency.IEntity](f func(v ...*T) (int64, error), opt *dependency.BaseOption, p ...*T) (int64, error)

BaseGroup

func CoreFrmCtx

func CoreFrmCtx(ctx context.Context, id string) *gorm.DB

CoreFrmCtx

func GetDbTX

func GetDbTX(id string) contextex.ContextKey

func GetTransactionDb

func GetTransactionDb(id string) *gorm.DB

func Log

func Log(ctx context.Context, lvl iLog.Level, msg string)

Log 记录指定级别的日志消息。

func NewContext

func NewContext(ctx context.Context, id string, newdb *gorm.DB) context.Context

func NewLogger

func NewLogger(opts ...GormLoggerOption) logger.Interface

NewLogger 创建一个新的GormLogger实例。 opts ...GormLoggerOption: 一个或多个GormLogger选项函数,用于配置GormLogger实例。 返回值 logger.Interface: 返回实现了logger.Interface接口的日志实例。

func NewMySqlClient

func NewMySqlClient(dsn string, log logger.Interface) (*gorm.DB, error)

reference docs: https://github.com/go-sql-driver/mysql#dsn-data-source-name dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local&timeout=1m" NewMySqlClient new a mysql client by dsn with logger

func NewUnitOfWork

func NewUnitOfWork(id string) dependency.IUnitOfWork

func Option2Page added in v0.0.7

func Option2Page(db *gorm.DB, opt *dependency.BaseOption) *gorm.DB

Option2Page

func ReadOnly

func ReadOnly(ctx context.Context, id string) *gorm.DB

ReadOnly

func SetDisableQueryFields

func SetDisableQueryFields()

func SyncDbStruct added in v0.0.11

func SyncDbStruct(dbShardingKeys [][]any, pos ...dependency.IPo) error

SyncDbStruct

func WithContext

func WithContext(ctx context.Context, id string) *gorm.DB

Types

type BaseRepository

type BaseRepository[T dependency.IEntity] struct{} // base repository

func (*BaseRepository[T]) BaseCount

func (r *BaseRepository[T]) BaseCount(ctx context.Context, opts ...dependency.BaseOptionFunc) (int64, error)

BaseCount

func (*BaseRepository[T]) BaseCreate

func (r *BaseRepository[T]) BaseCreate(ctx context.Context, ps []*T, opts ...dependency.BaseOptionFunc) (int64, error)

BaseCreate

func (*BaseRepository[T]) BaseDelete

func (r *BaseRepository[T]) BaseDelete(ctx context.Context, p *T, opts ...dependency.BaseOptionFunc) (int64, error)

BaseDelete

func (*BaseRepository[T]) BaseGet

func (r *BaseRepository[T]) BaseGet(ctx context.Context, opts ...dependency.BaseOptionFunc) (*T, error)

BaseGet

func (*BaseRepository[T]) BaseQuery

func (r *BaseRepository[T]) BaseQuery(ctx context.Context, opts ...dependency.BaseOptionFunc) ([]T, error)

BaseQuery

func (*BaseRepository[T]) BaseQueryWithCount added in v0.1.0

func (r *BaseRepository[T]) BaseQueryWithCount(ctx context.Context, opts ...dependency.BaseOptionFunc) ([]T, int64, error)

BaseQueryWithCount

func (*BaseRepository[T]) BaseSave

func (r *BaseRepository[T]) BaseSave(ctx context.Context, ps []*T, opts ...dependency.BaseOptionFunc) (int64, error)

BaseSave

func (*BaseRepository[T]) BaseUpdate

func (r *BaseRepository[T]) BaseUpdate(ctx context.Context, p *T, opts ...dependency.BaseOptionFunc) (int64, error)

BaseUpdate

func (*BaseRepository[T]) BuildConds

func (r *BaseRepository[T]) BuildConds(ctx context.Context, t *T, opt *dependency.BaseOption) *gorm.DB

BuildConds

func (*BaseRepository[T]) BuildFrmOption

func (r *BaseRepository[T]) BuildFrmOption(ctx context.Context, t *T, opt *dependency.BaseOption) *gorm.DB

BuildFrmOption

func (*BaseRepository[T]) BuildFrmOptions added in v0.0.7

func (r *BaseRepository[T]) BuildFrmOptions(ctx context.Context, t *T, opts ...dependency.BaseOptionFunc) *gorm.DB

BuildFrmOptions

type EventRepository added in v0.3.4

type EventRepository[T dependency.IEventMessage] struct {
	TaskQueueRepository[T]
}

func (EventRepository[T]) InsertAction added in v0.3.4

func (r EventRepository[T]) InsertAction(ctx context.Context, db string, t *T) func(context.Context) error

type GormLogger

type GormLogger struct {
	LogLevel                  logger.LogLevel // 当前日志级别
	IgnoreRecordNotFoundError bool            // 是否忽略RecordNotFoundError错误
	IgnoreNoAffect            bool            // 是否忽略未影响行的日志
	SlowThreshold             time.Duration   // 慢查询阈值
	// contains filtered or unexported fields
}

GormLogger 结构体定义了Gorm日志记录器的配置和实现。

func (*GormLogger) Error

func (l *GormLogger) Error(ctx context.Context, msg string, args ...interface{})

Error 记录错误级别的日志。

func (*GormLogger) Info

func (l *GormLogger) Info(ctx context.Context, msg string, args ...interface{})

Info 记录信息级别的日志。

func (*GormLogger) LogMode

func (l *GormLogger) LogMode(level logger.LogLevel) logger.Interface

LogMode 设置日志记录模式。

func (*GormLogger) Trace

func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)

Trace 记录SQL相关的日志,包括执行时间、影响的行数以及可能的错误。

func (*GormLogger) Warn

func (l *GormLogger) Warn(ctx context.Context, msg string, args ...interface{})

Warn 记录警告级别的日志。

type GormLoggerOption added in v0.3.18

type GormLoggerOption func(*GormLogger)

GormLoggerOption 定义了一个函数类型,用于设置GormLogger的配置。

func WithCore added in v0.3.18

func WithCore(core *zap.Logger) GormLoggerOption

WithCore 通过提供一个*zap.Logger实例来设置GormLogger的核心日志记录器。

func WithIgnoreNoAffect added in v0.3.18

func WithIgnoreNoAffect(v bool) GormLoggerOption

WithIgnoreNoAffect 设置是否忽略未影响行的日志记录。

func WithIgnoreRecordNotFoundError added in v0.3.18

func WithIgnoreRecordNotFoundError(v bool) GormLoggerOption

WithIgnoreRecordNotFoundError 设置是否忽略记录未找到的错误。

func WithLogLevel added in v0.3.18

func WithLogLevel(level logger.LogLevel) GormLoggerOption

WithLogLevel 设置日志级别。

func WithSlowThreshold added in v0.3.18

func WithSlowThreshold(v time.Duration) GormLoggerOption

WithSlowThreshold 设置慢查询的阈值。

type GormTransactionImpl

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

func (*GormTransactionImpl) Execute

func (t *GormTransactionImpl) Execute(ctx context.Context, fs ...dependency.DbAction) (e error)

Execute An execution function is passed in, and transactions are executed within the function.

type TaskQueueRepository added in v0.2.3

type TaskQueueRepository[T dependency.ITask] struct {
	BaseRepository[T]
}

func (TaskQueueRepository[T]) CountByCreator added in v0.2.3

func (r TaskQueueRepository[T]) CountByCreator(ctx context.Context, t T, createBy, status int64) (int64, error)

CountByCreator 请求者当前执行数量

func (TaskQueueRepository[T]) FindLockeds added in v0.2.3

func (r TaskQueueRepository[T]) FindLockeds(ctx context.Context, locker string) ([]T, error)

FindLockeds 找到被锁定的记录

func (TaskQueueRepository[T]) ReportExecResult added in v0.2.3

func (r TaskQueueRepository[T]) ReportExecResult(ctx context.Context, id int64, locker string, execResult string, execErr error) (int64, error)

ReportExecResult 汇报执行结果

func (TaskQueueRepository[T]) WaitExecWithLock added in v0.2.3

func (r TaskQueueRepository[T]) WaitExecWithLock(ctx context.Context, t T, batch int) (string, int64, error)

WaitExecWithLock 需要锁定的记录

Jump to

Keyboard shortcuts

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