ORE Studio 0.0.4
Loading...
Searching...
No Matches
mapper_helpers.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_DATABASE_REPOSITORY_MAPPER_HELPERS_HPP
21#define ORES_DATABASE_REPOSITORY_MAPPER_HELPERS_HPP
22
23#include <vector>
24#include <chrono>
25#include <iomanip>
26#include <sstream>
27#include <format>
28#include <sqlgen/postgres.hpp>
29#include "ores.utility/log/make_logger.hpp"
30
31namespace ores::database::repository {
32
56template<typename Source, typename Dest, typename MapFunc>
57std::vector<Dest> map_vector(
58 const std::vector<Source>& source,
59 MapFunc&& map_func,
60 utility::log::logger_t& lg,
61 const std::string& log_prefix) {
62
63 using namespace ores::utility::log;
64
65 BOOST_LOG_SEV(lg, debug) << "Mapping " << log_prefix
66 << ". Total: " << source.size();
67
68 std::vector<Dest> result;
69 result.reserve(source.size());
70 std::ranges::transform(source, std::back_inserter(result),
71 std::forward<MapFunc>(map_func));
72
73 BOOST_LOG_SEV(lg, debug) << "Mapped " << log_prefix << ".";
74 return result;
75}
76
89inline std::chrono::system_clock::time_point
90timestamp_to_timepoint(const sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S">& ts) {
91 const auto str = ts.str();
92 std::tm tm = {};
93 std::istringstream ss(str);
94 ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
95 return std::chrono::system_clock::from_time_t(std::mktime(&tm));
96}
97
111inline sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S">
112timepoint_to_timestamp(const std::chrono::system_clock::time_point& tp,
113 utility::log::logger_t& lg) {
114 using namespace ores::utility::log;
115
116 const auto s = std::format("{:%Y-%m-%d %H:%M:%S}", tp);
117 const auto r = sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S">::from_string(s);
118 if (!r) {
119 BOOST_LOG_SEV(lg, error) << "Error converting timepoint to timestamp";
120 return {};
121 }
122 return r.value();
123}
124
125}
126
127#endif
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30