Open and close database. The same database should not be opened concurrently in multiple threads or processes. On Windows, the input path is converted from the local code page to UTF-8 for compatibility with SQLite. On POSIX platforms, the path is assumed to be UTF-8.
| 492 | // for compatibility with SQLite. On POSIX platforms, the path is assumed to |
| 493 | // be UTF-8. |
| 494 | static std::shared_ptr<Database> Open(const std::filesystem::path& path) { |
| 495 | auto database = std::make_shared<SqliteDatabase>(path); |
| 496 | |
| 497 | // SQLITE_OPEN_NOMUTEX specifies that the connection should not have a |
| 498 | // mutex (so that we don't serialize the connection's operations). |
| 499 | // Modifications to the database will still be serialized, but multiple |
| 500 | // connections can read concurrently. |
| 501 | try { |
| 502 | SQLITE3_CALL(sqlite3_open_v2( |
| 503 | PlatformToUTF8(path.string()).c_str(), |
| 504 | &database->database_, |
| 505 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX, |
| 506 | nullptr)); |
| 507 | } catch (...) { |
| 508 | SQLITE3_CALL(sqlite3_close_v2(database->database_)); |
| 509 | throw; |
| 510 | } |
| 511 | |
| 512 | // Don't wait for the operating system to write the changes to disk |
| 513 | SQLITE3_EXEC(database->database_, "PRAGMA synchronous=OFF", nullptr); |
| 514 | |
| 515 | // Use faster journaling mode |
| 516 | SQLITE3_EXEC(database->database_, "PRAGMA journal_mode=WAL", nullptr); |
| 517 | |
| 518 | // Store temporary tables and indices in memory |
| 519 | SQLITE3_EXEC(database->database_, "PRAGMA temp_store=MEMORY", nullptr); |
| 520 | |
| 521 | // Disabled by default |
| 522 | SQLITE3_EXEC(database->database_, "PRAGMA foreign_keys=ON", nullptr); |
| 523 | |
| 524 | // Enable auto vacuum to reduce DB file size |
| 525 | SQLITE3_EXEC(database->database_, "PRAGMA auto_vacuum=1", nullptr); |
| 526 | |
| 527 | database->PreMigrateTables(); |
| 528 | database->CreateTables(); |
| 529 | database->PostMigrateTables(); |
| 530 | database->PrepareSQLStatements(); |
| 531 | |
| 532 | return database; |
| 533 | } |
| 534 | |
| 535 | void CloseImpl() { |
| 536 | if (database_ != nullptr) { |
nothing calls this directly
no test coverage detected