Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 21:13:15 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-12-04 09:15:32 * @FilePath: \go-sqlbuilder\repository\base.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-23 08:15:15 * @FilePath: \go-sqlbuilder\repository\filter.go * @Description: 过滤器和查询构建 - Filter、FilterGroup、Query等查询条件 * * 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-23 22:55:38 * @FilePath: \go-sqlbuilder\repository\helpers.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-23 00:00:00 * @FilePath: \go-sqlbuilder\repository\interfaces.go * @Description: 核心接口定义 - Repository、Transaction、Query 等 * * 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-23 22:50:00 * @FilePath: \go-sqlbuilder\model.go * @Description: 基础模型定义 - BaseModel、AuditModel、UUIDModel 等 * * 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-25 08:06:51 * @FilePath: \go-sqlbuilder\repository\paging.go * @Description: 分页工具 - Pagination分页元数据和辅助方法 * * 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-23 13:29:12 * @FilePath: \go-sqlbuilder\repository\query.go * @Description: 类型定义 - Filter、Pagination、QueryCondition 等 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
Index ¶
- Constants
- Variables
- func ApplyFieldSelection(db *gorm.DB, selectFields, omitFields, modelFields []string, autoFields bool) *gorm.DB
- func ApplyFilter(dbQuery *gorm.DB, filter *Filter) *gorm.DB
- func ApplyFilters(db *gorm.DB, filters []*Filter) *gorm.DB
- func ApplyOrdering(db *gorm.DB, orders []Order, defaultOrder string) *gorm.DB
- func ApplyOrders(db *gorm.DB, orders []Order) *gorm.DB
- func ApplyPreloads(db *gorm.DB, preloads []string) *gorm.DB
- func BuildSelectClause(tableName string, fields []string) string
- func ConcurrentSimpleQuery[T any](e *ConcurrentQueryExecutor, ctx context.Context, ...) (map[string]T, bool)
- func ConvertToInterfaceSlice(value interface{}) []interface{}
- func FilterFields(allFields, selectFields, omitFields []string) []string
- func GetDeleted[T any](ctx context.Context, db *gorm.DB, query *Query) ([]*T, error)
- func GetNonDeleted[T any](ctx context.Context, db *gorm.DB, query *Query) ([]*T, error)
- func GetStructFields(model interface{}) []string
- func GetTodayRange() (startTime, endTime time.Time)
- func IsSliceType(value interface{}) bool
- func IsTimeInTodayRange(t *time.Time, todayStart, todayEnd time.Time) bool
- func IsToday(t *time.Time) bool
- func IsTodayFromProto(t *timestamppb.Timestamp) bool
- func IsTodayRange(startTime, endTime *time.Time) bool
- func PermanentlyDelete[T any](ctx context.Context, db *gorm.DB, id interface{}) error
- func PermanentlyDeleteBatch[T any](ctx context.Context, db *gorm.DB, ids []interface{}) error
- func RestoreDeleted[T any](ctx context.Context, db *gorm.DB, id interface{}) error
- func RestoreDeletedBatch[T any](ctx context.Context, db *gorm.DB, ids []interface{}) error
- type AggregateField
- type AggregateOperation
- type AuditModel
- type AuditableModel
- type BaseModel
- func (m *BaseModel) BeforeUpdate(tx *gorm.DB) error
- func (m *BaseModel) Disable()
- func (m *BaseModel) Enable()
- func (m *BaseModel) GetID() uint
- func (m *BaseModel) GetVersion() int
- func (m *BaseModel) IsDeleted() bool
- func (m *BaseModel) IsEnabled() bool
- func (m *BaseModel) IsNew() bool
- func (m *BaseModel) SetCreatedAt(t time.Time)
- func (m *BaseModel) SetRemark(remark string)
- func (m *BaseModel) SetUpdatedAt(t time.Time)
- type BaseRepository
- func (r *BaseRepository[T]) BulkCreate(ctx context.Context, entities []*T, batchSize ...int) error
- func (r *BaseRepository[T]) ConcurrentQuery(ctx context.Context, ...) (map[string]int64, bool)
- func (r *BaseRepository[T]) Count(ctx context.Context, filters ...*Filter) (int64, error)
- func (r *BaseRepository[T]) CountByField(ctx context.Context, field string) (map[interface{}]int64, error)
- func (r *BaseRepository[T]) Create(ctx context.Context, entity *T) (*T, error)
- func (r *BaseRepository[T]) CreateBatch(ctx context.Context, entities ...*T) error
- func (r *BaseRepository[T]) CreateIfNotExists(ctx context.Context, entity *T, uniqueFields ...string) (*T, bool, error)
- func (r *BaseRepository[T]) CreateOrUpdate(ctx context.Context, entity *T, uniqueFields ...string) (*T, bool, error)
- func (r *BaseRepository[T]) DBHandler() db.Handler
- func (r *BaseRepository[T]) Delete(ctx context.Context, id interface{}) error
- func (r *BaseRepository[T]) DeleteBatch(ctx context.Context, ids ...interface{}) error
- func (r *BaseRepository[T]) DeleteByFilters(ctx context.Context, filters ...*Filter) error
- func (r *BaseRepository[T]) DisableAutoFields()
- func (r *BaseRepository[T]) Distinct(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error)
- func (r *BaseRepository[T]) EnableAutoFields()
- func (r *BaseRepository[T]) ExecuteConcurrentQueries(ctx context.Context, tasks []ConcurrentQueryTask[int64], ...) ([]ConcurrentQueryResult[int64], bool)
- func (r *BaseRepository[T]) Exists(ctx context.Context, filters ...*Filter) (bool, error)
- func (r *BaseRepository[T]) Find(ctx context.Context, options *FindOptions) ([]*T, error)
- func (r *BaseRepository[T]) FindOne(ctx context.Context, filters ...*Filter) (*T, error)
- func (r *BaseRepository[T]) First(ctx context.Context, filters ...*Filter) (*T, error)
- func (r *BaseRepository[T]) Get(ctx context.Context, id interface{}) (*T, error)
- func (r *BaseRepository[T]) GetAll(ctx context.Context) ([]*T, error)
- func (r *BaseRepository[T]) GetByFields(ctx context.Context, fields map[string]interface{}) (*T, error)
- func (r *BaseRepository[T]) GetByFilter(ctx context.Context, filter *Filter) (*T, error)
- func (r *BaseRepository[T]) GetByFilters(ctx context.Context, filters ...*Filter) (*T, error)
- func (r *BaseRepository[T]) GetModelFields() []string
- func (r *BaseRepository[T]) GetWithPreloads(ctx context.Context, id interface{}, preloads ...string) (*T, error)
- func (r *BaseRepository[T]) IsAutoFieldsEnabled() bool
- func (r *BaseRepository[T]) Last(ctx context.Context, filters ...*Filter) (*T, error)
- func (r *BaseRepository[T]) List(ctx context.Context, query *Query) ([]*T, error)
- func (r *BaseRepository[T]) ListWithPagination(ctx context.Context, query *Query, page *Pagination) ([]*T, *Pagination, error)
- func (r *BaseRepository[T]) ListWithPreloads(ctx context.Context, query *Query, preloads ...string) ([]*T, error)
- func (r *BaseRepository[T]) Pluck(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error)
- func (r *BaseRepository[T]) Restore(ctx context.Context, id interface{}, field string, restoreValue interface{}) error
- func (r *BaseRepository[T]) RestoreBatch(ctx context.Context, ids []interface{}, field string, restoreValue interface{}) error
- func (r *BaseRepository[T]) SoftDelete(ctx context.Context, id interface{}, field string, value interface{}) error
- func (r *BaseRepository[T]) SoftDeleteBatch(ctx context.Context, ids []interface{}, field string, value interface{}) error
- func (r *BaseRepository[T]) SoftDeleteByFilters(ctx context.Context, field string, value interface{}, filters ...*Filter) error
- func (r *BaseRepository[T]) Table() string
- func (r *BaseRepository[T]) Transaction(ctx context.Context, fn func(tx Transaction[T]) error) error
- func (r *BaseRepository[T]) TransactionWithRawDB(ctx context.Context, fn func(tx *gorm.DB) error) error
- func (r *BaseRepository[T]) Update(ctx context.Context, entity *T) (*T, error)
- func (r *BaseRepository[T]) UpdateBatch(ctx context.Context, entities ...*T) error
- func (r *BaseRepository[T]) UpdateByFilters(ctx context.Context, entity *T, filters ...*Filter) error
- func (r *BaseRepository[T]) UpdateFields(ctx context.Context, id interface{}, fields map[string]interface{}) error
- func (r *BaseRepository[T]) UpdateFieldsByFilters(ctx context.Context, fields map[string]interface{}, filters ...*Filter) error
- type ComparedColumn
- type ComparedValue
- type ConcurrentQueryBuilder
- func (b *ConcurrentQueryBuilder) Build() func(ctx context.Context) (int64, error)
- func (b *ConcurrentQueryBuilder) BuildWithTimeRange(startTime, endTime time.Time) func(ctx context.Context) (int64, error)
- func (b *ConcurrentQueryBuilder) Where(condition string, args ...interface{}) *ConcurrentQueryBuilder
- type ConcurrentQueryExecutor
- type ConcurrentQueryOption
- type ConcurrentQueryResult
- type ConcurrentQueryTask
- type Condition
- type ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) AvgWhen(condition, field string, alias string, args ...interface{}) *ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) Build() (*gorm.DB, []interface{})
- func (b *ConditionalAggregateBuilder) CountWhen(condition string, alias string, args ...interface{}) *ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) Execute(ctx context.Context) (map[string]interface{}, error)
- func (b *ConditionalAggregateBuilder) ExecuteInto(ctx context.Context, dest interface{}) error
- func (b *ConditionalAggregateBuilder) ExecuteList(ctx context.Context) ([]map[string]interface{}, error)
- func (b *ConditionalAggregateBuilder) GroupBy(fields ...string) *ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) Having(condition string, args ...interface{}) *ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) Limit(limit int) *ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) MaxWhen(condition, field string, alias string, args ...interface{}) *ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) MinWhen(condition, field string, alias string, args ...interface{}) *ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) OrderBy(order string) *ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) SumWhen(condition string, alias string, args ...interface{}) *ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) WithTimeField(field string) *ConditionalAggregateBuilder
- func (b *ConditionalAggregateBuilder) WithTimeRange(start, end time.Time) *ConditionalAggregateBuilder
- type ContextFieldExtractor
- type Dialect
- type EnhancedRepository
- func (r *EnhancedRepository[T]) BatchUpdateByField(ctx context.Context, field string, values []interface{}, ...) error
- func (r *EnhancedRepository[T]) CountByField(ctx context.Context, field string, value interface{}) (int64, error)
- func (r *EnhancedRepository[T]) CreateIfNotExists(ctx context.Context, entity *T, checkField string) (*T, bool, error)
- func (r *EnhancedRepository[T]) DecrementField(ctx context.Context, whereField string, whereValue interface{}, ...) error
- func (r *EnhancedRepository[T]) DeleteByField(ctx context.Context, field string, value interface{}) error
- func (r *EnhancedRepository[T]) ExistsBy(ctx context.Context, field string, value interface{}) (bool, error)
- func (r *EnhancedRepository[T]) FindByField(ctx context.Context, field string, value interface{}) ([]*T, error)
- func (r *EnhancedRepository[T]) FindByFieldWithCursor(ctx context.Context, field string, value interface{}, cursorField string, ...) ([]*T, bool, error)
- func (r *EnhancedRepository[T]) FindByFieldWithPagination(ctx context.Context, field string, value interface{}, limit, offset int) ([]*T, int64, error)
- func (r *EnhancedRepository[T]) FindByFields(ctx context.Context, conditions map[string]interface{}) ([]*T, error)
- func (r *EnhancedRepository[T]) FindByTimeRange(ctx context.Context, timeField string, startTime, endTime interface{}) ([]*T, error)
- func (r *EnhancedRepository[T]) FindInField(ctx context.Context, field string, values []interface{}) ([]*T, error)
- func (r *EnhancedRepository[T]) FindOneByField(ctx context.Context, field string, value interface{}) (*T, error)
- func (r *EnhancedRepository[T]) FindWithOrder(ctx context.Context, whereField string, whereValue interface{}, ...) ([]*T, error)
- func (r *EnhancedRepository[T]) GetDistinctValues(ctx context.Context, field string) ([]interface{}, error)
- func (r *EnhancedRepository[T]) IncrementField(ctx context.Context, whereField string, whereValue interface{}, ...) error
- func (r *EnhancedRepository[T]) UpdateByField(ctx context.Context, field string, value interface{}, ...) error
- func (r *EnhancedRepository[T]) UpdateSingleField(ctx context.Context, whereField string, whereValue interface{}, ...) error
- type Filter
- func NewBetweenFilter(field string, min, max interface{}) *Filter
- func NewContainsFilter(field string, value string) *Filter
- func NewEndsWithFilter(field string, value string) *Filter
- func NewEqFilter(field string, value interface{}) *Filter
- func NewFindInSetFilter(field string, value interface{}) *Filter
- func NewGtFilter(field string, value interface{}) *Filter
- func NewGteFilter(field string, value interface{}) *Filter
- func NewInFilter(field string, values ...interface{}) *Filter
- func NewInFilterSlice(field string, values []interface{}) *Filter
- func NewIsNotNullFilter(field string) *Filter
- func NewIsNullFilter(field string) *Filter
- func NewLikeFilter(field string, value string) *Filter
- func NewLtFilter(field string, value interface{}) *Filter
- func NewLteFilter(field string, value interface{}) *Filter
- func NewNeqFilter(field string, value interface{}) *Filter
- func NewNotInFilter(field string, values ...interface{}) *Filter
- func NewNotInFilterSlice(field string, values []interface{}) *Filter
- func NewNotLikeFilter(field string, value string) *Filter
- func NewStartsWithFilter(field string, value string) *Filter
- type FilterGroup
- func (fg *FilterGroup) AddBetweenFilterIfNotEmpty(field string, min, max interface{}) *FilterGroup
- func (fg *FilterGroup) AddContainsFilterIfNotEmpty(field string, value string) *FilterGroup
- func (fg *FilterGroup) AddEndsWithFilterIfNotEmpty(field string, value string) *FilterGroup
- func (fg *FilterGroup) AddEqFilterIfNotEmpty(field string, value interface{}) *FilterGroup
- func (fg *FilterGroup) AddFilter(filter *Filter) *FilterGroup
- func (fg *FilterGroup) AddFilterIf(condition bool, filter *Filter) *FilterGroup
- func (fg *FilterGroup) AddFilterIfNotEmpty(field string, operator constants.Operator, value interface{}) *FilterGroup
- func (fg *FilterGroup) AddFilters(filters ...*Filter) *FilterGroup
- func (fg *FilterGroup) AddFindInSetFilterIfNotEmpty(field string, value interface{}) *FilterGroup
- func (fg *FilterGroup) AddGroup(group *FilterGroup) *FilterGroup
- func (fg *FilterGroup) AddGroupIf(condition bool, group *FilterGroup) *FilterGroup
- func (fg *FilterGroup) AddGroupIfNotEmpty(group *FilterGroup) *FilterGroup
- func (fg *FilterGroup) AddGtFilterIfNotEmpty(field string, value interface{}) *FilterGroup
- func (fg *FilterGroup) AddGteFilterIfNotEmpty(field string, value interface{}) *FilterGroup
- func (fg *FilterGroup) AddInFilterIfNotEmpty(field string, values []interface{}) *FilterGroup
- func (fg *FilterGroup) AddLikeFilterIfNotEmpty(field string, value string) *FilterGroup
- func (fg *FilterGroup) AddLtFilterIfNotEmpty(field string, value interface{}) *FilterGroup
- func (fg *FilterGroup) AddLteFilterIfNotEmpty(field string, value interface{}) *FilterGroup
- func (fg *FilterGroup) AddNeqFilterIfNotEmpty(field string, value interface{}) *FilterGroup
- func (fg *FilterGroup) AddNotInFilterIfNotEmpty(field string, values []interface{}) *FilterGroup
- func (fg *FilterGroup) AddNotLikeFilterIfNotEmpty(field string, value string) *FilterGroup
- func (fg *FilterGroup) AddStartsWithFilterIfNotEmpty(field string, value string) *FilterGroup
- func (fg *FilterGroup) Clear() *FilterGroup
- func (fg *FilterGroup) Clone() *FilterGroup
- func (fg *FilterGroup) Count() int
- func (fg *FilterGroup) IsEmpty() bool
- type FindOptions
- type FullFeaturedModel
- type LightModel
- type ModelInterface
- type MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) AddCondition(tableName, condition string, args ...interface{}) *MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) Avg(tableName, field, alias string) *MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) Count(tableName, alias string) *MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) CountDistinct(tableName, field, alias string) *MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) Execute() (map[string]int64, error)
- func (b *MultiTableStatsBuilder) ExecuteWithDetails() ([]ConcurrentQueryResult[int64], bool, error)
- func (b *MultiTableStatsBuilder) Max(tableName, field, alias string) *MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) Min(tableName, field, alias string) *MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) Sum(tableName, field, alias string) *MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) WithTimeField(field string) *MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) WithTimeRange(start, end time.Time) *MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) WithTimeout(timeout time.Duration) *MultiTableStatsBuilder
- func (b *MultiTableStatsBuilder) WithWorkers(workers int) *MultiTableStatsBuilder
- type MySQLDialect
- type Order
- type OrderBy
- type Pagination
- type PostgreSQLDialect
- type Query
- func (q *Query) AddBetween(field string, start, end interface{}) *Query
- func (q *Query) AddBetweenFilterIfNotEmpty(field string, min, max interface{}) *Query
- func (q *Query) AddContainsFilterIfNotEmpty(field, value string) *Query
- func (q *Query) AddEndsWith(field, suffix string) *Query
- func (q *Query) AddEndsWithFilterIfNotEmpty(field, value string) *Query
- func (q *Query) AddEqFilterIfNotEmpty(field string, value interface{}) *Query
- func (q *Query) AddEqual(field string, value interface{}) *Query
- func (q *Query) AddFilter(filter *Filter) *Query
- func (q *Query) AddFilterIfNotEmpty(field string, value interface{}) *Query
- func (q *Query) AddFilters(filters ...*Filter) *Query
- func (q *Query) AddFindInSetFilterIfNotEmpty(field string, value interface{}) *Query
- func (q *Query) AddGreaterEqual(field string, value interface{}) *Query
- func (q *Query) AddGreaterThan(field string, value interface{}) *Query
- func (q *Query) AddGroupBy(fields ...string) *Query
- func (q *Query) AddGtFilterIfNotEmpty(field string, value interface{}) *Query
- func (q *Query) AddGteFilterIfNotEmpty(field string, value interface{}) *Query
- func (q *Query) AddHaving(filter *Filter) *Query
- func (q *Query) AddIn(field string, values ...interface{}) *Query
- func (q *Query) AddInFilterIfNotEmpty(field string, values interface{}) *Query
- func (q *Query) AddIsNotNull(field string) *Query
- func (q *Query) AddIsNull(field string) *Query
- func (q *Query) AddLessEqual(field string, value interface{}) *Query
- func (q *Query) AddLessThan(field string, value interface{}) *Query
- func (q *Query) AddLike(field, keyword string) *Query
- func (q *Query) AddLikeFilterIfNotEmpty(field, keyword string) *Query
- func (q *Query) AddLtFilterIfNotEmpty(field string, value interface{}) *Query
- func (q *Query) AddLteFilterIfNotEmpty(field string, value interface{}) *Query
- func (q *Query) AddNeqFilterIfNotEmpty(field string, value interface{}) *Query
- func (q *Query) AddNotEqual(field string, value interface{}) *Query
- func (q *Query) AddNotIn(field string, values ...interface{}) *Query
- func (q *Query) AddNotInFilterIfNotEmpty(field string, values interface{}) *Query
- func (q *Query) AddNotLikeFilterIfNotEmpty(field, value string) *Query
- func (q *Query) AddOrder(field, direction string) *Query
- func (q *Query) AddOrderAsc(field string) *Query
- func (q *Query) AddOrderDesc(field string) *Query
- func (q *Query) AddRawOrder(orderExpr string) *Query
- func (q *Query) AddSafeOrder(sortBy, sortOrder, defaultField, defaultDirection string, ...) *Query
- func (q *Query) AddStartsWith(field, prefix string) *Query
- func (q *Query) AddStartsWithFilterIfNotEmpty(field, value string) *Query
- func (q *Query) AddThisMonth(field string) *Query
- func (q *Query) AddThisWeek(field string) *Query
- func (q *Query) AddThisYear(field string) *Query
- func (q *Query) AddTimeAfter(field string, t time.Time) *Query
- func (q *Query) AddTimeBefore(field string, t time.Time) *Query
- func (q *Query) AddTimeBetween(field string, start, end time.Time) *Query
- func (q *Query) AddTimeRangeFilter(field string, startTime, endTime interface{}) *Query
- func (q *Query) AddToday(field string) *Query
- func (q *Query) BuildWhereClause() (string, []interface{})
- func (q *Query) GetAllFilters() []*Filter
- func (q *Query) HasFilters() bool
- func (q *Query) Limit(limit int) *Query
- func (q *Query) Offset(offset int) *Query
- func (q *Query) Omit(fields ...string) *Query
- func (q *Query) OmitLargeFields() *Query
- func (q *Query) OmitSensitive() *Query
- func (q *Query) Page(page, pageSize int) *Query
- func (q *Query) Select(fields ...string) *Query
- func (q *Query) SelectOnly(field string) *Query
- func (q *Query) SetDistinct() *Query
- func (q *Query) SetPagination(page, pageSize int) *Query
- func (q *Query) Skip(offset int) *Query
- func (q *Query) Take(limit int) *Query
- func (q *Query) WithDistinct(distinct bool) *Query
- func (q *Query) WithFilterGroup(group *FilterGroup) *Query
- func (q *Query) WithPaging(page, pageSize int) *Query
- type QueryCondition
- type RemarkableModel
- type Repository
- type RepositoryFactory
- type RepositoryOption
- func WithAutoFields[T any]() RepositoryOption[T]
- func WithBatchSize[T any](size int) RepositoryOption[T]
- func WithDefaultOrder[T any](order string) RepositoryOption[T]
- func WithDefaultPreloads[T any](preloads ...string) RepositoryOption[T]
- func WithLogger[T any](log logger.ILogger) RepositoryOption[T]
- func WithReadOnly[T any]() RepositoryOption[T]
- func WithTimeout[T any](seconds int) RepositoryOption[T]
- type RepositoryWithSoftDelete
- func (r *RepositoryWithSoftDelete[T]) ListDeleted(ctx context.Context, query *Query) ([]*T, error)
- func (r *RepositoryWithSoftDelete[T]) ListDeletedByIsDeleted(ctx context.Context, query *Query) ([]*T, error)
- func (r *RepositoryWithSoftDelete[T]) ListNotDeleted(ctx context.Context, query *Query) ([]*T, error)
- func (r *RepositoryWithSoftDelete[T]) ListNotDeletedByIsDeleted(ctx context.Context, query *Query) ([]*T, error)
- func (r *RepositoryWithSoftDelete[T]) RestoreBatchWithDeletedAt(ctx context.Context, ids []interface{}) error
- func (r *RepositoryWithSoftDelete[T]) RestoreBatchWithIsDeleted(ctx context.Context, ids []interface{}) error
- func (r *RepositoryWithSoftDelete[T]) RestoreWithDeletedAt(ctx context.Context, id interface{}) error
- func (r *RepositoryWithSoftDelete[T]) RestoreWithIsDeleted(ctx context.Context, id interface{}) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteBatchWithDeletedAt(ctx context.Context, ids []interface{}) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteBatchWithIsDeleted(ctx context.Context, ids []interface{}) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithDeletedAt(ctx context.Context, filters ...*Filter) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithIsDeleted(ctx context.Context, filters ...*Filter) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteWithDeletedAt(ctx context.Context, id interface{}) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteWithIsDeleted(ctx context.Context, id interface{}) error
- type SQLiteDialect
- type SimpleModel
- type SoftDeletableModel
- type SoftDeleteHelper
- type StatusModel
- type SubQuery
- type TimeEntityCursor
- func NewTimeEntityCursor(timestamp *time.Time, entityID string) *TimeEntityCursor
- func NewTimeEntityCursorFromTime(t time.Time, entityID string, format ...string) *TimeEntityCursor
- func ParseTimeEntityCursor(cursorStr string) *TimeEntityCursor
- func ParseTimeEntityCursorFromJSON(jsonStr string) (*TimeEntityCursor, error)
- func (c *TimeEntityCursor) Clone() *TimeEntityCursor
- func (c *TimeEntityCursor) Compare(other *TimeEntityCursor) (int, error)
- func (c *TimeEntityCursor) Equal(other *TimeEntityCursor) bool
- func (c *TimeEntityCursor) GetTime() (*time.Time, error)
- func (c *TimeEntityCursor) IsEmpty() bool
- func (c *TimeEntityCursor) IsValid() error
- func (c *TimeEntityCursor) String() string
- func (c *TimeEntityCursor) ToBase64() string
- func (c *TimeEntityCursor) ToJSON() (string, error)
- type TimeEntityCursorBuilder
- func (b *TimeEntityCursorBuilder) Build() (*TimeEntityCursor, error)
- func (b *TimeEntityCursorBuilder) MustBuild() *TimeEntityCursor
- func (b *TimeEntityCursorBuilder) WithEntityID(entityID string) *TimeEntityCursorBuilder
- func (b *TimeEntityCursorBuilder) WithTime(t *time.Time) *TimeEntityCursorBuilder
- func (b *TimeEntityCursorBuilder) WithTimestamp(timestamp string) *TimeEntityCursorBuilder
- type TimeGroupBuilder
- func (b *TimeGroupBuilder) AddGroupBy(fields ...string) *TimeGroupBuilder
- func (b *TimeGroupBuilder) Avg(field, alias string) *TimeGroupBuilder
- func (b *TimeGroupBuilder) Build() (*gorm.DB, []interface{})
- func (b *TimeGroupBuilder) Count(alias string) *TimeGroupBuilder
- func (b *TimeGroupBuilder) CountDistinct(field, alias string) *TimeGroupBuilder
- func (b *TimeGroupBuilder) CountWhen(condition, alias string, args ...interface{}) *TimeGroupBuilder
- func (b *TimeGroupBuilder) Execute(ctx context.Context) ([]map[string]interface{}, error)
- func (b *TimeGroupBuilder) ExecuteInto(ctx context.Context, dest interface{}) error
- func (b *TimeGroupBuilder) Having(condition string, args ...interface{}) *TimeGroupBuilder
- func (b *TimeGroupBuilder) Limit(limit int) *TimeGroupBuilder
- func (b *TimeGroupBuilder) Max(field, alias string) *TimeGroupBuilder
- func (b *TimeGroupBuilder) Min(field, alias string) *TimeGroupBuilder
- func (b *TimeGroupBuilder) OrderBy(order string) *TimeGroupBuilder
- func (b *TimeGroupBuilder) Sum(field, alias string) *TimeGroupBuilder
- func (b *TimeGroupBuilder) SumWhen(condition, alias string, args ...interface{}) *TimeGroupBuilder
- func (b *TimeGroupBuilder) Where(condition string, args ...interface{}) *TimeGroupBuilder
- func (b *TimeGroupBuilder) WithTimeField(field string) *TimeGroupBuilder
- func (b *TimeGroupBuilder) WithTimeRange(start, end time.Time) *TimeGroupBuilder
- type TimeGroupType
- type TimestampModel
- type Transaction
- type UUIDModel
- type VersionedModel
Constants ¶
const ( // CursorSeparator 游标字段分隔符 CursorSeparator = "|" // TimeFormat 默认时间格式 TimeFormat = time.RFC3339Nano )
Variables ¶
Functions ¶
func ApplyFieldSelection ¶ added in v0.3.4
func ApplyFieldSelection(db *gorm.DB, selectFields, omitFields, modelFields []string, autoFields bool) *gorm.DB
ApplyFieldSelection 应用字段选择 selectFields: 要选择的字段列表 omitFields: 要排除的字段列表 modelFields: 模型的所有字段(用于自动字段模式) autoFields: 是否启用自动字段模式
func ApplyFilter ¶ added in v0.3.4
ApplyFilter 应用单个过滤条件到 GORM 查询
func ApplyFilters ¶ added in v0.3.4
ApplyFilters 批量应用过滤器到查询
func ApplyOrdering ¶ added in v0.3.4
ApplyOrdering 应用排序条件 orders: 排序条件列表 defaultOrder: 默认排序(当orders为空时使用),可为空字符串
func ApplyOrders ¶ added in v0.3.4
ApplyOrders 批量应用排序条件到查询
func ApplyPreloads ¶ added in v0.3.4
ApplyPreloads 应用预加载关联
func BuildSelectClause ¶ added in v0.3.4
BuildSelectClause 构建 SELECT 子句
func ConcurrentSimpleQuery ¶ added in v0.3.4
func ConcurrentSimpleQuery[T any]( e *ConcurrentQueryExecutor, ctx context.Context, queries map[string]func(ctx context.Context) (T, error), ) (map[string]T, bool)
ConcurrentSimpleQuery 简化的并发查询接口(不需要创建 QueryTask) queries: map[任务名称]查询函数 返回: map[任务名称]查询结果, 是否有错误
func ConvertToInterfaceSlice ¶ added in v0.3.1
func ConvertToInterfaceSlice(value interface{}) []interface{}
ConvertToInterfaceSlice 将任意切片类型转换为 []interface{}
func FilterFields ¶ added in v0.3.4
FilterFields 根据 select 和 omit 规则过滤字段列表
func GetDeleted ¶ added in v0.1.7
GetDeleted 获取已删除的记录(使用deleted_at字段)
func GetNonDeleted ¶ added in v0.1.7
GetNonDeleted 获取未删除的记录(使用deleted_at字段)
func GetStructFields ¶ added in v0.3.4
func GetStructFields(model interface{}) []string
GetStructFields 获取结构体的所有字段名(基于 gorm tag 或 json tag) 返回数据库字段名列表
func GetTodayRange ¶ added in v0.3.1
GetTodayRange 获取今天的时间范围(00:00:00 - 23:59:59)
func IsTimeInTodayRange ¶ added in v0.3.5
IsTimeInTodayRange 判断时间是否在今天范围内
func IsTodayFromProto ¶ added in v0.3.1
func IsTodayFromProto(t *timestamppb.Timestamp) bool
IsTodayFromProto 判断 protobuf Timestamp 是否为今天
func IsTodayRange ¶ added in v0.3.1
IsTodayRange 判断时间范围是否包含今天
func PermanentlyDelete ¶ added in v0.1.7
PermanentlyDelete 永久删除记录(从数据库中完全删除)
func PermanentlyDeleteBatch ¶ added in v0.1.7
PermanentlyDeleteBatch 批量永久删除记录(从数据库中完全删除)
func RestoreDeleted ¶ added in v0.1.7
RestoreDeleted 恢复单个已删除记录(将deleted_at设为NULL)
Types ¶
type AggregateField ¶ added in v0.3.4
type AggregateField struct {
Function string // 聚合函数:SUM, COUNT, AVG, MAX, MIN
Condition string // CASE WHEN 条件
ThenValue string // THEN 子句的值(对于AVG/MAX/MIN是字段名,对于SUM/COUNT是1)
Args []interface{} // 条件参数
Alias string // 结果别名
ElseValue interface{} // ELSE 值(默认 0)
}
AggregateField 聚合字段定义
type AggregateOperation ¶ added in v0.3.4
type AggregateOperation struct {
Function string // COUNT, SUM, AVG, MAX, MIN
Field string // 字段名(COUNT(*) 时为空)
Alias string // 结果别名
Distinct bool // 是否 DISTINCT
Condition string // 可选的 CASE WHEN 条件
Args []interface{} // 条件参数
}
AggregateOperation 聚合操作
type AuditModel ¶ added in v0.1.2
type AuditModel struct {
BaseModel
CreatedBy uint `json:"created_by,omitempty" gorm:"index;comment:创建人ID"`
UpdatedBy uint `json:"updated_by,omitempty" gorm:"index;comment:更新人ID"`
}
AuditModel 审计模型(包含创建人和更新人信息) 适用于需要追踪操作人的业务场景
func (*AuditModel) GetCreatedBy ¶ added in v0.1.2
func (m *AuditModel) GetCreatedBy() uint
func (*AuditModel) GetUpdatedBy ¶ added in v0.1.2
func (m *AuditModel) GetUpdatedBy() uint
func (*AuditModel) SetCreatedBy ¶ added in v0.1.2
func (m *AuditModel) SetCreatedBy(userID uint)
func (*AuditModel) SetUpdatedBy ¶ added in v0.1.2
func (m *AuditModel) SetUpdatedBy(userID uint)
type AuditableModel ¶ added in v0.1.2
type AuditableModel interface {
ModelInterface
SetCreatedBy(userID uint)
SetUpdatedBy(userID uint)
GetCreatedBy() uint
GetUpdatedBy() uint
}
AuditableModel 支持审计的模型接口
type BaseModel ¶ added in v0.1.2
type BaseModel struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement;comment:自增主键"`
Version int `json:"version" gorm:"default:1;comment:版本号"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;comment:更新时间"`
DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"index;comment:删除时间"`
Remark string `json:"remark,omitempty" gorm:"type:varchar(500);comment:备注"`
Status int8 `json:"status" gorm:"default:1;index;comment:状态(1:启用 0:禁用)"`
}
BaseModel 公共基础模型(带软删除、状态管理、版本控制)
func (*BaseModel) BeforeUpdate ¶ added in v0.1.2
BeforeUpdate GORM更新前钩子
func (*BaseModel) GetVersion ¶ added in v0.1.2
GetVersion 获取版本号
func (*BaseModel) SetCreatedAt ¶ added in v0.1.2
func (*BaseModel) SetUpdatedAt ¶ added in v0.1.2
type BaseRepository ¶
type BaseRepository[T any] struct { // contains filtered or unexported fields }
BaseRepository 基础仓储实现,包含通用的 CRUD 操作
func NewBaseRepository ¶
func NewBaseRepository[T any](dbHandler db.Handler, logger logger.ILogger, table string, options ...RepositoryOption[T]) *BaseRepository[T]
NewBaseRepository 创建基础仓储
func (*BaseRepository[T]) BulkCreate ¶ added in v0.1.7
func (r *BaseRepository[T]) BulkCreate(ctx context.Context, entities []*T, batchSize ...int) error
BulkCreate 高性能批量创建
func (*BaseRepository[T]) ConcurrentQuery ¶ added in v0.3.4
func (r *BaseRepository[T]) ConcurrentQuery( ctx context.Context, queries map[string]func(ctx context.Context) (int64, error), opts ...ConcurrentQueryOption, ) (map[string]int64, bool)
ConcurrentQuery 简化的并发查询接口 直接传入查询函数的 map,返回结果的 map
func (*BaseRepository[T]) CountByField ¶ added in v0.1.1
func (r *BaseRepository[T]) CountByField(ctx context.Context, field string) (map[interface{}]int64, error)
CountByField 按字段计数(GROUP BY)
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]) CreateIfNotExists ¶ added in v0.1.7
func (r *BaseRepository[T]) CreateIfNotExists(ctx context.Context, entity *T, uniqueFields ...string) (*T, bool, error)
CreateIfNotExists 如果不存在则创建
func (*BaseRepository[T]) CreateOrUpdate ¶ added in v0.1.7
func (r *BaseRepository[T]) CreateOrUpdate(ctx context.Context, entity *T, uniqueFields ...string) (*T, bool, error)
CreateOrUpdate 创建或更新记录
func (*BaseRepository[T]) DBHandler ¶ added in v0.1.6
func (r *BaseRepository[T]) DBHandler() db.Handler
DBHandler 获取数据库处理器
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]) DisableAutoFields ¶ added in v0.3.4
func (r *BaseRepository[T]) DisableAutoFields()
DisableAutoFields 禁用自动字段模式
func (*BaseRepository[T]) Distinct ¶ added in v0.1.1
func (r *BaseRepository[T]) Distinct(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error)
Distinct 获取去重后的字段值列表
func (*BaseRepository[T]) EnableAutoFields ¶ added in v0.3.4
func (r *BaseRepository[T]) EnableAutoFields()
EnableAutoFields 启用自动字段模式
func (*BaseRepository[T]) ExecuteConcurrentQueries ¶ added in v0.3.4
func (r *BaseRepository[T]) ExecuteConcurrentQueries( ctx context.Context, tasks []ConcurrentQueryTask[int64], opts ...ConcurrentQueryOption, ) ([]ConcurrentQueryResult[int64], bool)
ExecuteConcurrentQueries 执行并发查询任务 这是一个便捷方法,直接在 repository 上执行并发查询
func (*BaseRepository[T]) Find ¶ added in v0.1.5
func (r *BaseRepository[T]) Find(ctx context.Context, options *FindOptions) ([]*T, error)
Find 通用查询方法,兼容旧的API调用方式
func (*BaseRepository[T]) FindOne ¶ added in v0.1.1
func (r *BaseRepository[T]) FindOne(ctx context.Context, filters ...*Filter) (*T, error)
FindOne 查找单条记录(不存在返回 nil)
func (*BaseRepository[T]) First ¶ added in v0.1.1
func (r *BaseRepository[T]) First(ctx context.Context, filters ...*Filter) (*T, error)
First 获取第一条记录
func (*BaseRepository[T]) Get ¶
func (r *BaseRepository[T]) Get(ctx context.Context, id interface{}) (*T, error)
Get 获取单个记录
func (*BaseRepository[T]) GetAll ¶ added in v0.1.1
func (r *BaseRepository[T]) GetAll(ctx context.Context) ([]*T, error)
GetAll 获取所有记录(不分页)
func (*BaseRepository[T]) GetByFields ¶ added in v0.1.7
func (r *BaseRepository[T]) GetByFields(ctx context.Context, fields map[string]interface{}) (*T, error)
GetByFields 根据多个字段获取记录
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]) GetModelFields ¶ added in v0.3.4
func (r *BaseRepository[T]) GetModelFields() []string
GetModelFields 获取缓存的模型字段
func (*BaseRepository[T]) GetWithPreloads ¶ added in v0.1.7
func (r *BaseRepository[T]) GetWithPreloads(ctx context.Context, id interface{}, preloads ...string) (*T, error)
GetWithPreloads 获取单个记录并指定预加载关联
func (*BaseRepository[T]) IsAutoFieldsEnabled ¶ added in v0.3.4
func (r *BaseRepository[T]) IsAutoFieldsEnabled() bool
IsAutoFieldsEnabled 检查是否启用自动字段模式
func (*BaseRepository[T]) Last ¶ added in v0.1.1
func (r *BaseRepository[T]) Last(ctx context.Context, filters ...*Filter) (*T, error)
Last 获取最后一条记录
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 *Pagination) ([]*T, *Pagination, error)
ListWithPagination 分页列表查询
func (*BaseRepository[T]) ListWithPreloads ¶ added in v0.1.7
func (r *BaseRepository[T]) ListWithPreloads(ctx context.Context, query *Query, preloads ...string) ([]*T, error)
ListWithPreloads 列表查询并指定预加载关联
func (*BaseRepository[T]) Pluck ¶ added in v0.1.1
func (r *BaseRepository[T]) Pluck(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error)
Pluck 提取单个字段的值列表
func (*BaseRepository[T]) Restore ¶ added in v0.1.1
func (r *BaseRepository[T]) Restore(ctx context.Context, id interface{}, field string, restoreValue interface{}) error
Restore 恢复软删除的记录 field: 软删除字段名,如 "deleted_at", "is_deleted" 等 restoreValue: 恢复时的值,如 nil, 0 等
func (*BaseRepository[T]) RestoreBatch ¶ added in v0.1.1
func (r *BaseRepository[T]) RestoreBatch(ctx context.Context, ids []interface{}, field string, restoreValue interface{}) error
RestoreBatch 批量恢复软删除的记录 field: 软删除字段名,如 "deleted_at", "is_deleted" 等 restoreValue: 恢复时的值,如 nil, 0 等
func (*BaseRepository[T]) SoftDelete ¶ added in v0.1.1
func (r *BaseRepository[T]) SoftDelete(ctx context.Context, id interface{}, field string, value interface{}) error
SoftDelete 软删除(需要指定删除标记字段和值) field: 软删除字段名,如 "deleted_at", "is_deleted" 等 value: 软删除标记值,如 time.Now(), 1 等
func (*BaseRepository[T]) SoftDeleteBatch ¶ added in v0.1.1
func (r *BaseRepository[T]) SoftDeleteBatch(ctx context.Context, ids []interface{}, field string, value interface{}) error
SoftDeleteBatch 批量软删除 field: 软删除字段名,如 "deleted_at", "is_deleted" 等 value: 软删除标记值,如 time.Now(), 1 等
func (*BaseRepository[T]) SoftDeleteByFilters ¶ added in v0.1.1
func (r *BaseRepository[T]) SoftDeleteByFilters(ctx context.Context, field string, value interface{}, filters ...*Filter) error
SoftDeleteByFilters 按过滤条件软删除 field: 软删除字段名,如 "deleted_at", "is_deleted" 等 value: 软删除标记值,如 time.Now(), 1 等
func (*BaseRepository[T]) Table ¶ added in v0.1.1
func (r *BaseRepository[T]) Table() string
Table 获取表名
func (*BaseRepository[T]) Transaction ¶
func (r *BaseRepository[T]) Transaction(ctx context.Context, fn func(tx Transaction[T]) error) error
Transaction 事务支持
func (*BaseRepository[T]) TransactionWithRawDB ¶ added in v0.3.2
func (r *BaseRepository[T]) TransactionWithRawDB(ctx context.Context, fn func(tx *gorm.DB) error) error
TransactionWithRawDB 通用事务支持(可以操作任意模型)
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 按过滤条件更新记录
func (*BaseRepository[T]) UpdateFields ¶ added in v0.1.1
func (r *BaseRepository[T]) UpdateFields(ctx context.Context, id interface{}, fields map[string]interface{}) error
UpdateFields 更新指定字段
func (*BaseRepository[T]) UpdateFieldsByFilters ¶ added in v0.1.1
func (r *BaseRepository[T]) UpdateFieldsByFilters(ctx context.Context, fields map[string]interface{}, filters ...*Filter) error
UpdateFieldsByFilters 按过滤条件更新指定字段
type ComparedColumn ¶ added in v0.1.7
type ComparedValue ¶ added in v0.1.7
type ComparedValue struct {
Value interface{}
IsRaw bool
}
type ConcurrentQueryBuilder ¶ added in v0.3.4
type ConcurrentQueryBuilder struct {
// contains filtered or unexported fields
}
ConcurrentQueryBuilder 简单查询构建器:用于构建标准 COUNT/SUM 查询
func NewConcurrentQueryBuilder ¶ added in v0.3.4
func NewConcurrentQueryBuilder(db *gorm.DB, tableName, selectSQL string) *ConcurrentQueryBuilder
NewConcurrentQueryBuilder 创建简单查询构建器
func (*ConcurrentQueryBuilder) Build ¶ added in v0.3.4
func (b *ConcurrentQueryBuilder) Build() func(ctx context.Context) (int64, error)
Build 构建查询函数
func (*ConcurrentQueryBuilder) BuildWithTimeRange ¶ added in v0.3.4
func (b *ConcurrentQueryBuilder) BuildWithTimeRange(startTime, endTime time.Time) func(ctx context.Context) (int64, error)
BuildWithTimeRange 构建带时间范围的查询函数(快捷方法)
func (*ConcurrentQueryBuilder) Where ¶ added in v0.3.4
func (b *ConcurrentQueryBuilder) Where(condition string, args ...interface{}) *ConcurrentQueryBuilder
Where 添加 WHERE 条件
type ConcurrentQueryExecutor ¶ added in v0.3.4
type ConcurrentQueryExecutor struct {
// contains filtered or unexported fields
}
ConcurrentQueryExecutor 并发查询执行器
func NewConcurrentQueryExecutor ¶ added in v0.3.4
func NewConcurrentQueryExecutor(db *gorm.DB) *ConcurrentQueryExecutor
NewConcurrentQueryExecutor 创建并发查询执行器
func (*ConcurrentQueryExecutor) WithLogger ¶ added in v0.3.4
func (e *ConcurrentQueryExecutor) WithLogger(log logger.ILogger) *ConcurrentQueryExecutor
WithLogger 设置日志记录器(链式调用)
func (*ConcurrentQueryExecutor) WithTimeout ¶ added in v0.3.4
func (e *ConcurrentQueryExecutor) WithTimeout(timeout time.Duration) *ConcurrentQueryExecutor
WithTimeout 设置查询超时时间(链式调用)
func (*ConcurrentQueryExecutor) WithWorkers ¶ added in v0.3.4
func (e *ConcurrentQueryExecutor) WithWorkers(workers int) *ConcurrentQueryExecutor
WithWorkers 设置工作协程数(链式调用)
type ConcurrentQueryOption ¶ added in v0.3.4
type ConcurrentQueryOption func(*ConcurrentQueryExecutor)
ConcurrentQueryOption 并发查询配置选项
func WithQueryTimeout ¶ added in v0.3.4
func WithQueryTimeout(timeout time.Duration) ConcurrentQueryOption
WithQueryTimeout 设置查询超时时间
func WithQueryWorkers ¶ added in v0.3.4
func WithQueryWorkers(workers int) ConcurrentQueryOption
WithQueryWorkers 设置工作协程数
type ConcurrentQueryResult ¶ added in v0.3.4
type ConcurrentQueryResult[T any] struct { Name string // 任务名称 Value T // 查询结果值 Error error // 错误信息(如果有) }
ConcurrentQueryResult 查询结果
func ExecuteConcurrentQueries ¶ added in v0.3.4
func ExecuteConcurrentQueries[T any]( ctx context.Context, db *gorm.DB, log logger.ILogger, tasks []ConcurrentQueryTask[T], opts ...ConcurrentQueryOption, ) ([]ConcurrentQueryResult[T], bool)
ExecuteConcurrentQueries 全局便捷函数:执行并发查询
func ExecuteConcurrentQuery ¶ added in v0.3.4
func ExecuteConcurrentQuery[T any](e *ConcurrentQueryExecutor, ctx context.Context, tasks []ConcurrentQueryTask[T]) ([]ConcurrentQueryResult[T], bool)
ExecuteConcurrentQuery 执行并发查询任务 tasks: 查询任务列表 返回: 所有查询结果和是否有错误发生
type ConcurrentQueryTask ¶ added in v0.3.4
type ConcurrentQueryTask[T any] struct { Name string // 任务名称(用于日志和错误追踪) Query func(ctx context.Context) (T, error) // 查询执行函数 OnSuccess func(T) // 成功回调(可选) OnError func(error) // 错误回调(可选) }
ConcurrentQueryTask 泛型查询任务定义 T: 查询结果的类型(支持任意类型:int64, float64, struct, []struct 等)
type ConditionalAggregateBuilder ¶ added in v0.3.4
type ConditionalAggregateBuilder struct {
// contains filtered or unexported fields
}
ConditionalAggregateBuilder 条件聚合构建器 用于构建复杂的 SUM(CASE WHEN ...) 查询
func NewConditionalAggregateBuilder ¶ added in v0.3.4
func NewConditionalAggregateBuilder(db *gorm.DB, tableName string) *ConditionalAggregateBuilder
NewConditionalAggregateBuilder 创建条件聚合构建器
func (*ConditionalAggregateBuilder) AvgWhen ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) AvgWhen(condition, field string, alias string, args ...interface{}) *ConditionalAggregateBuilder
AvgWhen 添加 AVG(CASE WHEN condition THEN field ELSE NULL END)
func (*ConditionalAggregateBuilder) Build ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) Build() (*gorm.DB, []interface{})
Build 构建 SQL 查询
func (*ConditionalAggregateBuilder) CountWhen ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) CountWhen(condition string, alias string, args ...interface{}) *ConditionalAggregateBuilder
CountWhen 添加 COUNT(CASE WHEN condition THEN 1 ELSE NULL END)
func (*ConditionalAggregateBuilder) Execute ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) Execute(ctx context.Context) (map[string]interface{}, error)
Execute 执行查询并返回单行结果(map)
func (*ConditionalAggregateBuilder) ExecuteInto ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) ExecuteInto(ctx context.Context, dest interface{}) error
ExecuteInto 执行查询并扫描到结构体
func (*ConditionalAggregateBuilder) ExecuteList ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) ExecuteList(ctx context.Context) ([]map[string]interface{}, error)
ExecuteList 执行查询并返回多行结果(用于 GROUP BY)
func (*ConditionalAggregateBuilder) GroupBy ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) GroupBy(fields ...string) *ConditionalAggregateBuilder
GroupBy 添加分组字段
func (*ConditionalAggregateBuilder) Having ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) Having(condition string, args ...interface{}) *ConditionalAggregateBuilder
Having 添加 HAVING 条件
func (*ConditionalAggregateBuilder) Limit ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) Limit(limit int) *ConditionalAggregateBuilder
Limit 设置限制
func (*ConditionalAggregateBuilder) MaxWhen ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) MaxWhen(condition, field string, alias string, args ...interface{}) *ConditionalAggregateBuilder
MaxWhen 添加 MAX(CASE WHEN condition THEN field ELSE NULL END)
func (*ConditionalAggregateBuilder) MinWhen ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) MinWhen(condition, field string, alias string, args ...interface{}) *ConditionalAggregateBuilder
MinWhen 添加 MIN(CASE WHEN condition THEN field ELSE NULL END)
func (*ConditionalAggregateBuilder) OrderBy ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) OrderBy(order string) *ConditionalAggregateBuilder
OrderBy 设置排序
func (*ConditionalAggregateBuilder) SumWhen ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) SumWhen(condition string, alias string, args ...interface{}) *ConditionalAggregateBuilder
SumWhen 添加 SUM(CASE WHEN condition THEN 1 ELSE 0 END)
func (*ConditionalAggregateBuilder) WithTimeField ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) WithTimeField(field string) *ConditionalAggregateBuilder
WithTimeField 设置时间字段
func (*ConditionalAggregateBuilder) WithTimeRange ¶ added in v0.3.4
func (b *ConditionalAggregateBuilder) WithTimeRange(start, end time.Time) *ConditionalAggregateBuilder
WithTimeRange 设置时间范围
type ContextFieldExtractor ¶ added in v0.1.7
ContextFieldExtractor context字段提取器函数类型 用于从context中提取需要记录到日志的字段
type Dialect ¶ added in v0.3.4
type Dialect interface {
// FormatTimeGroup 格式化时间分组表达式
FormatTimeGroup(field string, groupType TimeGroupType) string
}
Dialect 数据库方言接口
func DetectDialect ¶ added in v0.3.4
DetectDialect 自动检测数据库方言
type EnhancedRepository ¶ added in v0.1.3
type EnhancedRepository[T any] struct { *BaseRepository[T] // contains filtered or unexported fields }
EnhancedRepository 增强版仓储实现
func NewEnhancedRepository ¶ added in v0.1.3
func NewEnhancedRepository[T any](dbHandler db.Handler, logger gologger.ILogger, tableName string) *EnhancedRepository[T]
NewEnhancedRepository 创建增强版仓储实例
func NewEnhancedRepositoryWithDB ¶ added in v0.1.3
func NewEnhancedRepositoryWithDB[T any](gormDB *gorm.DB, logger gologger.ILogger, tableName string) *EnhancedRepository[T]
NewEnhancedRepositoryWithDB 使用GORM DB直接创建增强版仓储
func (*EnhancedRepository[T]) BatchUpdateByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) BatchUpdateByField(ctx context.Context, field string, values []interface{}, updates map[string]interface{}) error
BatchUpdateByField 根据字段批量更新
func (*EnhancedRepository[T]) CountByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) CountByField(ctx context.Context, field string, value interface{}) (int64, error)
CountByField 根据字段统计数量
func (*EnhancedRepository[T]) CreateIfNotExists ¶ added in v0.1.3
func (r *EnhancedRepository[T]) CreateIfNotExists(ctx context.Context, entity *T, checkField string) (*T, bool, error)
CreateIfNotExists 如果不存在则创建
func (*EnhancedRepository[T]) DecrementField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) DecrementField(ctx context.Context, whereField string, whereValue interface{}, decrementField string, step int64) error
DecrementField 字段自减
func (*EnhancedRepository[T]) DeleteByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) DeleteByField(ctx context.Context, field string, value interface{}) error
DeleteByField 根据字段删除记录
func (*EnhancedRepository[T]) ExistsBy ¶ added in v0.1.3
func (r *EnhancedRepository[T]) ExistsBy(ctx context.Context, field string, value interface{}) (bool, error)
ExistsBy 检查记录是否存在
func (*EnhancedRepository[T]) FindByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindByField(ctx context.Context, field string, value interface{}) ([]*T, error)
FindByField 根据单个字段查找记录
func (*EnhancedRepository[T]) FindByFieldWithCursor ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindByFieldWithCursor(ctx context.Context, field string, value interface{}, cursorField string, lastCursor interface{}, limit int) ([]*T, bool, error)
FindByFieldWithCursor 根据字段查找记录(游标分页)
func (*EnhancedRepository[T]) FindByFieldWithPagination ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindByFieldWithPagination(ctx context.Context, field string, value interface{}, limit, offset int) ([]*T, int64, error)
FindByFieldWithPagination 根据字段查找记录(带分页)
func (*EnhancedRepository[T]) FindByFields ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindByFields(ctx context.Context, conditions map[string]interface{}) ([]*T, error)
FindByFields 根据多个字段查找记录
func (*EnhancedRepository[T]) FindByTimeRange ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindByTimeRange(ctx context.Context, timeField string, startTime, endTime interface{}) ([]*T, error)
FindByTimeRange 根据时间范围查找
func (*EnhancedRepository[T]) FindInField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindInField(ctx context.Context, field string, values []interface{}) ([]*T, error)
FindInField 根据字段的IN查询
func (*EnhancedRepository[T]) FindOneByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindOneByField(ctx context.Context, field string, value interface{}) (*T, error)
FindOneByField 根据单个字段查找单条记录
func (*EnhancedRepository[T]) FindWithOrder ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindWithOrder(ctx context.Context, whereField string, whereValue interface{}, orderField string, orderDirection string) ([]*T, error)
FindWithOrder 根据条件查找并排序
func (*EnhancedRepository[T]) GetDistinctValues ¶ added in v0.1.3
func (r *EnhancedRepository[T]) GetDistinctValues(ctx context.Context, field string) ([]interface{}, error)
GetDistinctValues 获取字段的不同值
func (*EnhancedRepository[T]) IncrementField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) IncrementField(ctx context.Context, whereField string, whereValue interface{}, incrementField string, step int64) error
IncrementField 字段自增
func (*EnhancedRepository[T]) UpdateByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) UpdateByField(ctx context.Context, field string, value interface{}, updates map[string]interface{}) error
UpdateByField 根据字段更新记录
func (*EnhancedRepository[T]) UpdateSingleField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) UpdateSingleField(ctx context.Context, whereField string, whereValue interface{}, updateField string, updateValue interface{}) error
UpdateSingleField 更新单个字段
type Filter ¶
type Filter struct {
Field string // 字段名
Operator constants.Operator // 操作符
Value interface{} // 值(可以是普通值或 *SubQuery)
}
Filter 过滤条件
func NewBetweenFilter ¶
NewBetweenFilter 创建 BETWEEN 过滤条件
func NewContainsFilter ¶ added in v0.1.1
NewContainsFilter 创建包含匹配过滤条件(与 NewLikeFilter 相同)
func NewEndsWithFilter ¶ added in v0.1.1
NewEndsWithFilter 创建后缀匹配过滤条件
func NewFindInSetFilter ¶ added in v0.1.7
NewFindInSetFilter 创建 FIND_IN_SET 过滤条件(MySQL特定)
func NewGteFilter ¶
NewGteFilter 创建大于等于过滤条件
func NewInFilter ¶
NewInFilter 创建 IN 过滤条件
func NewInFilterSlice ¶ added in v0.3.1
NewInFilterSlice 创建 IN 过滤条件(使用切片参数)
func NewIsNotNullFilter ¶ added in v0.1.1
NewIsNotNullFilter 创建 IS NOT NULL 过滤条件
func NewIsNullFilter ¶ added in v0.1.1
NewIsNullFilter 创建 IS NULL 过滤条件
func NewLikeFilter ¶
NewLikeFilter 创建 LIKE 过滤条件
func NewLteFilter ¶
NewLteFilter 创建小于等于过滤条件
func NewNeqFilter ¶
NewNeqFilter 创建不等于过滤条件
func NewNotInFilter ¶ added in v0.1.1
NewNotInFilter 创建 NOT IN 过滤条件
func NewNotInFilterSlice ¶ added in v0.3.1
NewNotInFilterSlice 创建 NOT IN 过滤条件(使用切片参数)
func NewNotLikeFilter ¶ added in v0.1.7
NewNotLikeFilter 创建 NOT LIKE 过滤条件
func NewStartsWithFilter ¶ added in v0.1.1
NewStartsWithFilter 创建前缀匹配过滤条件
type FilterGroup ¶ added in v0.1.7
type FilterGroup struct {
Filters []*Filter // 过滤条件列表
Groups []*FilterGroup // 嵌套条件组
LogicOp constants.Operator // 逻辑操作符:AND/OR
}
FilterGroup 过滤条件组,支持逻辑操作
func NewFilterGroup ¶ added in v0.1.7
func NewFilterGroup(logicOp constants.Operator) *FilterGroup
NewFilterGroup 创建新的过滤条件组
func (*FilterGroup) AddBetweenFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddBetweenFilterIfNotEmpty(field string, min, max interface{}) *FilterGroup
AddBetweenFilterIfNotEmpty 当最小值和最大值都不为空时添加 BETWEEN 过滤条件
func (*FilterGroup) AddContainsFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddContainsFilterIfNotEmpty(field string, value string) *FilterGroup
AddContainsFilterIfNotEmpty 当值不为空时添加包含匹配过滤条件
func (*FilterGroup) AddEndsWithFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddEndsWithFilterIfNotEmpty(field string, value string) *FilterGroup
AddEndsWithFilterIfNotEmpty 当值不为空时添加后缀匹配过滤条件
func (*FilterGroup) AddEqFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddEqFilterIfNotEmpty(field string, value interface{}) *FilterGroup
AddEqFilterIfNotEmpty 当值不为空时添加等于过滤条件
func (*FilterGroup) AddFilter ¶ added in v0.1.7
func (fg *FilterGroup) AddFilter(filter *Filter) *FilterGroup
AddFilter 向条件组添加过滤条件
func (*FilterGroup) AddFilterIf ¶ added in v0.3.4
func (fg *FilterGroup) AddFilterIf(condition bool, filter *Filter) *FilterGroup
AddFilterIf 当条件为真时添加过滤条件
func (*FilterGroup) AddFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddFilterIfNotEmpty(field string, operator constants.Operator, value interface{}) *FilterGroup
AddFilterIfNotEmpty 当值不为空时添加过滤条件 支持 string, slice, map 等类型的空值判断
func (*FilterGroup) AddFilters ¶ added in v0.1.7
func (fg *FilterGroup) AddFilters(filters ...*Filter) *FilterGroup
AddFilters 向条件组批量添加过滤条件
func (*FilterGroup) AddFindInSetFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddFindInSetFilterIfNotEmpty(field string, value interface{}) *FilterGroup
AddFindInSetFilterIfNotEmpty 当值不为空时添加 FIND_IN_SET 过滤条件(MySQL特定)
func (*FilterGroup) AddGroup ¶ added in v0.1.7
func (fg *FilterGroup) AddGroup(group *FilterGroup) *FilterGroup
AddGroup 向条件组添加嵌套条件组
func (*FilterGroup) AddGroupIf ¶ added in v0.3.4
func (fg *FilterGroup) AddGroupIf(condition bool, group *FilterGroup) *FilterGroup
AddGroupIf 当条件为真时添加嵌套条件组
func (*FilterGroup) AddGroupIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddGroupIfNotEmpty(group *FilterGroup) *FilterGroup
AddGroupIfNotEmpty 当嵌套条件组不为空时添加
func (*FilterGroup) AddGtFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddGtFilterIfNotEmpty(field string, value interface{}) *FilterGroup
AddGtFilterIfNotEmpty 当值不为空时添加大于过滤条件
func (*FilterGroup) AddGteFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddGteFilterIfNotEmpty(field string, value interface{}) *FilterGroup
AddGteFilterIfNotEmpty 当值不为空时添加大于等于过滤条件
func (*FilterGroup) AddInFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddInFilterIfNotEmpty(field string, values []interface{}) *FilterGroup
AddInFilterIfNotEmpty 当切片不为空时添加 IN 过滤条件
func (*FilterGroup) AddLikeFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddLikeFilterIfNotEmpty(field string, value string) *FilterGroup
AddLikeFilterIfNotEmpty 当值不为空时添加 LIKE 过滤条件
func (*FilterGroup) AddLtFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddLtFilterIfNotEmpty(field string, value interface{}) *FilterGroup
AddLtFilterIfNotEmpty 当值不为空时添加小于过滤条件
func (*FilterGroup) AddLteFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddLteFilterIfNotEmpty(field string, value interface{}) *FilterGroup
AddLteFilterIfNotEmpty 当值不为空时添加小于等于过滤条件
func (*FilterGroup) AddNeqFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddNeqFilterIfNotEmpty(field string, value interface{}) *FilterGroup
AddNeqFilterIfNotEmpty 当值不为空时添加不等于过滤条件
func (*FilterGroup) AddNotInFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddNotInFilterIfNotEmpty(field string, values []interface{}) *FilterGroup
AddNotInFilterIfNotEmpty 当切片不为空时添加 NOT IN 过滤条件
func (*FilterGroup) AddNotLikeFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddNotLikeFilterIfNotEmpty(field string, value string) *FilterGroup
AddNotLikeFilterIfNotEmpty 当值不为空时添加 NOT LIKE 过滤条件
func (*FilterGroup) AddStartsWithFilterIfNotEmpty ¶ added in v0.3.4
func (fg *FilterGroup) AddStartsWithFilterIfNotEmpty(field string, value string) *FilterGroup
AddStartsWithFilterIfNotEmpty 当值不为空时添加前缀匹配过滤条件
func (*FilterGroup) Clear ¶ added in v0.3.4
func (fg *FilterGroup) Clear() *FilterGroup
Clear 清空所有过滤条件和条件组
func (*FilterGroup) Clone ¶ added in v0.3.4
func (fg *FilterGroup) Clone() *FilterGroup
Clone 克隆条件组(深拷贝)
func (*FilterGroup) IsEmpty ¶ added in v0.1.7
func (fg *FilterGroup) IsEmpty() bool
IsEmpty 检查条件组是否为空
type FindOptions ¶ added in v0.1.5
FindOptions 兼容旧API的查询选项结构
type FullFeaturedModel ¶ added in v0.1.2
type FullFeaturedModel interface {
ModelInterface
VersionedModel
SoftDeletableModel
AuditableModel
StatusModel
RemarkableModel
}
FullFeaturedModel 全功能模型接口(组合所有特性)
type LightModel ¶ added in v0.1.2
type LightModel struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement;comment:自增主键"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;comment:更新时间"`
Status int8 `json:"status" gorm:"default:1;index;comment:状态(1:启用 0:禁用)"`
}
LightModel 轻量级模型(仅包含基本字段,无软删除、无版本控制)
func (*LightModel) Disable ¶ added in v0.1.2
func (m *LightModel) Disable()
func (*LightModel) Enable ¶ added in v0.1.2
func (m *LightModel) Enable()
func (*LightModel) GetID ¶ added in v0.1.2
func (m *LightModel) GetID() uint
func (*LightModel) IsEnabled ¶ added in v0.1.2
func (m *LightModel) IsEnabled() bool
func (*LightModel) IsNew ¶ added in v0.1.2
func (m *LightModel) IsNew() bool
type ModelInterface ¶ added in v0.1.2
type ModelInterface interface {
IsNew() bool
}
ModelInterface 模型通用接口
type MultiTableStatsBuilder ¶ added in v0.3.4
type MultiTableStatsBuilder struct {
// contains filtered or unexported fields
}
MultiTableStatsBuilder 多表统计构建器 专门用于并发查询多个表的统计数据(如仪表盘)
func NewMultiTableStatsBuilder ¶ added in v0.3.4
func NewMultiTableStatsBuilder(ctx context.Context, db *gorm.DB, log logger.ILogger) *MultiTableStatsBuilder
NewMultiTableStatsBuilder 创建多表统计构建器
func (*MultiTableStatsBuilder) AddCondition ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) AddCondition(tableName, condition string, args ...interface{}) *MultiTableStatsBuilder
AddCondition 为特定表添加额外条件
func (*MultiTableStatsBuilder) Avg ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) Avg(tableName, field, alias string) *MultiTableStatsBuilder
Avg 添加 AVG(field) 查询
func (*MultiTableStatsBuilder) Count ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) Count(tableName, alias string) *MultiTableStatsBuilder
Count 添加 COUNT(*) 查询
func (*MultiTableStatsBuilder) CountDistinct ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) CountDistinct(tableName, field, alias string) *MultiTableStatsBuilder
CountDistinct 添加 COUNT(DISTINCT field) 查询
func (*MultiTableStatsBuilder) Execute ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) Execute() (map[string]int64, error)
Execute 执行所有查询并返回结果
func (*MultiTableStatsBuilder) ExecuteWithDetails ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) ExecuteWithDetails() ([]ConcurrentQueryResult[int64], bool, error)
ExecuteWithDetails 执行并返回详细结果(包含错误信息)
func (*MultiTableStatsBuilder) Max ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) Max(tableName, field, alias string) *MultiTableStatsBuilder
Max 添加 MAX(field) 查询
func (*MultiTableStatsBuilder) Min ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) Min(tableName, field, alias string) *MultiTableStatsBuilder
Min 添加 MIN(field) 查询
func (*MultiTableStatsBuilder) Sum ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) Sum(tableName, field, alias string) *MultiTableStatsBuilder
Sum 添加 SUM(field) 查询
func (*MultiTableStatsBuilder) WithTimeField ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) WithTimeField(field string) *MultiTableStatsBuilder
WithTimeField 设置时间字段名
func (*MultiTableStatsBuilder) WithTimeRange ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) WithTimeRange(start, end time.Time) *MultiTableStatsBuilder
WithTimeRange 设置时间范围(应用到所有表)
func (*MultiTableStatsBuilder) WithTimeout ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) WithTimeout(timeout time.Duration) *MultiTableStatsBuilder
WithTimeout 设置查询超时
func (*MultiTableStatsBuilder) WithWorkers ¶ added in v0.3.4
func (b *MultiTableStatsBuilder) WithWorkers(workers int) *MultiTableStatsBuilder
WithWorkers 设置并发工作数
type MySQLDialect ¶ added in v0.3.4
type MySQLDialect struct{}
MySQLDialect MySQL 方言
func (*MySQLDialect) FormatTimeGroup ¶ added in v0.3.4
func (d *MySQLDialect) FormatTimeGroup(field string, groupType TimeGroupType) string
type Pagination ¶ added in v0.1.7
type Pagination struct {
Page int32 `json:"page"` // 当前页码(从1开始)
PageSize int32 `json:"page_size"` // 每页记录数
Offset int32 `json:"offset"` // 数据库偏移量
Limit int32 `json:"limit"` // 查询限制数
Total int64 `json:"total"` // 总记录数
}
Pagination 分页元数据
func (*Pagination) GetLimit ¶ added in v0.1.7
func (p *Pagination) GetLimit() int
GetLimit 获取查询限制数(自动应用默认值和最大值限制)
func (*Pagination) GetOffset ¶ added in v0.1.7
func (p *Pagination) GetOffset() int
GetOffset 计算数据库偏移量
func (*Pagination) GetTotalPages ¶ added in v0.1.7
func (p *Pagination) GetTotalPages() int64
GetTotalPages 计算总页数
func (*Pagination) HasNextPage ¶ added in v0.1.7
func (p *Pagination) HasNextPage() bool
HasNextPage 是否有下一页
func (*Pagination) HasPrevPage ¶ added in v0.1.7
func (p *Pagination) HasPrevPage() bool
HasPrevPage 是否有上一页
type PostgreSQLDialect ¶ added in v0.3.4
type PostgreSQLDialect struct{}
PostgreSQLDialect PostgreSQL 方言
func (*PostgreSQLDialect) FormatTimeGroup ¶ added in v0.3.4
func (d *PostgreSQLDialect) FormatTimeGroup(field string, groupType TimeGroupType) string
type Query ¶
type Query struct {
Filters []*Filter // 简单过滤条件
FilterGroup *FilterGroup // 复合过滤条件组
Orders []Order // 排序条件
Pagination *Pagination // 分页信息
LimitValue *int // 限制数量
OffsetValue *int // 偏移量
Distinct bool // 是否去重
GroupBy []string // 分组字段
Having []*Filter // HAVING 条件
SelectFields []string // 要查询的字段列表(为空则查询所有字段)
OmitFields []string // 要排除的字段列表
}
Query 查询条件
func (*Query) AddBetween ¶ added in v0.3.1
AddBetween 添加BETWEEN条件
func (*Query) AddBetweenFilterIfNotEmpty ¶ added in v0.3.4
AddBetweenFilterIfNotEmpty 添加 BETWEEN 过滤条件(仅当最小值和最大值都不为空时)
func (*Query) AddContainsFilterIfNotEmpty ¶ added in v0.3.4
AddContainsFilterIfNotEmpty 添加包含匹配过滤条件(仅当值不为空时)
func (*Query) AddEndsWith ¶ added in v0.3.1
AddEndsWith 添加后缀匹配条件
func (*Query) AddEndsWithFilterIfNotEmpty ¶ added in v0.3.4
AddEndsWithFilterIfNotEmpty 添加后缀匹配过滤条件(仅当值不为空时)
func (*Query) AddEqFilterIfNotEmpty ¶ added in v0.3.4
AddEqFilterIfNotEmpty 添加等于过滤条件(仅当值不为空时)
func (*Query) AddFilterIfNotEmpty ¶ added in v0.3.1
AddFilterIfNotEmpty 添加过滤条件(仅当值不为空时) 支持泛型自动处理不同类型的值
func (*Query) AddFilters ¶
AddFilters 批量添加过滤条件
func (*Query) AddFindInSetFilterIfNotEmpty ¶ added in v0.3.4
AddFindInSetFilterIfNotEmpty 添加 FIND_IN_SET 过滤条件(仅当值不为空时,MySQL特定)
func (*Query) AddGreaterEqual ¶ added in v0.3.1
AddGreaterEqual 添加大于等于条件
func (*Query) AddGreaterThan ¶ added in v0.3.1
AddGreaterThan 添加大于条件
func (*Query) AddGroupBy ¶ added in v0.1.7
AddGroupBy 添加分组字段
func (*Query) AddGtFilterIfNotEmpty ¶ added in v0.3.4
AddGtFilterIfNotEmpty 添加大于过滤条件(仅当值不为空时)
func (*Query) AddGteFilterIfNotEmpty ¶ added in v0.3.4
AddGteFilterIfNotEmpty 添加大于等于过滤条件(仅当值不为空时)
func (*Query) AddInFilterIfNotEmpty ¶ added in v0.3.1
AddInFilterIfNotEmpty 添加 IN 过滤条件(仅当切片不为空时)
func (*Query) AddIsNotNull ¶ added in v0.3.1
AddIsNotNull 添加IS NOT NULL条件
func (*Query) AddLessEqual ¶ added in v0.3.1
AddLessEqual 添加小于等于条件
func (*Query) AddLessThan ¶ added in v0.3.1
AddLessThan 添加小于条件
func (*Query) AddLikeFilterIfNotEmpty ¶ added in v0.3.1
AddLikeFilterIfNotEmpty 添加 LIKE 过滤条件(仅当关键词不为空时)
func (*Query) AddLtFilterIfNotEmpty ¶ added in v0.3.4
AddLtFilterIfNotEmpty 添加小于过滤条件(仅当值不为空时)
func (*Query) AddLteFilterIfNotEmpty ¶ added in v0.3.4
AddLteFilterIfNotEmpty 添加小于等于过滤条件(仅当值不为空时)
func (*Query) AddNeqFilterIfNotEmpty ¶ added in v0.3.4
AddNeqFilterIfNotEmpty 添加不等于过滤条件(仅当值不为空时)
func (*Query) AddNotEqual ¶ added in v0.3.1
AddNotEqual 添加不等于条件
func (*Query) AddNotInFilterIfNotEmpty ¶ added in v0.3.4
AddNotInFilterIfNotEmpty 添加 NOT IN 过滤条件(仅当切片不为空时)
func (*Query) AddNotLikeFilterIfNotEmpty ¶ added in v0.3.4
AddNotLikeFilterIfNotEmpty 添加 NOT LIKE 过滤条件(仅当值不为空时)
func (*Query) AddOrderAsc ¶ added in v0.3.1
AddOrderAsc 添加升序排序
func (*Query) AddOrderDesc ¶ added in v0.3.1
AddOrderDesc 添加降序排序
func (*Query) AddRawOrder ¶ added in v0.3.4
AddRawOrder 添加原始SQL排序表达式(用于复杂排序,如多字段排序、函数排序等) 注意:此方法直接将排序表达式添加到SQL中,使用时需确保SQL注入安全
func (*Query) AddSafeOrder ¶ added in v0.3.1
func (q *Query) AddSafeOrder(sortBy, sortOrder, defaultField, defaultDirection string, allowedFields ...[]string) *Query
AddSafeOrder 安全地添加排序条件 参数:
- sortBy: 排序字段(可选,为空时使用defaultField)
- sortOrder: 排序方向(可选,仅支持"ASC"/"DESC",为空时使用defaultDirection)
- defaultField: 默认排序字段
- defaultDirection: 默认排序方向
- allowedFields: 允许的字段白名单(可选,为空则不限制)
示例:
query.AddSafeOrder(filter.SortBy, filter.SortOrder, "created_at", "DESC", []string{"created_at", "updated_at", "id"})
func (*Query) AddStartsWith ¶ added in v0.3.1
AddStartsWith 添加前缀匹配条件
func (*Query) AddStartsWithFilterIfNotEmpty ¶ added in v0.3.4
AddStartsWithFilterIfNotEmpty 添加前缀匹配过滤条件(仅当值不为空时)
func (*Query) AddThisMonth ¶ added in v0.3.1
AddThisMonth 添加本月条件
func (*Query) AddThisWeek ¶ added in v0.3.1
AddThisWeek 添加本周条件
func (*Query) AddThisYear ¶ added in v0.3.1
AddThisYear 添加今年条件
func (*Query) AddTimeAfter ¶ added in v0.3.1
AddTimeAfter 添加时间晚于条件
func (*Query) AddTimeBefore ¶ added in v0.3.1
AddTimeBefore 添加时间早于条件
func (*Query) AddTimeBetween ¶ added in v0.3.1
AddTimeBetween 添加时间范围条件
func (*Query) AddTimeRangeFilter ¶ added in v0.3.1
AddTimeRangeFilter 添加时间范围过滤条件 自动过滤掉nil和零值时间,避免生成无效的SQL条件
func (*Query) BuildWhereClause ¶ added in v0.3.4
BuildWhereClause 构建WHERE子句和参数 返回值: whereClause (SQL条件字符串), args (参数数组) 示例: "agent_id = ? AND work_status = ? AND start_time >= ?", []interface{}{"user123", 2, time.Now()}
func (*Query) GetAllFilters ¶ added in v0.1.7
GetAllFilters 获取所有过滤条件(扁平化)
func (*Query) OmitLargeFields ¶ added in v0.3.4
OmitLargeFields 排除大字段的便捷方法 默认排除: content, description, detail, data, payload, body
func (*Query) OmitSensitive ¶ added in v0.3.4
OmitSensitive 排除敏感字段的便捷方法 默认排除: password, secret, token, api_key
func (*Query) SelectOnly ¶ added in v0.3.4
SelectOnly 只查询指定的单个字段(便捷方法)
func (*Query) SetDistinct ¶ added in v0.3.1
SetDistinct 设置去重(简化版)
func (*Query) SetPagination ¶ added in v0.3.4
SetPagination 设置分页条件 - WithPaging 的别名,提供更直观的API
func (*Query) WithDistinct ¶ added in v0.1.7
WithDistinct 设置去重查询
func (*Query) WithFilterGroup ¶ added in v0.1.7
func (q *Query) WithFilterGroup(group *FilterGroup) *Query
WithFilterGroup 设置复合过滤条件组
type QueryCondition ¶ added in v0.1.7
type QueryCondition struct {
Filters []Filter
Orders []OrderBy
Pagination *Pagination
}
type RemarkableModel ¶ added in v0.1.2
type RemarkableModel interface {
ModelInterface
SetRemark(remark string)
}
RemarkableModel 支持备注的模型接口
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) GetAll(ctx context.Context) ([]*T, error) First(ctx context.Context, filters ...*Filter) (*T, error) Last(ctx context.Context, filters ...*Filter) (*T, error) FindOne(ctx context.Context, filters ...*Filter) (*T, error) List(ctx context.Context, query *Query) ([]*T, error) ListWithPagination(ctx context.Context, query *Query, page *Pagination) ([]*T, *Pagination, 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 UpdateFields(ctx context.Context, id interface{}, fields map[string]interface{}) error UpdateFieldsByFilters(ctx context.Context, fields map[string]interface{}, filters ...*Filter) error // 删除 Delete(ctx context.Context, id interface{}) error DeleteBatch(ctx context.Context, ids ...interface{}) error DeleteByFilters(ctx context.Context, filters ...*Filter) error SoftDelete(ctx context.Context, id interface{}, field string, value interface{}) error SoftDeleteBatch(ctx context.Context, ids []interface{}, field string, value interface{}) error SoftDeleteByFilters(ctx context.Context, field string, value interface{}, filters ...*Filter) error Restore(ctx context.Context, id interface{}, field string, restoreValue interface{}) error RestoreBatch(ctx context.Context, ids []interface{}, field string, restoreValue interface{}) error // 事务 Transaction(ctx context.Context, fn func(tx Transaction[T]) error) error TransactionWithRawDB(ctx context.Context, fn func(tx *gorm.DB) error) error // 工具方法 Count(ctx context.Context, filters ...*Filter) (int64, error) Exists(ctx context.Context, filters ...*Filter) (bool, error) CountByField(ctx context.Context, field string) (map[interface{}]int64, error) Pluck(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error) Distinct(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error) // 并发查询 ExecuteConcurrentQueries(ctx context.Context, tasks []ConcurrentQueryTask[int64], opts ...ConcurrentQueryOption) ([]ConcurrentQueryResult[int64], bool) ConcurrentQuery(ctx context.Context, queries map[string]func(ctx context.Context) (int64, error), opts ...ConcurrentQueryOption) (map[string]int64, bool) }
Repository 通用仓储接口
type RepositoryFactory ¶
type RepositoryFactory[T any] interface { // 创建仓储 CreateRepository(table string) Repository[T] // 关闭工厂 Close() error }
RepositoryFactory 仓储工厂接口
type RepositoryOption ¶ added in v0.1.7
type RepositoryOption[T any] func(*BaseRepository[T])
RepositoryOption 仓储配置选项
func WithAutoFields ¶ added in v0.3.4
func WithAutoFields[T any]() RepositoryOption[T]
WithAutoFields 启用自动字段模式(根据model自动提取字段)
func WithBatchSize ¶ added in v0.1.7
func WithBatchSize[T any](size int) RepositoryOption[T]
WithBatchSize 设置批处理大小
func WithDefaultOrder ¶ added in v0.1.7
func WithDefaultOrder[T any](order string) RepositoryOption[T]
WithDefaultOrder 设置默认排序
func WithDefaultPreloads ¶ added in v0.1.7
func WithDefaultPreloads[T any](preloads ...string) RepositoryOption[T]
WithDefaultPreloads 设置默认预加载关联
func WithLogger ¶ added in v0.1.7
func WithLogger[T any](log logger.ILogger) RepositoryOption[T]
WithLogger 设置日志记录器
func WithReadOnly ¶ added in v0.1.7
func WithReadOnly[T any]() RepositoryOption[T]
WithReadOnly 设置为只读模式
func WithTimeout ¶ added in v0.1.7
func WithTimeout[T any](seconds int) RepositoryOption[T]
WithTimeout 设置查询超时时间
type RepositoryWithSoftDelete ¶ added in v0.1.1
type RepositoryWithSoftDelete[T any] struct { *BaseRepository[T] }
RepositoryWithSoftDelete 带软删除功能的仓储
func NewRepositoryWithSoftDelete ¶ added in v0.1.1
func NewRepositoryWithSoftDelete[T any](repo *BaseRepository[T]) *RepositoryWithSoftDelete[T]
NewRepositoryWithSoftDelete 创建带软删除功能的仓储
func (*RepositoryWithSoftDelete[T]) ListDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) ListDeleted(ctx context.Context, query *Query) ([]*T, error)
ListDeleted 查询已删除的记录(deleted_at 字段)
func (*RepositoryWithSoftDelete[T]) ListDeletedByIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) ListDeletedByIsDeleted(ctx context.Context, query *Query) ([]*T, error)
ListDeletedByIsDeleted 查询已删除的记录(is_deleted 字段)
func (*RepositoryWithSoftDelete[T]) ListNotDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) ListNotDeleted(ctx context.Context, query *Query) ([]*T, error)
ListNotDeleted 查询未删除的记录(deleted_at 字段)
func (*RepositoryWithSoftDelete[T]) ListNotDeletedByIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) ListNotDeletedByIsDeleted(ctx context.Context, query *Query) ([]*T, error)
ListNotDeletedByIsDeleted 查询未删除的记录(is_deleted 字段)
func (*RepositoryWithSoftDelete[T]) RestoreBatchWithDeletedAt ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) RestoreBatchWithDeletedAt(ctx context.Context, ids []interface{}) error
RestoreBatchWithDeletedAt 使用 deleted_at 字段批量恢复
func (*RepositoryWithSoftDelete[T]) RestoreBatchWithIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) RestoreBatchWithIsDeleted(ctx context.Context, ids []interface{}) error
RestoreBatchWithIsDeleted 使用 is_deleted 字段批量恢复
func (*RepositoryWithSoftDelete[T]) RestoreWithDeletedAt ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) RestoreWithDeletedAt(ctx context.Context, id interface{}) error
RestoreWithDeletedAt 使用 deleted_at 字段恢复
func (*RepositoryWithSoftDelete[T]) RestoreWithIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) RestoreWithIsDeleted(ctx context.Context, id interface{}) error
RestoreWithIsDeleted 使用 is_deleted 字段恢复
func (*RepositoryWithSoftDelete[T]) SoftDeleteBatchWithDeletedAt ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteBatchWithDeletedAt(ctx context.Context, ids []interface{}) error
SoftDeleteBatchWithDeletedAt 使用 deleted_at 字段批量软删除
func (*RepositoryWithSoftDelete[T]) SoftDeleteBatchWithIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteBatchWithIsDeleted(ctx context.Context, ids []interface{}) error
SoftDeleteBatchWithIsDeleted 使用 is_deleted 字段批量软删除
func (*RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithDeletedAt ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithDeletedAt(ctx context.Context, filters ...*Filter) error
SoftDeleteByFiltersWithDeletedAt 使用 deleted_at 字段按条件软删除
func (*RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithIsDeleted(ctx context.Context, filters ...*Filter) error
SoftDeleteByFiltersWithIsDeleted 使用 is_deleted 字段按条件软删除
func (*RepositoryWithSoftDelete[T]) SoftDeleteWithDeletedAt ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteWithDeletedAt(ctx context.Context, id interface{}) error
SoftDeleteWithDeletedAt 使用 deleted_at 字段软删除
func (*RepositoryWithSoftDelete[T]) SoftDeleteWithIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteWithIsDeleted(ctx context.Context, id interface{}) error
SoftDeleteWithIsDeleted 使用 is_deleted 字段软删除
type SQLiteDialect ¶ added in v0.3.4
type SQLiteDialect struct{}
SQLiteDialect SQLite 方言
func (*SQLiteDialect) FormatTimeGroup ¶ added in v0.3.4
func (d *SQLiteDialect) FormatTimeGroup(field string, groupType TimeGroupType) string
type SimpleModel ¶ added in v0.1.2
type SimpleModel struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement;comment:自增主键"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;comment:更新时间"`
}
SimpleModel 简化模型(不带软删除)
func (*SimpleModel) GetID ¶ added in v0.1.2
func (m *SimpleModel) GetID() uint
func (*SimpleModel) IsNew ¶ added in v0.1.2
func (m *SimpleModel) IsNew() bool
type SoftDeletableModel ¶ added in v0.1.2
type SoftDeletableModel interface {
ModelInterface
IsDeleted() bool
}
SoftDeletableModel 支持软删除的模型接口
type SoftDeleteHelper ¶ added in v0.1.1
type SoftDeleteHelper[T any] interface { // 使用 deleted_at 字段的软删除 SoftDeleteWithDeletedAt(ctx context.Context, id interface{}) error SoftDeleteBatchWithDeletedAt(ctx context.Context, ids []interface{}) error SoftDeleteByFiltersWithDeletedAt(ctx context.Context, filters ...*Filter) error RestoreWithDeletedAt(ctx context.Context, id interface{}) error RestoreBatchWithDeletedAt(ctx context.Context, ids []interface{}) error // 使用 is_deleted 字段的软删除 SoftDeleteWithIsDeleted(ctx context.Context, id interface{}) error SoftDeleteBatchWithIsDeleted(ctx context.Context, ids []interface{}) error SoftDeleteByFiltersWithIsDeleted(ctx context.Context, filters ...*Filter) error RestoreWithIsDeleted(ctx context.Context, id interface{}) error RestoreBatchWithIsDeleted(ctx context.Context, ids []interface{}) error }
SoftDeleteHelper 软删除辅助接口
type StatusModel ¶ added in v0.1.2
type StatusModel interface {
ModelInterface
Enable()
Disable()
IsEnabled() bool
}
StatusModel 支持状态管理的模型接口
type SubQuery ¶ added in v0.3.4
type SubQuery struct {
SQL string // 子查询 SQL
Args []interface{} // 子查询参数
}
SubQuery 子查询结构
func NewSubQuery ¶ added in v0.3.4
NewSubQuery 创建子查询
type TimeEntityCursor ¶ added in v0.3.4
type TimeEntityCursor struct {
Timestamp string // 时间戳(RFC3339Nano格式)
EntityID string // 实体ID(如用户ID、工单ID等)
}
TimeEntityCursor 时间戳+实体ID游标(用于复合排序分页)
func NewTimeEntityCursor ¶ added in v0.3.4
func NewTimeEntityCursor(timestamp *time.Time, entityID string) *TimeEntityCursor
NewTimeEntityCursor 从时间戳和实体ID创建游标
func NewTimeEntityCursorFromTime ¶ added in v0.3.4
func NewTimeEntityCursorFromTime(t time.Time, entityID string, format ...string) *TimeEntityCursor
NewTimeEntityCursorFromTime 从时间对象和实体ID创建游标(支持自定义时间格式)
func ParseTimeEntityCursor ¶ added in v0.3.4
func ParseTimeEntityCursor(cursorStr string) *TimeEntityCursor
ParseTimeEntityCursor 从字符串解析时间戳+实体ID游标
func ParseTimeEntityCursorFromJSON ¶ added in v0.3.4
func ParseTimeEntityCursorFromJSON(jsonStr string) (*TimeEntityCursor, error)
ParseTimeEntityCursorFromJSON 从JSON字符串解析游标
func (*TimeEntityCursor) Clone ¶ added in v0.3.4
func (c *TimeEntityCursor) Clone() *TimeEntityCursor
Clone 深拷贝游标
func (*TimeEntityCursor) Compare ¶ added in v0.3.4
func (c *TimeEntityCursor) Compare(other *TimeEntityCursor) (int, error)
Compare 比较两个游标的先后顺序 返回值: -1(c<other), 0(c==other), 1(c>other)
func (*TimeEntityCursor) Equal ¶ added in v0.3.4
func (c *TimeEntityCursor) Equal(other *TimeEntityCursor) bool
Equal 判断两个游标是否相等
func (*TimeEntityCursor) GetTime ¶ added in v0.3.4
func (c *TimeEntityCursor) GetTime() (*time.Time, error)
GetTime 获取解析后的时间对象
func (*TimeEntityCursor) IsEmpty ¶ added in v0.3.4
func (c *TimeEntityCursor) IsEmpty() bool
IsEmpty 判断游标是否为空
func (*TimeEntityCursor) IsValid ¶ added in v0.3.4
func (c *TimeEntityCursor) IsValid() error
IsValid 验证游标是否有效(两个字段都有值且时间戳格式正确)
func (*TimeEntityCursor) String ¶ added in v0.3.4
func (c *TimeEntityCursor) String() string
String 返回游标的字符串表示(格式:时间戳|实体ID)
func (*TimeEntityCursor) ToBase64 ¶ added in v0.3.4
func (c *TimeEntityCursor) ToBase64() string
ToBase64 返回Base64编码的游标字符串(更安全的传输)
func (*TimeEntityCursor) ToJSON ¶ added in v0.3.4
func (c *TimeEntityCursor) ToJSON() (string, error)
ToJSON 序列化为JSON
type TimeEntityCursorBuilder ¶ added in v0.3.4
type TimeEntityCursorBuilder struct {
// contains filtered or unexported fields
}
TimeEntityCursorBuilder 游标构建器
func NewCursorBuilder ¶ added in v0.3.4
func NewCursorBuilder() *TimeEntityCursorBuilder
NewCursorBuilder 创建游标构建器
func (*TimeEntityCursorBuilder) Build ¶ added in v0.3.4
func (b *TimeEntityCursorBuilder) Build() (*TimeEntityCursor, error)
Build 构建游标
func (*TimeEntityCursorBuilder) MustBuild ¶ added in v0.3.4
func (b *TimeEntityCursorBuilder) MustBuild() *TimeEntityCursor
MustBuild 构建游标(如果验证失败则panic)
func (*TimeEntityCursorBuilder) WithEntityID ¶ added in v0.3.4
func (b *TimeEntityCursorBuilder) WithEntityID(entityID string) *TimeEntityCursorBuilder
WithEntityID 设置实体ID
func (*TimeEntityCursorBuilder) WithTime ¶ added in v0.3.4
func (b *TimeEntityCursorBuilder) WithTime(t *time.Time) *TimeEntityCursorBuilder
WithTime 设置时间对象
func (*TimeEntityCursorBuilder) WithTimestamp ¶ added in v0.3.4
func (b *TimeEntityCursorBuilder) WithTimestamp(timestamp string) *TimeEntityCursorBuilder
WithTimestamp 设置时间戳字符串
type TimeGroupBuilder ¶ added in v0.3.4
type TimeGroupBuilder struct {
// contains filtered or unexported fields
}
TimeGroupBuilder 时间分组聚合构建器
func NewTimeGroupBuilder ¶ added in v0.3.4
func NewTimeGroupBuilder(db *gorm.DB, tableName string, groupType TimeGroupType) *TimeGroupBuilder
NewTimeGroupBuilder 创建时间分组构建器
func (*TimeGroupBuilder) AddGroupBy ¶ added in v0.3.4
func (b *TimeGroupBuilder) AddGroupBy(fields ...string) *TimeGroupBuilder
AddGroupBy 添加额外的分组字段
func (*TimeGroupBuilder) Avg ¶ added in v0.3.4
func (b *TimeGroupBuilder) Avg(field, alias string) *TimeGroupBuilder
Avg 添加 AVG 聚合
func (*TimeGroupBuilder) Build ¶ added in v0.3.4
func (b *TimeGroupBuilder) Build() (*gorm.DB, []interface{})
Build 构建 SQL 查询
func (*TimeGroupBuilder) Count ¶ added in v0.3.4
func (b *TimeGroupBuilder) Count(alias string) *TimeGroupBuilder
Count 添加 COUNT 聚合
func (*TimeGroupBuilder) CountDistinct ¶ added in v0.3.4
func (b *TimeGroupBuilder) CountDistinct(field, alias string) *TimeGroupBuilder
CountDistinct 添加 COUNT DISTINCT 聚合
func (*TimeGroupBuilder) CountWhen ¶ added in v0.3.4
func (b *TimeGroupBuilder) CountWhen(condition, alias string, args ...interface{}) *TimeGroupBuilder
CountWhen 添加条件计数
func (*TimeGroupBuilder) Execute ¶ added in v0.3.4
func (b *TimeGroupBuilder) Execute(ctx context.Context) ([]map[string]interface{}, error)
Execute 执行查询并返回结果
func (*TimeGroupBuilder) ExecuteInto ¶ added in v0.3.4
func (b *TimeGroupBuilder) ExecuteInto(ctx context.Context, dest interface{}) error
ExecuteInto 执行查询并扫描到结构体切片
func (*TimeGroupBuilder) Having ¶ added in v0.3.4
func (b *TimeGroupBuilder) Having(condition string, args ...interface{}) *TimeGroupBuilder
Having 添加 HAVING 条件
func (*TimeGroupBuilder) Limit ¶ added in v0.3.4
func (b *TimeGroupBuilder) Limit(limit int) *TimeGroupBuilder
Limit 设置限制
func (*TimeGroupBuilder) Max ¶ added in v0.3.4
func (b *TimeGroupBuilder) Max(field, alias string) *TimeGroupBuilder
Max 添加 MAX 聚合
func (*TimeGroupBuilder) Min ¶ added in v0.3.4
func (b *TimeGroupBuilder) Min(field, alias string) *TimeGroupBuilder
Min 添加 MIN 聚合
func (*TimeGroupBuilder) OrderBy ¶ added in v0.3.4
func (b *TimeGroupBuilder) OrderBy(order string) *TimeGroupBuilder
OrderBy 设置排序
func (*TimeGroupBuilder) Sum ¶ added in v0.3.4
func (b *TimeGroupBuilder) Sum(field, alias string) *TimeGroupBuilder
Sum 添加 SUM 聚合
func (*TimeGroupBuilder) SumWhen ¶ added in v0.3.4
func (b *TimeGroupBuilder) SumWhen(condition, alias string, args ...interface{}) *TimeGroupBuilder
SumWhen 添加条件求和
func (*TimeGroupBuilder) Where ¶ added in v0.3.4
func (b *TimeGroupBuilder) Where(condition string, args ...interface{}) *TimeGroupBuilder
Where 添加 WHERE 条件
func (*TimeGroupBuilder) WithTimeField ¶ added in v0.3.4
func (b *TimeGroupBuilder) WithTimeField(field string) *TimeGroupBuilder
WithTimeField 设置时间字段
func (*TimeGroupBuilder) WithTimeRange ¶ added in v0.3.4
func (b *TimeGroupBuilder) WithTimeRange(start, end time.Time) *TimeGroupBuilder
WithTimeRange 设置时间范围
type TimeGroupType ¶ added in v0.3.4
type TimeGroupType string
TimeGroupType 时间分组类型
const ( GroupByHour TimeGroupType = "hour" // 按小时分组 GroupByDay TimeGroupType = "day" // 按天分组 GroupByWeek TimeGroupType = "week" // 按周分组 GroupByMonth TimeGroupType = "month" // 按月分组 GroupByYear TimeGroupType = "year" // 按年分组 )
type TimestampModel ¶ added in v0.1.2
type TimestampModel struct {
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;comment:更新时间"`
}
TimestampModel 仅包含时间戳的模型
type Transaction ¶
type Transaction[T any] interface { // 执行事务内的操作 Create(ctx context.Context, entity *T) error CreateBatch(ctx context.Context, entities ...*T) error Update(ctx context.Context, entity *T) error UpdateBatch(ctx context.Context, entities ...*T) error Delete(ctx context.Context, entity *T) error DeleteBatch(ctx context.Context, entities ...*T) error }
Transaction 事务接口
type UUIDModel ¶ added in v0.1.2
type UUIDModel struct {
ID string `json:"id" gorm:"primaryKey;type:char(36);comment:UUID主键"`
Version int `json:"version" gorm:"default:1;comment:版本号"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;comment:更新时间"`
DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"index;comment:删除时间"`
}
UUIDModel UUID作为主键的模型
func (*UUIDModel) BeforeUpdate ¶ added in v0.1.2
func (*UUIDModel) GetVersion ¶ added in v0.1.7
type VersionedModel ¶ added in v0.1.2
type VersionedModel interface {
ModelInterface
GetVersion() int
}
VersionedModel 支持版本控制的模型接口