core

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

contrib/dao/base/enumerable.go

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultBatchInsertOption = &BatchInsertOption{
	BatchSize:    1000,
	UseNamedExec: false,
}

DefaultBatchInsertOption 默认的批量插入配置

View Source
var DefaultBatchUpdateOption = &BatchUpdateOption{
	BatchSize: 1000,
}

DefaultBatchUpdateOption 默认的批量更新配置

Functions

This section is empty.

Types

type AggregateInfo

type AggregateInfo struct {
	Field    string
	Function string
	Alias    string
}

AggregateInfo 定义聚合操作的信息

type AggregateResult

type AggregateResult struct {
	Groups map[string]interface{} // 分组字段的值
	Sums   map[string]float64     // 各字段的sum结果
	Count  int64                  // 该组的记录数
}

AggregateResult 用于存储聚合结果 AggregateResult 优化结构

type BatchInsertOption

type BatchInsertOption struct {
	BatchSize    int  // 每批次处理的数据量
	UseNamedExec bool // 是否使用NamedExec方式
}

BatchInsertOption 批量插入的配置选项

type BatchUpdateOption

type BatchUpdateOption struct {
	BatchSize       int      // 每批次处理的数据量
	UpdateFields    []string // 需要更新的字段
	KeyField        string   // 用于WHERE条件的键字段
	AdditionalWhere goqu.Ex  // 附加的WHERE条件
}

BatchUpdateOption 批量更新的配置选项

type DBLogger

type DBLogger struct {
	*sqlx.DB
	// contains filtered or unexported fields
}

DBLogger wraps sqlx.DB with logging capabilities

func ConnectMySQL

func ConnectMySQL(dsn string, logger *zap.Logger, prefix string) (*DBLogger, error)

ConnectMySQL connects to MySQL database

func NewDBLogger

func NewDBLogger(db *sqlx.DB, logger *zap.Logger, prefix string) *DBLogger

NewDBLogger creates a new DBLogger instance

func (*DBLogger) Begin

func (db *DBLogger) Begin() (*Tx, error)

Begin starts a transaction

func (*DBLogger) ExecContext

func (db *DBLogger) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext executes a query with context

func (*DBLogger) GetPrefix

func (db *DBLogger) GetPrefix() string

GetPrefix returns the database prefix

func (*DBLogger) QueryContext

func (db *DBLogger) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext queries with context

type DialectType

type DialectType string
const (
	MySQL     DialectType = "mysql"
	StarRocks DialectType = "starrocks"
)

type Enumerable

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

Enumerable 实现

func (*Enumerable[T]) All

func (e *Enumerable[T]) All(predicate func(T) bool) bool

func (*Enumerable[T]) Any

func (e *Enumerable[T]) Any(predicate func(T) bool) bool

func (*Enumerable[T]) Average

func (e *Enumerable[T]) Average(selector func(T) float64) float64

func (*Enumerable[T]) Count

func (e *Enumerable[T]) Count() int

func (*Enumerable[T]) Distinct

func (e *Enumerable[T]) Distinct(comparer func(T, T) bool) IEnumerable[T]

func (*Enumerable[T]) FirstOrDefault

func (e *Enumerable[T]) FirstOrDefault(predicate func(T) bool) *T

func (*Enumerable[T]) GroupBy

func (e *Enumerable[T]) GroupBy(keySelector func(T) interface{}) map[interface{}][]T

func (*Enumerable[T]) LastOrDefault

func (e *Enumerable[T]) LastOrDefault(predicate func(T) bool) *T

func (*Enumerable[T]) Max

func (e *Enumerable[T]) Max(selector func(T) float64) float64

func (*Enumerable[T]) Min

func (e *Enumerable[T]) Min(selector func(T) float64) float64

func (*Enumerable[T]) OrderBy

func (e *Enumerable[T]) OrderBy(less func(T, T) bool) IEnumerable[T]

OrderBy 排序

func (*Enumerable[T]) Select

func (e *Enumerable[T]) Select(selector func(T) interface{}) IEnumerable[interface{}]

Select 投影

func (*Enumerable[T]) Skip

func (e *Enumerable[T]) Skip(count int) IEnumerable[T]

func (*Enumerable[T]) Sum

func (e *Enumerable[T]) Sum(selector func(T) float64) float64

func (*Enumerable[T]) Take

func (e *Enumerable[T]) Take(count int) IEnumerable[T]

