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
-
| T | The type of the result (must support operator bool and have an error() method) |
- Parameters
-
| result | The result to check |
- Exceptions
-
| repository_exception | if the result indicates failure |
auto result = session.and_then(query); ensure_success(result); // Throws if query failed
#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) {
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) {
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 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