db

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 22 Imported by: 2 modules, 6 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendWhereFromStruct

func AppendWhereFromStruct(db *gorm.DB, s any)

AppendWhereFromStruct 根据结构体中的字段名,生成 where 条件 NameLike = name like AgeGt = where age > AgeLt = where age <

func GetModelsByIDs

func GetModelsByIDs[T any, ID comparable](ids []ID, setID func(*T, ID)) []T

GetModelsByIDs 构造仅含主键的模型切片,供 GORM 关联写入。 setID 由调用方写入主键,避免反射:

db.GetModelsByIDs(ids, func(m *Menu, id uint) { m.ID = id })

func InitDB

func InitDB(opt *Options, log log.Logger, plugins ...gorm.Plugin) (*gorm.DB, error)

func NewPgSqlGetter

func NewPgSqlGetter(opt *Options) *pgsqlConnector

func NewSqliteGetter

func NewSqliteGetter(opt *Options) *sqliteGetter

func NewharborGormLogger added in v1.0.1

func NewharborGormLogger(l logContract.Logger) *harborGormLogger

func Paginate

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB

Types

type BaseModel

type BaseModel struct {
	ID          uint            `gorm:"primarykey" json:"id,string"`
	CreatedAt   *time.Time      `json:"createdAt,omitempty"`
	UpdatedAt   *time.Time      `json:"updatedAt,omitempty"`
	DeletedAt   *gorm.DeletedAt `gorm:"index" json:"deletedAt,omitempty"`           // 删除时间
	CreatorID   string          `gorm:"comment:创建人" json:"creatorID,omitempty"`     // 创建人
	CreatorName string          `gorm:"comment:创建人名称" json:"creatorName,omitempty"` // 创建人名称
	UpdaterID   string          `gorm:"comment:修改人" json:"updaterID,omitempty"`     // 修改人
	UpdaterName string          `gorm:"comment:修改人名称" json:"updaterName,omitempty"` // 修改人名称
}

func (*BaseModel) BeforeCreate

func (i *BaseModel) BeforeCreate(tx *gorm.DB) (err error)

func (*BaseModel) BeforeUpdate

func (i *BaseModel) BeforeUpdate(tx *gorm.DB) (err error)

type BaseRepository

type BaseRepository[T any, ID comparable] struct {
	// contains filtered or unexported fields
}

BaseRepository 泛型 CRUD 基座;ID 为主键类型(如 uint、string)。

func NewBaseRepository

func NewBaseRepository[T any, ID comparable](db *gorm.DB) BaseRepository[T, ID]

func (*BaseRepository[T, ID]) ChangeStatus

func (i *BaseRepository[T, ID]) ChangeStatus(req *ChangeStatus) error

ChangeStatus 更改状态(启用/禁用等)。

func (*BaseRepository[T, ID]) Delete

func (i *BaseRepository[T, ID]) Delete(ids ...ID) error

Delete 按主键批量删除。

func (*BaseRepository[T, ID]) Detail

func (i *BaseRepository[T, ID]) Detail(id ID) (*T, error)

Detail 按主键获取详情。

func (*BaseRepository[T, ID]) Retrieve

func (i *BaseRepository[T, ID]) Retrieve(page, pageSize int, fn func(db *gorm.DB)) (count int64, list []T, err error)

Retrieve 分页列表。

func (*BaseRepository[T, ID]) Save

func (i *BaseRepository[T, ID]) Save(data *T) error

Save 保存或者更新,save 会保存所有字段,包括零值字段。

func (*BaseRepository[T, ID]) Unique

func (i *BaseRepository[T, ID]) Unique(id ID, fn func(db *gorm.DB)) (*T, bool)

Unique 唯一性判断;id 为零值时不排除自身。

func (*BaseRepository[T, ID]) WithContext

func (i *BaseRepository[T, ID]) WithContext(ctx context.Context) *BaseRepository[T, ID]

type ChangeStatus

type ChangeStatus struct {
	ID     uint `json:"id,string" binding:"required"`
	Status int  `json:"status"`
}

type Connector

type Connector interface {
	Open(cfg *gorm.Config) (*gorm.DB, error) // 连接数据库
	Options() *Options                       // 返回配置
	DefaultOptions() *Options                // 返回默认配置
	GetDSN() string                          // 获取 DSN
}

type CustomReplacer

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

func (CustomReplacer) Replace

func (r CustomReplacer) Replace(name string) string

type DBServiceProvider

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

func (*DBServiceProvider) Boot

func (i *DBServiceProvider) Boot(ctx context.Context) error

func (*DBServiceProvider) Conf

func (i *DBServiceProvider) Conf() map[string]string

func (*DBServiceProvider) Description

func (i *DBServiceProvider) Description() string

func (*DBServiceProvider) HealthCheck added in v1.1.0

func (i *DBServiceProvider) HealthCheck(ctx context.Context) error

func (*DBServiceProvider) Register

func (i *DBServiceProvider) Register(ctx context.Context) error

func (*DBServiceProvider) SetApplication added in v1.1.0

func (i *DBServiceProvider) SetApplication(app foundation.Application)

func (*DBServiceProvider) Shutdown added in v1.1.0

func (i *DBServiceProvider) Shutdown(ctx context.Context) error

type Driver

type Driver string
const ClickHouse Driver = "clickhouse"
const DM Driver = "dm" // 达梦数据库
const Mysql Driver = "mysql"
const Oracle Driver = "oracle"
const Pgsql Driver = "pgsql"
const Sqlite Driver = "sqlite"

type MysqlConnector

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

func NewMysqlConnector

func NewMysqlConnector(opt *Options) *MysqlConnector

func (*MysqlConnector) DefaultOptions

func (i *MysqlConnector) DefaultOptions() *Options

func (*MysqlConnector) GetDSN

func (i *MysqlConnector) GetDSN() string

func (*MysqlConnector) Open

func (i *MysqlConnector) Open(cfg *gorm.Config) (*gorm.DB, error)

func (*MysqlConnector) Options

func (i *MysqlConnector) Options() *Options

type Options

type Options struct {
	Username     string `json:"username"`
	Password     string `json:"password"`
	Host         string `json:"host"`
	Port         int    `json:"port"`
	Driver       Driver `json:"driver"` // 数据库类型
	Database     string `json:"database"`
	Schema       string `json:"schema"`
	Charset      string `json:"charset"`
	Query        string `json:"query"`
	MaxIdleConns int    `json:"max-idle-conns"`
	MaxConns     int    `json:"max-conns"`
	TimeZone     string `json:"time-zone"`
}

Source Files

  • base_model.go
  • base_repository.go
  • base_scope.go
  • connector.go
  • connector_mysql.go
  • connector_pgsql.go
  • connector_sqlite.go
  • const.go
  • db_service_provider.go
  • gorm_logger.go
  • helper.go
  • init_db.go

Jump to

Keyboard shortcuts

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