Documentation
¶
Overview ¶
Package database provides database service interfaces and implementations.
Index ¶
- type DatabaseConfig
- type DatabaseFactory
- type DatabasePool
- type DatabaseService
- type LoadBalanceStrategy
- type MySQLService
- func (s *MySQLService) Backup(ctx context.Context, path string) error
- func (s *MySQLService) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
- func (s *MySQLService) Connect(ctx context.Context) error
- func (s *MySQLService) Disconnect(ctx context.Context) error
- func (s *MySQLService) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (s *MySQLService) GetConfig() *registry.ServiceConfig
- func (s *MySQLService) GetDB() *sql.DB
- func (s *MySQLService) GetSchemaVersion() (int, error)
- func (s *MySQLService) Health(ctx context.Context) (*registry.ServiceHealth, error)
- func (s *MySQLService) ID() string
- func (s *MySQLService) Metrics(ctx context.Context) (*registry.ServiceMetrics, error)
- func (s *MySQLService) Ping(ctx context.Context) error
- func (s *MySQLService) Provider() registry.ServiceProvider
- func (s *MySQLService) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (s *MySQLService) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (s *MySQLService) Restore(ctx context.Context, path string) error
- func (s *MySQLService) RunMigrations(ctx context.Context, migrationsPath string) error
- func (s *MySQLService) SetConnMaxLifetime(d time.Duration)
- func (s *MySQLService) SetMaxIdleConns(n int)
- func (s *MySQLService) SetMaxOpenConns(n int)
- func (s *MySQLService) Type() registry.ServiceType
- func (s *MySQLService) UpdateConfig(config *registry.ServiceConfig) error
- type PostgresService
- func (s *PostgresService) Backup(ctx context.Context, path string) error
- func (s *PostgresService) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
- func (s *PostgresService) Connect(ctx context.Context) error
- func (s *PostgresService) Disconnect(ctx context.Context) error
- func (s *PostgresService) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (s *PostgresService) GetConfig() *registry.ServiceConfig
- func (s *PostgresService) GetDB() *sql.DB
- func (s *PostgresService) GetSchemaVersion() (int, error)
- func (s *PostgresService) Health(ctx context.Context) (*registry.ServiceHealth, error)
- func (s *PostgresService) ID() string
- func (s *PostgresService) Metrics(ctx context.Context) (*registry.ServiceMetrics, error)
- func (s *PostgresService) Ping(ctx context.Context) error
- func (s *PostgresService) Provider() registry.ServiceProvider
- func (s *PostgresService) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (s *PostgresService) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (s *PostgresService) Restore(ctx context.Context, path string) error
- func (s *PostgresService) RunMigrations(ctx context.Context, migrationsPath string) error
- func (s *PostgresService) SetConnMaxLifetime(d time.Duration)
- func (s *PostgresService) SetMaxIdleConns(n int)
- func (s *PostgresService) SetMaxOpenConns(n int)
- func (s *PostgresService) Type() registry.ServiceType
- func (s *PostgresService) UpdateConfig(config *registry.ServiceConfig) error
- type QueryBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DatabaseConfig ¶
type DatabaseConfig struct {
*registry.ServiceConfig
// Database-specific settings
SSLMode string `yaml:"ssl_mode" json:"ssl_mode"`
ApplicationName string `yaml:"application_name" json:"application_name"`
SearchPath string `yaml:"search_path" json:"search_path"`
StatementTimeout int `yaml:"statement_timeout" json:"statement_timeout"`
LockTimeout int `yaml:"lock_timeout" json:"lock_timeout"`
// Connection pool settings
MaxOpenConns int `yaml:"max_open_conns" json:"max_open_conns"`
MaxIdleConns int `yaml:"max_idle_conns" json:"max_idle_conns"`
ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime" json:"conn_max_lifetime"`
ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time" json:"conn_max_idle_time"`
// Replication settings
Role string `yaml:"role" json:"role"` // "primary" or "replica"
ReplicationSlots []string `yaml:"replication_slots" json:"replication_slots"`
}
DatabaseConfig provides database-specific configuration.
type DatabaseFactory ¶
type DatabaseFactory struct {
// contains filtered or unexported fields
}
DatabaseFactory creates database service instances.
func NewDatabaseFactory ¶
func NewDatabaseFactory() *DatabaseFactory
NewDatabaseFactory creates a new database factory.
func (*DatabaseFactory) CreateService ¶
func (f *DatabaseFactory) CreateService(config *registry.ServiceConfig) (registry.ServiceInterface, error)
CreateService creates a database service instance.
func (*DatabaseFactory) RegisterProvider ¶
func (f *DatabaseFactory) RegisterProvider(provider registry.ServiceProvider, constructor func(*registry.ServiceConfig) (DatabaseService, error))
RegisterProvider registers a database provider constructor.
func (*DatabaseFactory) SupportedProviders ¶
func (f *DatabaseFactory) SupportedProviders() []registry.ServiceProvider
SupportedProviders returns the list of supported database providers.
type DatabasePool ¶
type DatabasePool struct {
// contains filtered or unexported fields
}
DatabasePool manages multiple database connections with load balancing.
func NewDatabasePool ¶
func NewDatabasePool(primary DatabaseService, replicas []DatabaseService, strategy LoadBalanceStrategy) *DatabasePool
NewDatabasePool creates a new database pool.
func (*DatabasePool) Primary ¶
func (p *DatabasePool) Primary() DatabaseService
Primary returns the primary database for writes.
func (*DatabasePool) Replica ¶
func (p *DatabasePool) Replica() DatabaseService
Replica returns a replica database for reads.
type DatabaseService ¶
type DatabaseService interface {
registry.ServiceInterface
// Database operations
Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
// Transaction support
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
// Connection pool management
GetDB() *sql.DB
SetMaxOpenConns(n int)
SetMaxIdleConns(n int)
SetConnMaxLifetime(d time.Duration)
// Migration support
RunMigrations(ctx context.Context, migrationsPath string) error
GetSchemaVersion() (int, error)
// Backup and restore
Backup(ctx context.Context, path string) error
Restore(ctx context.Context, path string) error
}
DatabaseService extends the base ServiceInterface with database-specific methods.
func NewMySQLService ¶
func NewMySQLService(config *registry.ServiceConfig) (DatabaseService, error)
NewMySQLService creates a new MySQL service instance.
func NewPostgresService ¶
func NewPostgresService(config *registry.ServiceConfig) (DatabaseService, error)
NewPostgresService creates a new PostgreSQL service instance.
type LoadBalanceStrategy ¶
type LoadBalanceStrategy string
LoadBalanceStrategy defines how to distribute read queries.
const ( StrategyRoundRobin LoadBalanceStrategy = "round-robin" StrategyLeastConn LoadBalanceStrategy = "least-conn" StrategyRandom LoadBalanceStrategy = "random" StrategyPrimary LoadBalanceStrategy = "primary-only" )
type MySQLService ¶
type MySQLService struct {
// contains filtered or unexported fields
}
MySQLService implements DatabaseService for MySQL.
func (*MySQLService) Backup ¶
func (s *MySQLService) Backup(ctx context.Context, path string) error
Backup performs a database backup.
func (*MySQLService) Connect ¶
func (s *MySQLService) Connect(ctx context.Context) error
Connect establishes connection to MySQL.
func (*MySQLService) Disconnect ¶
func (s *MySQLService) Disconnect(ctx context.Context) error
Disconnect closes the database connection.
func (*MySQLService) Exec ¶
func (s *MySQLService) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Exec executes a query without returning any rows.
func (*MySQLService) GetConfig ¶
func (s *MySQLService) GetConfig() *registry.ServiceConfig
GetConfig returns the service configuration.
func (*MySQLService) GetDB ¶
func (s *MySQLService) GetDB() *sql.DB
GetDB returns the underlying database connection.
func (*MySQLService) GetSchemaVersion ¶
func (s *MySQLService) GetSchemaVersion() (int, error)
GetSchemaVersion returns the current schema version.
func (*MySQLService) Health ¶
func (s *MySQLService) Health(ctx context.Context) (*registry.ServiceHealth, error)
Health returns the service health status.
func (*MySQLService) Metrics ¶
func (s *MySQLService) Metrics(ctx context.Context) (*registry.ServiceMetrics, error)
Metrics returns service metrics.
func (*MySQLService) Ping ¶
func (s *MySQLService) Ping(ctx context.Context) error
Ping tests the database connection.
func (*MySQLService) Provider ¶
func (s *MySQLService) Provider() registry.ServiceProvider
Provider returns the service provider.
func (*MySQLService) Query ¶
func (s *MySQLService) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Query executes a query that returns rows.
func (*MySQLService) Restore ¶
func (s *MySQLService) Restore(ctx context.Context, path string) error
Restore restores a database from backup.
func (*MySQLService) RunMigrations ¶
func (s *MySQLService) RunMigrations(ctx context.Context, migrationsPath string) error
RunMigrations runs database migrations.
func (*MySQLService) SetConnMaxLifetime ¶
func (s *MySQLService) SetConnMaxLifetime(d time.Duration)
SetConnMaxLifetime sets the maximum lifetime of connections.
func (*MySQLService) SetMaxIdleConns ¶
func (s *MySQLService) SetMaxIdleConns(n int)
SetMaxIdleConns sets the maximum number of idle connections.
func (*MySQLService) SetMaxOpenConns ¶
func (s *MySQLService) SetMaxOpenConns(n int)
SetMaxOpenConns sets the maximum number of open connections.
func (*MySQLService) Type ¶
func (s *MySQLService) Type() registry.ServiceType
Type returns the service type.
func (*MySQLService) UpdateConfig ¶
func (s *MySQLService) UpdateConfig(config *registry.ServiceConfig) error
UpdateConfig updates the service configuration.
type PostgresService ¶
type PostgresService struct {
// contains filtered or unexported fields
}
PostgresService implements DatabaseService for PostgreSQL.
func (*PostgresService) Backup ¶
func (s *PostgresService) Backup(ctx context.Context, path string) error
Backup creates a database backup.
func (*PostgresService) Connect ¶
func (s *PostgresService) Connect(ctx context.Context) error
Connect establishes connection to PostgreSQL.
func (*PostgresService) Disconnect ¶
func (s *PostgresService) Disconnect(ctx context.Context) error
Disconnect closes the database connection.
func (*PostgresService) Exec ¶
func (s *PostgresService) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Exec executes a query that doesn't return rows.
func (*PostgresService) GetConfig ¶
func (s *PostgresService) GetConfig() *registry.ServiceConfig
GetConfig returns the service configuration.
func (*PostgresService) GetDB ¶
func (s *PostgresService) GetDB() *sql.DB
GetDB returns the underlying database connection.
func (*PostgresService) GetSchemaVersion ¶
func (s *PostgresService) GetSchemaVersion() (int, error)
GetSchemaVersion returns the current schema version.
func (*PostgresService) Health ¶
func (s *PostgresService) Health(ctx context.Context) (*registry.ServiceHealth, error)
Health returns the health status of the service.
func (*PostgresService) Metrics ¶
func (s *PostgresService) Metrics(ctx context.Context) (*registry.ServiceMetrics, error)
Metrics returns performance metrics.
func (*PostgresService) Ping ¶
func (s *PostgresService) Ping(ctx context.Context) error
Ping checks if the database is reachable.
func (*PostgresService) Provider ¶
func (s *PostgresService) Provider() registry.ServiceProvider
Provider returns the service provider.
func (*PostgresService) Query ¶
func (s *PostgresService) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Query executes a query that returns rows.
func (*PostgresService) Restore ¶
func (s *PostgresService) Restore(ctx context.Context, path string) error
Restore restores from a backup.
func (*PostgresService) RunMigrations ¶
func (s *PostgresService) RunMigrations(ctx context.Context, migrationsPath string) error
RunMigrations runs database migrations.
func (*PostgresService) SetConnMaxLifetime ¶
func (s *PostgresService) SetConnMaxLifetime(d time.Duration)
SetConnMaxLifetime sets the maximum connection lifetime.
func (*PostgresService) SetMaxIdleConns ¶
func (s *PostgresService) SetMaxIdleConns(n int)
SetMaxIdleConns sets the maximum number of idle connections.
func (*PostgresService) SetMaxOpenConns ¶
func (s *PostgresService) SetMaxOpenConns(n int)
SetMaxOpenConns sets the maximum number of open connections.
func (*PostgresService) Type ¶
func (s *PostgresService) Type() registry.ServiceType
Type returns the service type.
func (*PostgresService) UpdateConfig ¶
func (s *PostgresService) UpdateConfig(config *registry.ServiceConfig) error
UpdateConfig updates the service configuration.
type QueryBuilder ¶
type QueryBuilder interface {
Select(columns ...string) QueryBuilder
From(table string) QueryBuilder
Where(condition string, args ...interface{}) QueryBuilder
Join(joinType, table, condition string) QueryBuilder
GroupBy(columns ...string) QueryBuilder
Having(condition string, args ...interface{}) QueryBuilder
OrderBy(column string, desc bool) QueryBuilder
Limit(limit int) QueryBuilder
Offset(offset int) QueryBuilder
Build() (string, []interface{})
}
QueryBuilder provides a fluent interface for building queries.