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#include "ores.platform/time/time_utils.hpp"
29#include "ores.database/repository/db_types.hpp"
30
32
56template<typename Source, typename Dest, typename MapFunc>
57std::vector<Dest> map_vector(
58 const std::vector<Source>& source,
59 MapFunc&& map_func,
60 logging::logger_t& lg,
61 const std::string& log_prefix) {
62
63 using namespace ores::logging;
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
91inline std::chrono::system_clock::time_point
92timestamp_to_timepoint(std::string_view timestamp_str) {
94 std::string{timestamp_str});
95}
96
110inline std::chrono::system_clock::time_point
111timestamp_to_timepoint(const db_timestamp& ts) {
113}
114
128inline db_timestamp
129timepoint_to_timestamp(const std::chrono::system_clock::time_point& tp,
130 logging::logger_t& lg) {
131 using namespace ores::logging;
132
134 // Strip the Z suffix — db_timestamp::from_string expects no timezone designator
135 const auto bare = s.substr(0, s.size() - 1);
136 const auto r = db_timestamp::from_string(bare);
137 if (!r) {
138 BOOST_LOG_SEV(lg, error) << "Error converting timepoint to timestamp";
139 return {};
140 }
141 return r.value();
142}
143
144}
145
146#endif
Repository infrastructure and bitemporal operations.
Definition bitemporal_operations.hpp:31
ores::database::repository::db_timestamp db_timestamp
Canonical database timestamp type.
Definition database_info_entity.hpp:31
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
static std::chrono::system_clock::time_point from_iso8601_utc(const std::string &str)
Parses an ISO 8601 UTC string to a time point.
Definition datetime.cpp:45
static std::string to_iso8601_utc(const std::chrono::system_clock::time_point &tp)
Serialises a time point to ISO 8601 UTC string with 'Z' suffix.
Definition datetime.cpp:30
static std::chrono::system_clock::time_point to_time_point_utc(std::tm tm)
Converts a UTC tm struct directly to a system_clock::time_point.
Definition time_utils.cpp:64