Documentation
¶
Overview ¶
Package sqlite provides SQLite database utilities wrapping modernc.org/sqlite.
Index ¶
- func InMemory() (*sql.DB, error)
- func Open(path string) (*sql.DB, error)
- func OpenContext(ctx context.Context, path string) (*sql.DB, error)
- func OpenWithConfig(ctx context.Context, cfg Config) (*sql.DB, error)
- func Transaction(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error) error
- type Config
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InMemory ¶
InMemory creates an in-memory SQLite database for testing.
Example:
db, err := sqlite.InMemory()
if err != nil {
t.Fatal(err)
}
defer db.Close()
func Open ¶
Open opens a SQLite database with default settings.
Uses modernc.org/sqlite (pure Go, zero CGO) with production-ready defaults:
- WAL mode for better concurrency
- Foreign keys enabled
- Connection pooling configured
- Performance pragmas applied
Example:
db, err := sqlite.Open("app.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
func OpenContext ¶
OpenContext opens a SQLite database with context.
func OpenWithConfig ¶
OpenWithConfig opens a SQLite database with custom settings.
Example:
db, err := sqlite.OpenWithConfig(ctx, sqlite.Config{
Path: "app.db",
WALMode: true,
MaxOpenConns: 50,
})
func Transaction ¶
Transaction executes a function within a SQLite transaction. Automatically commits on success, rolls back on error or panic.
Example:
err := sqlite.Transaction(ctx, db, func(tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, "INSERT INTO users (name) VALUES (?)", "John")
return err
})
Types ¶
type Config ¶
type Config struct {
// Path is the database file path. Use ":memory:" for in-memory database.
Path string
// WALMode enables Write-Ahead Logging for better concurrency.
// Default: true
WALMode bool
// BusyTimeout is how long to wait for locks.
// Default: 5 seconds
BusyTimeout time.Duration
// MaxOpenConns is the maximum number of open connections.
// Default: 25
MaxOpenConns int
// MaxIdleConns is the maximum number of idle connections.
// Default: 5
MaxIdleConns int
// ConnMaxLifetime is how long a connection can be reused.
// Default: 1 hour
ConnMaxLifetime time.Duration
// ForeignKeys enables foreign key constraints.
// Default: true
ForeignKeys bool
}
Config configures SQLite connection behavior.
func DefaultConfig ¶
DefaultConfig returns production-ready defaults.
Click to show internal directories.
Click to hide internal directories.