winget-cli

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

CheckpointTable.cpp (2505B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "CheckpointTable.h"
      5 #include <winget/SQLiteStatementBuilder.h>
      6 
      7 namespace AppInstaller::Repository::Microsoft::Schema::Checkpoint_V1_0
      8 {
      9     using namespace std::string_view_literals;
     10     static constexpr std::string_view s_CheckpointTable_Table_Name = "Checkpoints"sv;
     11     static constexpr std::string_view s_CheckpointTable_Index_Name = "Checkpoints_pkindex"sv;
     12     static constexpr std::string_view s_CheckpointTable_Name_Column = "Name";
     13     static constexpr std::string_view s_CheckpointTable_CreationTime_Column = "CreationTime";
     14 
     15     std::string_view CheckpointTable::TableName()
     16     {
     17         return s_CheckpointTable_Table_Name;
     18     }
     19     
     20     void CheckpointTable::Create(SQLite::Connection& connection)
     21     {
     22         using namespace SQLite::Builder;
     23 
     24         SQLite::Savepoint savepoint = SQLite::Savepoint::Create(connection, "createCheckpointTable_v1_0");
     25 
     26         StatementBuilder createTableBuilder;
     27         createTableBuilder.CreateTable(s_CheckpointTable_Table_Name).Columns({
     28             ColumnBuilder(s_CheckpointTable_Index_Name, Type::Integer).PrimaryKey(),
     29             ColumnBuilder(s_CheckpointTable_Name_Column, Type::Text).NotNull(),
     30             ColumnBuilder(s_CheckpointTable_CreationTime_Column, Type::Int64).NotNull()
     31             });
     32 
     33         createTableBuilder.Execute(connection);
     34 
     35         savepoint.Commit();
     36     }
     37 
     38     std::vector<SQLite::rowid_t> CheckpointTable::GetCheckpointIds(SQLite::Connection& connection)
     39     {
     40         SQLite::Builder::StatementBuilder builder;
     41         builder.Select(SQLite::RowIDName).From(s_CheckpointTable_Table_Name).OrderBy(SQLite::RowIDName).Descending();
     42 
     43         SQLite::Statement select = builder.Prepare(connection);
     44 
     45         std::vector<SQLite::rowid_t> checkpoints;
     46 
     47         while (select.Step())
     48         {
     49             checkpoints.emplace_back(select.GetColumn<SQLite::rowid_t>(0));
     50         }
     51 
     52         return checkpoints;
     53     }
     54 
     55     SQLite::rowid_t CheckpointTable::AddCheckpoint(SQLite::Connection& connection, std::string_view checkpointName)
     56     {
     57         SQLite::Builder::StatementBuilder builder;
     58         builder.InsertInto(s_CheckpointTable_Table_Name)
     59             .Columns({ s_CheckpointTable_Name_Column,
     60                 s_CheckpointTable_CreationTime_Column })
     61             .Values(checkpointName, Utility::GetCurrentUnixEpoch());
     62 
     63         builder.Execute(connection);
     64         return connection.GetLastInsertRowID();
     65     }
     66 }