gormgenerics

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: GPL-3.0 Imports: 14 Imported by: 0

README

gorm-generics

Documentation

Index

Constants

View Source
const (
	Postgresql DriverName = "postgresql"
	MySQL                 = "mysql"
	SQLite                = "sqlite"
	SQLServer             = "sqlserver"
	TiDB       DriverName = "tidb"
)
View Source
const (
	// CallBackBeforeName sql执行之前tag
	CallBackBeforeName = "opentracing:before"
	// CallBackAfterName sql执行之后tag
	CallBackAfterName = "opentracing:after"
)
View Source
const GormSpanKey = "__gorm_span"

GormSpanKey 包内静态变量

Variables

This section is empty.

Functions

func DBConnect

func DBConnect(driver DriverName, dsn string, option options) (*gorm.DB, error)

DBConnect init database connect

func DBWhereExpression

func DBWhereExpression(column string, value any, symbol string) clause.Expression

DBWhereExpression process field symbol expression

Types

type BasicRepo

type BasicRepo[T any] interface {
	// Create 创建一条数据
	Create(ctx context.Context, ent *T) error
	// Creates 批量创建
	Creates(ctx context.Context, ent []*T) error
	// Update 通过id更新数据
	Update(ctx context.Context, id uint, ent *T) error
	// UpdateColumns 通过id更新部分字段数据
	UpdateColumns(ctx context.Context, id uint, data any) error
	// UpdateColumnsBy 通过条件表达式更新数据
	UpdateColumnsBy(ctx context.Context, expression []clause.Expression, data any) error
	// Find 查询
	Find(ctx context.Context, expression ...clause.Expression) ([]*T, error)
	// FindOneBy 通过条件表达式查询一条数据
	FindOneBy(ctx context.Context, expression []clause.Expression) (*T, error)
	// FindOne 通过id查询一条数据
	FindOne(ctx context.Context, id uint) (*T, error)
	// DeleteOne 通过id删除一条数据
	DeleteOne(ctx context.Context, id uint) error
	// DeleteBy 通过条件删除一条或者多条数据
	DeleteBy(ctx context.Context, expression []clause.Expression) error
	// Count 进行统计数据
	Count(ctx context.Context, expression ...clause.Expression) (count int64, err error)
	// ExecTX 开启事务
	ExecTX(ctx context.Context, fc func(ctx context.Context) error) error
	// Database 获取db
	Database() Database
	// Model 获取模型
	Model() *T
}

BasicRepo 基本的存储库

func NewBasicRepository

func NewBasicRepository[T any](database Database) BasicRepo[T]

NewBasicRepository 新建基础存储库

type DB

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

func (*DB) Close

func (d *DB) Close() error

Close 关闭db连接

func (*DB) DB

func (d *DB) DB(ctx context.Context) *gorm.DB

DB 获取db

func (*DB) ExecTx

func (d *DB) ExecTx(ctx context.Context, fc func(context.Context) error) error

ExecTx 执行事务

func (*DB) TranslateGormError

func (d *DB) TranslateGormError(ctx context.Context, db *gorm.DB) error

type Database

type Database interface {
	DB(ctx context.Context) *gorm.DB
	Close() error
	TranslateGormError(ctx context.Context, db *gorm.DB) error
	ExecTx(ctx context.Context, fc func(context.Context) error) error
}

func NewDataBase

func NewDataBase(driver DriverName, dsn string, opts ...Option) (Database, error)

NewDataBase new database

type DriverName

type DriverName string

DriverName 数据库驱动名称

type ILike

type ILike clause.Eq

ILike ilike

func (ILike) Build

func (like ILike) Build(builder clause.Builder)

Build builder sql

func (ILike) NegationBuild

func (like ILike) NegationBuild(builder clause.Builder)

NegationBuild builder sql

type Limit

type Limit struct {
	All      bool // true获取全部条目
	PageSize uint // 每页数量
	PageNum  uint // 页数
	Count    bool // true获取总数
}

Limit 数据库数量查询限制

type OpentracingPlugin

type OpentracingPlugin struct {
}

OpentracingPlugin 追踪插件

func (*OpentracingPlugin) Initialize

func (op *OpentracingPlugin) Initialize(db *gorm.DB) error

func (*OpentracingPlugin) Name

func (op *OpentracingPlugin) Name() string

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithAutoMigrate

func WithAutoMigrate(a bool) Option

func WithAutoMigrateDst

func WithAutoMigrateDst(models []any) Option

func WithLogger

func WithLogger(log logger.Interface) Option

func WithOpentracingPlugin

func WithOpentracingPlugin(op *OpentracingPlugin) Option

func WithTablePrefix

func WithTablePrefix(s string) Option

func WithTranslateError

func WithTranslateError(translateError translateErrorFc) Option

Jump to

Keyboard shortcuts

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