libnick 2024.9.2
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
sqldatabase.h
Go to the documentation of this file.
1
23#ifndef SQLDATABASE_H
24#define SQLDATABASE_H
25
26#ifndef SQLITE_HAS_CODEC
27#define SQLITE_HAS_CODEC
28#endif
29
30#include <filesystem>
31#include <functional>
32#include <memory>
33#include <mutex>
34#include <string>
35#include <unordered_map>
36#ifdef _WIN32
37#include <sqlcipher/sqlite3.h>
38#else
39#include "sqlite3.h"
40#endif
41#include "sqlcontext.h"
42#include "sqlstatement.h"
43
45{
46 using SqliteCustomFunction = std::function<void(SqlContext&)>;
47
52 {
53 public:
60 SqlDatabase(const std::filesystem::path& path, int flags);
65 SqlDatabase(const SqlDatabase& database);
70 SqlDatabase(SqlDatabase&& database) noexcept;
75 const std::filesystem::path& getPath() const;
80 bool isEncrypted() const;
86 sqlite3* c_obj();
93 bool unlock(const std::string& password);
102 bool changePassword(const std::string& password);
108 bool exec(const std::string& command);
115 SqlStatement createStatement(const std::string& command);
123 bool registerFunction(const std::string& name, const SqliteCustomFunction& func, int expectedArgs = -1);
135 SqlDatabase& operator=(SqlDatabase&& database) noexcept;
140 operator bool() const;
141
142 private:
143 mutable std::mutex m_mutex;
144 std::filesystem::path m_path;
145 int m_flags;
146 bool m_isEncrypted;
147 std::shared_ptr<sqlite3> m_database;
148 bool m_isUnlocked;
149 std::unordered_map<std::string, SqliteCustomFunction> m_custom;
150 };
151}
152
153#endif //SQLDATABASE_H
A sqlite function context.
Definition sqlcontext.h:45
A sqlite (sqlcipher) database.
Definition sqldatabase.h:52
SqlDatabase & operator=(SqlDatabase &&database) noexcept
Moves a SqlDatabase object.
SqlDatabase(const std::filesystem::path &path, int flags)
Constructs a SqlDatabase.
SqlDatabase & operator=(const SqlDatabase &database)
Copies a SqlDatabase object.
const std::filesystem::path & getPath() const
Gets the path of the database file.
bool exec(const std::string &command)
Executes an sql command on the database.
SqlStatement createStatement(const std::string &command)
Creates an new SqlStatement for the database.
SqlDatabase(SqlDatabase &&database) noexcept
Moves a SqlDatabase object.
SqlDatabase(const SqlDatabase &database)
Copies a SqlDatabase object.
bool unlock(const std::string &password)
Unlocks the database.
bool registerFunction(const std::string &name, const SqliteCustomFunction &func, int expectedArgs=-1)
Registers a custom sql function to the database.
bool changePassword(const std::string &password)
Changes the database's password.
bool isEncrypted() const
Gets whether or not the database is encrypted.
sqlite3 * c_obj()
Returns the underlying sqlite3 object pointer for the database.
A sqlite statement.
Definition sqlstatement.h:45
Definition sqlcontext.h:40
std::function< void(SqlContext &)> SqliteCustomFunction
Definition sqldatabase.h:46