libnick 2025.9.1
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
sqlitedatabase.h
Go to the documentation of this file.
1
22
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#include "sqlite.h"
38#include "sqlitestatement.h"
39
41{
42 using SqliteCustomFunction = std::function<void(SqliteFunctionContext&)>;
43
48 {
49 public:
57 SqliteDatabase(const std::filesystem::path& path, int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
63 SqliteDatabase(SqliteDatabase&& other) noexcept;
67 ~SqliteDatabase() noexcept;
72 const std::filesystem::path& getPath() const noexcept;
77 bool isEncrypted() const noexcept;
82 bool isUnlocked() const noexcept;
90 bool unlock(const std::string& password) noexcept;
101 bool setPassword(const std::string& password);
109 bool registerFunction(const std::string& name, const SqliteCustomFunction& func, int expectedArgs = -1) noexcept;
118 SqliteStatement createStatement(const std::string& command);
124 bool execute(const std::string& command) noexcept;
125 SqliteDatabase& operator=(const SqliteDatabase&) = delete;
131 SqliteDatabase& operator=(SqliteDatabase&& other) noexcept;
132
133 private:
134 mutable std::mutex m_mutex;
135 std::filesystem::path m_path;
136 int m_flags;
137 bool m_isEncrypted;
138 bool m_isUnlocked;
139 sqlite3* m_database;
140 std::unordered_map<std::string, SqliteCustomFunction> m_customFunctions;
141 };
142}
143
144#endif //SQLDATABASE_H
~SqliteDatabase() noexcept
Destructs a SqliteDatabase.
SqliteStatement createStatement(const std::string &command)
Creates a new SqlStatement for the database.
bool unlock(const std::string &password) noexcept
Unlocks the database.
const std::filesystem::path & getPath() const noexcept
Gets the path of the database file.
SqliteDatabase(SqliteDatabase &&other) noexcept
Constructs a SqliteDatabase via move.
bool isEncrypted() const noexcept
Gets whether or not the database is encrypted.
bool registerFunction(const std::string &name, const SqliteCustomFunction &func, int expectedArgs=-1) noexcept
Registers a custom sql function to the database.
SqliteDatabase(const std::filesystem::path &path, int flags=SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE)
Constructs a SqliteDatabase.
bool execute(const std::string &command) noexcept
Executes a sql command on the database.
bool setPassword(const std::string &password)
Sets the database's password.
SqliteDatabase(const SqliteDatabase &)=delete
bool isUnlocked() const noexcept
Gets whether or not the database is unlocked.
A sqlite function context.
Definition sqlitefunctioncontext.h:36
A sqlite statement.
Definition sqlitestatement.h:35
Definition sqlite.h:40
std::function< void(SqliteFunctionContext &)> SqliteCustomFunction
Definition sqlitedatabase.h:42