func (*Enumerable[T]) ToList

func (e *Enumerable[T]) ToList() []T

func (*Enumerable[T]) Where

func (e *Enumerable[T]) Where(predicate func(T) bool) IEnumerable[T]

type GroupAggregateBuilder

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

GroupAggregateBuilder 用于构建分组聚合查询

func NewGroupAggregateBuilder

func NewGroupAggregateBuilder[T any]() *GroupAggregateBuilder[T]

NewGroupAggregateBuilder 创建分组聚合构建器

func (*GroupAggregateBuilder[T]) Average

func (b *GroupAggregateBuilder[T]) Average(field string) *GroupAggregateBuilder[T]

Average 添加平均值聚合

func (*GroupAggregateBuilder[T]) Count

func (b *GroupAggregateBuilder[T]) Count() *GroupAggregateBuilder[T]

Count 添加计数聚合

func (*GroupAggregateBuilder[T]) GetAggregations

func (b *GroupAggregateBuilder[T]) GetAggregations() []AggregateInfo

GetAggregations 获取所有聚合操作

func (*GroupAggregateBuilder[T]) Max

func (b *GroupAggregateBuilder[T]) Max(field string) *GroupAggregateBuilder[T]

Max 添加最大值聚合

func (*GroupAggregateBuilder[T]) Min

func (b *GroupAggregateBuilder[T]) Min(field string) *GroupAggregateBuilder[T]

Min 添加最小值聚合

func (*GroupAggregateBuilder[T]) Sum

func (b *GroupAggregateBuilder[T]) Sum(field string) *GroupAggregateBuilder[T]

Sum 添加求和聚合

func (*GroupAggregateBuilder[T]) WithAlias

func (b *GroupAggregateBuilder[T]) WithAlias(alias string) *GroupAggregateBuilder[T]

WithAlias 为最后添加的聚合操作设置别名

type GroupField

type GroupField struct {
	Field string
	Alias string
}

GroupField 结构用于定义分组字段

type GroupResult

type GroupResult[TKey any, TElement any] struct {
	// contains filtered or unexported fields
}

func (*GroupResult[TKey, TElement]) Elements

func (g *GroupResult[TKey, TElement]) Elements() []TElement

func (*GroupResult[TKey, TElement]) Key

func (g *GroupResult[TKey, TElement]) Key() TKey

type GroupingQuery

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

func (*GroupingQuery[T]) Aggregate

func (g *GroupingQuery[T]) Aggregate(builder *GroupAggregateBuilder[T]) IQueryable[T]

func (*GroupingQuery[T]) Average

func (g *GroupingQuery[T]) Average(field string) (map[interface{}]float64, error)

func (*GroupingQuery[T]) Count

func (g *GroupingQuery[T]) Count() (map[interface{}]int64, error)

func (*GroupingQuery[T]) Having

func (g *GroupingQuery[T]) Having(condition goqu.Ex) IGroupingQuery[T]

在 group.go 中添加 Having 方法的实现

func (*GroupingQuery[T]) Select

func (g *GroupingQuery[T]) Select(selector func(key interface{}, elements []T) interface{}) IQueryable[T]

func (*GroupingQuery[T]) Sum

func (g *GroupingQuery[T]) Sum(field string) (map[interface{}]float64, error)

type IEnumerable

type IEnumerable[T any] interface {
	Where(predicate func(T) bool) IEnumerable[T]
	Select(selector func(T) interface{}) IEnumerable[interface{}]
	OrderBy(less func(T, T) bool) IEnumerable[T]
	Skip(count int) IEnumerable[T]
	Take(count int) IEnumerable[T]
	FirstOrDefault(predicate func(T) bool) *T
	LastOrDefault(predicate func(T) bool) *T
	ToList() []T
	Count() int
	Any(predicate func(T) bool) bool
	All(predicate func(T) bool) bool
	Sum(selector func(T) float64) float64
	Max(selector func(T) float64) float64
	Min(selector func(T) float64) float64
	Average(selector func(T) float64) float64
	GroupBy(keySelector func(T) interface{}) map[interface{}][]T
	Distinct(comparer func(T, T) bool) IEnumerable[T]
}

IEnumerable 内存操作接口

func NewEnumerable

func NewEnumerable[T any](data []T) IEnumerable[T]

type IGrouping

type IGrouping[TKey any, TElement any] interface {
	Key() TKey
	Elements() []TElement
}

