database

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package database provides database abstraction and migration support for program-director.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	// Core operations
	Close() error
	Ping(ctx context.Context) error

	// Transaction support
	BeginTx(ctx context.Context) (Tx, error)

	// Query 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)

	// Migration
	Migrate(ctx context.Context) error
}

DB is the interface for database operations

func New

func New(ctx context.Context, cfg *config.DatabaseConfig, logger *slog.Logger) (DB, error)

New creates a new database connection based on configuration

type Migration

type Migration struct {
	Version int
	Name    string
	SQL     string
}

Migration represents a database migration

type PostgresDB

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

PostgresDB implements DB interface for PostgreSQL

func NewPostgres

func NewPostgres(ctx context.Context, cfg *config.PostgresConfig, logger *slog.Logger) (*PostgresDB, error)

NewPostgres creates a new PostgreSQL connection

func (*PostgresDB) BeginTx

func (p *PostgresDB) BeginTx(ctx context.Context) (Tx, error)

BeginTx starts a new transaction

func (*PostgresDB) Close

func (p *PostgresDB) Close() error

Close closes the database connection

func (*PostgresDB) Exec

func (p *PostgresDB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec executes a query that doesn't return rows

func (*PostgresDB) Migrate

func (p *PostgresDB) Migrate(ctx context.Context) error

Migrate runs all pending migrations

func (*PostgresDB) Ping

func (p *PostgresDB) Ping(ctx context.Context) error

Ping checks if the database connection is alive

func (*PostgresDB) Query

func (p *PostgresDB) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

Query executes a query that returns rows

func (*PostgresDB) QueryRow

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

QueryRow executes a query that returns a single row

type PostgresTx

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

PostgresTx wraps sql.Tx to implement Tx interface

func (*PostgresTx) Commit

func (t *PostgresTx) Commit() error

Commit commits the transaction

func (*PostgresTx) Exec

func (t *PostgresTx) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec executes a query without returning any rows

func (*PostgresTx) Query

func (t *PostgresTx) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

Query executes a query that returns rows

func (*PostgresTx) QueryRow

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

QueryRow executes a query that is expected to return at most one row

func (*PostgresTx) Rollback

func (t *PostgresTx) Rollback() error

Rollback rolls back the transaction

type SQLiteDB

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

SQLiteDB implements DB interface for SQLite

func NewSQLite

func NewSQLite(ctx context.Context, cfg *config.SQLiteConfig, logger *slog.Logger) (*SQLiteDB, error)

NewSQLite creates a new SQLite connection

func (*SQLiteDB) BeginTx

func (s *SQLiteDB) BeginTx(ctx context.Context) (Tx, error)

BeginTx starts a new transaction

func (*SQLiteDB) Close

func (s *SQLiteDB) Close() error

Close closes the database connection

func (*SQLiteDB) Exec

func (s *SQLiteDB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec executes a query that doesn't return rows

func (*SQLiteDB) Migrate

func (s *SQLiteDB) Migrate(ctx context.Context) error

Migrate runs all pending migrations

func (*SQLiteDB) Ping

func (s *SQLiteDB) Ping(ctx context.Context) error

Ping checks if the database connection is alive

func (*SQLiteDB) Query

func (s *SQLiteDB) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

Query executes a query that returns rows

func (*SQLiteDB) QueryRow

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

QueryRow executes a query that returns a single row

type SQLiteTx

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

SQLiteTx wraps sql.Tx to implement Tx interface

func (*SQLiteTx) Commit

func (t *SQLiteTx) Commit() error

Commit commits the transaction

func (*SQLiteTx) Exec

func (t *SQLiteTx) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec executes a query without returning any rows

func (*SQLiteTx) Query

func (t *SQLiteTx) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

Query executes a query that returns rows

func (*SQLiteTx) QueryRow

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

QueryRow executes a query that is expected to return at most one row

func (*SQLiteTx) Rollback

func (t *SQLiteTx) Rollback() error

Rollback rolls back the transaction

type Tx

type Tx interface {
	Commit() error
	Rollback() error
	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)
}

Tx represents a database transaction

Directories

Path Synopsis
Package repository provides database repository implementations for media, history, and cooldowns.
Package repository provides database repository implementations for media, history, and cooldowns.

Jump to

Keyboard shortcuts

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