From 479c3635a73c8a8812a2fb323db6a508f5a2336c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 26 Jul 2025 13:48:05 +0100 Subject: Modernise dbUtils --- gentoobrowse-api/service/utils/dbUtils.cpp | 35 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/gentoobrowse-api/service/utils/dbUtils.cpp b/gentoobrowse-api/service/utils/dbUtils.cpp index 49cd4de..396e75a 100644 --- a/gentoobrowse-api/service/utils/dbUtils.cpp +++ b/gentoobrowse-api/service/utils/dbUtils.cpp @@ -1,54 +1,55 @@ #include "dbUtils.h" #include -#include #include +#include #include #include #include namespace Gentoo::Utils::Database { std::string - emptyClone(DB::Connection * db, const std::string & orig) + emptyClone(DB::Connection * dbc, const std::string & orig) { auto tempTable = orig; auto dot = tempTable.rfind('.'); if (dot != std::string::npos) { tempTable = tempTable.substr(dot + 1); } - tempTable += AdHoc::scprintf<"_clone_%?%?">(getpid(), db); - db->execute("CREATE TEMPORARY TABLE " + tempTable + " AS SELECT * FROM " + orig + " WHERE false"); + tempTable += std::format("_clone_{}{}", getpid(), static_cast(dbc)); + dbc->execute(std::format("CREATE TEMPORARY TABLE {} AS SELECT * FROM {} WHERE false", tempTable, orig)); return tempTable; } std::pair - namedTemp(DB::Connection * db, const std::string & tempTable, const std::map & cols) + namedTemp( + DB::Connection * dbc, const std::string & tempTable, const std::map & cols) { std::set keys; std::set defs; - for (const auto & c : cols) { - keys.insert(c.first); - defs.insert(c.first + " " + c.second); + for (const auto & col : cols) { + keys.insert(col.first); + defs.insert(std::format("{} {}", col.first, col.second)); } - db->execute("CREATE TEMPORARY TABLE " + tempTable + "(" + boost::join(defs, ",") + ")"); - return {tempTable, tablePatchInserter(db, tempTable, keys)}; + dbc->execute(std::format("CREATE TEMPORARY TABLE {}({})", tempTable, boost::join(defs, ","))); + return {tempTable, tablePatchInserter(dbc, tempTable, keys)}; } void - drop(DB::Connection * db, const std::string & table) + drop(DB::Connection * dbc, const std::string & table) { - db->execute("DROP TABLE " + table); + dbc->execute(std::format("DROP TABLE {}", table)); } DB::ModifyCommandPtr - tablePatchInserter(DB::Connection * dbc, const DB::TablePatch & p) + tablePatchInserter(DB::Connection * dbc, const DB::TablePatch & patch) { - return tablePatchInserter(dbc, p.src, p.cols); + return tablePatchInserter(dbc, patch.src, patch.cols); } DB::ModifyCommandPtr - tablePatchInserter(DB::Connection * dbc, const std::string & t, const std::set & c) + tablePatchInserter(DB::Connection * dbc, const std::string & table, const std::set & columns) { - return dbc->modify("INSERT INTO " + t + "(" + boost::algorithm::join(c, ", ") + ") VALUES(" - + boost::algorithm::join(std::vector(c.size(), "?"), ", ") + ")"); + return dbc->modify(std::format("INSERT INTO {}({}) VALUES({})", table, boost::join(columns, ", "), + boost::join(std::vector(columns.size(), "?"), ", "))); } } -- cgit v1.2.3