type IGroupingQuery

type IGroupingQuery[T any] interface {
	// 基础聚合操作
	Count() (map[interface{}]int64, error)
	Sum(field string) (map[interface{}]float64, error)
	Average(field string) (map[interface{}]float64, error)

	// 高级操作
	Select(selector func(key interface{}, elements []T) interface{}) IQueryable[T]
	Having(condition goqu.Ex) IGroupingQuery[T]

	// 链式聚合操作
	Aggregate(builder *GroupAggregateBuilder[T]) IQueryable[T]
}

IGroupingQuery 分组查询接口

type IJoinable

type IJoinable[T any] interface {
	Join(table string, on map[string]string) IQueryable[T]
	LeftJoin(table string, on map[string]string) IQueryable[T]
	RightJoin(table string, on map[string]string) IQueryable[T]
	InnerJoin(table string, on map[string]string) IQueryable[T]
}

IJoinable 接口定义简化的连表操作

type IQueryable

type IQueryable[T any] interface {
	// 现有的链式操作
	Where(condition goqu.Ex) IQueryable[T]
	WhereRaw(condition string, args ...interface{}) IQueryable[T]
	OrderBy(cols ...string) IQueryable[T]
	OrderByRaw(order string) IQueryable[T]
	Skip(offset int) IQueryable[T]
	Take(take int) IQueryable[T]
	Limit(limit int) IQueryable[T]
	Scan(dest interface{}) error
	ScanTx(ctx context.Context, dest interface{}) error
	ScanVal() (interface{}, error)
	ScanInt64() (int64, error)
	ScanFloat64() (float64, error)
	ScanString() (string, error)
	ScanInt64Slice() ([]int64, error)
	ScanInt() (int, error)

	Select(cols ...interface{}) IQueryable[T]
	SelectRaw(cols ...string) IQueryable[T] // 原生 SQL 查询
	// 分组操作 - 新增 Lambda 风格
	GroupBy(keySelector func(T) interface{}) IGroupingQuery[T]
	// 保留原有的字符串方式,用于简单场景
	GroupByColumns(cols ...string) IQueryable[T]

	// 连接操作
	IJoinable[T]

	// 执行方法
	FirstOrDefault() (*T, error)

	ToListTx(ctx context.Context) ([]*T, error)

	CountTx(ctx context.Context) (int64, error)

	ToGroupedListTx(ctx context.Context) ([]*T, error)
	AnyTx(ctx context.Context, condition goqu.Ex) (bool, error)
	SumTx(ctx context.Context, field string) (float64, error)
	MaxTx(ctx context.Context, field string) (interface{}, error)
	MinTx(ctx context.Context, field string) (interface{}, error)
	ToPagedListTx(ctx context.Context, page, size int, condition goqu.Ex) (*PageResult[T], error)
	ToInt64SliceTx(ctx context.Context) ([]int64, error)
	ToStringSliceTx(ctx context.Context) ([]string, error)
	ToFloat64SliceTx(ctx context.Context) ([]float64, error)
	ToMapSliceTx(ctx context.Context) ([]map[string]interface{}, error)
	ToMapTx(ctx context.Context) (map[string]interface{}, error)
	ToStructTx(ctx context.Context) (*T, error)
	ToResultTx(ctx context.Context, result interface{}) error
	ToPagedResultTx(ctx context.Context, page, pageSize int, dest interface{}) (*PagedResult, error)
	OverTx(ctx context.Context, windowFunc string, partitionBy ...interface{}) IQueryable[T]
	ToLookupTx(ctx context.Context, keySelector func(T) interface{}) map[interface{}][]*T
	ToList() ([]*T, error)
	Count() (int64, error)
	Any(condition goqu.Ex) (bool, error)

	// 聚合方法
	Sum(field string) (float64, error)
	Max(field string) (interface{}, error)
	Min(field string) (interface{}, error)

	// 分页相关
	ToPagedList(page, size int, condition goqu.Ex) (*PageResult[T], error)
	ToPagedListWithTotal(page, size int, condition goqu.Ex) ([]*T, int64, error)
	ToPagedResult(page, pageSize int, dest interface{}) (*PagedResult, error)

	// 结果转换
	ToInt64Slice() ([]int64, error)
	ToStringSlice() ([]string, error)
	ToFloat64Slice() ([]float64, error)
	ToMapSlice() ([]map[string]interface{}, error)
	ToMap() (map[string]interface{}, error)
	ToStruct() (*T, error)
	ToResult(result interface{}) error

	// 特殊查询
	Over(windowFunc string, partitionBy ...interface{}) IQueryable[T]
	ToLookup(keySelector func(T) interface{}) map[interface{}][]*T
	ToSQL() (sql string, params []interface{}, err error)
}

