From ac433870c2249ef7fd12c2061cf03c02984bc171 Mon Sep 17 00:00:00 2001 From: chris bell Date: Mon, 3 Aug 2026 22:31:41 -0500 Subject: [PATCH] sqlite database setup --- CMakeLists.txt | 8 +++--- assets/local.db | Bin 0 -> 40960 bytes flake.nix | 1 + src/critter.hpp | 21 +++++++-------- src/main.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 80 insertions(+), 20 deletions(-) create mode 100644 assets/local.db diff --git a/CMakeLists.txt b/CMakeLists.txt index e7b3f09..9353726 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,10 @@ find_package(Lua REQUIRED) pkg_check_modules(LUNASVG REQUIRED IMPORTED_TARGET lunasvg) add_library(lunasvg::lunasvg ALIAS PkgConfig::LUNASVG) -pkg_check_modules(SQLITE3 REQUIRED IMPORTED_TARGET sqlite3) -add_library(sqlite3::sqlite3 ALIAS PkgConfig::SQLITE3) +#pkg_check_modules(SQLITE3 REQUIRED IMPORTED_TARGET sqlite3) +#add_library(sqlite3::sqlite3 ALIAS PkgConfig::SQLITE3) + +find_package(SQLiteCpp REQUIRED) find_package(toml11 CONFIG REQUIRED) @@ -43,8 +45,8 @@ target_link_libraries(${PROJECT_NAME} PRIVATE Freetype::Freetype lunasvg::lunasvg glfw - sqlite3::sqlite3 toml11::toml11 + SQLiteCpp ) install(TARGETS ${PROJECT_NAME} DESTINATION bin) diff --git a/assets/local.db b/assets/local.db new file mode 100644 index 0000000000000000000000000000000000000000..0eadb6bd81da4d8ec290b90a74d86bf0ea152741 GIT binary patch literal 40960 zcmWFz^vNtqRY=P(%1ta$FlG>7U}R))P*7lCVBlk5VBmscCI$uu83qOh76t@hL@;r( z_!#thfAaE1F|c#}X5i)E5#d+m`px%<_Yv<#-hAGCoccy3MnhmU1V%$(Gz3ONU^E0q zLtr!nMnizwARoAEMwF>f@sj>gO34>Z-%Vsg#$Po2tYa z;u;YGmjwx@FXF2so?DzsR44Wf@5fizo(ybkgKn&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@ lunasvg lua sqlite.dev + sqlitecpp toml11 ]; in diff --git a/src/critter.hpp b/src/critter.hpp index cbf11b1..6b385a6 100644 --- a/src/critter.hpp +++ b/src/critter.hpp @@ -1,10 +1,11 @@ #pragma once #include -#include class CritterEvent { public: + int id; + int critter_id; std::string name; std::string date; std::string time; @@ -13,23 +14,21 @@ class CritterEvent { class CritterDocument { public: + int id; + int critter_id; std::string name; std::string doc_type; std::string cached_path; }; class Critter { - - enum Sex {Male, Female}; - public: + int id; std::string name; - Sex sex; std::string birthdate; - Critter *mother; - Critter *father; - std::vector children; std::string notes; - std::vector events; - std::vector documents; -}; \ No newline at end of file + int mother_id; + int father_id; + std::string gender; + std::string species; +}; diff --git a/src/main.cpp b/src/main.cpp index 0d2fe14..1a98982 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -13,15 +15,17 @@ #include #include #include "critter.hpp" -#include +#include +#include +#include static bool is_initalized = false; static bool is_running = false; static Rml::Context *ctx; +static std::map critters = {}; struct AppData { Rml::String page_title = "Main"; - sqlite3 *db; } app_data; void Shutdown() { @@ -48,22 +52,74 @@ bool Initalize() { std::cout << "[ERROR] Failed to initalize the backend."; return false; } + std::cout << "[INFO] RmlUI Backend initalized.\n"; Rml::SetSystemInterface(Backend::GetSystemInterface()); Rml::SetRenderInterface(Backend::GetRenderInterface()); Rml::Initialise(); Rml::Lua::Initialise(); + + // TODO: Add more error handling for initalization + + std::cout << "[INFO] Loading local DB...\n"; + try { + SQLite::Database db = SQLite::Database("./assets/local.db", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE); + + SQLite::Statement q(db, "SELECT * FROM critters"); + + while(q.executeStep()) { + int id = q.getColumn(0); + std::string name = q.getColumn(1); + std::string gender = q.getColumn(2); + int mother_id = q.getColumn(3); + int father_id = q.getColumn(4); + std::string notes = q.getColumn(5); + std::string species = q.getColumn(6); + + Critter c; + c.id = id; + c.name = name; + c.gender = gender; + c.mother_id = mother_id; + c.father_id = father_id; + c.notes = notes; + c.species = species; + + critters[c.id] = c; + } + + std::cout << "[INFO] Loaded " << critters.size() << " critters from local db.\n"; + } + catch (std::exception &e){ + std::cout << "[ERROR] DB exception: " << e.what() << std::endl; + return false; + } + std::cout << "[INFO] Local DB initalized.\n"; + + // ------- DB TESTING + std::cout << "\n"; + for (auto c : critters) { + std::cout << "\nCritter " << c.first << "\n"; + std::cout << "ID: " << c.second.id << std::endl; + std::cout << "Name:" << c.second.name << std::endl; + std::cout << "Gender: " << c.second.gender << std::endl; + std::cout << "Mother ID: " << c.second.mother_id << std::endl; + std::cout << "Father ID: " << c.second.father_id << std::endl; + std::cout << "Notes: " << c.second.notes << std::endl; + std::cout << "Species: " << c.second.species << std::endl; + } + std::cout << "\n"; + // ------- END DB TESTING ctx = Rml::CreateContext("main", Rml::Vector2i(800, 600)); if (!ctx) { std::cout << "[ERROR] Failed to create context."; - Shutdown(); + return false; } + std::cout << "[INFO] Main context initalized.\n"; Rml::Debugger::Initialise(ctx); - - // lua_State *L = Rml::Lua::Interpreter::GetLuaState(); return true; @@ -74,7 +130,7 @@ void Update(Rml::DataModelHandle model_handle) { } int main() { - std::cout << "Hello world!\n"; + std::cout << "[INFO] Starting critterfolio...\n"; is_initalized = Initalize(); if (!is_initalized) { @@ -82,6 +138,7 @@ int main() { Shutdown(); return 1; } + std::cout << "[INFO] Application initalized, all systems running.\n"; is_running = true; @@ -96,6 +153,7 @@ int main() { Shutdown(); return 1; } + std::cout << "[INFO] Main page loaded.\n"; doc->Show();