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 <sqlgen/postgres.hpp>
26#include "ores.logging/make_logger.hpp"
27#include "ores.platform/time/datetime.hpp"
28
30
54template<typename Source, typename Dest, typename MapFunc>
55std::vector<Dest> map_vector(
56 const std::vector<Source>& source,
57 MapFunc&& map_func,
58 logging::logger_t& lg,
59 const std::string& log_prefix) {
60
61 using namespace ores::logging;
62
63 BOOST_LOG_SEV(lg, debug) << "Mapping " << log_prefix
64 << ". Total: " << source.size();
65
66 std::vector<Dest> result;
67 result.reserve(source.size());
68 std::ranges::transform(source, std::back_inserter(result),
69 std::forward<MapFunc>(map_func));
70
71 BOOST_LOG_SEV(lg, debug) << "Mapped " << log_prefix << ".";
72 return result;
73}
74
86inline std::chrono::system_clock::time_point
87timestamp_to_timepoint(std::string_view timestamp_str) {
89 std::string{timestamp_str});
90}
91
103inline std::chrono::system_clock::time_point
104timestamp_to_timepoint(const sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S">& ts) {
105 return timestamp_to_timepoint(std::string_view{ts.str()});
106}
107
121inline sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S">
122timepoint_to_timestamp(const std::chrono::system_clock::time_point& tp,
123 logging::logger_t& lg) {
124 using namespace ores::logging;
125
127 const auto r = sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S">::from_string(s);
128 if (!r) {
129 BOOST_LOG_SEV(lg, error) << "Error converting timepoint to timestamp";
130 return {};
131 }
132 return r.value();
133}
134
135}
136
137#endif
Repository infrastructure and bitemporal operations.
Definition bitemporal_operations.hpp:31
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
static std::string format_time_point_utc(const std::chrono::system_clock::time_point &tp, const std::string &format="%Y-%m-%d %H:%M:%S")
Formats a time point as a string using UTC time.
Definition datetime.cpp:45
static std::chrono::system_clock::time_point parse_time_point_utc(const std::string &str, const std::string &format="%Y-%m-%d %H:%M:%S")
Parses a string into a time point, treating the input as UTC.
Definition datetime.cpp:75