Enigma  3.2.0
A Simple, Reliable and Efficient Encryption Tool
Database.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Core/Core.hpp>
4 #include <Logger/Logger.hpp>
5 #include <Utility/FileUtils.hpp>
6 #include <Utility/SizeUtils.hpp>
8 
9 #include <SQLiteCpp/SQLiteCpp.h>
10 //#include <Utility/GZip.hpp>
11 #include "Models/Encryption.hpp"
12 #include <memory>
13 #include <vector>
14 
15 
17 
23 class Database final {
24  ENIGMA_STATIC_CLASS(Database);
25 
26  public:
27 #pragma region enums
29  enum class OrderBy : byte {
30  ID,
31  Title,
32  DateTime,
33  Size,
34 
36  };
38  friend const char *operator*(OrderBy order_by) {
39 #define CASE_STR(e) \
40  case OrderBy::e: \
41  return #e
42  switch (order_by) {
43  CASE_STR(ID);
44  CASE_STR(Title);
45  CASE_STR(DateTime);
46  CASE_STR(Size);
47  default:
48  return "<unknown OrderBy>";
49  }
50 #undef CASE_STR
51  }
52  friend std::ostream& operator<<(std::ostream& os, const OrderBy order_by) noexcept // for constructing sql
53  {
54  switch (order_by) {
55  case OrderBy::ID:
56  os << "ide";
57  break;
58  case OrderBy::Title:
59  os << "title";
60  break;
61  case OrderBy::DateTime:
62  os << "date_time";
63  break;
64  case OrderBy::Size:
65  os << "size";
66  break;
67  default:
68  break;
69  }
70  return os;
71  }
73  enum class Order : byte {
74  Ascending,
75  Descending,
76 
78  };
80  friend const char *operator*(Order order) {
81 #define CASE_STR(e) \
82  case Order::e: \
83  return #e
84  switch (order) {
85  CASE_STR(Ascending);
86  CASE_STR(Descending);
87  default:
88  return "<unknown Order>";
89  }
90 #undef CASE_STR
91  }
92 
93  friend std::ostream& operator<<(std::ostream& os, const Order order) noexcept // for constructing sql
94  {
95  return os << (order == Order::Ascending ? "ASC" : "DESC");
96  }
97 #pragma endregion enums
98 
99  public:
101  static void initialize();
103  static void shutdown();
104 
105  public:
109  static std::int64_t addEncryption(const std::unique_ptr<Encryption>& e);
110 
115  static std::int64_t addCipherChunk(const std::unique_ptr<CipherChunk>& cc);
116 
118  static std::unique_ptr<Encryption> getEncryption(const std::int64_t ide);
119 
123  static std::unique_ptr<CipherChunk> getCipherChunk(const std::int64_t ide);
124 
128  static void getCipherChunks(const std::int64_t ide, const std::function<bool(std::unique_ptr<CipherChunk>&& cc)>& callback);
129 
131  static std::vector<std::unique_ptr<Encryption>> getAllEncryptions(const OrderBy order_by = OrderBy::ID, const Order order = Order::Descending);
132 
137  static bool deleteEncryption(const std::int64_t ide);
138 
143  static bool deleteAllEncryptions();
144 
148  static std::vector<std::unique_ptr<Encryption>> searchEncryptionsByTitle(const std::string& qtitle, OrderBy order_by = OrderBy::ID, Order order = Order::Descending);
149 
150  public: // Accessors
152  static const std::unique_ptr<SQLite::Database>& getStorage() noexcept { return m_database; }
153 
154  public: // Modifiers
162  static void Vacuum() noexcept;
163 
164  static void Export(const fs::path& filename);
165  static void Import(const fs::path& filename);
166 
167  private:
168  inline static std::unique_ptr<SQLite::Database> m_database{nullptr};
169 };
170 
#define CASE_STR(e)
#define NS_ENIGMA_BEGIN
Enable/Disable Assertions.
Definition: Macros.hpp:13
#define NS_ENIGMA_END
Definition: Macros.hpp:14
#define ENIGMA_ENUM_DECLARE_BEGIN_END(begin)
Allows looping over an enum by providing a BEGIN and END enum values NOTE: should be placed at the en...
Definition: Macros.hpp:148
static bool deleteEncryption(const std::int64_t ide)
static bool deleteAllEncryptions()
static std::unique_ptr< CipherChunk > getCipherChunk(const std::int64_t ide)
friend std::ostream & operator<<(std::ostream &os, const OrderBy order_by) noexcept
Definition: Database.hpp:52
static std::int64_t addCipherChunk(const std::unique_ptr< CipherChunk > &cc)
static const std::unique_ptr< SQLite::Database > & getStorage() noexcept
Definition: Database.hpp:152
static std::unique_ptr< Encryption > getEncryption(const std::int64_t ide)
friend std::ostream & operator<<(std::ostream &os, const Order order) noexcept
Definition: Database.hpp:93
static std::int64_t addEncryption(const std::unique_ptr< Encryption > &e)
static void initialize()
static void Import(const fs::path &filename)
static std::vector< std::unique_ptr< Encryption > > getAllEncryptions(const OrderBy order_by=OrderBy::ID, const Order order=Order::Descending)
friend const char * operator*(Order order)
Definition: Database.hpp:80
static void getCipherChunks(const std::int64_t ide, const std::function< bool(std::unique_ptr< CipherChunk > &&cc)> &callback)
static void Export(const fs::path &filename)
static std::vector< std::unique_ptr< Encryption > > searchEncryptionsByTitle(const std::string &qtitle, OrderBy order_by=OrderBy::ID, Order order=Order::Descending)
friend const char * operator*(OrderBy order_by)
Definition: Database.hpp:38
static void Vacuum() noexcept
static void shutdown()
std::uint8_t byte
Definition: Types.hpp:12