repository

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: 10 Imported by: 0

Documentation

Overview

* @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-10 01:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-13 07:45:23 * @FilePath: \go-sqlbuilder\repository\cache_manager.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-10 01:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-11 09:20:54 * @FilePath: \go-sqlbuilder\repository\interfaces.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseRepository

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

BaseRepository 基础仓储实现,包含通用的 CRUD 操作

func NewBaseRepository

func NewBaseRepository[T any](dbHandler db.Handler, table string) *BaseRepository[T]

NewBaseRepository 创建基础仓储

func (*BaseRepository[T]) Count

func (r *BaseRepository[T]) Count(ctx context.Context, filters ...*Filter) (int64, error)

Count 计数

func (*BaseRepository[T]) Create

func (r *BaseRepository[T]) Create(ctx context.Context, entity *T) (*T, error)

Create 创建单个记录

func (*BaseRepository[T]) CreateBatch

func (r *BaseRepository[T]) CreateBatch(ctx context.Context, entities ...*T) error

CreateBatch 批量创建记录

func (*BaseRepository[T]) Delete

func (r *BaseRepository[T]) Delete(ctx context.Context, id interface{}) error

Delete 删除单个记录

func (*BaseRepository[T]) DeleteBatch

func (r *BaseRepository[T]) DeleteBatch(ctx context.Context, ids ...interface{}) error

DeleteBatch 批量删除记录

func (*BaseRepository[T]) DeleteByFilters

func (r *BaseRepository[T]) DeleteByFilters(ctx context.Context, filters ...*Filter) error

DeleteByFilters 按过滤条件删除记录

func (*BaseRepository[T]) Exists

func (r *BaseRepository[T]) Exists(ctx context.Context, filters ...*Filter) (bool, error)

Exists 检查记录是否存在

func (*BaseRepository[T]) Get

func (r *BaseRepository[T]) Get(ctx context.Context, id interface{}) (*T, error)

Get 获取单个记录

func (*BaseRepository[T]) GetByFilter

func (r *BaseRepository[T]) GetByFilter(ctx context.Context, filter *Filter) (*T, error)

GetByFilter 按单个过滤条件获取记录

func (*BaseRepository[T]) GetByFilters

func (r *BaseRepository[T]) GetByFilters(ctx context.Context, filters ...*Filter) (*T, error)

GetByFilters 按多个过滤条件获取记录

func (*BaseRepository[T]) List

func (r *BaseRepository[T]) List(ctx context.Context, query *Query) ([]*T, error)

List 列表查询

func (*BaseRepository[T]) ListWithPagination

func (r *BaseRepository[T]) ListWithPagination(ctx context.Context, query *Query, page *meta.Paging) ([]*T, *meta.Paging, error)

ListWithPagination 分页列表查询

func (*BaseRepository[T]) Transaction

func (r *BaseRepository[T]) Transaction(ctx context.Context, fn func(tx Transaction) error) error

Transaction 事务支持

func (*BaseRepository[T]) Update

func (r *BaseRepository[T]) Update(ctx context.Context, entity *T) (*T, error)

Update 更新单个记录

func (*BaseRepository[T]) UpdateBatch

func (r *BaseRepository[T]) UpdateBatch(ctx context.Context, entities ...*T) error

UpdateBatch 批量更新记录

func (*BaseRepository[T]) UpdateByFilters

func (r *BaseRepository[T]) UpdateByFilters(ctx context.Context, entity *T, filters ...*Filter) error

UpdateByFilters 按过滤条件更新记录

type CacheConfig

type CacheConfig struct {
	Enabled bool
	TTL     time.Duration
}

CacheConfig 缓存配置

type CacheManager

