winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

OneToOneTable.h (7179B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include <winget/SQLiteWrapper.h>
      5 #include <optional>
      6 #include <string>
      7 #include <string_view>
      8 #include <vector>
      9 
     10 
     11 namespace AppInstaller::Repository::Microsoft::Schema::V1_0
     12 {
     13     namespace details
     14     {
     15         // Creates the table.
     16         void CreateOneToOneTable(SQLite::Connection& connection, std::string_view tableName, std::string_view valueName, bool useNamedIndices);
     17 
     18         // Drops the table.
     19         void DropOneToOneTable(SQLite::Connection& connection, std::string_view tableName);
     20 
     21         // Selects the value from the table, returning the rowid if it exists.
     22         std::optional<SQLite::rowid_t> OneToOneTableSelectIdByValue(const SQLite::Connection& connection, std::string_view tableName, std::string_view valueName, std::string_view value, bool useLike = false);
     23 
     24         // Selects the value from the table, returning the rowid if it exists.
     25         std::optional<std::string> OneToOneTableSelectValueById(SQLite::Connection& connection, std::string_view tableName, std::string_view valueName, SQLite::rowid_t id);
     26 
     27         // Gets all row ids from the table.
     28         std::vector<SQLite::rowid_t> OneToOneTableGetAllRowIds(const SQLite::Connection& connection, std::string_view tableName, std::string_view valueName, size_t limit);
     29 
     30         // Ensures that the values exists in the table.
     31         SQLite::rowid_t OneToOneTableEnsureExists(SQLite::Connection& connection, std::string_view tableName, std::string_view valueName, std::string_view value, bool overwriteLikeMatch = false);
     32 
     33         // Removes data that is no longer needed for an index that is to be published.
     34         void OneToOneTablePrepareForPackaging(SQLite::Connection& connection, std::string_view tableName, bool useNamedIndices, bool preserveValuesIndex);
     35 
     36         // Gets the total number of rows in the table.
     37         uint64_t OneToOneTableGetCount(const SQLite::Connection& connection, std::string_view tableName);
     38 
     39         // Determines if the table is empty.
     40         bool OneToOneTableIsEmpty(SQLite::Connection& connection, std::string_view tableName);
     41 
     42         // Removes the given row by its rowid if it is no longer referenced.
     43         void OneToOneTableDeleteById(SQLite::Connection& connection, std::string_view tableName, SQLite::rowid_t id);
     44 
     45         // Checks the consistency of the table.
     46         bool OneToOneTableCheckConsistency(const SQLite::Connection& connection, std::string_view tableName, std::string_view valueName, bool log);
     47     }
     48 
     49     // A table that represents a value that is 1:1 with a primary entry.
     50     template <typename TableInfo>
     51     struct OneToOneTable
     52     {
     53         // The value type.
     54         using value_t = std::string;
     55 
     56         // The id type
     57         using id_t = SQLite::rowid_t;
     58 
     59         // Creates the table with named indices.
     60         static void Create(SQLite::Connection& connection)
     61         {
     62             details::CreateOneToOneTable(connection, TableInfo::TableName(), TableInfo::ValueName(), true);
     63         }
     64 
     65         // Creates the table with standard primary keys.
     66         static void Create_deprecated(SQLite::Connection& connection)
     67         {
     68             details::CreateOneToOneTable(connection, TableInfo::TableName(), TableInfo::ValueName(), false);
     69         }
     70 
     71         // Drops the table.
     72         static void Drop(SQLite::Connection& connection)
     73         {
     74             details::DropOneToOneTable(connection, TableInfo::TableName());
     75         }
     76 
     77         // The name of the table.
     78         static constexpr std::string_view TableName()
     79         {
     80             return TableInfo::TableName();
     81         }
     82 
     83         // The value name of the table.
     84         static constexpr std::string_view ValueName()
     85         {
     86             return TableInfo::ValueName();
     87         }
     88 
     89         // Value indicating type.
     90         static constexpr bool IsOneToOne()
     91         {
     92             return true;
     93         }
     94 
     95         // Selects the value from the table, returning the rowid if it exists.
     96         static std::optional<SQLite::rowid_t> SelectIdByValue(const SQLite::Connection& connection, std::string_view value, bool useLike = false)
     97         {
     98             return details::OneToOneTableSelectIdByValue(connection, TableInfo::TableName(), TableInfo::ValueName(), value, useLike);
     99         }
    100 
    101         // Selects the value from the table, returning it if it exists.
    102         static std::optional<value_t> SelectValueById(SQLite::Connection& connection, id_t id)
    103         {
    104             return details::OneToOneTableSelectValueById(connection, TableInfo::TableName(), TableInfo::ValueName(), id);
    105         }
    106 
    107         // Gets all row ids from the table.
    108         static std::vector<SQLite::rowid_t> GetAllRowIds(const SQLite::Connection& connection, size_t limit = 0)
    109         {
    110             return details::OneToOneTableGetAllRowIds(connection, TableInfo::TableName(), TableInfo::ValueName(), limit);
    111         }
    112 
    113         // Ensures that the given value exists in the table, returning the rowid.
    114         static SQLite::rowid_t EnsureExists(SQLite::Connection& connection, std::string_view value, bool overwriteLikeMatch = false)
    115         {
    116             return details::OneToOneTableEnsureExists(connection, TableInfo::TableName(), TableInfo::ValueName(), value, overwriteLikeMatch);
    117         }
    118 
    119         // Removes the given row by its rowid if it is no longer referenced.
    120         static void DeleteById(SQLite::Connection& connection, SQLite::rowid_t id)
    121         {
    122             return details::OneToOneTableDeleteById(connection, TableInfo::TableName(), id);
    123         }
    124 
    125         // Removes data that is no longer needed for an index that is to be published.
    126         // Preserving the values index will improve searching when it is primarily done by equality.
    127         static void PrepareForPackaging(SQLite::Connection& connection, bool preserveValuesIndex = false)
    128         {
    129             details::OneToOneTablePrepareForPackaging(connection, TableInfo::TableName(), true, preserveValuesIndex);
    130         }
    131 
    132         // Removes data that is no longer needed for an index that is to be published.
    133         static void PrepareForPackaging_deprecated(SQLite::Connection& connection)
    134         {
    135             details::OneToOneTablePrepareForPackaging(connection, TableInfo::TableName(), false, false);
    136         }
    137 
    138         // Gets the total number of rows in the table.
    139         static uint64_t GetCount(const SQLite::Connection& connection)
    140         {
    141             return details::OneToOneTableGetCount(connection, TableInfo::TableName());
    142         }
    143 
    144         // Determines if the table is empty.
    145         static bool IsEmpty(SQLite::Connection& connection)
    146         {
    147             return details::OneToOneTableIsEmpty(connection, TableInfo::TableName());
    148         }
    149 
    150         // Checks the consistency of the table.
    151         static bool CheckConsistency(const SQLite::Connection& connection, bool log)
    152         {
    153             return details::OneToOneTableCheckConsistency(connection, TableInfo::TableName(), TableInfo::ValueName(), log);
    154         }
    155     };
    156 }