finished database layer

This commit is contained in:
2025-12-18 15:45:43 -06:00
parent 2ebffaec48
commit 9ca1e8784f
3 changed files with 29 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
#include "nlohmann/json_fwd.hpp" #include "nlohmann/json_fwd.hpp"
#include "skwyward-api-utils.hpp" #include "skwyward-api-utils.hpp"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "types.hpp"
#include <exception> #include <exception>
#include <memory> #include <memory>
#include <sodium.h> #include <sodium.h>
@@ -38,6 +39,10 @@ int main (int argc, char *argv[]) {
std::shared_ptr<clickhouse::Client> client_shared_ptr = std::make_shared<clickhouse::Client>(clickhouse::ClientOptions().SetHost(clickhouse_host_name).SetPassword(clickhouse_password).SetUser(clickhouse_username).SetDefaultDatabase(clickhouse_schema)); std::shared_ptr<clickhouse::Client> client_shared_ptr = std::make_shared<clickhouse::Client>(clickhouse::ClientOptions().SetHost(clickhouse_host_name).SetPassword(clickhouse_password).SetUser(clickhouse_username).SetDefaultDatabase(clickhouse_schema));
database_utils::register_user(client_shared_ptr, test_username, test_password);
spdlog::info("USER UUID: {}", database_utils::uuid_to_string( database_utils::get_user_uuid(client_shared_ptr, test_username).value() ));
spdlog::info("Connected to the database"); spdlog::info("Connected to the database");
return 0; return 0;

View File

@@ -185,5 +185,23 @@ void insert_grade_updates(
} }
std::optional<clickhouse::UUID> get_user_uuid(const CHClient& client, const std::string& username) {
std::optional<clickhouse::UUID> result;
client->Select(
"SELECT user_id FROM users WHERE username = '" + username + "' LIMIT 1",
[&](const clickhouse::Block& b) {
if (b.GetRowCount() == 0)
return;
auto col_uuid = b[0]->As<clickhouse::ColumnUUID>();
auto uuid_val = col_uuid->At(0);
result = uuid_val; // your helper that converts UUID pair → string
}
);
return result;
}
} // namespace database_utils } // namespace database_utils

View File

@@ -6,10 +6,14 @@
#include <vector> #include <vector>
#include <clickhouse/client.h> #include <clickhouse/client.h>
#include "clickhouse/base/uuid.h"
#include "skwyward-api-utils.hpp" #include "skwyward-api-utils.hpp"
namespace database_utils { namespace database_utils {
clickhouse::UUID parse_uuid(const std::string& str);
std::string uuid_to_string(const clickhouse::UUID& u);
// ---------- DB Handle ---------- // ---------- DB Handle ----------
using CHClient = std::shared_ptr<clickhouse::Client>; using CHClient = std::shared_ptr<clickhouse::Client>;
@@ -93,5 +97,7 @@ insert_grade_updates(
const std::vector<AssignmentDiff>& diffs const std::vector<AssignmentDiff>& diffs
); );
std::optional<clickhouse::UUID> get_user_uuid(const CHClient& client, const std::string& username);
} // namespace database_utils } // namespace database_utils