Documentation
¶
Overview ¶
Package sqlite provides a thin wrapper around database/sql for SQLite.
It automatically selects the appropriate driver: CGO-based (mattn/go-sqlite3) when CGO is enabled, or pure Go (modernc.org/sqlite) otherwise.
Basic usage:
db, err := sqlite.NewDB(ctx, "app.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
With functional options:
db, err := sqlite.NewDB(ctx, "app.db",
sqlite.WithConnMaxOpen(10),
sqlite.WithJournalMode("wal"),
)
With config struct (useful for loading from config files):
cfg := sqlite.Config{ConnMaxOpen: 10, JournalMode: "wal"}
db, err := sqlite.NewDB(ctx, "app.db", sqlite.WithConfig(cfg))
Index ¶
- Constants
- func NewDB(ctx context.Context, filepath string, opts ...func(*connectionOption)) (*sql.DB, error)
- func WithConfig(cfg Config) func(*connectionOption)
- func WithConnMaxIdleTime(connMaxIdleTime int) func(*connectionOption)
- func WithConnMaxLifetime(connMaxLifetime int) func(*connectionOption)
- func WithConnMaxOpen(connMaxOpen int) func(*connectionOption)
- func WithJournalMode(journalMode string) func(*connectionOption)
- func WithMode(mode string) func(*connectionOption)
- type Config
Constants ¶
const (
ENGINE = "sqlite3"
)
Variables ¶
This section is empty.
Functions ¶
func NewDB ¶
NewDB creates a new SQLite database connection with the specified options. Provide the filepath only as the relative or absolute path to the database file. For example "test.db", "./my/relative/path/to/test.db", or "/absolute/path/to/test.db". For options, you can use the provided functions to set the connection parameters.
func WithConfig ¶
func WithConfig(cfg Config) func(*connectionOption)
WithConfig applies a Config struct as connection options. Zero values in the config are ignored, preserving defaults.
func WithConnMaxIdleTime ¶
func WithConnMaxIdleTime(connMaxIdleTime int) func(*connectionOption)
func WithConnMaxLifetime ¶
func WithConnMaxLifetime(connMaxLifetime int) func(*connectionOption)
func WithConnMaxOpen ¶
func WithConnMaxOpen(connMaxOpen int) func(*connectionOption)
func WithJournalMode ¶
func WithJournalMode(journalMode string) func(*connectionOption)