IQueryable 接口增加 Lambda 风格的分组方法

type IReadRepository

type IReadRepository[T any] interface {
	Query() IQueryable[T]
}

contrib/dao/base/interfaces.go

type IRepository

type IRepository[T any] interface {
	IReadRepository[T]
	IWriteRepository[T]
}

type IUnitOfWork

type IUnitOfWork interface {
	GetTx() *Tx
	Begin() error
	Commit() error
	Rollback() error
}

----------------------------------------------------------工作单元---------------------------------------------------------- IUnitOfWork 工作单元接口

type IWriteRepository

type IWriteRepository[T any] interface {
	Create(entity *T) error
	Update(entity *T) error
	UpdateByCondition(condition goqu.Ex, entity *T) error
	UpdateFieldsByCondition(condition goqu.Ex, fields map[string]interface{}) error
	BatchCreate(entities []*T) error
	BatchDelete(condition goqu.Ex) error
	BatchInsert(entities []*T, opt *BatchInsertOption) error
	BatchUpdate(entities []*T, opt *BatchUpdateOption) error
}

type PageResult

type PageResult[T any] struct {
	Items    []*T
	Total    int64
	Page     int
	PageSize int
}

type PagedResult

type PagedResult struct {
	Total      int64 // 总记录数
	Page       int   // 当前页码
	PageSize   int   // 每页大小
	TotalPages int   // 总页数
}

type Queryable

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

func (*Queryable[T]) Any

func (q *Queryable[T]) Any(condition goqu.Ex) (bool, error)

func (*Queryable[T]) AnyTx

func (q *Queryable[T]) AnyTx(ctx context.Context, condition goqu.Ex) (bool, error)

func (*Queryable[T]) Count

func (q *Queryable[T]) Count() (int64, error)

func (*Queryable[T]) CountTx

func (q *Queryable[T]) CountTx(ctx context.Context) (int64, error)

func (*Queryable[T]) FirstOrDefault

func (q *Queryable[T]) FirstOrDefault() (*T, error)

需要添加的方法

func (*Queryable[T]) FirstOrDefaultTx

func (q *Queryable[T]) FirstOrDefaultTx(ctx context.Context) (*T, error)

FirstOrDefaultTx(ctx context.Context) (*T, error)

func (*Queryable[T]) GroupBy

func (q *Queryable[T]) GroupBy(keySelector func(T) interface{}) IGroupingQuery[T]

func (*Queryable[T]) GroupByColumns

func (q *Queryable[T]) GroupByColumns(cols ...string) IQueryable[T]

在 Queryable 中添加 GroupByColumns 实现

func (*Queryable[T]) GroupByHaving

func (q *Queryable[T]) GroupByHaving(having goqu.Ex) IQueryable[T]

func (*Queryable[T]) GroupSumMultiple

func (q *Queryable[T]) GroupSumMultiple(groupFields []GroupField, sumFields []string) ([]*AggregateResult, error)

GroupSumMultiple 实现

func (*Queryable[T]) InnerJoin

func (q *Queryable[T]) InnerJoin(table string, on map[string]string) IQueryable[T]

InnerJoin 实现

func (*Queryable[T]) Join

func (q *Queryable[T]) Join(table string, on map[string]string) IQueryable[T]

func (*Queryable[T]) LeftJoin

func (q *Queryable[T]) LeftJoin(table string, on map[string]string) IQueryable[T]

func (*Queryable[T]) Limit

func (q *Queryable[T]) Limit(limit int) IQueryable[T]

Limit(limit int) IQueryable[T]

func (*Queryable[T]) Max

func (q *Queryable[T]) Max(field string) (interface{}, error)

func (*Queryable[T]) MaxTx

func (q *Queryable[T]) MaxTx(ctx context.Context, field string) (interface{}, error)

MaxTx

func (*Queryable[T]) Min

func (q *Queryable[T]) Min(field string) (interface{}, error)

Min(field string) (interface{}, error)

func (*Queryable[T]) MinTx

func (q *Queryable[T]) MinTx(ctx context.Context, field string) (interface{}, error)

