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 ¶
- func Create[T any](ctx context.Context, tx DBHandler, tCreates ...*T) (err error)
- func CreateWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tCreates ...*T) (err error)
- func Delete[T Model](ctx context.Context, tx DBHandler, tDeletes ...*T) (err error)
- func DeleteWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tDeletes ...*T) (err error)
- func Get[T Model](ctx context.Context, tx DBHandler, t *T) (tGet *T, err error)
- func GetByFilters[T Model](ctx context.Context, tx DBHandler, filters Filters, orders ...Order) (tGet *T, err error)
- func GetWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, t *T) (tGet *T, err error)
- func List[T Model](ctx context.Context, tx DBHandler, filters Filters, page *meta.Paging, ...) (tList []*T, err error)
- func NewDBHandler(config *DBConfig) (*gorm.DB, error)
- func Save[T Model](ctx context.Context, tx DBHandler, tSaves ...*T) (err error)
- func SaveWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tSaves ...*T) (err error)
- func UnsafeBytes(s string) []byte
- func Update[T Model](ctx context.Context, tx DBHandler, tUpdates ...*T) (err error)
- func UpdateWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tUpdates ...*T) (err error)
- type CacheHandler
- type DBConfig
- type DBHandler
- type EqFilter
- type FieldOrder
- type Filter
- func NewEqFilter(name string, value any) Filter
- func NewInFilter[T any](name string, values []T) Filter
- func NewJSONArrayContainsFilter(name string, value any) Filter
- func NewLikeFilter(name string, value string) Filter
- func NewNeFilter(name string, value any) Filter
- func NewOrFilter[T any](name string, values []T) Filter
- func NewOriginalOrFilter(filters Filters) Filter
- func NewOriginalWhereFilter(query string, args ...any) Filter
- func NewPeriodFilter(name string, start, end time.Time) Filter
- func NewPrefixFilter(name string, value string) Filter
- func NewRangeFilter[T Number](name string, start, end T) Filter
- func NewSuffixFilter(name string, value string) Filter
- type Filters
- type InFilter
- type JSONArrayContainsFilter
- type LikeFilter
- type Model
- type NeFilter
- type Number
- type Option
- type OrFilter
- type Order
- type OriginalOrFilter
- type OriginalWhereFilter
- type PeriodFilter
- type PrefixFilter
- type QueryParam
- type RangeFilter
- type SuffixFilter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateWithCache ¶
func CreateWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tCreates ...*T) (err error)
CreateWithCache 创建记录并删除缓存
func DeleteWithCache ¶
func DeleteWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tDeletes ...*T) (err error)
DeleteWithCache 删除记录并删除缓存
func GetByFilters ¶
func GetWithCache ¶
func GetWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, t *T) (tGet *T, err error)
GetWithCache 获取记录,优先从缓存读取
func NewDBHandler ¶
NewDBHandler 根据配置创建数据库连接(MySQL/SQLite)
func SaveWithCache ¶
func SaveWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tSaves ...*T) (err error)
SaveWithCache 保存记录并删除缓存
func UpdateWithCache ¶
func UpdateWithCache[T Model](ctx context.Context, tx DBHandler, cache CacheHandler, tUpdates ...*T) (err error)
UpdateWithCache 更新记录并删除缓存
Types ¶
type CacheHandler ¶
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 ¶
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 FieldOrder ¶
type Filter ¶
func NewEqFilter ¶
func NewInFilter ¶
func NewLikeFilter ¶
func NewNeFilter ¶
func NewOrFilter ¶
func NewOriginalOrFilter ¶
func NewOriginalWhereFilter ¶
func NewPrefixFilter ¶
func NewRangeFilter ¶
func NewSuffixFilter ¶
type JSONArrayContainsFilter ¶
type LikeFilter ¶
type Option ¶
func WithOrders ¶
type OriginalOrFilter ¶
type OriginalOrFilter struct {
Filters Filters
}
type OriginalWhereFilter ¶
type PeriodFilter ¶
type PrefixFilter ¶
type PrefixFilter struct {
*LikeFilter
}
type QueryParam ¶
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
type RangeFilter ¶
type SuffixFilter ¶
type SuffixFilter struct {
*LikeFilter
}