added index attribute to field class

This commit is contained in:
Sascha Kuehl 2024-03-10 17:22:38 +01:00
parent 6bbc870362
commit 9a02abacea
3 changed files with 22 additions and 4 deletions

View File

@ -64,8 +64,9 @@ class field
public: public:
explicit field(std::string name); explicit field(std::string name);
template<typename Type> template<typename Type>
field(std::string name, Type value) field(std::string name, Type value, int index = -1)
: name_(std::move(name)) : name_(std::move(name))
, index_(index)
, value_(value) , value_(value)
, type_(field_traits<Type>::type()) {} , type_(field_traits<Type>::type()) {}
field(const field &x) = default; field(const field &x) = default;
@ -81,6 +82,7 @@ public:
} }
[[nodiscard]] const std::string& name() const; [[nodiscard]] const std::string& name() const;
[[nodiscard]] int index() const;
template<class Type> template<class Type>
std::optional<Type> as() const std::optional<Type> as() const
@ -103,6 +105,7 @@ public:
private: private:
std::string name_; std::string name_;
int index_{-1};
any_type value_; any_type value_;
field_type type_; field_type type_;
}; };

View File

@ -3,21 +3,29 @@
namespace matador::sql { namespace matador::sql {
field::field(std::string name) field::field(std::string name)
: name_(std::move(name)), value_(nullptr), type_(field_traits<nullptr_t>::type()) : name_(std::move(name))
, value_(nullptr)
, type_(field_traits<nullptr_t>::type())
{} {}
field::field(field &&x) noexcept field::field(field &&x) noexcept
: name_(std::move(x.name_)), value_(std::move(x.value_)), type_(x.type_) : name_(std::move(x.name_))
, index_(x.index_)
, value_(std::move(x.value_))
, type_(x.type_)
{ {
x.value_ = nullptr; x.value_ = nullptr;
x.index_ = -1;
x.type_ = field_type::Null; x.type_ = field_type::Null;
} }
field &field::operator=(field &&x) noexcept field &field::operator=(field &&x) noexcept
{ {
name_ = std::move(x.name_); name_ = std::move(x.name_);
index_ = x.index_;
value_ = std::move(x.value_); value_ = std::move(x.value_);
type_ = x.type_; type_ = x.type_;
x.index_ = -1;
x.value_ = nullptr; x.value_ = nullptr;
x.type_ = field_type::Null; x.type_ = field_type::Null;
@ -29,6 +37,11 @@ const std::string &field::name() const
return name_; return name_;
} }
int field::index() const
{
return index_;
}
std::ostream &operator<<(std::ostream &out, const field &col) std::ostream &operator<<(std::ostream &out, const field &col)
{ {
out << col.str(); out << col.str();

View File

@ -8,6 +8,7 @@ TEST_CASE("Field test", "[field]") {
sql::field f("name"); sql::field f("name");
REQUIRE(f.name() == "name"); REQUIRE(f.name() == "name");
REQUIRE(f.index() == -1);
REQUIRE(f.is_null()); REQUIRE(f.is_null());
REQUIRE(!f.is_integer()); REQUIRE(!f.is_integer());
REQUIRE(!f.is_floating_point()); REQUIRE(!f.is_floating_point());
@ -36,7 +37,8 @@ TEST_CASE("Field test", "[field]") {
REQUIRE(bool_val.has_value()); REQUIRE(bool_val.has_value());
REQUIRE(bool_val.value()); REQUIRE(bool_val.value());
f = utils::blob{ 7,8,6,5,4,3 }; f = sql::field("name", utils::blob{ 7,8,6,5,4,3 }, 1);
REQUIRE(f.index() == 1);
REQUIRE(!f.is_null()); REQUIRE(!f.is_null());
REQUIRE(!f.is_integer()); REQUIRE(!f.is_integer());
REQUIRE(!f.is_floating_point()); REQUIRE(!f.is_floating_point());