提交 e64669e0 编写于 作者: Q qq_36105691

feat[SQLiteObject.h]: Add simple reflection ability

上级 166d2b19
......@@ -12,5 +12,6 @@ add_executable(DWASearch DWASearch.cpp
DatabaseManager.cpp
DatabaseManager.h
Logger.cpp
Logger.h)
Logger.h
SQLiteObject.h)
target_link_libraries(DWASearch sqlite3)
#include "Logger.h"
#include "DatabaseManager.h"
#include "SQLiteObject.h"
#include <map>
class Player {
SQLITE_OBJECT
DECLARE_PROPERTY(id, int)
DECLARE_PROPERTY(firstName, std::string)
DECLARE_PROPERTY(lastName, std::string)
DECLARE_PROPERTY(country, std::string)
};
int main(int argc, char *argv[], [[maybe_unused]] char *envp[]) {
LOG_INFO("Starting DWASearch");
LOG_DEBUG("Debugging is enabled");
LOG_TRACE("Tracing is enabled");
LOG_ERROR("This is an error");
LOG_WARNING("This is a warning");
const std::string sql = "SELECT * FROM Player";
auto db = Mika::DatabaseManager::getInstance();
auto results = db->executeQuery<Player>(sql);
for (const auto &result: results) {
LOG_INFO("Player: " + result.getfirstName() + " " + result.getlastName() + " from " + result.getcountry());
}
}
//
// Created by Adarion on 2024/2/18.
//
#ifndef DWASEARCH_SQLITEOBJECT_H
#define DWASEARCH_SQLITEOBJECT_H
#include <set>
#include <functional>
#include "sqlite3.h"
#include "DatabaseManager.h"
#define DECLARE_PROPERTY(name, type) \
private: \
type name = {}; \
[[maybe_unused]] int name##Setter = [this]() { \
propertiesSetters.emplace_back([&](sqlite3_stmt *stmt, int index) { \
name = Mika::DatabaseManager::getResultValue<type>(stmt, index); \
}); \
\
return 0; \
}(); \
public: \
[[nodiscard]]type get##name() const { return name; } \
void set##name(type &&value) { name = std::move(value); }
#define SQLITE_OBJECT \
private: \
std::vector<std::function<void(sqlite3_stmt *, int)>> propertiesSetters; \
public: \
void fillData(sqlite3_stmt *stmt) { \
int index = 0; \
for (auto &setter : propertiesSetters) { \
setter(stmt, index++); \
} \
};
#endif //DWASEARCH_SQLITEOBJECT_H
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册