Documentation
¶
Index ¶
- Variables
- func GetConnMaxIdleTime(maxIdleTime int) time.Duration
- func GetConnMaxLifetime(maxLifetime int) time.Duration
- func NewClickHouseConnection(chCfg ClickHouseConfig, commonCfg Database) (*gorm.DB, error)
- func ProvideClickHouse(manager Manager) *gorm.DB
- func Read() dbresolver.Operation
- func ReadDB(db *gorm.DB) *gorm.DB
- func Use(name string) clause.Expression
- func UseResolver(db *gorm.DB, resolverName string) *gorm.DB
- func UseResolverRead(db *gorm.DB, resolverName string) *gorm.DB
- func UseResolverWrite(db *gorm.DB, resolverName string) *gorm.DB
- func Write() dbresolver.Operation
- func WriteDB(db *gorm.DB) *gorm.DB
- type ClickHouseConfig
- type Database
- type DatabaseSourceConfig
- type GormLoggerAdapter
- func (l *GormLoggerAdapter) Error(ctx context.Context, msg string, data ...interface{})
- func (l *GormLoggerAdapter) Info(ctx context.Context, msg string, data ...interface{})
- func (l *GormLoggerAdapter) LogMode(level logger.LogLevel) logger.Interface
- func (l *GormLoggerAdapter) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
- func (l *GormLoggerAdapter) Warn(ctx context.Context, msg string, data ...interface{})
- type IDatabase
- type Manager
- type MySQLConfig
Constants ¶
This section is empty.
Variables ¶
var ProviderSet = wire.NewSet( ProvideManager, ProvideClickHouse, ProvideIDatabase, )
ProviderSet provides database-related dependencies
Functions ¶
func GetConnMaxIdleTime ¶
GetConnMaxIdleTime returns ConnMaxIdleTime as time.Duration from common config
func GetConnMaxLifetime ¶
GetConnMaxLifetime returns ConnMaxLifetime as time.Duration from common config
func NewClickHouseConnection ¶
func NewClickHouseConnection(chCfg ClickHouseConfig, commonCfg Database) (*gorm.DB, error)
NewClickHouseConnection creates a ClickHouse GORM connection This is the unified function for creating ClickHouse connections using GORM
func ProvideClickHouse ¶
ProvideClickHouse provides ClickHouse database instance from Manager Returns nil if ClickHouse is not configured (optional)
func Read ¶
func Read() dbresolver.Operation
Read forces the query to use replicas (read-only) Usage: db.Clauses(Read()).Find(&users)
func ReadDB ¶
ReadDB returns a DB instance configured for read operations (replicas) This is a convenience method for read-only queries
func Use ¶
func Use(name string) clause.Expression
Use specifies which resolver to use (for named resolvers) Usage: db.Clauses(Use("secondary")).Find(&orders)
func UseResolver ¶
UseResolver returns a DB instance configured to use a specific named resolver This is useful when you have multiple resolvers registered
func UseResolverRead ¶
UseResolverRead returns a DB instance configured to use a specific named resolver in read mode
func UseResolverWrite ¶
UseResolverWrite returns a DB instance configured to use a specific named resolver in write mode
func Write ¶
func Write() dbresolver.Operation
Write forces the query to use sources (write) Usage: db.Clauses(Write()).First(&user)
Types ¶
type ClickHouseConfig ¶
type ClickHouseConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
DBName string `mapstructure:"dbname"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
DialTimeout int `mapstructure:"dialTimeout"` // Dial timeout in seconds
ReadTimeout int `mapstructure:"readTimeout"` // Read timeout in seconds
}
ClickHouseConfig represents ClickHouse data source configuration
type Database ¶
type Database struct {
// Common configuration for all data sources
OutPut bool `mapstructure:"output"`
MaxOpenConns int `mapstructure:"maxOpenConns"`
MaxIdleConns int `mapstructure:"maxIdleConns"`
MaxLifetime int `mapstructure:"maxLifeTime"`
MaxIdleTime int `mapstructure:"maxIdleTime"`
// Data source configurations
MySQL MySQLConfig `mapstructure:"mysql"`
ClickHouse ClickHouseConfig `mapstructure:"clickhouse"`
}
Database represents the database configuration with common settings and data sources
type DatabaseSourceConfig ¶
type DatabaseSourceConfig struct {
Host string
Port string
User string
Password string
DBName string
}
DatabaseSourceConfig represents a single database source/replica configuration
type GormLoggerAdapter ¶
func NewGormLoggerAdapter ¶
func NewGormLoggerAdapter(config logger.Config, logLevel logger.LogLevel) *GormLoggerAdapter
func (*GormLoggerAdapter) Error ¶
func (l *GormLoggerAdapter) Error(ctx context.Context, msg string, data ...interface{})
func (*GormLoggerAdapter) Info ¶
func (l *GormLoggerAdapter) Info(ctx context.Context, msg string, data ...interface{})
func (*GormLoggerAdapter) LogMode ¶
func (l *GormLoggerAdapter) LogMode(level logger.LogLevel) logger.Interface
type IDatabase ¶
type IDatabase interface {
// Database returns the underlying *gorm.DB (MySQL)
Database() *gorm.DB
}
IDatabase defines database interface for backward compatibility It provides access to the underlying MySQL database connection
func NewDatabaseAdapter ¶
NewDatabaseAdapter creates an IDatabase adapter from Manager
func ProvideIDatabase ¶
ProvideIDatabase provides IDatabase interface instance for backward compatibility
type Manager ¶
type Manager interface {
// MySQL returns the MySQL database connection
MySQL() *gorm.DB
// ClickHouse returns the ClickHouse database connection
ClickHouse() *gorm.DB
// Close closes all database connections
Close() error
}
Manager defines the unified database interface for managing MySQL and ClickHouse connections
func NewManager ¶
NewManager creates a new database manager with MySQL and ClickHouse connections
type MySQLConfig ¶
type MySQLConfig struct {
Host string `mapstructure:"host"`
Port string `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DBName string `mapstructure:"dbname"`
// Primary and Replicas for DBResolver support
// If Primary is empty, use Host/Port/User/Password/DBName as the default source
// If Replicas is empty, no read-write separation will be configured
Primary []DatabaseSourceConfig `mapstructure:"primary"`
Replicas []DatabaseSourceConfig `mapstructure:"replicas"`
}
MySQLConfig represents MySQL data source configuration