database

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package database provides database service interfaces and implementations.

Index

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

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) BeginTx

func (s *MySQLService) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a database transaction.

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

Health returns the service health status.

func (*MySQLService) ID

func (s *MySQLService) ID() string

ID returns the service ID.

func (*MySQLService) Metrics

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) QueryRow

func (s *MySQLService) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRow executes a query that returns at most one row.

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) BeginTx

func (s *PostgresService) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a database transaction.

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

Health returns the health status of the service.

func (*PostgresService) ID

func (s *PostgresService) ID() string

ID returns the service ID.

func (*PostgresService) Metrics

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

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) QueryRow

func (s *PostgresService) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRow executes a query that returns a single row.

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

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.

Jump to

Keyboard shortcuts

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