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.utility/log/make_logger.hpp"
28#include "ores.database/repository/repository_exception.hpp"
29
30namespace ores::database::repository {
31
39inline constexpr const char* MAX_TIMESTAMP = "9999-12-31 23:59:59";
40
56template<typename T>
57void ensure_success(const T& result, utility::log::logger_t& lg) {
58 using namespace ores::utility::log;
59
60 if (!result) {
61 BOOST_LOG_SEV(lg, error) << result.error().what();
62 BOOST_THROW_EXCEPTION(
63 repository_exception(std::format("Repository error: {}",
64 result.error().what())));
65 }
66}
67
82inline auto make_timestamp(const std::string& s, utility::log::logger_t& lg) {
83 using namespace ores::utility::log;
84
85 const auto r = sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S">::from_string(s);
86 if (!r) {
87 BOOST_LOG_SEV(lg, error) << "Error converting timestamp: '" << s
88 << "'. Error: " << r.error().what();
89 BOOST_THROW_EXCEPTION(
90 repository_exception(
91 std::format("Timestamp conversion error: {}", s)));
92 }
93 return r;
94}
95
110template<typename EntityType>
111std::string generate_create_table_sql(utility::log::logger_t& lg) {
112 using namespace ores::utility::log;
113 using namespace sqlgen;
114
115 const auto query = create_table<EntityType> | if_not_exists;
116 const auto sql = postgres::to_sql(query);
117
118 BOOST_LOG_SEV(lg, debug) << sql;
119
120 return sql;
121}
122
123}
124
125#endif
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30