Aktualizr
C++ SOTA Client
sqlstorage_base.h
1 #ifndef SQLSTORAGE_BASE_H_
2 #define SQLSTORAGE_BASE_H_
3 
4 #include <boost/filesystem.hpp>
5 #include <boost/interprocess/sync/file_lock.hpp>
6 #include <boost/interprocess/sync/scoped_lock.hpp>
7 
8 #include "libaktualizr/config.h"
9 #include "sql_utils.h"
10 
11 enum class DbVersion : int32_t { kEmpty = -1, kInvalid = -2 };
12 
13 class StorageLock {
14  public:
15  StorageLock() = default;
16  StorageLock(boost::filesystem::path path);
17  StorageLock(StorageLock &other) = delete;
18  StorageLock &operator=(StorageLock &other) = delete;
19  StorageLock(StorageLock &&other) = default;
20  StorageLock &operator=(StorageLock &&other) = default;
21  virtual ~StorageLock();
22 
23  class locked_exception : std::runtime_error {
24  public:
25  locked_exception() : std::runtime_error("locked") {}
26  };
27 
28  private:
29  boost::filesystem::path lock_path;
30  boost::interprocess::file_lock fl_;
31 };
32 
34  public:
35  explicit SQLStorageBase(boost::filesystem::path sqldb_path, bool readonly, std::vector<std::string> schema_migrations,
36  std::vector<std::string> schema_rollback_migrations, std::string current_schema,
37  int current_schema_version);
38  ~SQLStorageBase() = default;
39  std::string getTableSchemaFromDb(const std::string &tablename);
40  bool dbMigrateForward(int version_from, int version_to = 0);
41  bool dbMigrateBackward(int version_from, int version_to = 0);
42  bool dbMigrate();
43  DbVersion getVersion(); // non-negative integer on success or -1 on error
44  boost::filesystem::path dbPath() const;
45 
46  protected:
47  boost::filesystem::path sqldb_path_;
48  bool readonly_{false};
49 
50  StorageLock lock;
51  std::shared_ptr<std::mutex> mutex_;
52 
53  const std::vector<std::string> schema_migrations_;
54  std::vector<std::string> schema_rollback_migrations_;
55  const std::string current_schema_;
56  const int current_schema_version_;
57 
58  SQLite3Guard dbConnection() const;
59  bool dbInsertBackMigrations(SQLite3Guard &db, int version_latest);
60 };
61 
62 #endif // SQLSTORAGE_BASE_H_
StorageLock
Definition: sqlstorage_base.h:13
StorageLock::locked_exception
Definition: sqlstorage_base.h:23
SQLite3Guard
Definition: sql_utils.h:131
SQLStorageBase
Definition: sqlstorage_base.h:33