From e64669e02f6099a1c910ebfd030acf58f68d58da Mon Sep 17 00:00:00 2001 From: qq_36105691 Date: Sun, 18 Feb 2024 09:57:43 +0800 Subject: [PATCH] feat[SQLiteObject.h]: Add simple reflection ability --- 222100414/src/CMakeLists.txt | 3 ++- 222100414/src/DWASearch.cpp | 30 +++++++++++++++++++++------ 222100414/src/SQLiteObject.h | 40 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 222100414/src/SQLiteObject.h diff --git a/222100414/src/CMakeLists.txt b/222100414/src/CMakeLists.txt index 1f4b0e8..f9095d3 100644 --- a/222100414/src/CMakeLists.txt +++ b/222100414/src/CMakeLists.txt @@ -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) diff --git a/222100414/src/DWASearch.cpp b/222100414/src/DWASearch.cpp index e8ef7d5..6ada5ec 100644 --- a/222100414/src/DWASearch.cpp +++ b/222100414/src/DWASearch.cpp @@ -1,9 +1,27 @@ -#include "Logger.h" +#include "DatabaseManager.h" +#include "SQLiteObject.h" +#include + + +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(sql); + for (const auto &result: results) { + LOG_INFO("Player: " + result.getfirstName() + " " + result.getlastName() + " from " + result.getcountry()); + } } diff --git a/222100414/src/SQLiteObject.h b/222100414/src/SQLiteObject.h new file mode 100644 index 0000000..57ef492 --- /dev/null +++ b/222100414/src/SQLiteObject.h @@ -0,0 +1,40 @@ +// +// Created by Adarion on 2024/2/18. +// + +#ifndef DWASEARCH_SQLITEOBJECT_H +#define DWASEARCH_SQLITEOBJECT_H + +#include +#include +#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(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> propertiesSetters; \ +public: \ + void fillData(sqlite3_stmt *stmt) { \ + int index = 0; \ + for (auto &setter : propertiesSetters) { \ + setter(stmt, index++); \ + } \ + }; + + +#endif //DWASEARCH_SQLITEOBJECT_H -- GitLab