ORE Studio 0.0.4
Loading...
Searching...
No Matches
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_HELPERS_HPP
21#define ORES_DATABASE_REPOSITORY_HELPERS_HPP
22
23#include <string>
24#include <format>
25#include <boost/exception/diagnostic_information.hpp>
26#include <sqlgen/postgres.hpp>
27#include "ores.logging/make_logger.hpp"
28#include "ores.database/repository/db_types.hpp"
29#include "ores.database/repository/repository_exception.hpp"
30
32
40inline constexpr const char* MAX_TIMESTAMP = "9999-12-31 23:59:59";
41
57template<typename T>
58void ensure_success(const T& result, logging::logger_t& lg) {
59 using namespace ores::logging;
60
61 if (!result) {
62 BOOST_LOG_SEV(lg, error) << result.error().what();
63 BOOST_THROW_EXCEPTION(
64 repository_exception(std::format("Repository error: {}",
65 result.error().what())));
66 }
67}
68
83inline auto make_timestamp(const std::string& s, logging::logger_t& lg) {
84 using namespace ores::logging;
85
86 const auto r = db_timestamp::from_string(s);
87 if (!r) {
88 BOOST_LOG_SEV(lg, error) << "Error converting timestamp: '" << s
89 << "'. Error: " << r.error().what();
90 BOOST_THROW_EXCEPTION(
91 repository_exception(
92 std::format("Timestamp conversion error: {}", s)));
93 }
94 return r;
95}
96
111template<typename EntityType>
112std::string generate_create_table_sql(logging::logger_t& lg) {
113 using namespace ores::logging;
114 using namespace sqlgen;
115
116 const auto query = create_table<EntityType> | if_not_exists;
117 const auto sql = postgres::to_sql(query);
118
119 BOOST_LOG_SEV(lg, debug) << sql;
120
121 return sql;
122}
123
124}
125
126#endif
Repository infrastructure and bitemporal operations.
Definition bitemporal_operations.hpp:31
constexpr const char * MAX_TIMESTAMP
Maximum timestamp used for bitemporal records (represents "infinity").
Definition helpers.hpp:40
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
A fatal error has occurred whilst reading or writing to the repository.
Definition repository_exception.hpp:33