Documentation
¶
Overview ¶
Package mysql 提供 MySQL 数据库的 ORM 封装,基于 GORM,包含连接创建、连接池配置、表名处理与分页查询等辅助方法。
Index ¶
- type AllTableName
- type FindPageOptions
- type Instance
- func (c *Instance) AddError(err error)
- func (c *Instance) Debug() *Instance
- func (c *Instance) EnableSQLLogging(debuggerLogger debugger.LoggerInterface, opts ...interface{}) *Instance
- func (c *Instance) Error() []error
- func (c *Instance) FindForPage(model interface{}, options *FindPageOptions) (jcbaseGo.ListData, error)
- func (c *Instance) GetAllTableName() (tableNames []AllTableName, err error)
- func (c *Instance) GetConf() interface{}
- func (c *Instance) GetDb() *gorm.DB
- func (c *Instance) GetDebuggerLogger() debugger.LoggerInterface
- func (c *Instance) SetDebuggerLogger(debuggerLogger debugger.LoggerInterface)
- func (c *Instance) TableName(tableName *string, quotes ...bool) *Instance
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AllTableName ¶
type AllTableName struct {
TableName string `gorm:"table_name"`
}
AllTableName 用于映射 information_schema 查询结果的表名字段。
type FindPageOptions ¶ added in v0.11.1
type FindPageOptions struct {
// 查询配置
Page int `default:"1"` // 页码,默认 1
PageSize int `default:"10"` // 分页大小,默认 10,最大 1000
ShowDeleted bool `default:"false"` // 是否显示软删除数据,默认 false
// 模型配置
PkId string `default:"id"` // 主键字段名,默认 "id"
ModelTableAlias string `default:""` // 模型表别名,默认为表名
// 回调
ListQuery func(*gorm.DB) *gorm.DB // 可选,自定义查询回调
ListSelect func(*gorm.DB) *gorm.DB // 可选,自定义查询字段回调
ListOrder func() interface{} // 可选,自定义排序回调
ListEach func(interface{}) interface{} // 可选,自定义遍历回调
ListReturn func(jcbaseGo.ListData) jcbaseGo.ListData // 可选,自定义返回回调
}
FindPageOptions 定义分页查询选项,语义与 CRUD list 保持一致;
type Instance ¶
type Instance struct {
Dsn string
Conf jcbaseGo.DbStruct
Db *gorm.DB
Errors []error
// contains filtered or unexported fields
}
Instance 表示 MySQL 连接实例,封装 DSN、数据库配置、连接句柄、调试状态与错误收集。
func NewWithDebugger ¶ added in v0.13.8
func NewWithDebugger(dbConfig jcbaseGo.DbStruct, debuggerLogger debugger.LoggerInterface, opts ...string) *Instance
NewWithDebugger 创建MySQL实例并集成debugger日志记录 参数:
- dbConfig: 数据库配置
- debuggerLogger: debugger日志记录器
- opts: 可选参数,第一个参数为配置别名
返回:
- *Instance: MySQL实例
func (*Instance) EnableSQLLogging ¶ added in v0.13.8
func (c *Instance) EnableSQLLogging(debuggerLogger debugger.LoggerInterface, opts ...interface{}) *Instance
EnableSQLLogging 为当前数据库实例启用SQL日志记录 参数:
- debuggerLogger: debugger日志记录器
- logLevel: GORM日志级别(可选,默认logger.Info)
- slowThreshold: 慢查询阈值(可选,默认200ms)
返回:
- *Instance: 支持SQL日志记录的数据库实例
func (*Instance) FindForPage ¶ added in v0.11.1
func (c *Instance) FindForPage(model interface{}, options *FindPageOptions) (jcbaseGo.ListData, error)
FindForPage 按分页选项查询并返回列表数据;
func (*Instance) GetAllTableName ¶
func (c *Instance) GetAllTableName() (tableNames []AllTableName, err error)
GetAllTableName 查询并返回当前数据库中的所有表名(过滤视图与系统表)。
func (*Instance) GetConf ¶ added in v0.10.2
func (c *Instance) GetConf() interface{}
GetConf 返回当前实例的原始配置结构。
func (*Instance) GetDb ¶
GetDb 返回当前数据库连接;若已开启调试模式返回 Debug 包装的连接。 调试模式优先级:debuggerLogger > debug标志 注意:此方法为只读方法,不会修改实例状态
func (*Instance) GetDebuggerLogger ¶ added in v0.13.8
func (c *Instance) GetDebuggerLogger() debugger.LoggerInterface
GetDebuggerLogger 获取debugger日志记录器
func (*Instance) SetDebuggerLogger ¶ added in v0.13.8
func (c *Instance) SetDebuggerLogger(debuggerLogger debugger.LoggerInterface)
SetDebuggerLogger 设置debugger日志记录器 参数:
- debuggerLogger: debugger日志记录器实例
Click to show internal directories.
Click to hide internal directories.