database

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 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 {
	// 分离的连接参数(推荐使用,便于通过环境变量配置)
	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"` // 数据库名

	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"` // 慢查询阈值
}

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 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"`
	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 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