Documentation
¶
Index ¶
- func Ctx(ctx context.Context) *gorm.DB
- func CtxWith(ctx context.Context, key string) *gorm.DB
- func Default() *gorm.DB
- func RecordNotFound(err error) bool
- func RegisterDriver(t Driver, f func(*Config) gorm.Dialector)
- func Use(key string) *gorm.DB
- func WithContext(ctx context.Context, key string) context.Context
- func WithContextDB(ctx context.Context, db *gorm.DB) context.Context
- type Config
- type DB
- type Driver
- type GroupConfig
- type Logger
- func (l *Logger) Error(ctx context.Context, msg string, args ...interface{})
- func (l *Logger) Info(ctx context.Context, msg string, args ...interface{})
- func (l *Logger) LogMode(level logger.LogLevel) logger.Interface
- func (l *Logger) ParamsFilter(ctx context.Context, sql string, params ...interface{}) (string, []interface{})
- func (l *Logger) Trace(ctx context.Context, begin time.Time, ...)
- func (l *Logger) Warn(ctx context.Context, msg string, args ...interface{})
- type Option
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterDriver ¶
RegisterDriver 注册数据库驱动 默认会注册mysql t 数据库驱动名 f 数据库驱动函数 传入配置返回 gorm.Dialector
func WithContext ¶
WithContext 根据 key 参数指定数据库,并将数据库实例存入 context 如果你想修改后续的数据库实例,你可以使用 db.WithContext ctx:=db.WithContext(ctx, "db2") SomeMethod(ctx)
Types ¶
type Config ¶
type Config struct {
Driver Driver `yaml:"driver"`
Dsn string `yaml:"dsn"`
Prefix string `yaml:"prefix"`
// 开启 gorm 的 debug 模式
Debug bool `yaml:"debug"`
// 读写分离,以后再说吧
//WriterDsn []string `yaml:"writerDsn,omitempty"` // 写入数据源
//ReaderDsn []string `yaml:"readerDsn,omitempty"` // 读取数据源
// gorm配置
PrepareStmt bool `yaml:"prepareStmt,omitempty"` // 是否开启预编译
}
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
func Database ¶
func Database() *DB
Database gorm数据库封装,支持多数据库,如果你配置了 trace 的话会自动开启链路追踪 默认注册和使用 mysql 驱动,如果你需要其他驱动,可以使用 RegisterDriver 注册 或者导入对应的数据库驱动,例如: import _ "clickhouse"
func (*DB) CloseHandle ¶
func (d *DB) CloseHandle()
type GroupConfig ¶
type Logger ¶
func NewLogger ¶
NewLogger create new logger 自定义了gorm的日志输出,会屏蔽掉一些gorm的ErrRecordNotFound错误 将日志输出重定向到了cago的日志库
func (*Logger) ParamsFilter ¶
func (l *Logger) ParamsFilter(ctx context.Context, sql string, params ...interface{}) (string, []interface{})
ParamsFilter Trace print sql message
Click to show internal directories.
Click to hide internal directories.