Min

func (*Queryable[T]) OrderBy

func (q *Queryable[T]) OrderBy(cols ...string) IQueryable[T]

func (*Queryable[T]) OrderByRaw

func (q *Queryable[T]) OrderByRaw(column string) IQueryable[T]

OrderByRaw 支持原始排序语句 OrderByRaw 支持原始排序语句

func (*Queryable[T]) Over

func (q *Queryable[T]) Over(windowFunc string, partitionBy ...interface{}) IQueryable[T]

支持窗口函数,如 ROW_NUMBER, RANK, DENSE_RANK 等

func (*Queryable[T]) OverTx

func (q *Queryable[T]) OverTx(ctx context.Context, windowFunc string, partitionBy ...interface{}) IQueryable[T]

OverTx

func (*Queryable[T]) RightJoin

func (q *Queryable[T]) RightJoin(table string, on map[string]string) IQueryable[T]

RightJoin 实现

func (*Queryable[T]) Scan

func (q *Queryable[T]) Scan(dest interface{}) error

写一个 Scan(&maxSort) 的方法

func (*Queryable[T]) ScanFloat64

func (q *Queryable[T]) ScanFloat64() (float64, error)

ScanFloat64() (float64, error)

func (*Queryable[T]) ScanInt

func (q *Queryable[T]) ScanInt() (int, error)

func (*Queryable[T]) ScanInt64

func (q *Queryable[T]) ScanInt64() (int64, error)

ScanInt64() (int64, error)

ScanString() (string, error)
ScanInt() (int, error)

func (*Queryable[T]) ScanInt64Slice

func (q *Queryable[T]) ScanInt64Slice() ([]int64, error)

ScanInt64Slice() ([]int64, error)

func (*Queryable[T]) ScanString

func (q *Queryable[T]) ScanString() (string, error)

func (*Queryable[T]) ScanTx

func (q *Queryable[T]) ScanTx(ctx context.Context, dest interface{}) error

ScanTx(ctx context.Context, dest interface{}) error

func (*Queryable[T]) ScanVal

func (q *Queryable[T]) ScanVal() (interface{}, error)

ScanVal() (interface{}, error)

func (*Queryable[T]) Select

func (q *Queryable[T]) Select(cols ...interface{}) IQueryable[T]

Select(cols ...interface{}) IQueryable[T]

func (*Queryable[T]) SelectRaw

func (q *Queryable[T]) SelectRaw(cols ...string) IQueryable[T]

func (*Queryable[T]) Skip

func (q *Queryable[T]) Skip(offset int) IQueryable[T]

func (*Queryable[T]) Sum

func (q *Queryable[T]) Sum(field string) (float64, error)

func (*Queryable[T]) SumTx

func (q *Queryable[T]) SumTx(ctx context.Context, field string) (float64, error)

sum

func (*Queryable[T]) Take

func (q *Queryable[T]) Take(limit int) IQueryable[T]

func (*Queryable[T]) ToFloat64Slice

func (q *Queryable[T]) ToFloat64Slice() ([]float64, error)

func (*Queryable[T]) ToFloat64SliceTx

func (q *Queryable[T]) ToFloat64SliceTx(ctx context.Context) ([]float64, error)

func (*Queryable[T]) ToGroupedList

func (q *Queryable[T]) ToGroupedList() ([]*T, error)

func (*Queryable[T]) ToGroupedListTx

func (q *Queryable[T]) ToGroupedListTx(ctx context.Context) ([]*T, error)

func (*Queryable[T]) ToInt64Slice

func (q *Queryable[T]) ToInt64Slice() ([]int64, error)

在 Queryable 中添加

func (*Queryable[T]) ToInt64SliceTx

func (q *Queryable[T]) ToInt64SliceTx(ctx context.Context) ([]int64, error)

func (*Queryable[T]) ToList

func (q *Queryable[T]) ToList() ([]*T, error)

func (*Queryable[T]) ToListTx

func (q *Queryable[T]) ToListTx(ctx context.Context) ([]*T, error)

func (*Queryable[T]) ToLookup

func (q *Queryable[T]) ToLookup(keySelector func(T) interface{}) map[interface{}][]*T

ToLookup(keySelector func(T) interface{}) map[interface{}][]*T

func (*Queryable[T]) ToLookupTx

func (q *Queryable[T]) ToLookupTx(ctx context.Context, keySelector func(T) interface{}) map[interface{}][]*T

