database

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DatabaseConnection added in v0.3.3

type DatabaseConnection interface {
	// Type 返回数据库类型
	Type() DatabaseType
	// Connect 建立连接
	Connect() error
	// Close 关闭连接
	Close() error
	// HealthCheck 健康检查
	HealthCheck(ctx context.Context) error
	// GetClient 获取原始客户端
	GetClient() interface{}
}

DatabaseConnection 数据库连接接口

type DatabaseType added in v0.3.3

type DatabaseType string

DatabaseType 数据库类型

const (
	MySQL   DatabaseType = "mysql"
	Redis   DatabaseType = "redis"
	MongoDB DatabaseType = "mongodb"
)

type MongoConfig added in v0.3.3

type MongoConfig struct {
	// 显式连接 URI(优先级最高)
	URL string `json:"url,omitempty" mapstructure:"url"`

	// 分离的连接参数(推荐使用,便于通过环境变量配置)
	Host     string `json:"host,omitempty"     mapstructure:"host"`     // 主机地址,格式: host:port
	Username string `json:"username,omitempty" mapstructure:"username"` // 用户名
	Password string `json:"-"                  mapstructure:"password"` // 密码(不输出到JSON)
	Database string `json:"database,omitempty" mapstructure:"database"` // 数据库名

	ReplicaSet       string `json:"replica-set,omitempty" mapstructure:"replica-set"`
	DirectConnection bool   `json:"direct-connection,omitempty" mapstructure:"direct-connection"`

	UseSSL                   bool   `json:"use-ssl" mapstructure:"use-ssl"`
	SSLInsecureSkipVerify    bool   `json:"ssl-insecure-skip-verify" mapstructure:"ssl-insecure-skip-verify"`
	SSLAllowInvalidHostnames bool   `json:"ssl-allow-invalid-hostnames" mapstructure:"ssl-allow-invalid-hostnames"`
	SSLCAFile                string `json:"ssl-ca-file" mapstructure:"ssl-ca-file"`
	SSLPEMKeyfile            string `json:"ssl-pem-keyfile" mapstructure:"ssl-pem-keyfile"`

	// 日志配置
	EnableLogger  bool          `json:"enable-logger"  mapstructure:"enable-logger"`  // 是否启用日志
	SlowThreshold time.Duration `json:"slow-threshold" mapstructure:"slow-threshold"` // 慢查询阈值
	// 详细日志配置
	LogCommandDetail bool `json:"log-command-detail" mapstructure:"log-command-detail"`
	LogReplyDetail   bool `json:"log-reply-detail"   mapstructure:"log-reply-detail"`
	LogStarted       bool `json:"log-started"        mapstructure:"log-started"`
}

MongoConfig MongoDB 数据库配置

func (*MongoConfig) BuildURL added in v0.3.5

func (c *MongoConfig) BuildURL() string

BuildURL 根据配置参数构建 MongoDB 连接 URL

type MongoDBConnection added in v0.3.3

type MongoDBConnection struct {
	// contains filtered or unexported fields
}

MongoDBConnection MongoDB 连接实现

func NewMongoDBConnection added in v0.3.3

func NewMongoDBConnection(config *MongoConfig) *MongoDBConnection

NewMongoDBConnection 创建 MongoDB 连接

func (*MongoDBConnection) Close added in v0.3.3

func (m *MongoDBConnection) Close() error

Close 关闭 MongoDB 连接

func (*MongoDBConnection) Connect added in v0.3.3

func (m *MongoDBConnection) Connect() error

Connect 连接 MongoDB 数据库

func (*MongoDBConnection) GetClient added in v0.3.3

func (m *MongoDBConnection) GetClient() interface{}

GetClient 获取 MongoDB 客户端

func (*MongoDBConnection) HealthCheck added in v0.3.3

func (m *MongoDBConnection) HealthCheck(ctx context.Context) error

HealthCheck 检查 MongoDB 连接是否健康

func (*MongoDBConnection) Type added in v0.3.3

func (m *MongoDBConnection) Type() DatabaseType

