PinningIndex.h (3206B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <winget/SQLiteWrapper.h> 5 #include "Microsoft/Schema/IPinningIndex.h" 6 #include <winget/SQLiteStorageBase.h> 7 #include <winget/Pin.h> 8 #include <winget/ManagedFile.h> 9 10 namespace AppInstaller::Repository::Microsoft 11 { 12 struct PinningIndex : SQLite::SQLiteStorageBase 13 { 14 // An id that refers to a specific Pinning file. 15 using IdType = SQLite::rowid_t; 16 17 PinningIndex(const PinningIndex&) = delete; 18 PinningIndex& operator=(const PinningIndex&) = delete; 19 20 PinningIndex(PinningIndex&&) = default; 21 PinningIndex& operator=(PinningIndex&&) = default; 22 23 // Creates a new PinningIndex database of the given version. 24 static PinningIndex CreateNew(const std::string& filePath, SQLite::Version version = SQLite::Version::Latest()); 25 26 // Opens an existing PinningIndex database. 27 static PinningIndex Open(const std::string& filePath, OpenDisposition disposition, Utility::ManagedFile&& indexFile = {}) 28 { 29 return { filePath, disposition, std::move(indexFile) }; 30 } 31 32 // Opens the PinningIndex database on the default path if it exists. 33 // Returns nullptr in case of error. 34 static std::shared_ptr<PinningIndex> OpenIfExists(OpenDisposition openDisposition = OpenDisposition::Read); 35 36 // Opens or creates a PinningIndex database on the default path. 37 // openDisposition is only used when opening an existing database. 38 // Returns nullptr in case of error. 39 static std::shared_ptr<PinningIndex> OpenOrCreateDefault(OpenDisposition openDisposition = OpenDisposition::ReadWrite); 40 41 // Adds a pin to the index. 42 IdType AddPin(const Pinning::Pin& pin); 43 44 // Updates a pin type, and gated version if needed. 45 // Return value indicates whether there were any changes. 46 bool UpdatePin(const Pinning::Pin& pin); 47 48 // Adds a pin or updates it if it already exists. 49 void AddOrUpdatePin(const Pinning::Pin& pin); 50 51 // Removes a pin from the index. 52 void RemovePin(const Pinning::PinKey& pinKey); 53 54 // Returns the current pin for a given package if it exists. 55 std::optional<Pinning::Pin> GetPin(const Pinning::PinKey& pinKey); 56 57 // Returns a vector containing all the existing pins. 58 std::vector<Pinning::Pin> GetAllPins(); 59 60 // Deletes all pins from a given source, or from all sources if none is specified 61 bool ResetAllPins(std::string_view sourceId = {}); 62 63 private: 64 // Constructor used to open an existing index. 65 PinningIndex(const std::string& target, SQLiteStorageBase::OpenDisposition disposition, Utility::ManagedFile&& indexFile); 66 67 // Constructor used to create a new index. 68 PinningIndex(const std::string& target, SQLite::Version version); 69 70 // Creates the IPinningIndex interface object for this version. 71 std::unique_ptr<Schema::IPinningIndex> CreateIPinningIndex() const; 72 73 std::unique_ptr<Schema::IPinningIndex> m_interface; 74 }; 75 }