SQLiteStorageBase.h (2152B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <winget/SQLiteWrapper.h> 5 #include <winget/SQLiteVersion.h> 6 #include <winget/ManagedFile.h> 7 8 #include <filesystem> 9 #include <mutex> 10 11 namespace AppInstaller::SQLite 12 { 13 // Type that wraps the basic SQLite storage functionality; the connection and metadata like schema version. 14 struct SQLiteStorageBase 15 { 16 // The disposition for opening the database. 17 enum class OpenDisposition 18 { 19 // Open for read only. 20 Read, 21 // Open for read and write. 22 ReadWrite, 23 // The database will not change while in use; open for immutable read. 24 Immutable, 25 }; 26 27 // Gets the last write time for the database. 28 std::chrono::system_clock::time_point GetLastWriteTime() const; 29 30 // Gets the identifier written to the database when it was created. 31 std::string GetDatabaseIdentifier() const; 32 33 // Gets the schema version of the database. 34 const Version& GetVersion() const { return m_version; } 35 36 // Renames the database file and any auxiliary files given the inputs. 37 // Should only be used on an inactive database. 38 // If overwrite is given, existing destination files will be removed first. 39 static void RenameSQLiteDatabase(const std::filesystem::path& source, const std::filesystem::path& destination, bool overwrite = false); 40 41 protected: 42 SQLiteStorageBase(const std::string& target, const Version& version); 43 44 SQLiteStorageBase(const std::string& filePath, SQLiteStorageBase::OpenDisposition disposition, Utility::ManagedFile&& indexFile); 45 46 SQLiteStorageBase(const std::string& target, SQLiteStorageBase& source); 47 48 // Sets the last write time metadata value in the database. 49 void SetLastWriteTime(); 50 51 Utility::ManagedFile m_indexFile; 52 SQLite::Connection m_dbconn; 53 Version m_version; 54 std::unique_ptr<std::mutex> m_interfaceLock = std::make_unique<std::mutex>(); 55 }; 56 }