Compare commits

...

3 Commits

Author SHA1 Message Date
Sascha Kühl 19de5714f4 added data_type_traits for enums 2024-08-22 16:08:05 +02:00
Sascha Kühl 484c2d8b05 removed quotes for enum value in condition 2024-08-22 16:07:45 +02:00
Sascha Kühl 43d2f81b95 added all value constructor to class table 2024-08-22 16:07:10 +02:00
3 changed files with 77 additions and 2 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 )

View File

@ -102,7 +102,7 @@ public:
auto at = data_type_traits<T>::create_value(value);
any_type_to_string_visitor value_to_string(d, query);
std::visit(value_to_string, at);
return "'" + value_to_string.result + "' " + operand + " " + d.prepare_identifier(field_);
return value_to_string.result + " " + operand + " " + d.prepare_identifier(field_);
}
};

View File

@ -14,13 +14,22 @@ struct table
table(const char *name, std::string as = "") // NOLINT(*-explicit-constructor)
: name(name), alias(std::move(as)) {}
table(std::string name, std::string as = "") // NOLINT(*-explicit-constructor)
: name(std::move(name)), alias(std::move(as)) {}
: name(std::move(name))
, alias(std::move(as)) {}
table(std::string name, std::string as, const std::vector<column> &columns)
: name(std::move(name))
, alias(std::move(as))
, columns(columns) {}
table& as(const std::string &a) {
alias = a;
return *this;
}
[[nodiscard]] table as(const std::string &a) const {
return { name, a, columns };
}
std::string name;
std::string alias;