Type 返回数据库类型

type MySQLConfig added in v0.3.3

type MySQLConfig struct {
	Host                  string        `json:"host" mapstructure:"host"`
	Username              string        `json:"username" mapstructure:"username"`
	Password              string        `json:"password" mapstructure:"password"`
	Database              string        `json:"database" mapstructure:"database"`
	MaxIdleConnections    int           `json:"max-idle-connections" mapstructure:"max-idle-connections"`
	MaxOpenConnections    int           `json:"max-open-connections" mapstructure:"max-open-connections"`
	MaxConnectionLifeTime time.Duration `json:"max-connection-life-time" mapstructure:"max-connection-life-time"`
	LogLevel              int           `json:"log-level" mapstructure:"log-level"`
	Logger                logger.Interface
}

MySQLConfig MySQL 数据库配置

type MySQLConnection added in v0.3.3

type MySQLConnection struct {
	// contains filtered or unexported fields
}

MySQLConnection MySQL 连接实现

func NewMySQLConnection added in v0.3.3

func NewMySQLConnection(config *MySQLConfig) *MySQLConnection

NewMySQLConnection 创建 MySQL 连接

func (*MySQLConnection) Close added in v0.3.3

func (m *MySQLConnection) Close() error

Close 关闭 MySQL 连接

func (*MySQLConnection) Connect added in v0.3.3

func (m *MySQLConnection) Connect() error

Connect 连接 MySQL 数据库

func (*MySQLConnection) GetClient added in v0.3.3

func (m *MySQLConnection) GetClient() interface{}

GetClient 获取 MySQL 客户端

func (*MySQLConnection) HealthCheck added in v0.3.3

func (m *MySQLConnection) HealthCheck(ctx context.Context) error

HealthCheck 检查 MySQL 连接是否健康

func (*MySQLConnection) Type added in v0.3.3

func (m *MySQLConnection) Type() DatabaseType

Type 返回数据库类型

type NamedRedisRegistry added in v0.4.8

type NamedRedisRegistry struct {
	// contains filtered or unexported fields
}

NamedRedisRegistry manages a default Redis connection plus optional named profiles.

func NewNamedRedisRegistry added in v0.4.8

func NewNamedRedisRegistry(defaultConfig *RedisConfig, profiles map[string]*RedisConfig) *NamedRedisRegistry

NewNamedRedisRegistry creates a Redis registry with an optional default config and a set of optional named profile configs.

func (*NamedRedisRegistry) Close added in v0.4.8

func (r *NamedRedisRegistry) Close() error

Close closes every initialized Redis connection.

func (*NamedRedisRegistry) Connect added in v0.4.8

func (r *NamedRedisRegistry) Connect() error

Connect initializes the default and named Redis connections.

func (*NamedRedisRegistry) GetClient added in v0.4.8

func (r *NamedRedisRegistry) GetClient(name string) (goredis.UniversalClient, error)

GetClient returns the named profile Redis client. Missing profiles fall back to default, while configured-but-unavailable profiles return an error.

func (*NamedRedisRegistry) GetConnection added in v0.4.8

func (r *NamedRedisRegistry) GetConnection(name string) (*RedisConnection, error)

GetConnection returns the named profile connection. Missing profiles fall back to default, while configured-but-unavailable profiles return an error.

func (*NamedRedisRegistry) HasConnections added in v0.4.8

func (r *NamedRedisRegistry) HasConnections() bool

HasConnections reports whether the registry has at least one configured connection.

func (*NamedRedisRegistry) HealthCheck added in v0.4.8

func (r *NamedRedisRegistry) HealthCheck(ctx context.Context) error

HealthCheck validates the default and named Redis connections.

func (*NamedRedisRegistry) ProfileStatus added in v0.4.9

func (r *NamedRedisRegistry) ProfileStatus(name string) RedisProfileStatus

ProfileStatus reports whether a named profile is missing, available, or unavailable.

func (*NamedRedisRegistry) ProfileStatuses added in v0.4.9

