Documentation
¶
Index ¶
- Variables
- func RegisterFactory(driver string, factory SQLFactory)
- func UnregisterFactory(driver string)
- type BatchOptions
- type ColumnInfo
- type Condition
- func Between(field string, start, end interface{}) *Condition
- func Eq(field string, value interface{}) *Condition
- func Ge(field string, value interface{}) *Condition
- func Gt(field string, value interface{}) *Condition
- func In(field string, values ...interface{}) *Condition
- func IsNotNull(field string) *Condition
- func IsNull(field string) *Condition
- func Le(field string, value interface{}) *Condition
- func Like(field string, value interface{}) *Condition
- func Lt(field string, value interface{}) *Condition
- func Ne(field string, value interface{}) *Condition
- func NewCondition(field string, op OpType, value interface{}) *Condition
- func NotBetween(field string, start, end interface{}) *Condition
- func NotIn(field string, values ...interface{}) *Condition
- func NotLike(field string, value interface{}) *Condition
- type Conditions
- type DBOptions
- type ITableModel
- type IsolationLevel
- type JoinType
- type LockType
- type OpType
- type OrderBy
- type OrderType
- type Result
- type SQLFactory
- type TableInfo
- type Transaction
- type TransactionFunc
- type TransactionOptions
- type TransactionPropagation
Constants ¶
This section is empty.
Variables ¶
var Debug bool
Debug flag for enabling debug mode
var ErrManualRollback = errors.New("manual rollback")
ErrManualRollback is used to manually trigger a transaction rollback
Functions ¶
func RegisterFactory ¶
func RegisterFactory(driver string, factory SQLFactory)
RegisterFactory registers a SQL factory for a specific driver
func UnregisterFactory ¶
func UnregisterFactory(driver string)
UnregisterFactory removes a SQL factory for a specific driver
Types ¶
type BatchOptions ¶ added in v4.1.4
type BatchOptions struct {
// BatchSize is the size of each batch
BatchSize int
// Concurrency is the number of concurrent goroutines for batch processing
// If set to 0, defaults to 1 (no concurrency)
Concurrency int
// Timeout is the maximum duration for the entire batch operation
// If set to 0, no timeout is applied
Timeout time.Duration
// RetryCount is the number of times to retry a failed batch
// If set to 0, no retries are attempted
RetryCount int
// RetryInterval is the duration to wait between retries
// If set to 0, defaults to 1 second
RetryInterval time.Duration
}
BatchOptions defines configuration options for batch operations
func DefaultBatchOptions ¶ added in v4.1.4
func DefaultBatchOptions() BatchOptions
DefaultBatchOptions returns the default batch operation options
func (*BatchOptions) Validate ¶ added in v4.1.4
func (o *BatchOptions) Validate() error
Validate validates the batch options and sets defaults if necessary
type ColumnInfo ¶
type ColumnInfo struct {
Name string // 列名
Type string // 数据库类型
Length int64 // 长度
Precision int // 精度
Scale int // 小数位数
IsNullable bool // 是否可空
IsPrimaryKey bool // 是否主键
IsAutoIncrement bool // 是否自增
DefaultValue string // 默认值
Comment string // 注释
}
ColumnInfo 列信息
type Condition ¶
type Condition struct {
Field string
Op OpType
Value interface{}
JoinType JoinType
SubConds []*Condition // Sub-conditions for nested queries
IsSubGroup bool // Whether this is a sub-group of conditions
}
Condition represents a WHERE condition
func In ¶ added in v4.1.3
In creates an IN condition with variadic parameters that may include arrays
func NewCondition ¶ added in v4.1.3
NewCondition creates a new condition
func NotBetween ¶ added in v4.1.3
func NotIn ¶ added in v4.1.3
NotIn creates a NOT IN condition with variadic parameters that may include arrays
type Conditions ¶ added in v4.1.5
type Conditions []Condition
Conditions represents a slice of Condition
type DBOptions ¶ added in v4.1.4
type DBOptions struct {
// MaxOpenConns is the maximum number of open connections to the database
// If MaxOpenConns <= 0, then there is no limit on the number of open connections
MaxOpenConns int
// MaxIdleConns is the maximum number of connections in the idle connection pool
// If MaxIdleConns <= 0, no idle connections are retained
MaxIdleConns int
// ConnMaxLifetime is the maximum amount of time a connection may be reused
// If ConnMaxLifetime <= 0, connections are not closed due to their age
ConnMaxLifetime time.Duration
// ConnMaxIdleTime is the maximum amount of time a connection may be idle
// If ConnMaxIdleTime <= 0, connections are not closed due to idle time
ConnMaxIdleTime time.Duration
// Debug enables debug logging of SQL queries
Debug bool
}
DBOptions defines database connection and pool configuration
func DefaultDBOptions ¶ added in v4.1.4
func DefaultDBOptions() DBOptions
DefaultDBOptions returns the default database options
type ITableModel ¶
type ITableModel interface {
// TableName returns the custom table name
TableName() string
// CreateSql returns the custom CREATE TABLE SQL statement
CreateSql() string
}
ITableModel defines the interface for custom table models
type IsolationLevel ¶ added in v4.1.5
type IsolationLevel sql.IsolationLevel
IsolationLevel represents the transaction isolation level
const ( // LevelDefault is the default isolation level for the database LevelDefault IsolationLevel = IsolationLevel(sql.LevelDefault) // LevelReadUncommitted is the read uncommitted isolation level LevelReadUncommitted IsolationLevel = IsolationLevel(sql.LevelReadUncommitted) // LevelReadCommitted is the read committed isolation level LevelReadCommitted IsolationLevel = IsolationLevel(sql.LevelReadCommitted) // LevelWriteCommitted is the write committed isolation level LevelWriteCommitted IsolationLevel = IsolationLevel(sql.LevelWriteCommitted) // LevelRepeatableRead is the repeatable read isolation level LevelRepeatableRead IsolationLevel = IsolationLevel(sql.LevelRepeatableRead) // LevelSnapshot is the snapshot isolation level LevelSnapshot IsolationLevel = IsolationLevel(sql.LevelSnapshot) // LevelSerializable is the serializable isolation level LevelSerializable IsolationLevel = IsolationLevel(sql.LevelSerializable) // LevelLinearizable is the linearizable isolation level LevelLinearizable IsolationLevel = IsolationLevel(sql.LevelLinearizable) )
type OpType ¶ added in v4.1.3
type OpType int
OpType represents the type of operation
const ( // OpEq represents equals operation OpEq OpType = iota // OpNe represents not equals operation OpNe // OpGt represents greater than operation OpGt // OpGe represents greater than or equals operation OpGe // OpLt represents less than operation OpLt // OpLe represents less than or equals operation OpLe // OpLike represents LIKE operation OpLike // OpNotLike represents NOT LIKE operation OpNotLike // OpIn represents IN operation OpIn // OpNotIn represents NOT IN operation OpNotIn // OpIsNull represents IS NULL operation OpIsNull // OpIsNotNull represents IS NOT NULL operation OpIsNotNull // OpBetween represents BETWEEN operation OpBetween // OpNotBetween represents NOT BETWEEN operation OpNotBetween // OpCustom represents custom operation OpCustom )
type Result ¶
type Result struct {
ID int64
Affected int64
Error error
Data []map[string]any `json:"data"`
Columns []string `json:"columns"`
}
Result implements sql.Result interface and includes query result functionality
func (*Result) LastInsertId ¶
LastInsertId returns the last inserted ID
func (*Result) RowsAffected ¶
RowsAffected returns the number of rows affected
type SQLFactory ¶
type SQLFactory interface {
// Connect creates a new database connection
Connect(dsn string) (*sql.DB, error)
// GetType returns the database type (e.g., "mysql", "postgres")
GetType() string
// BuildSelect builds a SELECT query
BuildSelect(table string, fields []string, conditions []*Condition, orderBy string, limit, offset int) (string, []interface{})
// BuildUpdate builds an UPDATE query
BuildUpdate(table string, fields map[string]interface{}, fieldOrder []string, conditions []*Condition) (string, []interface{})
// BuildInsert builds an INSERT query
BuildInsert(table string, fields map[string]interface{}, fieldOrder []string) (string, []interface{})
// BuildBatchInsert builds a batch INSERT query
BuildBatchInsert(table string, values []map[string]interface{}) (string, []interface{})
// BuildDelete builds a DELETE query
BuildDelete(table string, conditions []*Condition) (string, []interface{})
// BuildCreateTable builds a CREATE TABLE query
BuildCreateTable(table string, modelType reflect.Type) string
// GetTableInfo 获取表信息
GetTableInfo(db *sql.DB, tableName string) (*TableInfo, error)
// GetTables 获取符合模式的所有表
// pattern: 表名匹配模式,支持 * 通配符
// 对于 PostgreSQL,pattern 可以是 schema.table 格式
GetTables(db *sql.DB, pattern string) ([]string, error)
// BuildOrderBy builds the ORDER BY clause
BuildOrderBy(orders []OrderBy) string
}
SQLFactory defines the interface for SQL query builders
func GetFactory ¶
func GetFactory(driver string) (SQLFactory, error)
GetFactory returns the SQL factory for a specific driver
type TableInfo ¶
type TableInfo struct {
TableName string // 表名
TableComment string // 表注释
PrimaryKeys []string // 主键列表
Columns []ColumnInfo // 列信息
HasDecimal bool // 是否包含 Decimal 类型
HasUUID bool // 是否包含 UUID 类型
HasIP bool // 是否包含 IP 类型
}
TableInfo 表信息
type Transaction ¶ added in v4.1.5
type Transaction struct {
// contains filtered or unexported fields
}
Transaction represents a database transaction
func NewTransaction ¶ added in v4.1.5
func NewTransaction(db interface{}, tx interface{}, driver string) *Transaction
NewTransaction creates a new transaction
func (*Transaction) GetDB ¶ added in v4.1.5
func (t *Transaction) GetDB() interface{}
GetDB returns the database instance
func (*Transaction) GetDriver ¶ added in v4.1.5
func (t *Transaction) GetDriver() string
GetDriver returns the database driver name
func (*Transaction) GetTx ¶ added in v4.1.5
func (t *Transaction) GetTx() interface{}
GetTx returns the transaction instance
type TransactionFunc ¶ added in v4.1.5
type TransactionFunc func(tx *Transaction) Result
TransactionFunc represents a function that executes within a transaction
type TransactionOptions ¶ added in v4.1.5
type TransactionOptions struct {
Timeout time.Duration
IsolationLevel 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 )