{"modulePath":"github.com/vidyesh95/Go-Tutorial","version":"v0.0.0-20261123112932-610d9af314ca","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package sqldb\n\nPackage sqldb holds the small helpers shared by every lesson in the SQL track: opening a SQLite database with sensible settings and loading the sample schema + data.\n\nIt is deliberately tiny. In a real service this code lives in an \\`internal/db\\` or \\`internal/store\\` package and also owns pool settings, health checks and error mapping.\n\n## Functions\n\n```go\nfunc DSN(path string) string\n```\nDSN builds a modernc.org/sqlite connection string for a file path.\n\nPragmas are applied to EVERY new connection the pool opens, which matters because:\n\n  - foreign\\_keys is OFF by default in SQLite and is a per-connection setting. Without the pragma in the DSN, only the first connection would enforce FKs and bugs would appear randomly.\n  - journal\\_mode(WAL) lets readers and one writer work at the same time.\n  - busy\\_timeout(5000) makes a blocked writer wait up to 5s instead of failing instantly with \"database is locked\".\n\nIN-MEMORY GOTCHA: with \":memory:\" every connection in the pool gets its OWN private empty database. Tables created on one connection are invisible on the next one, giving confusing \"no such table\" errors. Fix: db.SetMaxOpenConns(1), or (what we do) use a temp file.\n\n```go\nfunc Open(ctx context.Context, path string) (*sql.DB, error)\n```\nOpen opens (creating if needed) the SQLite database at path and verifies it is reachable. sql.Open alone never touches the database; PingContext forces a real connection so that configuration mistakes fail here, at startup, rather than on the first query.\n\n```go\nfunc OpenTemp(ctx context.Context) (*sql.DB, func(), error)\n```\nOpenTemp creates a fresh database file in a new temp directory, loads schema + seed, and returns the handle plus a cleanup function. Every lesson uses this so runs are repeatable: the previous run's data can never leak into the next one.\n\n```go\nfunc Schema(ctx context.Context, db *sql.DB) error\n```\nSchema applies only the schema (no sample rows). Useful for tests that insert their own data.\n\n```go\nfunc Setup(ctx context.Context, db *sql.DB) error\n```\nSetup creates the tables and inserts the sample data (sql/schema.sql then sql/seed.sql). database/sql lets a single Exec contain many statements with this driver, which is convenient for scripts. Do not rely on that with other drivers (pgx wants one per call unless you use its simple protocol).\n\n","path":"github.com/vidyesh95/Go-Tutorial/03-sql-sqlite/internal/sqldb","name":"sqldb","synopsis":"Package sqldb holds the small helpers shared by every lesson in the SQL track: opening a SQLite database with sensible settings and loading the sample schema + data.","isRedistributable":true}
