Break the 1,870-line monolithic stats-db.ts into 13 focused modules under
src/main/stats/, following the same pattern used in the agents module refactor.
Key improvements:
- StatementCache for prepared statement reuse across CRUD operations
- DB guard accessor (get database()) replaces 18 repeated null checks
- Transaction-wrapped clearOldData for atomic multi-table cleanup
- _meta table replaces external file for vacuum timestamp storage
- Row mapper functions centralize snake_case-to-camelCase conversion
- runStatements() helper eliminates repeated split-by-semicolon patterns
- Migration functions accept db param instead of relying on class context
- CSV export adds isRemote column and RFC 4180 compliant escaping
- Statement caches properly cleared on database close
Split the 6,400-line monolithic test file into 8 focused test files under
src/__tests__/main/stats/ matching the source module structure.