ISQLiteIndex.h (7799B)
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 "ISource.h" 7 #include "Microsoft/Schema/SQLiteIndexContextData.h" 8 #include <AppInstallerVersions.h> 9 #include <winget/Manifest.h> 10 #include <winget/NameNormalization.h> 11 12 #include <filesystem> 13 #include <optional> 14 15 16 namespace AppInstaller::Repository::Microsoft::Schema 17 { 18 // Contains the database connection and any other data that the owning index might need to pass in. 19 struct SQLiteIndexContext 20 { 21 SQLite::Connection& Connection; 22 SQLiteIndexContextData& Data; 23 }; 24 25 // The common interface used to interact with all schema versions of the index. 26 struct ISQLiteIndex 27 { 28 virtual ~ISQLiteIndex() = default; 29 30 // The non-version specific return value of Search. 31 // New fields must have initializers to their down-schema defaults. 32 struct SearchResult 33 { 34 std::vector<std::pair<SQLite::rowid_t, PackageMatchFilter>> Matches; 35 bool Truncated = false; 36 }; 37 38 // The non-version specific return value of GetMetadataByManifestId. 39 using MetadataResult = std::vector<std::pair<PackageVersionMetadata, std::string>>; 40 41 // Version 1.0 42 43 // Gets the schema version that this index interface is built for. 44 virtual SQLite::Version GetVersion() const = 0; 45 46 // Options for creating the index. 47 enum class CreateOptions 48 { 49 // Standard 50 None = 0x0, 51 // Enable support for passing in nullopt values to Add/UpdateManifest 52 SupportPathless = 0x1, 53 // Disable support for dependencies 54 DisableDependenciesSupport = 0x2, 55 }; 56 57 // Contains both the object representation of the version key and the rows. 58 struct VersionKey 59 { 60 Utility::VersionAndChannel VersionAndChannel; 61 SQLite::rowid_t ManifestId; 62 63 bool operator<(const VersionKey& other) const { return VersionAndChannel < other.VersionAndChannel; } 64 }; 65 66 // Creates all of the version dependent tables within the database. 67 virtual void CreateTables(SQLite::Connection& connection, CreateOptions options) = 0; 68 69 // Adds the manifest at the repository relative path to the index. 70 virtual SQLite::rowid_t AddManifest(SQLite::Connection& connection, const Manifest::Manifest& manifest, const std::optional<std::filesystem::path>& relativePath) = 0; 71 72 // Updates the manifest with matching { Id, Version, Channel } in the index. 73 // The return value indicates whether the index was modified by the function. 74 virtual std::pair<bool, SQLite::rowid_t> UpdateManifest( 75 SQLite::Connection& connection, 76 const Manifest::Manifest& manifest, 77 const std::optional<std::filesystem::path>& relativePath) = 0; 78 79 // Removes the manifest with matching { Id, Version, Channel } from the index. 80 virtual SQLite::rowid_t RemoveManifest(SQLite::Connection& connection, const Manifest::Manifest& manifest) = 0; 81 82 // Removes the manifest with the given id. 83 virtual void RemoveManifestById(SQLite::Connection& connection, SQLite::rowid_t manifestId) = 0; 84 85 // Removes data that is no longer needed for an index that is to be published. 86 virtual void PrepareForPackaging(SQLite::Connection& connection) = 0; 87 88 // Removes data that is no longer needed for an index that is to be published. 89 virtual void PrepareForPackaging(const SQLiteIndexContext& context); 90 91 // Checks the consistency of the index to ensure that every referenced row exists. 92 // Returns true if index is consistent; false if it is not. 93 virtual bool CheckConsistency(const SQLite::Connection& connection, bool log) const = 0; 94 95 // Performs a search based on the given criteria. 96 virtual SearchResult Search(const SQLite::Connection& connection, const SearchRequest& request) const = 0; 97 98 // Gets the string for the given property and primary id, if present. 99 virtual std::optional<std::string> GetPropertyByPrimaryId(const SQLite::Connection& connection, SQLite::rowid_t primaryId, PackageVersionProperty property) const = 0; 100 101 // Gets the string values for the given property and primary id, if present. 102 virtual std::vector<std::string> GetMultiPropertyByPrimaryId(const SQLite::Connection& connection, SQLite::rowid_t primaryId, PackageVersionMultiProperty property) const = 0; 103 104 // Gets the manifest id for the given { id, version, channel }, if present. 105 // If version is empty, gets the value for the 'latest' version. 106 virtual std::optional<SQLite::rowid_t> GetManifestIdByKey(const SQLite::Connection& connection, SQLite::rowid_t id, std::string_view version, std::string_view channel) const = 0; 107 108 // Gets the manifest id for the given manifest, if present. 109 virtual std::optional<SQLite::rowid_t> GetManifestIdByManifest(const SQLite::Connection& connection, const Manifest::Manifest& manifest) const = 0; 110 111 // Gets all versions and channels for the given id. 112 virtual std::vector<VersionKey> GetVersionKeysById(const SQLite::Connection& connection, SQLite::rowid_t id) const = 0; 113 114 // Version 1.1 115 116 // Gets the string for the given metadata and manifest id, if present. 117 virtual MetadataResult GetMetadataByManifestId(const SQLite::Connection& connection, SQLite::rowid_t manifestId) const = 0; 118 119 // Sets the string for the given metadata and manifest id. 120 virtual void SetMetadataByManifestId(SQLite::Connection& connection, SQLite::rowid_t manifestId, PackageVersionMetadata metadata, std::string_view value) = 0; 121 122 // Version 1.2 123 124 // Normalizes a name using the internal rules used by the index. 125 // Largely a utility function; should not be used to do work on behalf of the index by the caller. 126 virtual Utility::NormalizedName NormalizeName(std::string_view name, std::string_view publisher) const = 0; 127 128 // Version 1.4 129 130 // Get all the dependencies for a specific manifest. 131 virtual std::set<std::pair<SQLite::rowid_t, Utility::NormalizedString>> GetDependenciesByManifestRowId(const SQLite::Connection& connection, SQLite::rowid_t manifestRowId) const = 0; 132 133 virtual std::vector<std::pair<SQLite::rowid_t, Utility::NormalizedString>> GetDependentsById(const SQLite::Connection& connection, AppInstaller::Manifest::string_t packageId) const = 0; 134 135 // Version 1.7 136 137 // Drops all tables that would have been created. 138 virtual void DropTables(SQLite::Connection& connection) = 0; 139 140 // Version 2.0 141 142 // Migrates from the current interface given. 143 // Returns true if supported; false if not. 144 // Throws on errors that occur during an attempted migration. 145 virtual bool MigrateFrom(SQLite::Connection& connection, const ISQLiteIndex* current) = 0; 146 147 // Set the property value. 148 virtual void SetProperty(SQLite::Connection& connection, Property property, const std::string& value); 149 }; 150 151 DEFINE_ENUM_FLAG_OPERATORS(ISQLiteIndex::CreateOptions); 152 153 // Creates the ISQLiteIndex interface object for the given version. 154 std::unique_ptr<ISQLiteIndex> CreateISQLiteIndex(const SQLite::Version& version); 155 156 // For a given match type, gets the set of match types that are more specific subsets of it. 157 std::vector<MatchType> GetDefaultMatchTypeOrder(MatchType type); 158 }