ORE Studio 0.0.4
Loading...
Searching...
No Matches
/home/runner/work/OreStudio/OreStudio/projects/ores.database/include/ores.database/repository/helpers.hpp

Ensures a repository operation result is successful, throwing an exception if not.

Ensures a repository operation result is successful, throwing an exception if not.This helper checks if a repository operation returned a successful result. If the result indicates an error, it logs the error and throws a repository_exception with details about the failure.

Template Parameters
TThe type of the result (must support operator bool and have an error() method)
Parameters
resultThe result to check
Exceptions
repository_exceptionif the result indicates failure

auto result = session.and_then(query); ensure_success(result); // Throws if query failed

/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef ORES_DATABASE_REPOSITORY_HELPERS_HPP
#define ORES_DATABASE_REPOSITORY_HELPERS_HPP
#include <string>
#include <format>
#include <boost/exception/diagnostic_information.hpp>
#include <sqlgen/postgres.hpp>
#include "ores.logging/make_logger.hpp"
#include "ores.database/repository/db_types.hpp"
#include "ores.database/repository/repository_exception.hpp"
inline constexpr const char* MAX_TIMESTAMP = "9999-12-31 23:59:59";
template<typename T>
void ensure_success(const T& result, logging::logger_t& lg) {
using namespace ores::logging;
if (!result) {
BOOST_LOG_SEV(lg, error) << result.error().what();
BOOST_THROW_EXCEPTION(
repository_exception(std::format("Repository error: {}",
result.error().what())));
}
}
inline auto make_timestamp(const std::string& s, logging::logger_t& lg) {
using namespace ores::logging;
const auto r = db_timestamp::from_string(s);
if (!r) {
BOOST_LOG_SEV(lg, error) << "Error converting timestamp: '" << s
<< "'. Error: " << r.error().what();
BOOST_THROW_EXCEPTION(
repository_exception(
std::format("Timestamp conversion error: {}", s)));
}
return r;
}
template<typename EntityType>
std::string generate_create_table_sql(logging::logger_t& lg) {
using namespace ores::logging;
using namespace sqlgen;
const auto query = create_table<EntityType> | if_not_exists;
const auto sql = postgres::to_sql(query);
BOOST_LOG_SEV(lg, debug) << sql;
return sql;
}
}
#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