sqlite database setup

This commit is contained in:
2026-08-03 22:31:41 -05:00
parent d87b2e3730
commit ac433870c2
5 changed files with 80 additions and 20 deletions

View File

@@ -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)

BIN
assets/local.db Normal file

Binary file not shown.

View File

@@ -67,6 +67,7 @@
lunasvg
lua
sqlite.dev
sqlitecpp
toml11
];
in

View File

@@ -1,10 +1,11 @@
#pragma once
#include <string>
#include <vector>
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<Critter*> children;
std::string notes;
std::vector<CritterEvent> events;
std::vector<CritterDocument> documents;
};
int mother_id;
int father_id;
std::string gender;
std::string species;
};

View File

@@ -5,6 +5,8 @@
#include <RmlUi/Core/Math.h>
#include <RmlUi/Lua/Interpreter.h>
#include <RmlUi/Lua/Lua.h>
#include <SQLiteCpp/Database.h>
#include <exception>
#include <iostream>
#include <RmlUi/Core.h>
#include <RmlUi_Backend.h>
@@ -13,15 +15,17 @@
#include <lua.h>
#include <RmlUi/Debugger/Debugger.h>
#include "critter.hpp"
#include <sqlite3.h>
#include <ostream>
#include <SQLiteCpp/SQLiteCpp.h>
#include <map>
static bool is_initalized = false;
static bool is_running = false;
static Rml::Context *ctx;
static std::map<int, Critter> 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();