summaryrefslogtreecommitdiff
path: root/bot/src/timer.cpp
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-07-02 03:31:54 +0500
committerilotterytea <iltsu@alright.party>2025-07-02 03:31:54 +0500
commit79be89ad0491bfdd110b2c612e21a0f28c29fa87 (patch)
tree589daab514de11cc87e424b62d87f8cac12494ab /bot/src/timer.cpp
parentfea3c12d6b621796bb239cebb57a5a5014dfe350 (diff)
feat: MARIADB SUPPORT!!!!
Diffstat (limited to 'bot/src/timer.cpp')
-rw-r--r--bot/src/timer.cpp44
1 files changed, 19 insertions, 25 deletions
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 <chrono>
-#include <pqxx/pqxx>
#include <string>
#include <thread>
#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<db::BaseDatabase> 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>();
- int interval_sec = timer[1].as<int>();
- std::string message = timer[2].as<std::string>();
- int channel_id = timer[3].as<int>();
+ 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<std::string>());
+ 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<std::string>();
+ 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));
}