SystemReferenceStringTable.h (6464B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <winget/SQLiteWrapper.h> 5 #include <winget/SQLiteStatementBuilder.h> 6 #include <AppInstallerStrings.h> 7 #include <string> 8 #include <string_view> 9 #include <vector> 10 11 12 namespace AppInstaller::Repository::Microsoft::Schema::V2_0 13 { 14 namespace details 15 { 16 // Returns the primary column name. 17 std::string_view SystemReferenceStringTableGetPrimaryColumnName(); 18 19 // Create the table. 20 void SystemReferenceStringTableCreate(SQLite::Connection& connection, std::string_view tableName, std::string_view valueName); 21 22 // Drops the table. 23 void SystemReferenceStringTableDrop(SQLite::Connection& connection, std::string_view tableName); 24 25 // Gets all values associated with the given primary id. 26 std::vector<std::string> SystemReferenceStringTableGetValuesByPrimaryId( 27 const SQLite::Connection& connection, 28 std::string_view tableName, 29 std::string_view valueName, 30 SQLite::rowid_t primaryId); 31 32 // Ensures that the value exists and inserts mapping entries. 33 void SystemReferenceStringTableEnsureExists( 34 SQLite::Connection& connection, 35 std::string_view tableName, 36 std::string_view valueName, 37 const std::vector<std::string>& values, 38 SQLite::rowid_t primaryId); 39 40 // Checks the consistency of the index to ensure that every referenced row exists. 41 // Returns true if index is consistent; false if it is not. 42 bool SystemReferenceStringTableCheckConsistency(const SQLite::Connection& connection, std::string_view tableName, std::string_view valueName, bool log); 43 44 // Determines if the table is empty. 45 bool SystemReferenceStringTableIsEmpty(SQLite::Connection& connection, std::string_view tableName); 46 47 // Builds the search select statement base on the given value. 48 // The return value is the bind index of the value to match against. 49 int SystemReferenceStringTableBuildSearchStatement( 50 SQLite::Builder::StatementBuilder& builder, 51 std::string_view tableName, 52 std::string_view valueName, 53 std::string_view primaryAlias, 54 std::string_view valueAlias, 55 bool useLike); 56 57 // Builds the search select statement base on the given value. 58 // The return value is the bind index of the value to match against. 59 std::vector<int> SystemReferenceStringTableBuildPairedSearchStatement( 60 SQLite::Builder::StatementBuilder& builder, 61 std::string_view tableName, 62 std::string_view valueName, 63 std::string_view pairedTableName, 64 std::string_view pairedValueName, 65 std::string_view primaryAlias, 66 std::string_view valueAlias, 67 bool useLike); 68 } 69 70 // A table that represents a value that is 1:N with a primary entry. 71 template <typename TableInfo> 72 struct SystemReferenceStringTable 73 { 74 // The name of the table. 75 static constexpr std::string_view TableName() 76 { 77 return TableInfo::TableName(); 78 } 79 80 // The value name of the table. 81 static constexpr std::string_view ValueName() 82 { 83 return TableInfo::ValueName(); 84 } 85 86 // Creates the table. 87 static void Create(SQLite::Connection& connection) 88 { 89 details::SystemReferenceStringTableCreate(connection, TableInfo::TableName(), TableInfo::ValueName()); 90 } 91 92 // Drops the table. 93 static void Drop(SQLite::Connection& connection) 94 { 95 details::SystemReferenceStringTableDrop(connection, TableInfo::TableName()); 96 } 97 98 // Gets all values associated with the given primary id. 99 static std::vector<std::string> GetValuesByPrimaryId(const SQLite::Connection& connection, SQLite::rowid_t primaryId) 100 { 101 return details::SystemReferenceStringTableGetValuesByPrimaryId(connection, TableInfo::TableName(), TableInfo::ValueName(), primaryId); 102 } 103 104 // Ensures that all values exist in the data table, and inserts into the mapping table for the given primary id. 105 static void EnsureExists(SQLite::Connection& connection, const std::vector<std::string>& values, SQLite::rowid_t primaryId) 106 { 107 details::SystemReferenceStringTableEnsureExists(connection, TableInfo::TableName(), TableInfo::ValueName(), values, primaryId); 108 } 109 110 // Checks the consistency of the index to ensure that every referenced row exists. 111 // Returns true if index is consistent; false if it is not. 112 static bool CheckConsistency(const SQLite::Connection& connection, bool log) 113 { 114 return details::SystemReferenceStringTableCheckConsistency(connection, TableInfo::TableName(), TableInfo::ValueName(), log); 115 } 116 117 // Determines if the table is empty. 118 static bool IsEmpty(SQLite::Connection& connection) 119 { 120 return details::SystemReferenceStringTableIsEmpty(connection, TableInfo::TableName()); 121 } 122 123 // Builds the search select statement base on the given value. 124 // The return value is the bind index of the value to match against. 125 static int BuildSearchStatement(SQLite::Builder::StatementBuilder& builder, std::string_view primaryAlias, std::string_view valueAlias, bool useLike) 126 { 127 return details::SystemReferenceStringTableBuildSearchStatement(builder, TableInfo::TableName(), TableInfo::ValueName(), primaryAlias, valueAlias, useLike); 128 } 129 130 // Builds the search select statement base on the given value. 131 // The return value is the bind index of the value to match against. 132 template<typename PairedTable> 133 static std::vector<int> BuildPairedSearchStatement(SQLite::Builder::StatementBuilder& builder, std::string_view primaryAlias, std::string_view valueAlias, bool useLike) 134 { 135 return details::SystemReferenceStringTableBuildPairedSearchStatement(builder, TableInfo::TableName(), TableInfo::ValueName(), PairedTable::TableName(), PairedTable::ValueName(), primaryAlias, valueAlias, useLike); 136 } 137 }; 138 }