From 79be89ad0491bfdd110b2c612e21a0f28c29fa87 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 2 Jul 2025 03:31:54 +0500 Subject: feat: MARIADB SUPPORT!!!! --- bot/src/timer.cpp | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) (limited to 'bot/src/timer.cpp') diff --git a/bot/src/timer.cpp b/bot/src/timer.cpp index 055dde0..e4f3508 100644 --- a/bot/src/timer.cpp +++ b/bot/src/timer.cpp @@ -1,11 +1,11 @@ #include "timer.hpp" #include -#include #include #include #include "config.hpp" +#include "database.hpp" #include "irc/client.hpp" #include "utils/chrono.hpp" @@ -13,22 +13,22 @@ namespace bot { void create_timer_thread(irc::Client *irc_client, Configuration *configuration) { while (true) { - pqxx::connection conn(GET_DATABASE_CONNECTION_URL_POINTER(configuration)); - pqxx::work *work = new pqxx::work(conn); + std::unique_ptr conn = + db::create_connection(*configuration); - pqxx::result timers = work->exec( + db::DatabaseRows timers = conn->exec( "SELECT id, interval_sec, message, channel_id, last_executed_at FROM " "timers"); for (const auto &timer : timers) { - int id = timer[0].as(); - int interval_sec = timer[1].as(); - std::string message = timer[2].as(); - int channel_id = timer[3].as(); + int id = std::stoi(timer.at("id")); + int interval_sec = std::stoi(timer.at("interval_sec")); + std::string message = timer.at("message"); + int channel_id = std::stoi(timer.at("channel_id")); // it could be done in sql query std::chrono::system_clock::time_point last_executed_at = - utils::chrono::string_to_time_point(timer[4].as()); + utils::chrono::string_to_time_point(timer.at("last_executed_at")); auto now = std::chrono::system_clock::now(); auto now_time_it = std::chrono::system_clock::to_time_t(now); auto now_tm = std::gmtime(&now_time_it); @@ -38,31 +38,25 @@ namespace bot { now - last_executed_at); if (difference.count() > interval_sec) { - pqxx::result channels = work->exec( - "SELECT alias_name, opted_out_at FROM channels WHERE id = " + - std::to_string(channel_id)); + db::DatabaseRows channels = conn->exec( + "SELECT alias_name, opted_out_at FROM channels WHERE id = $1", + {std::to_string(channel_id)}); - if (!channels.empty() && channels[0][1].is_null()) { - std::string alias_name = channels[0][0].as(); + if (!channels.empty() && channels[0].at("opted_out_at").empty()) { + std::string alias_name = channels[0].at("alias_name"); irc_client->say(alias_name, message); } - work->exec( - "UPDATE timers SET last_executed_at = timezone('utc', now()) " + conn->exec( + "UPDATE timers SET last_executed_at = UTC_TIMESTAMP " "WHERE " - "id = " + - std::to_string(id)); - - work->commit(); - - delete work; - work = new pqxx::work(conn); + "id = $1", + {std::to_string(id)}); } } - delete work; - conn.close(); + conn->close(); std::this_thread::sleep_for(std::chrono::seconds(1)); } -- cgit v1.2.3