Documentation
¶
Index ¶
- type Chain
- func (c *Chain) AddSensitiveField(field string, options SensitiveOptions) *Chain
- func (c *Chain) And(field string, op define.OpType, value interface{}) *Chain
- func (c *Chain) AndGroup(conditions []*define.Condition) *Chain
- func (c *Chain) BatchDelete(batchSize int) (int64, error)
- func (c *Chain) BatchInsert(batchSize int, enableConcurrent bool) (int64, error)
- func (c *Chain) BatchInsert2(models []interface{}, batchSize int, enableConcurrent bool) (int64, error)
- func (c *Chain) BatchInsertModels(models interface{}, batchSize int, enableConcurrent bool) (int64, error)
- func (c *Chain) BatchUpdate(batchSize int) (int64, error)
- func (c *Chain) BatchValues(values []map[string]interface{}) *Chain
- func (c *Chain) Begin() (*Chain, error)
- func (c *Chain) BeginChain() (*Chain, error)
- func (c *Chain) BeginNested() (*Chain, error)
- func (c *Chain) Between(field string, start, end interface{}) *Chain
- func (c *Chain) BuildSelect() *define.SqlProto
- func (c *Chain) Commit() error
- func (c *Chain) CommitNested() error
- func (c *Chain) Count() (int64, error)
- func (c *Chain) Count2(field string) (int64, error)
- func (c *Chain) CreateTable(model interface{}) error
- func (c *Chain) Delete(models ...interface{}) *define.Result
- func (c *Chain) Eq(field string, value interface{}) *Chain
- func (c *Chain) Exec() *define.Result
- func (c *Chain) Fields(fields ...string) *Chain
- func (c *Chain) First(dest ...interface{}) *define.Result
- func (c *Chain) From(model interface{}) *Chain
- func (c *Chain) Ge(field string, value interface{}) *Chain
- func (c *Chain) GetLastQueryStats() *QueryStats
- func (c *Chain) GetTransactionLevel() int
- func (c *Chain) GroupBy(fields ...string) *Chain
- func (c *Chain) Gt(field string, value interface{}) *Chain
- func (c *Chain) Having(condition interface{}, args ...interface{}) *Chain
- func (c *Chain) In(field string, value interface{}) *Chain
- func (c *Chain) InnerJoin(joinExpr string) *Chain
- func (c *Chain) Insert(model interface{}) *define.Result
- func (c *Chain) Into(dest interface{}) error
- func (c *Chain) IsInTransaction() bool
- func (c *Chain) IsNotNull(field string) *Chain
- func (c *Chain) IsNull(field string) *Chain
- func (c *Chain) Join(joinExpr string) *Chain
- func (c *Chain) Le(field string, value interface{}) *Chain
- func (c *Chain) LeftJoin(joinExpr string) *Chain
- func (c *Chain) Like(field string, value interface{}) *Chain
- func (c *Chain) Limit(count int) *Chain
- func (c *Chain) List(dest ...interface{}) *define.Result
- func (c *Chain) Lt(field string, value interface{}) *Chain
- func (c *Chain) Ne(field string, value interface{}) *Chain
- func (c *Chain) NewCondition() *define.Condition
- func (c *Chain) NotBetween(field string, start, end interface{}) *Chain
- func (c *Chain) NotIn(field string, value interface{}) *Chain
- func (c *Chain) NotLike(field string, value interface{}) *Chain
- func (c *Chain) Offset(count int) *Chain
- func (c *Chain) One(dest ...interface{}) *define.Result
- func (c *Chain) Or(field string, op define.OpType, value interface{}) *Chain
- func (c *Chain) OrBetween(field string, start, end interface{}) *Chain
- func (c *Chain) OrCond(cond *define.Condition) *Chain
- func (c *Chain) OrEq(field string, value interface{}) *Chain
- func (c *Chain) OrGe(field string, value interface{}) *Chain
- func (c *Chain) OrGroup(conditions []*define.Condition) *Chain
- func (c *Chain) OrGt(field string, value interface{}) *Chain
- func (c *Chain) OrIn(field string, value interface{}) *Chain
- func (c *Chain) OrIsNotNull(field string) *Chain
- func (c *Chain) OrIsNull(field string) *Chain
- func (c *Chain) OrLe(field string, value interface{}) *Chain
- func (c *Chain) OrLike(field string, value interface{}) *Chain
- func (c *Chain) OrLt(field string, value interface{}) *Chain
- func (c *Chain) OrNe(field string, value interface{}) *Chain
- func (c *Chain) OrNotBetween(field string, start, end interface{}) *Chain
- func (c *Chain) OrNotIn(field string, value interface{}) *Chain
- func (c *Chain) OrNotLike(field string, value interface{}) *Chain
- func (c *Chain) OrWhere(field string, op define.OpType, value interface{}) *Chain
- func (c *Chain) OrWhereGroup() *define.Condition
- func (c *Chain) OrWhereRaw(expr string, args ...interface{}) *Chain
- func (c *Chain) OrderBy(field string) *Chain
- func (c *Chain) OrderByDesc(field string) *Chain
- func (c *Chain) Page(pageNum, pageSize int) *Chain
- func (c *Chain) PageInfo(models ...interface{}) (*PageInfo, error)
- func (c *Chain) Query() *define.Result
- func (c *Chain) Raw(query string, args ...interface{}) *Chain
- func (c *Chain) RawExecute(sql string, args ...interface{}) define.Result
- func (c *Chain) RawQuery(sqlStr string, args ...interface{}) *define.Result
- func (c *Chain) ReleaseSavepoint(name string) error
- func (c *Chain) RightJoin(joinExpr string) *Chain
- func (c *Chain) Rollback() error
- func (c *Chain) RollbackNested() error
- func (c *Chain) RollbackTo(name string) error
- func (c *Chain) Savepoint(name string) error
- func (c *Chain) Set(field string, value interface{}) *Chain
- func (c *Chain) SetContext(ctx context.Context) *Chain
- func (c *Chain) SetEncryptionConfig(config *EncryptionConfig) *Chain
- func (c *Chain) SetIsolationLevel(level sql.IsolationLevel) *Chain
- func (c *Chain) Sets(fields map[string]interface{}) *Chain
- func (c *Chain) Sum(field string) (float64, error)
- func (c *Chain) Table(table string) *Chain
- func (c *Chain) Transaction(fn func(tx *Chain) error) error
- func (c *Chain) TransactionWithOptions(opts define.TransactionOptions, fn func(tx *Chain) error) error
- func (c *Chain) Update(models ...interface{}) *define.Result
- func (c *Chain) Values(fields map[string]interface{}) *Chain
- func (c *Chain) Where(field string, op define.OpType, value interface{}) *Chain
- func (c *Chain) Where2(cond *define.Condition) *Chain
- func (c *Chain) WhereGroup() *define.Condition
- func (c *Chain) WhereRaw(expr string, args ...interface{}) *Chain
- func (c *Chain) WithContext(ctx context.Context) *Chain
- type DB
- func (db *DB) Begin() (*sql.Tx, error)
- func (db *DB) BeginChain() (*Chain, error)
- func (db *DB) Chain() *Chain
- func (db *DB) Close() error
- func (db *DB) GenerateStruct(tableName, outputDir, packageName string) error
- func (db *DB) GenerateStructs(opts GenerateOptions) error
- func (db *DB) GetDB() *sql.DB
- func (db *DB) GetMetrics() DBMetrics
- func (db *DB) GetOptions() define.DBOptions
- func (db *DB) GetTableInfo(tableName string) (*define.TableInfo, error)
- func (db *DB) GetTableName(model interface{}) (string, error)
- func (db *DB) GetTableStruct(i any, table string) (*define.TableStruct, error)
- func (db *DB) GetTableStruct2(i any) (*define.TableStruct, error)
- func (db *DB) GetTables(pattern string) ([]string, error)
- func (db *DB) SetMaxOpenConns(n int)
- func (db *DB) Stats() sql.DBStats
- func (db *DB) UpdateOptions(opts define.DBOptions) error
- type DBError
- type DBErrorType
- type DBMetrics
- type EncryptionAlgorithm
- type EncryptionConfig
- type GenerateOptions
- type KeySource
- type PageInfo
- type QueryStats
- type SensitiveOptions
- type SensitiveType
- type TransactionOptions
- type TransactionPropagation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain represents the base chain structure
func NewChain ¶ added in v4.2.4
func NewChain(db *DB, factory define.SQLFactory) *Chain
NewChain creates a new Chain instance with the given database and factory
func (*Chain) AddSensitiveField ¶ added in v4.1.5
func (c *Chain) AddSensitiveField(field string, options SensitiveOptions) *Chain
AddSensitiveField adds a sensitive field to the chain
func (*Chain) BatchDelete ¶ added in v4.1.5
BatchDelete performs batch delete operation with the given batch size
func (*Chain) BatchInsert ¶ added in v4.1.3
BatchInsert performs batch insert operation with the given batch size
func (*Chain) BatchInsert2 ¶ added in v4.7.3
func (c *Chain) BatchInsert2(models []interface{}, batchSize int, enableConcurrent bool) (int64, error)
BatchInsert2 performs batch insert operation with a slice of struct models
func (*Chain) BatchInsertModels ¶ added in v4.7.3
func (c *Chain) BatchInsertModels(models interface{}, batchSize int, enableConcurrent bool) (int64, error)
BatchInsertModels 是 BatchInsert2 的简化版本,更易于使用 示例: db.Chain().BatchInsertModels(users, 1000, true)
func (*Chain) BatchUpdate ¶ added in v4.1.5
BatchUpdate performs batch update operation with the given batch size
func (*Chain) BatchValues ¶
BatchValues sets batch insert values
func (*Chain) BeginChain ¶ added in v4.1.5
BeginChain starts a new transaction and returns a Chain
func (*Chain) BeginNested ¶ added in v4.1.5
BeginNested starts a new nested transaction
func (*Chain) BuildSelect ¶ added in v4.2.4
BuildSelect builds a SELECT query
func (*Chain) CommitNested ¶ added in v4.1.5
CommitNested commits the current nested transaction
func (*Chain) Count ¶ added in v4.1.3
Count returns the count of records matching the current conditions
func (*Chain) CreateTable ¶
CreateTable creates a table based on the model struct
func (*Chain) GetLastQueryStats ¶ added in v4.1.5
func (c *Chain) GetLastQueryStats() *QueryStats
GetLastQueryStats returns the statistics of the last executed query
func (*Chain) GetTransactionLevel ¶ added in v4.1.5
GetTransactionLevel returns the current transaction nesting level
func (*Chain) IsInTransaction ¶
IsInTransaction returns whether the chain is currently in a transaction
func (*Chain) NewCondition ¶ added in v4.1.3
NewCondition creates a new condition with AND join type
func (*Chain) NotBetween ¶ added in v4.1.3
NotBetween adds a NOT BETWEEN condition
func (*Chain) OrIsNotNull ¶ added in v4.1.3
OrIsNotNull adds an OR IS NOT NULL condition
func (*Chain) OrNotBetween ¶ added in v4.1.3
OrNotBetween adds an OR NOT BETWEEN condition
func (*Chain) OrWhereGroup ¶ added in v4.1.3
OrWhereGroup starts a new condition group with OR join type
func (*Chain) OrWhereRaw ¶ added in v4.5.5
OrWhereRaw adds a raw SQL expression as an OR WHERE condition
func (*Chain) OrderByDesc ¶
OrderByDesc adds a descending order by clause
func (*Chain) PageInfo ¶ added in v4.1.3
PageInfo executes a paginated query and returns pagination information
func (*Chain) RawExecute ¶
RawExecute executes a raw SQL query
func (*Chain) ReleaseSavepoint ¶
ReleaseSavepoint releases the specified savepoint
func (*Chain) RollbackNested ¶ added in v4.1.5
RollbackNested rolls back to the last savepoint
func (*Chain) RollbackTo ¶
RollbackTo rolls back to the specified savepoint
func (*Chain) SetContext ¶ added in v4.6.4
SetContext 设置当前链对象的上下文 注意: 这将修改当前对象,而不是创建新对象
func (*Chain) SetEncryptionConfig ¶ added in v4.4.1
func (c *Chain) SetEncryptionConfig(config *EncryptionConfig) *Chain
SetEncryptionConfig sets the encryption configuration for the chain
func (*Chain) SetIsolationLevel ¶
func (c *Chain) SetIsolationLevel(level sql.IsolationLevel) *Chain
SetIsolationLevel sets the isolation level for the next transaction
func (*Chain) Transaction ¶
Transaction executes a function within a transaction
func (*Chain) TransactionWithOptions ¶ added in v4.1.5
func (c *Chain) TransactionWithOptions(opts define.TransactionOptions, fn func(tx *Chain) error) error
TransactionWithOptions starts a new transaction with options
func (*Chain) WhereGroup ¶ added in v4.1.3
WhereGroup starts a new condition group with AND join type
type DB ¶
type DB struct {
sync.RWMutex
DB *sql.DB
Factory define.SQLFactory
RoutineID int64
// contains filtered or unexported fields
}
DB represents the database connection
func OpenWithDefaults ¶ added in v4.1.4
OpenWithDefaults creates a new DB connection with default options
func (*DB) BeginChain ¶ added in v4.1.5
BeginChain starts a new transaction and returns a Chain
func (*DB) GenerateStruct ¶
GenerateStruct 生成单个表的结构体代码
func (*DB) GenerateStructs ¶
func (db *DB) GenerateStructs(opts GenerateOptions) error
GenerateStructs 批量生成表的结构体代码
func (*DB) GetMetrics ¶ added in v4.1.5
GetMetrics returns the current database metrics
func (*DB) GetOptions ¶ added in v4.1.4
GetOptions returns the current database options
func (*DB) GetTableInfo ¶
GetTableInfo 获取表信息
func (*DB) GetTableName ¶ added in v4.1.3
GetTableName returns the table name for a model
func (*DB) GetTableStruct ¶ added in v4.2.8
func (*DB) GetTableStruct2 ¶ added in v4.2.8
func (db *DB) GetTableStruct2(i any) (*define.TableStruct, error)
func (*DB) SetMaxOpenConns ¶ added in v4.5.0
type DBError ¶ added in v4.1.3
type DBError struct {
Type DBErrorType
Op string
Err error
Details string
Query string // Optional, for debugging (only set in debug mode)
}
DBError represents a database operation error with enhanced context
type DBErrorType ¶ added in v4.1.5
type DBErrorType int
DBErrorType represents specific types of database errors
const ( ErrConnection DBErrorType = iota ErrQuery ErrTransaction ErrValidation ErrConfiguration )
type DBMetrics ¶ added in v4.1.5
type DBMetrics struct {
OpenConnections int64 // Current number of open connections
InUseConnections int64 // Current number of connections in use
IdleConnections int64 // Current number of idle connections
WaitCount int64 // Total number of connections waited for
WaitDuration time.Duration // Total time waited for connections
MaxIdleTimeClosed int64 // Number of connections closed due to max idle time
}
DBMetrics tracks database connection pool statistics
type EncryptionAlgorithm ¶ added in v4.1.5
type EncryptionAlgorithm string
EncryptionAlgorithm defines the encryption algorithm to use
const ( // AES256 uses AES-256 encryption AES256 EncryptionAlgorithm = "AES256" // AES192 uses AES-192 encryption AES192 EncryptionAlgorithm = "AES192" // AES128 uses AES-128 encryption AES128 EncryptionAlgorithm = "AES128" )
type EncryptionConfig ¶ added in v4.1.5
type EncryptionConfig struct {
Algorithm EncryptionAlgorithm `json:"algorithm"`
KeyRotation time.Duration `json:"key_rotation"`
KeySource KeySource `json:"key_source"`
KeySourceConfig map[string]string `json:"key_source_config"`
}
EncryptionConfig represents configuration for data encryption
type GenerateOptions ¶
type GenerateOptions struct {
OutputDir string // 输出目录
PackageName string // 包名
Pattern string // 表名匹配模式
}
GenerateOptions 代码生成选项
type KeySource ¶ added in v4.1.5
type KeySource string
KeySource defines where encryption keys are sourced from
type PageInfo ¶ added in v4.1.3
type PageInfo struct {
PageNum int `json:"pageNum"` // 当前页码
PageSize int `json:"pageSize"` // 每页大小
Total int64 `json:"total"` // 总记录数
Pages int `json:"pages"` // 总页数
HasPrev bool `json:"hasPrev"` // 是否有上一页
HasNext bool `json:"hasNext"` // 是否有下一页
List interface{} `json:"list"` // 当前页数据
IsFirstPage bool `json:"isFirstPage"` // 是否是第一页
IsLastPage bool `json:"isLastPage"` // 是否是最后页
}
PageInfo represents pagination information
type QueryStats ¶ added in v4.1.5
type QueryStats struct {
SQL string
Duration time.Duration
RowsAffected int64
StartTime time.Time
Args []interface{}
}
QueryStats tracks statistics for a single query execution
type SensitiveOptions ¶ added in v4.1.5
type SensitiveOptions struct {
Type SensitiveType
Encryption *security.EncryptionConfig
Mask string
}
SensitiveOptions represents options for sensitive data handling
type SensitiveType ¶ added in v4.1.5
type SensitiveType int
SensitiveType defines the type of sensitive data
const ( // SensitiveNone indicates no sensitivity SensitiveNone SensitiveType = iota // SensitivePhone for phone numbers SensitivePhone // SensitiveEmail for email addresses SensitiveEmail // SensitiveIDCard for ID card numbers SensitiveIDCard // SensitiveBankCard for bank card numbers SensitiveBankCard // SensitiveAddress for addresses SensitiveAddress // SensitiveEncrypted for encrypted data SensitiveEncrypted // SensitiveMasked for masked data SensitiveMasked )
type TransactionOptions ¶ added in v4.1.5
type TransactionOptions struct {
Timeout time.Duration
IsolationLevel sql.IsolationLevel
PropagationMode TransactionPropagation
ReadOnly bool
}
TransactionOptions represents options for transaction
type TransactionPropagation ¶ added in v4.1.5
type TransactionPropagation int
TransactionPropagation defines transaction propagation behavior
const ( // PropagationRequired starts a new transaction if none exists PropagationRequired TransactionPropagation = iota // PropagationRequiresNew always starts a new transaction PropagationRequiresNew // PropagationNested starts a nested transaction if possible PropagationNested // PropagationSupports uses existing transaction if available PropagationSupports // PropagationNotSupported suspends current transaction if exists PropagationNotSupported // PropagationNever throws exception if transaction exists PropagationNever // PropagationMandatory throws exception if no transaction exists PropagationMandatory )