type CacheManager interface {
	// 获取
	Get(ctx context.Context, key string) (interface{}, error)

	// 设置
	Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

	// 删除
	Delete(ctx context.Context, keys ...string) error

	// 删除匹配的键
	DeletePattern(ctx context.Context, pattern string) error

	// 检查存在
	Exists(ctx context.Context, key string) (bool, error)

	// 获取 TTL
	TTL(ctx context.Context, key string) (time.Duration, error)

	// 批量操作
	BatchGet(ctx context.Context, keys ...string) (map[string]interface{}, error)
	BatchSet(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
	BatchDelete(ctx context.Context, keys ...string) error
}

CacheManager 缓存管理接口

func NewCacheManager

func NewCacheManager(handler cache.Handler) CacheManager

NewCacheManager 创建缓存管理器

type CacheManagerImpl

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

CacheManagerImpl 缓存管理器实现

func (*CacheManagerImpl) BatchDelete

func (m *CacheManagerImpl) BatchDelete(ctx context.Context, keys ...string) error

BatchDelete 批量删除缓存键

func (*CacheManagerImpl) BatchGet

func (m *CacheManagerImpl) BatchGet(ctx context.Context, keys ...string) (map[string]interface{}, error)

BatchGet 批量获取缓存值

func (*CacheManagerImpl) BatchSet

func (m *CacheManagerImpl) BatchSet(ctx context.Context, items map[string]interface{}, ttl time.Duration) error

BatchSet 批量设置缓存值

func (*CacheManagerImpl) Delete

func (m *CacheManagerImpl) Delete(ctx context.Context, keys ...string) error

Delete 删除单个或多个缓存键

func (*CacheManagerImpl) DeletePattern

func (m *CacheManagerImpl) DeletePattern(ctx context.Context, pattern string) error

DeletePattern 删除匹配模式的所有键(仅适用于支持的后端)

func (*CacheManagerImpl) Exists

func (m *CacheManagerImpl) Exists(ctx context.Context, key string) (bool, error)

Exists 检查缓存键是否存在

func (*CacheManagerImpl) Get

func (m *CacheManagerImpl) Get(ctx context.Context, key string) (interface{}, error)

Get 获取缓存值

func (*CacheManagerImpl) Set

func (m *CacheManagerImpl) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

Set 设置缓存值

func (*CacheManagerImpl) TTL

TTL 获取键的剩余 TTL

type Filter

type Filter struct {
	Field    string
	Operator string // "=", ">", "<", ">=", "<=", "!=", "in", "like", "between"
	Value    interface{}
}

Filter 数据库查询过滤条件

func NewBetweenFilter

func NewBetweenFilter(field string, min, max interface{}) *Filter

NewBetweenFilter 创建 BETWEEN 过滤条件

func NewEqFilter

func NewEqFilter(field string, value interface{}) *Filter

NewEqFilter 创建等于过滤条件

func NewGtFilter

func NewGtFilter(field string, value interface{}) *Filter

NewGtFilter 创建大于过滤条件

func NewGteFilter

func NewGteFilter(field string, value interface{}) *Filter

NewGteFilter 创建大于等于过滤条件

func NewInFilter

func NewInFilter(field string, values ...interface{}) *Filter

NewInFilter 创建 IN 过滤条件

func NewLikeFilter

func NewLikeFilter(field string, value string) *Filter

NewLikeFilter 创建 LIKE 过滤条件

func NewLtFilter

func NewLtFilter(field string, value interface{}) *Filter

NewLtFilter 创建小于过滤条件

func NewLteFilter

func NewLteFilter(field string, value interface{}) *Filter

NewLteFilter 创建小于等于过滤条件

func NewNeqFilter

func NewNeqFilter(field string, value interface{}) *Filter

NewNeqFilter 创建不等于过滤条件

type Order

type Order struct {
	Field     string
	Direction string // "ASC" or "DESC"
}

Order 排序条件

type Query

type Query struct {
	Filters    []*Filter
	Orders     []Order
	Pagination *meta.Paging
}

Query 查询条件

func NewQuery

func NewQuery() *Query

NewQuery 创建查询条件

func (*Query) AddFilter

func (q *Query) AddFilter(filter *Filter) *Query

AddFilter 添加过滤条件

func (*Query) AddFilters

func (q *Query) AddFilters(filters ...*Filter) *Query

AddFilters 批量添加过滤条件

func (*Query) AddOrder

func (q *Query) AddOrder(field, direction string) *Query

AddOrder 添加排序条件

func (*Query) WithPaging

func (q *Query) WithPaging(page, pageSize int) *Query

WithPaging 设置分页条件

type Repository

type Repository[T any] interface {
	// 创建
	Create(ctx context.Context, entity *T) (*T, error)
	CreateBatch(ctx context.Context, entities ...*T) error

	// 读取
	Get(ctx context.Context, id interface{}) (*T, error)
	GetByFilter(ctx context.Context, filter *Filter) (*T, error)
	GetByFilters(ctx context.Context, filters ...*Filter) (*T, error)
	List(ctx context.Context, query *Query) ([]*T, error)
	ListWithPagination(ctx context.Context, query *Query, page *meta.Paging) ([]*T, *meta.Paging, error)

	// 更新
	Update(ctx context.Context, entity *T) (*T, error)
	UpdateBatch(ctx context.Context, entities ...*T) error
	UpdateByFilters(ctx context.Context, entity *T, filters ...*Filter) error

	// 删除
	Delete(ctx context.Context, id interface{}) error
	DeleteBatch(ctx context.Context, ids ...interface{}) error
	DeleteByFilters(ctx context.Context, filters ...*Filter) error

	// 事务
	Transaction(ctx context.Context, fn func(tx Transaction) error) error

	// 工具方法
	Count(ctx context.Context, filters ...*Filter) (int64, error)
	Exists(ctx context.Context, filters ...*Filter) (bool, error)
}

Repository 通用仓储接口

type RepositoryFactory

type RepositoryFactory interface {
	// 缓存管理
	CacheManager() CacheManager

	// 关闭工厂
	Close() error
}

RepositoryFactory 仓储工厂接口

type Transaction

type Transaction interface {
	// 执行事务内的操作
	Create(ctx context.Context, entity interface{}) error
	CreateBatch(ctx context.Context, entities ...interface{}) error
	Update(ctx context.Context, entity interface{}) error
	UpdateBatch(ctx context.Context, entities ...interface{}) error
	Delete(ctx context.Context, entity interface{}) error
	DeleteBatch(ctx context.Context, entities ...interface{}) error
}

Transaction 事务接口

Jump to

Keyboard shortcuts

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