ToLookupTx

func (*Queryable[T]) ToMap

func (q *Queryable[T]) ToMap() (map[string]interface{}, error)

func (*Queryable[T]) ToMapSlice

func (q *Queryable[T]) ToMapSlice() ([]map[string]interface{}, error)

func (*Queryable[T]) ToMapSliceTx

func (q *Queryable[T]) ToMapSliceTx(ctx context.Context) ([]map[string]interface{}, error)

func (*Queryable[T]) ToMapTx

func (q *Queryable[T]) ToMapTx(ctx context.Context) (map[string]interface{}, error)

func (*Queryable[T]) ToPagedList

func (q *Queryable[T]) ToPagedList(page, size int, condition goqu.Ex) (*PageResult[T], error)

在 Queryable 中添加

func (*Queryable[T]) ToPagedListTx

func (q *Queryable[T]) ToPagedListTx(ctx context.Context, page, size int, condition goqu.Ex) (*PageResult[T], error)

func (*Queryable[T]) ToPagedListWithTotal

func (q *Queryable[T]) ToPagedListWithTotal(page, size int, condition goqu.Ex) ([]*T, int64, error)

ToPagedListWithTotal(page, size int, condition goqu.Ex) ([]*T, int64, error)

func (*Queryable[T]) ToPagedResult

func (q *Queryable[T]) ToPagedResult(page, pageSize int, dest interface{}) (*PagedResult, error)

Queryable 实现

func (*Queryable[T]) ToPagedResultTx

func (q *Queryable[T]) ToPagedResultTx(ctx context.Context, page, pageSize int, dest interface{}) (*PagedResult, error)

ToPagedResultTx

func (*Queryable[T]) ToResult

func (q *Queryable[T]) ToResult(result interface{}) error

func (*Queryable[T]) ToResultTx

func (q *Queryable[T]) ToResultTx(ctx context.Context, result interface{}) error

func (*Queryable[T]) ToSQL

func (q *Queryable[T]) ToSQL() (sql string, params []interface{}, err error)

func (*Queryable[T]) ToStringSlice

func (q *Queryable[T]) ToStringSlice() ([]string, error)

func (*Queryable[T]) ToStringSliceTx

func (q *Queryable[T]) ToStringSliceTx(ctx context.Context) ([]string, error)

func (*Queryable[T]) ToStruct

func (q *Queryable[T]) ToStruct() (*T, error)

func (*Queryable[T]) ToStructTx

func (q *Queryable[T]) ToStructTx(ctx context.Context) (*T, error)

func (*Queryable[T]) Where

func (q *Queryable[T]) Where(condition goqu.Ex) IQueryable[T]

func (*Queryable[T]) WhereRaw

func (q *Queryable[T]) WhereRaw(condition string, args ...interface{}) IQueryable[T]

type Repository

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

func NewRepository

func NewRepository[T any](db *DBLogger, table string, dbType DialectType) *Repository[T]

func (*Repository[T]) BatchCreate

func (r *Repository[T]) BatchCreate(entities []*T) error

func (*Repository[T]) BatchDelete

func (r *Repository[T]) BatchDelete(condition goqu.Ex) error

BatchDelete - 批量删除

func (*Repository[T]) BatchDeleteWithTx

func (r *Repository[T]) BatchDeleteWithTx(condition goqu.Ex) error

BatchDeleteWithTx

func (*Repository[T]) BatchInsert

func (r *Repository[T]) BatchInsert(entities []*T, opt *BatchInsertOption) error

BatchInsert 批量插入数据的通用方法

func (*Repository[T]) BatchUpdate

func (r *Repository[T]) BatchUpdate(entities []*T, opt *BatchUpdateOption) error

BatchUpdate 批量更新数据 entities 要更新的实体数组 opt 更新选项

func (*Repository[T]) Create

func (r *Repository[T]) Create(entity *T) error

func (*Repository[T]) CreateAndReturnID

func (r *Repository[T]) CreateAndReturnID(entity *T) (int64, error)

func (*Repository[T]) CreateAndReturnIDWithTx

func (r *Repository[T]) CreateAndReturnIDWithTx(entity *T) (int64, error)

func (*Repository[T]) CreateWithTx

func (r *Repository[T]) CreateWithTx(entity *T) error

func (*Repository[T]) GetDB

func (r *Repository[T]) GetDB() *DBLogger

获取db

