persist

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 21:57:26 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-11 21:57:35 * @FilePath: \go-sqlbuilder\persist\connect.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-13 11:06:59 * @FilePath: \go-sqlbuilder\persist\extension.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 21:14:54 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-11 21:21:09 * @FilePath: \go-sqlbuilder\persist\handler.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 21:18:55 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-13 11:07:30 * @FilePath: \go-sqlbuilder\persist\persist.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 21:13:15 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-13 11:07:41 * @FilePath: \go-sqlbuilder\persist\query_param.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 21:13:15 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-13 11:07:41 * @FilePath: \go-sqlbuilder\persist\query_param.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create[T any](ctx context.Context, tx DBHandler, tCreates ...*T) (err error)

func CreateWithCache

func CreateWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tCreates ...*T) (err error)

CreateWithCache 创建记录并删除缓存

func Delete

func Delete[T Model](ctx context.Context, tx DBHandler, tDeletes ...*T) (err error)

func DeleteWithCache

func DeleteWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tDeletes ...*T) (err error)

DeleteWithCache 删除记录并删除缓存

func Get

func Get[T Model](ctx context.Context, tx DBHandler, t *T) (tGet *T, err error)

func GetByFilters

func GetByFilters[T Model](ctx context.Context, tx DBHandler, filters Filters, orders ...Order) (tGet *T, err error)

func GetWithCache

func GetWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, t *T) (tGet *T, err error)

GetWithCache 获取记录,优先从缓存读取

func List

func List[T Model](ctx context.Context, tx DBHandler, filters Filters, page *meta.Paging, orders ...Order) (tList []*T, err error)

func NewDBHandler

func NewDBHandler(config *DBConfig) (*gorm.DB, error)

NewDBHandler 根据配置创建数据库连接(MySQL/SQLite)

func Save

func Save[T Model](ctx context.Context, tx DBHandler, tSaves ...*T) (err error)

func SaveWithCache

func SaveWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tSaves ...*T) (err error)

SaveWithCache 保存记录并删除缓存

func UnsafeBytes

func UnsafeBytes(s string) []byte

UnsafeBytes 将字符串转换为字节切片

func Update

func Update[T Model](ctx context.Context, tx DBHandler, tUpdates ...*T) (err error)

func UpdateWithCache

func UpdateWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tUpdates ...*T) (err error)

UpdateWithCache 更新记录并删除缓存

Types

type CacheHandler

type CacheHandler interface {
	Set([]byte, []byte) error
	SetWithTTL([]byte, []byte, time.Duration) error
	Get([]byte) ([]byte, error)
	GetTTL([]byte) (time.Duration, error)
	Del(...[]byte) error

	Close() error
}

type DBConfig

type DBConfig struct {
	Type         string // "mysql" 或 "sqlite",可扩展 "pg"
	Host         string // MySQL/PG
	Port         int    // MySQL/PG
	DBName       string // MySQL/PG
	Args         string // 连接参数
	Username     string // MySQL/PG
	Password     string // MySQL/PG
	Path         string // SQLite 路径
	ShowSQL      bool   // 是否显示 SQL 日志
	MaxOpenConns int    // 最大连接数
	MaxIdleConns int    // 最大空闲连接数
}

DBConfig 统一数据库配置结构,支持 MySQL、SQLite

func LoadDBConfigFromFile

func LoadDBConfigFromFile(configPath string) (*DBConfig, error)

type DBHandler

type DBHandler interface {
	DB() *gorm.DB
	Query(param *QueryParam) *gorm.DB
	// auto migrate
	AutoMigrate(dst ...any) error
	// tx
	Begin(opts ...*sql.TxOptions) DBHandler
	Commit() error
	Rollback() error

	Close() error
	// 兼容 go-sqlbuilder,执行原生SQL
	ExecSQL(ctx context.Context, sql string, args ...interface{}) (*gorm.DB, error)
	RawQuery(ctx context.Context, sql string, args ...interface{}) (*gorm.DB, error)
}

type EqFilter

type EqFilter struct {
	Name  string
	Value any
}

func (*EqFilter) Where

func (eq *EqFilter) Where(db *gorm.DB) *gorm.DB

type FieldOrder

type FieldOrder struct {
	Field    string
	Reversed bool
}

func (*FieldOrder) Order

func (f *FieldOrder) Order(db *gorm.DB) *gorm.DB

type Filter

type Filter interface {
	Where(db *gorm.DB) *gorm.DB
}

func NewEqFilter

func NewEqFilter(name string, value any) Filter

func NewInFilter

func NewInFilter[T any](name string, values []T) Filter

func NewJSONArrayContainsFilter

func NewJSONArrayContainsFilter(name string, value any) Filter

func NewLikeFilter

func NewLikeFilter(name string, value string) Filter

func NewNeFilter

func NewNeFilter(name string, value any) Filter

func NewOrFilter

func NewOrFilter[T any](name string, values []T) Filter

func NewOriginalOrFilter

func NewOriginalOrFilter(filters Filters) Filter

func NewOriginalWhereFilter

func NewOriginalWhereFilter(query string, args ...any) Filter

func NewPeriodFilter

func NewPeriodFilter(name string, start, end time.Time) Filter

func NewPrefixFilter

func NewPrefixFilter(name string, value string) Filter

func NewRangeFilter

func NewRangeFilter[T Number](name string, start, end T) Filter

func NewSuffixFilter

func NewSuffixFilter(name string, value string) Filter

type Filters

type Filters []Filter

func (Filters) Where

func (fs Filters) Where(db *gorm.DB) *gorm.DB

type InFilter

type InFilter[T any] struct {
	Name   string
	Values []T
}

func (*InFilter[T]) Where

func (in *InFilter[T]) Where(db *gorm.DB) *gorm.DB

type JSONArrayContainsFilter

type JSONArrayContainsFilter struct {
	Name  string
	Value any
}

func (*JSONArrayContainsFilter) Where

func (jc *JSONArrayContainsFilter) Where(db *gorm.DB) *gorm.DB

type LikeFilter

type LikeFilter struct {
	Name  string
	Value string
}

func (*LikeFilter) Where

func (f *LikeFilter) Where(db *gorm.DB) *gorm.DB

type Model

type Model interface {
	CacheKey() string
	CacheTTL() time.Duration
}

type NeFilter

type NeFilter struct {
	Name  string
	Value any
}

func (*NeFilter) Where

func (ne *NeFilter) Where(db *gorm.DB) *gorm.DB

type Number

type Number interface {
	~int | ~int32 | ~int64 | ~uint | ~uint32 | ~uint64 | ~float32 | ~float64
}

type Option

type Option func(db *gorm.DB) *gorm.DB

func WithOrders

func WithOrders(orders ...Order) Option

type OrFilter

type OrFilter[T any] struct {
	Name   string
	Values []T
}

func (*OrFilter[T]) Where

func (or *OrFilter[T]) Where(db *gorm.DB) *gorm.DB

type Order

type Order interface {
	Order(db *gorm.DB) *gorm.DB
}

func NewOrder

func NewOrder(field string, reversed bool) Order

type OriginalOrFilter

type OriginalOrFilter struct {
	Filters Filters
}

func (*OriginalOrFilter) Where

func (o *OriginalOrFilter) Where(db *gorm.DB) *gorm.DB

type OriginalWhereFilter

type OriginalWhereFilter struct {
	Query string
	Args  []any
}

func (*OriginalWhereFilter) Where

func (w *OriginalWhereFilter) Where(db *gorm.DB) *gorm.DB

type PeriodFilter

type PeriodFilter struct {
	Name  string
	Start time.Time
	End   time.Time
}

func (*PeriodFilter) Where

func (p *PeriodFilter) Where(db *gorm.DB) *gorm.DB

type PrefixFilter

type PrefixFilter struct {
	*LikeFilter
}

func (*PrefixFilter) Where

func (p *PrefixFilter) Where(db *gorm.DB) *gorm.DB

type QueryParam

type QueryParam struct {
	Filters Filters
	Page    *meta.Paging
	Options []Option
}

func NewQueryParam

func NewQueryParam(filters []Filter, page *meta.Paging, opts ...Option) *QueryParam

func (*QueryParam) GetLimit

func (qa *QueryParam) GetLimit() int32

func (*QueryParam) GetOffset

func (qa *QueryParam) GetOffset() int32

func (*QueryParam) Where

func (qa *QueryParam) Where(db *gorm.DB) *gorm.DB

type RangeFilter

type RangeFilter[T Number] struct {
	Name  string
	Start T
	End   T
}

func (RangeFilter[T]) Where

func (r RangeFilter[T]) Where(db *gorm.DB) *gorm.DB

type SuffixFilter

type SuffixFilter struct {
	*LikeFilter
}

func (*SuffixFilter) Where

func (s *SuffixFilter) Where(db *gorm.DB) *gorm.DB

Jump to

Keyboard shortcuts

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