database

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 23 Imported by: 6 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	RedisProfileStateMissing     = redisruntime.ProfileStateMissing
	RedisProfileStateAvailable   = redisruntime.ProfileStateAvailable
	RedisProfileStateUnavailable = redisruntime.ProfileStateUnavailable
)

Variables

This section is empty.

Functions

func ConfigureMySQLTLS added in v0.8.1

func ConfigureMySQLTLS(config *mysqldriver.Config, mode, caFile string) error

ConfigureMySQLTLS applies the same certificate policy to maintenance clients that already own a driver Config (including their timeouts and DSN options).

func NewMySQLConnector added in v0.8.1

func NewMySQLConnector(config *MySQLConfig) (driver.Connector, error)

NewMySQLConnector preserves TLS verification on every pooled connection. Passing a typed Config avoids losing the CA context during DSN serialization.

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"`
	// Location controls DATETIME parsing. Empty preserves the historical Local
	// behavior for existing callers.
	Location string `json:"location" mapstructure:"location"`
	// SessionTimeZone is applied by the MySQL driver whenever it opens a pooled
	// connection. Empty leaves the server/session default unchanged.
	SessionTimeZone string `json:"session-time-zone" mapstructure:"session-time-zone"`
	Logger          logger.Interface
	TLSMode         string `json:"tls-mode" mapstructure:"tls-mode"`
	TLSCAFile       string `json:"tls-ca-file" mapstructure:"tls-ca-file"`
}

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 管理默认 Redis 连接与可选命名 profile。

func NewNamedRedisRegistry added in v0.4.8

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

NewNamedRedisRegistry 使用可选默认配置与命名 profile 配置创建 Redis 注册表。

func (*NamedRedisRegistry) Close added in v0.4.8

func (r *NamedRedisRegistry) Close() error

Close 关闭所有已初始化的 Redis 连接。

func (*NamedRedisRegistry) Connect added in v0.4.8

func (r *NamedRedisRegistry) Connect() error

Connect 初始化默认连接与命名 profile 连接。

func (*NamedRedisRegistry) GetClient added in v0.4.8

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

GetClient 返回指定命名 profile 的 Redis 客户端。 缺失 profile 会回退到默认连接;已配置但不可用的 profile 会返回错误。

func (*NamedRedisRegistry) GetConnection added in v0.4.8

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

GetConnection 返回指定命名 profile 的连接。 缺失 profile 会回退到默认连接;已配置但不可用的 profile 会返回错误。

func (*NamedRedisRegistry) HasConnections added in v0.4.8

func (r *NamedRedisRegistry) HasConnections() bool

HasConnections 返回注册表是否至少存在一个已配置连接。

func (*NamedRedisRegistry) HealthCheck added in v0.4.8

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

HealthCheck 检查默认连接与命名 profile 连接的健康状态。

func (*NamedRedisRegistry) ProfileStatus added in v0.4.9

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

ProfileStatus 返回指定命名 profile 的状态。

func (*NamedRedisRegistry) ProfileStatuses added in v0.4.9

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

ProfileStatuses 返回所有已配置命名 profile 的当前状态。

func (*NamedRedisRegistry) Profiles added in v0.5.3

Profiles 返回已配置命名 profile 的快照信息。

type RedisConfig added in v0.3.3

type RedisConfig = redisruntime.Config

RedisConfig 是 Foundation 层运行时配置的兼容别名。

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) Client added in v0.5.3

Client 返回强类型 Redis 客户端。

func (*RedisConnection) Close added in v0.3.3

func (r *RedisConnection) Close() error

Close 关闭 Redis 连接

func (*RedisConnection) Config added in v0.5.3

func (r *RedisConnection) Config() *RedisConfig

Config 返回解析后的 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 = redisruntime.ProfileState

type RedisProfileStatus added in v0.4.9

type RedisProfileStatus = redisruntime.ProfileStatus

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