Compare commits

..

2 Commits

Author SHA1 Message Date
Sascha Kuehl 1dbe4e6096 adjust types in requested columns 2023-11-22 23:00:17 +01:00
Sascha Kuehl 376beded43 added type setter to column 2023-11-22 22:59:40 +01:00
4 changed files with 14 additions and 3 deletions

View File

@ -50,6 +50,7 @@ public:
[[nodiscard]] const std::string& ref_table() const; [[nodiscard]] const std::string& ref_table() const;
[[nodiscard]] const std::string& ref_column() const; [[nodiscard]] const std::string& ref_column() const;
void type(data_type_t type);
void alias(const std::string &as); void alias(const std::string &as);
template< typename Type > template< typename Type >

View File

@ -68,6 +68,11 @@ const std::string &column::ref_column() const
return ref_column_; return ref_column_;
} }
void column::type(data_type_t type)
{
type_ = type;
}
void column::alias(const std::string &as) void column::alias(const std::string &as)
{ {
alias_ = as; alias_ = as;

View File

@ -50,9 +50,14 @@ query_result<record> session::fetch(const query &q) const
if (it == prototypes_.end()) { if (it == prototypes_.end()) {
it = prototypes_.emplace(q.table_name, c->describe(q.table_name)).first; it = prototypes_.emplace(q.table_name, c->describe(q.table_name)).first;
} }
// adjust columns from given query
for (auto &col : q.prototype) {
if (const auto rit = it->second.find(col.name()); col.type() == data_type_t::type_unknown && rit != it->second.end()) {
const_cast<column&>(col).type(rit->type());
}
}
auto res = c->call_fetch(q.sql); auto res = c->call_fetch(q.sql);
return query_result<record>{std::move(res), q.prototype}; return query_result<record>{std::move(res), q.prototype};
// return query_result<record>{std::move(res), [it]() { return new record(it->second); }};
} }
//query_result<record> session::fetch(const std::string &sql) const //query_result<record> session::fetch(const std::string &sql) const

View File

@ -72,13 +72,13 @@ TEST_CASE("Execute select statement with where clause", "[session]") {
for (const auto& i : result_record) { for (const auto& i : result_record) {
REQUIRE(i.size() == 3); REQUIRE(i.size() == 3);
REQUIRE(i.at(0).name() == "id"); REQUIRE(i.at(0).name() == "id");
REQUIRE(i.at(0).type() == data_type_t::type_long_long); REQUIRE(i.at(0).type() == data_type_t::type_unsigned_long);
REQUIRE(i.at(0).as<long long>() == george.id); REQUIRE(i.at(0).as<long long>() == george.id);
REQUIRE(i.at(1).name() == "name"); REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).type() == data_type_t::type_varchar); REQUIRE(i.at(1).type() == data_type_t::type_varchar);
REQUIRE(i.at(1).as<std::string>() == george.name); REQUIRE(i.at(1).as<std::string>() == george.name);
REQUIRE(i.at(2).name() == "age"); REQUIRE(i.at(2).name() == "age");
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_long_long); REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_unsigned_int);
REQUIRE(i.at(2).as<long long>() == george.age); REQUIRE(i.at(2).as<long long>() == george.age);
} }