SQLiteMetadataTable.h (2831B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <winget/SQLiteWrapper.h> 5 6 #include <wil/result_macros.h> 7 #include <string_view> 8 9 namespace AppInstaller::SQLite 10 { 11 using namespace std::string_view_literals; 12 13 static constexpr std::string_view s_MetadataValueName_DatabaseIdentifier = "databaseIdentifier"sv; 14 static constexpr std::string_view s_MetadataValueName_MajorVersion = "majorVersion"sv; 15 static constexpr std::string_view s_MetadataValueName_MinorVersion = "minorVersion"sv; 16 static constexpr std::string_view s_MetadataValueName_LastWriteTime = "lastwritetime"sv; 17 18 // The metadata table for the database. 19 // Contains a fixed-schema set of named values that can be used to determine how to read the rest of the database. 20 struct MetadataTable 21 { 22 static void Create(Connection& connection); 23 24 // Gets the named value from the metadata table, interpreting it as the given type. 25 template <typename Value> 26 static Value GetNamedValue(const Connection& connection, std::string_view name) 27 { 28 Statement statement = GetNamedValueStatement(connection, name); 29 return statement.GetColumn<Value>(0); 30 } 31 32 // Gets the named value from the metadata table, interpreting it as the given type. 33 // Returns nullopt if the value is not present. 34 template <typename Value> 35 static std::optional<Value> TryGetNamedValue(const Connection& connection, std::string_view name) 36 { 37 std::optional<Statement> statement = TryGetNamedValueStatement(connection, name); 38 if (statement) 39 { 40 return statement->GetColumn<Value>(0); 41 } 42 else 43 { 44 return std::nullopt; 45 } 46 } 47 48 // Sets the named value into the metadata table. 49 template <typename Value> 50 static void SetNamedValue(const Connection& connection, std::string_view name, Value&& v) 51 { 52 Statement statement = SetNamedValueStatement(connection, name); 53 statement.Bind(2, std::forward<Value>(v)); 54 statement.Execute(); 55 } 56 57 private: 58 // Internal function that gets the named value. 59 static Statement GetNamedValueStatement(const Connection& connection, std::string_view name); 60 61 // Internal function that gets the named value, or nullopt if it is not present. 62 static std::optional<Statement> TryGetNamedValueStatement(const Connection& connection, std::string_view name); 63 64 // Internal function that sets the named value. 65 static Statement SetNamedValueStatement(const Connection& connection, std::string_view name); 66 }; 67 }