diff --git a/include/matador/sql/field.hpp b/include/matador/sql/field.hpp index 9c0f477..c6f282c 100644 --- a/include/matador/sql/field.hpp +++ b/include/matador/sql/field.hpp @@ -64,8 +64,9 @@ class field public: explicit field(std::string name); template - field(std::string name, Type value) + field(std::string name, Type value, int index = -1) : name_(std::move(name)) + , index_(index) , value_(value) , type_(field_traits::type()) {} field(const field &x) = default; @@ -81,6 +82,7 @@ public: } [[nodiscard]] const std::string& name() const; + [[nodiscard]] int index() const; template std::optional as() const @@ -103,6 +105,7 @@ public: private: std::string name_; + int index_{-1}; any_type value_; field_type type_; }; diff --git a/src/sql/field.cpp b/src/sql/field.cpp index be005a4..e29d7b7 100644 --- a/src/sql/field.cpp +++ b/src/sql/field.cpp @@ -3,21 +3,29 @@ namespace matador::sql { field::field(std::string name) - : name_(std::move(name)), value_(nullptr), type_(field_traits::type()) + : name_(std::move(name)) + , value_(nullptr) + , type_(field_traits::type()) {} 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.index_ = -1; x.type_ = field_type::Null; } field &field::operator=(field &&x) noexcept { name_ = std::move(x.name_); + index_ = x.index_; value_ = std::move(x.value_); type_ = x.type_; + x.index_ = -1; x.value_ = nullptr; x.type_ = field_type::Null; @@ -29,6 +37,11 @@ const std::string &field::name() const return name_; } +int field::index() const +{ + return index_; +} + std::ostream &operator<<(std::ostream &out, const field &col) { out << col.str(); diff --git a/test/FieldTest.cpp b/test/FieldTest.cpp index 1b44b13..996c173 100644 --- a/test/FieldTest.cpp +++ b/test/FieldTest.cpp @@ -8,6 +8,7 @@ TEST_CASE("Field test", "[field]") { sql::field f("name"); REQUIRE(f.name() == "name"); + REQUIRE(f.index() == -1); REQUIRE(f.is_null()); REQUIRE(!f.is_integer()); REQUIRE(!f.is_floating_point()); @@ -36,7 +37,8 @@ TEST_CASE("Field test", "[field]") { REQUIRE(bool_val.has_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_integer()); REQUIRE(!f.is_floating_point());