func (r *NamedRedisRegistry) ProfileStatuses() map[string]RedisProfileStatus

ProfileStatuses returns the current status of every configured named profile.

type RedisConfig added in v0.3.3

type RedisConfig struct {
	Host                  string   `json:"host" mapstructure:"host"`
	Port                  int      `json:"port" mapstructure:"port"`
	Addrs                 []string `json:"addrs" mapstructure:"addrs"`
	Username              string   `json:"username" mapstructure:"username"`
	Password              string   `json:"password" mapstructure:"password"`
	Database              int      `json:"database" mapstructure:"database"`
	MaxIdle               int      `json:"max-idle" mapstructure:"max-idle"`
	MaxActive             int      `json:"max-active" mapstructure:"max-active"`
	Timeout               int      `json:"timeout" mapstructure:"timeout"`
	MinIdleConns          int      `json:"min-idle-conns" mapstructure:"min-idle-conns"`
	PoolTimeout           int      `json:"pool-timeout" mapstructure:"pool-timeout"`
	DialTimeout           int      `json:"dial-timeout" mapstructure:"dial-timeout"`
	ReadTimeout           int      `json:"read-timeout" mapstructure:"read-timeout"`
	WriteTimeout          int      `json:"write-timeout" mapstructure:"write-timeout"`
	EnableCluster         bool     `json:"enable-cluster" mapstructure:"enable-cluster"`
	UseSSL                bool     `json:"use-ssl" mapstructure:"use-ssl"`
	SSLInsecureSkipVerify bool     `json:"ssl-insecure-skip-verify" mapstructure:"ssl-insecure-skip-verify"`
}

RedisConfig Redis 数据库配置

type RedisConnection added in v0.3.3

type RedisConnection struct {
	// contains filtered or unexported fields
}

RedisConnection Redis 连接实现

func NewRedisConnection added in v0.3.3

func NewRedisConnection(config *RedisConfig) *RedisConnection

NewRedisConnection 创建 Redis 连接

func (*RedisConnection) Close added in v0.3.3

func (r *RedisConnection) Close() error

Close 关闭 Redis 连接

func (*RedisConnection) Connect added in v0.3.3

func (r *RedisConnection) Connect() error

Connect 连接 Redis 数据库

func (*RedisConnection) GetClient added in v0.3.3

func (r *RedisConnection) GetClient() interface{}

GetClient 获取 Redis 客户端

func (*RedisConnection) HealthCheck added in v0.3.3

func (r *RedisConnection) HealthCheck(ctx context.Context) error

HealthCheck 检查 Redis 连接是否健康

func (*RedisConnection) Type added in v0.3.3

func (r *RedisConnection) Type() DatabaseType

Type 返回数据库类型

type RedisProfileState added in v0.4.9

type RedisProfileState string
const (
	RedisProfileStateMissing     RedisProfileState = "missing"
	RedisProfileStateAvailable   RedisProfileState = "available"
	RedisProfileStateUnavailable RedisProfileState = "unavailable"
)

type RedisProfileStatus added in v0.4.9

type RedisProfileStatus struct {
	Name        string
	State       RedisProfileState
	Err         error
	NextRetryAt time.Time
}

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry 数据库注册器

func NewRegistry

func NewRegistry() *Registry

NewRegistry 创建数据库注册器

func (*Registry) Close

func (r *Registry) Close() error

Close 关闭所有数据库连接

func (*Registry) GetClient

func (r *Registry) GetClient(dbType DatabaseType) (interface{}, error)

GetClient 获取数据库客户端

func (*Registry) GetConnection

func (r *Registry) GetConnection(dbType DatabaseType) (DatabaseConnection, error)

GetConnection 获取数据库连接

func (*Registry) HealthCheck

func (r *Registry) HealthCheck(ctx context.Context) error

HealthCheck 健康检查

func (*Registry) Init

func (r *Registry) Init() error

Init 初始化所有数据库连接

func (*Registry) Register

func (r *Registry) Register(dbType DatabaseType, config interface{}, connection DatabaseConnection) error

Register 注册数据库连接

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL