database

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder[T any] struct {
	// contains filtered or unexported fields
}

Builder 查询构建器(泛型版本)

func NewBuilder

func NewBuilder[T any](db *gorm.DB) *Builder[T]

NewBuilder 创建新的查询构建器

func (*Builder[T]) Chunk

func (b *Builder[T]) Chunk(size int, callback func([]T) error) error

Chunk 分块处理记录

func (*Builder[T]) Count

func (b *Builder[T]) Count() (int64, error)

Count 统计记录数

func (*Builder[T]) Create

func (b *Builder[T]) Create(model *T) error

Create 创建记录

func (*Builder[T]) Delete

func (b *Builder[T]) Delete() error

Delete 删除记录(软删除)

func (*Builder[T]) Exists

func (b *Builder[T]) Exists() (bool, error)

Exists 检查记录是否存在

func (*Builder[T]) Find

func (b *Builder[T]) Find(id interface{}) (*T, error)

Find 根据 ID 查找记录

func (*Builder[T]) First

func (b *Builder[T]) First() (*T, error)

First 获取第一条记录

func (*Builder[T]) ForceDelete

func (b *Builder[T]) ForceDelete() error

ForceDelete 强制删除记录

func (*Builder[T]) Get

func (b *Builder[T]) Get() ([]T, error)

Get 获取所有结果

func (*Builder[T]) Limit

func (b *Builder[T]) Limit(limit int) *Builder[T]

Limit 限制结果数量

func (*Builder[T]) Offset

func (b *Builder[T]) Offset(offset int) *Builder[T]

Offset 设置偏移量

func (*Builder[T]) OrWhere

func (b *Builder[T]) OrWhere(query interface{}, args ...interface{}) *Builder[T]

OrWhere 添加 or where 条件

func (*Builder[T]) OrderBy

func (b *Builder[T]) OrderBy(column string, direction ...string) *Builder[T]

OrderBy 添加排序

func (*Builder[T]) Paginate

func (b *Builder[T]) Paginate(page, perPage int) (*Pagination[T], error)

Paginate 分页查询

func (*Builder[T]) Select

func (b *Builder[T]) Select(fields ...string) *Builder[T]

Select 选择字段

func (*Builder[T]) Transaction

func (b *Builder[T]) Transaction(fn func(*Builder[T]) error) error

Transaction 执行事务

func (*Builder[T]) Update

func (b *Builder[T]) Update(updates interface{}) error

Update 更新记录

func (*Builder[T]) Where

func (b *Builder[T]) Where(query interface{}, args ...interface{}) *Builder[T]

Where 添加 where 条件

func (*Builder[T]) WhereBetween

func (b *Builder[T]) WhereBetween(column string, start, end interface{}) *Builder[T]

WhereBetween 添加 where between 条件

func (*Builder[T]) WhereIn

func (b *Builder[T]) WhereIn(column string, values interface{}) *Builder[T]

WhereIn 添加 where in 条件

func (*Builder[T]) WhereNotIn

func (b *Builder[T]) WhereNotIn(column string, values interface{}) *Builder[T]

WhereNotIn 添加 where not in 条件

func (*Builder[T]) WhereNotNull

func (b *Builder[T]) WhereNotNull(column string) *Builder[T]

WhereNotNull 添加 where not null 条件

func (*Builder[T]) WhereNull

func (b *Builder[T]) WhereNull(column string) *Builder[T]

WhereNull 添加 where null 条件

func (*Builder[T]) With

func (b *Builder[T]) With(relations ...string) *Builder[T]

With 预加载关联

type Config

type Config struct {
	Driver   string // mysql, sqlite
	Host     string
	Port     int
	Database string
	Username string
	Password string
	Charset  string
	LogLevel string // silent, error, warn, info
}

Config 数据库配置

type DB

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

DB 数据库连接管理器

func New

func New() *DB

New 创建新的数据库管理器

func (*DB) AddConnection

func (db *DB) AddConnection(name string, cfg Config) error

AddConnection 添加数据库连接

func (*DB) Close

func (db *DB) Close() error

Close 关闭所有数据库连接

func (*DB) Connection

func (db *DB) Connection(name ...string) *gorm.DB

Connection 获取数据库连接

type Model

type Model struct {
	ID        uint           `gorm:"primarykey" json:"id"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
}

Model Eloquent 风格的基础模型

type Pagination

type Pagination[T any] struct {
	Data       []T   `json:"data"`
	Total      int64 `json:"total"`
	Page       int   `json:"page"`
	PerPage    int   `json:"per_page"`
	TotalPages int   `json:"total_pages"`
}

Pagination 分页结果

Jump to

Keyboard shortcuts

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