added data_type_traits for enums

This commit is contained in:
Sascha Kühl 2024-08-22 16:08:05 +02:00
parent 484c2d8b05
commit 19de5714f4
1 changed files with 66 additions and 0 deletions

View File

@ -5,6 +5,7 @@
#include "matador/sql/entity.hpp"
#include "matador/utils/access.hpp"
#include "matador/utils/enum_mapper.hpp"
#include "matador/sql/query_helper.hpp"
@ -90,6 +91,71 @@ struct job {
}
};
static const matador::utils::enum_mapper<job::job_state> job_state_enum({
{job::job_state::Pending, "Pending"},
{job::job_state::Running, "Running"},
{job::job_state::Succeeded, "Succeeded"},
{job::job_state::Failed, "Failed"},
{job::job_state::Canceled, "Canceled"}
});
static const matador::utils::enum_mapper<job::job_mode> job_mode_enum({
{job::job_mode::Foreground, "Foreground"},
{job::job_mode::Background, "Background"}
});
template<>
struct matador::sql::data_type_traits<job::job_state, void>
{
inline static data_type_t builtin_type(std::size_t size)
{ return data_type_traits<std::string>::builtin_type(size); }
static void read_value(query_result_reader &reader, const char *id, size_t index, job::job_state &value)
{
std::string enum_string;
reader.read_value(id, index, enum_string, 64);
if (const auto enum_opt = job_state_enum.to_enum(enum_string)) {
value = enum_opt.value();
}
}
static any_type create_value(const job::job_state &value)
{
return job_state_enum.to_string(value);
}
static void bind_value(parameter_binder &binder, size_t index, job::job_state &value)
{
binder.bind(index, job_state_enum.to_string(value));
}
};
template<>
struct matador::sql::data_type_traits<job::job_mode, void>
{
inline static data_type_t builtin_type(std::size_t size)
{ return data_type_traits<std::string>::builtin_type(size); }
static void read_value(query_result_reader &reader, const char *id, size_t index, job::job_mode &value)
{
std::string enum_string;
reader.read_value(id, index, enum_string, 64);
if (const auto enum_opt = job_mode_enum.to_enum(enum_string)) {
value = enum_opt.value();
}
}
static any_type create_value(const job::job_mode &value)
{
return job_mode_enum.to_string(value);
}
static void bind_value(parameter_binder &binder, size_t index, job::job_mode &value)
{
binder.bind(index, job_mode_enum.to_string(value));
}
};
QUERY_HELPER( authors, id, first_name, last_name, date_of_birth, year_of_birth, distinguished )
QUERY_HELPER( books, id, author_id, title, published_in )