func (*Repository[T]) GetPrefix

func (r *Repository[T]) GetPrefix() string

获取前缀

func (*Repository[T]) Query

func (r *Repository[T]) Query() IQueryable[T]

Query returns a queryable interface for building queries

Example

Example of how to use the repository (documentation)

// This example shows the basic usage pattern
// In a real scenario, initialize db properly

var db *DBLogger // Initialize from ConnectMySQL
repo := NewRepository[TestEntity](db, "test_entities", MySQL)

// Query with conditions
_ = repo.Query().
	Where(goqu.Ex{"status": 1}).
	OrderByRaw("created_at DESC").
	Limit(10)
Output:
(example only, won't actually run)

func (*Repository[T]) QueryFrom

func (r *Repository[T]) QueryFrom(dbType DialectType) IQueryable[T]

func (*Repository[T]) QuerySingle

func (r *Repository[T]) QuerySingle(condition goqu.Ex) (*T, error)

写一个方法根据条件查询单个对象

func (*Repository[T]) QuerySingleTx

func (r *Repository[T]) QuerySingleTx(ctx context.Context, condition goqu.Ex) (*T, error)

QuerySingleTx 带事务的 工作单元

func (*Repository[T]) ScanFloat64

func (r *Repository[T]) ScanFloat64() (float64, error)

ScanFloat64() (float64, error)

func (*Repository[T]) ScanInt64Slice

func (r *Repository[T]) ScanInt64Slice() ([]int64, error)

ScanInt64Slice() ([]int64, error)

func (*Repository[T]) ScanTx

func (r *Repository[T]) ScanTx(ctx context.Context, dest interface{}) error

ScanTx(ctx context.Context, dest interface{}) error

func (*Repository[T]) ToSQL

func (r *Repository[T]) ToSQL() (sql string, params []interface{}, err error)

func (*Repository[T]) Update

func (r *Repository[T]) Update(entity *T) error

func (*Repository[T]) UpdateByCondition

func (r *Repository[T]) UpdateByCondition(condition goqu.Ex, entity *T) error

继续写

func (*Repository[T]) UpdateByConditionWithTx

func (r *Repository[T]) UpdateByConditionWithTx(condition goqu.Ex, entity *T) error

UpdateByConditionWithTx

func (*Repository[T]) UpdateFieldsByCondition

func (r *Repository[T]) UpdateFieldsByCondition(condition goqu.Ex, fields map[string]interface{}) error

更新指定字段 根据指定条件

func (*Repository[T]) UpdateFieldsByConditionWithTx

func (r *Repository[T]) UpdateFieldsByConditionWithTx(condition goqu.Ex, fields map[string]interface{}) error

UpdateFieldsByConditionWithTx

func (*Repository[T]) UpdateFieldsById

func (r *Repository[T]) UpdateFieldsById(id int64, fields map[string]interface{}) error

UpdateFieldsById(current.Id, map[string]interface{}{

func (*Repository[T]) UpdateFieldsByIdWithTx

func (r *Repository[T]) UpdateFieldsByIdWithTx(id int64, fields map[string]interface{}) error

UpdateFieldsByIdWithTx

func (*Repository[T]) UpdateFieldsByIds

func (r *Repository[T]) UpdateFieldsByIds(ids []int64, fields map[string]interface{}) error

func (*Repository[T]) UpdateWithTx

func (r *Repository[T]) UpdateWithTx(entity *T) error

UpdateWithTx(entity)

func (*Repository[T]) WithUnitOfWork

func (r *Repository[T]) WithUnitOfWork(uow IUnitOfWork) *Repository[T]

type Tx

type Tx = sqlx.Tx

Tx wraps sqlx.Tx for transaction operations

type UnitOfWork

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

UnitOfWork 实现

func NewUnitOfWork

func NewUnitOfWork(db *DBLogger) *UnitOfWork

func (*UnitOfWork) Begin

func (u *UnitOfWork) Begin() error

工作单元方法实现

func (*UnitOfWork) Commit

func (u *UnitOfWork) Commit() error

func (*UnitOfWork) GetTx

func (u *UnitOfWork) GetTx() *Tx

func (*UnitOfWork) Rollback

func (u *UnitOfWork) Rollback() error

func (*UnitOfWork) RunInTransaction

func (u *UnitOfWork) RunInTransaction(fn func(IUnitOfWork) error) error

辅助函数

Jump to

Keyboard shortcuts

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