database

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package database provides SQLite connection management with optimized pragmas.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DatabaseConfig

type DatabaseConfig struct {
	// Path is the filesystem path to the SQLite database file.
	// Use ":memory:" for an in-memory database (useful for testing).
	Path string

	// MaxOpenConns sets the maximum number of open connections.
	// Default: 25
	MaxOpenConns int

	// MaxIdleConns sets the maximum number of idle connections.
	// Default: 10
	MaxIdleConns int

	// ConnMaxLifetime sets the maximum lifetime of a connection.
	// Default: 30 minutes
	ConnMaxLifetime time.Duration

	// EnableWAL enables Write-Ahead Logging mode.
	// Default: true
	EnableWAL bool

	// CacheSize sets the SQLite cache size in kilobytes.
	// Negative values set the cache size in pages.
	// Default: -64000 (64MB)
	CacheSize int

	// MMapSize sets the memory-mapped I/O size in bytes.
	// Default: 268435456 (256MB)
	MMapSize int64

	// BusyTimeout sets the timeout for busy waiting in milliseconds.
	// Default: 5000 (5 seconds)
	BusyTimeout int
}

DatabaseConfig holds configuration for database connection.

func DefaultConfig

func DefaultConfig() DatabaseConfig

DefaultConfig returns a DatabaseConfig with sensible defaults.

func InMemoryConfig

func InMemoryConfig() DatabaseConfig

InMemoryConfig returns a DatabaseConfig for an in-memory database.

type Manager

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

Manager manages SQLite database connections with optimized settings.

func NewManager

func NewManager(cfg DatabaseConfig) (*Manager, error)

NewManager creates a new database manager with the given configuration.

func (*Manager) ApplyPragmas

func (m *Manager) ApplyPragmas(ctx context.Context) error

ApplyPragmas applies SQLite pragma settings for optimal performance. These pragmas are applied on each connection in the pool.

func (*Manager) BeginTx

func (m *Manager) BeginTx(ctx context.Context) (*sql.Tx, error)

BeginTx starts a new transaction with the given context. The transaction must be committed or rolled back by the caller.

func (*Manager) Close

func (m *Manager) Close() error

Close closes the database connection gracefully.

func (*Manager) DB

func (m *Manager) DB() *sql.DB

DB returns the underlying database connection. The returned *sql.DB is safe for concurrent use by multiple goroutines.

func (*Manager) IsInMemory

func (m *Manager) IsInMemory() bool

IsInMemory returns true if the database is in-memory.

func (*Manager) Path

func (m *Manager) Path() string

Path returns the database file path.

func (*Manager) Ping

func (m *Manager) Ping(ctx context.Context) error

Ping verifies the database connection is still alive.

func (*Manager) Stats

func (m *Manager) Stats() sql.DBStats

Stats returns database statistics for monitoring.

Jump to

Keyboard shortcuts

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