From b15b8da31f81978c26433fe3dfd66702b57bc8cf Mon Sep 17 00:00:00 2001 From: Sascha Kuehl Date: Tue, 19 Mar 2024 19:49:17 +0100 Subject: [PATCH] fixed field class and all tests --- backends/tests/QueryRecordTest.cpp | 2 +- backends/tests/QueryTest.cpp | 6 +-- include/matador/sql/column_definition.hpp | 4 +- include/matador/sql/field.hpp | 60 +++-------------------- src/sql/column_definition.cpp | 2 +- src/sql/field.cpp | 49 ++++++++---------- src/sql/query_result.cpp | 6 +-- test/ColumnTest.cpp | 10 ++-- test/FieldTest.cpp | 3 +- 9 files changed, 43 insertions(+), 99 deletions(-) diff --git a/backends/tests/QueryRecordTest.cpp b/backends/tests/QueryRecordTest.cpp index a2b09d9..10c6f37 100644 --- a/backends/tests/QueryRecordTest.cpp +++ b/backends/tests/QueryRecordTest.cpp @@ -209,7 +209,7 @@ TEST_CASE_METHOD(QueryRecordFixture, "Execute update record statement", "[sessio REQUIRE(i.at(0).is_integer()); REQUIRE(i.at(0).as() == 7); REQUIRE(i.at(1).name() == "name"); - REQUIRE(i.at(1).is_string()); + REQUIRE(i.at(1).is_varchar()); REQUIRE(i.at(1).as() == "jane"); REQUIRE(i.at(2).name() == "age"); REQUIRE(i.at(2).is_integer()); diff --git a/backends/tests/QueryTest.cpp b/backends/tests/QueryTest.cpp index 7247533..868ac9a 100644 --- a/backends/tests/QueryTest.cpp +++ b/backends/tests/QueryTest.cpp @@ -96,7 +96,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[s REQUIRE(i.at(0).is_integer()); REQUIRE(i.at(0).as() == george.id); REQUIRE(i.at(1).name() == "name"); - REQUIRE(i.at(1).is_string()); + REQUIRE(i.at(1).is_varchar()); REQUIRE(i.at(1).as() == george.name); REQUIRE(i.at(2).name() == "age"); REQUIRE(i.at(2).is_integer()); @@ -147,10 +147,10 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[session]") REQUIRE(i.at(0).is_integer()); REQUIRE(i.at(0).as() == 7); REQUIRE(i.at(1).name() == "name"); - REQUIRE(i.at(1).is_string()); + REQUIRE(i.at(1).is_varchar()); REQUIRE(i.at(1).as() == "george"); REQUIRE(i.at(2).name() == "color"); - REQUIRE(i.at(2).is_string()); + REQUIRE(i.at(2).is_varchar()); REQUIRE(i.at(2).as() == "green"); } diff --git a/include/matador/sql/column_definition.hpp b/include/matador/sql/column_definition.hpp index e4a6ae4..af72bc8 100644 --- a/include/matador/sql/column_definition.hpp +++ b/include/matador/sql/column_definition.hpp @@ -46,7 +46,7 @@ public: column_definition(std::string name, data_type_t type, size_t index, std::string ref_table, std::string ref_column, utils::field_attributes attr, null_option null_opt); [[nodiscard]] const std::string& name() const; - [[nodiscard]] size_t index() const; + [[nodiscard]] int index() const; [[nodiscard]] const utils::field_attributes& attributes() const; [[nodiscard]] bool is_nullable() const; [[nodiscard]] data_type_t type() const; @@ -111,7 +111,7 @@ private: static const data_type_index data_type_index_; std::string name_; - size_t index_{}; + int index_{-1}; utils::field_attributes attributes_; null_option null_option_{null_option::NOT_NULL}; data_type_t type_{data_type_t::type_unknown}; diff --git a/include/matador/sql/field.hpp b/include/matador/sql/field.hpp index 2436905..0dcefdc 100644 --- a/include/matador/sql/field.hpp +++ b/include/matador/sql/field.hpp @@ -12,54 +12,6 @@ namespace matador::sql { -enum class field_type { - Integer, - FloatingPoint, - String, - Boolean, - Blob, - Null -}; - -template < typename Type, typename Enable = void > -struct field_traits; - -template -struct field_traits::value && !std::is_same::value>::type> -{ - static field_type type() { return field_type::Integer; } -}; - -template -struct field_traits::value>::type> -{ - static field_type type() { return field_type::FloatingPoint; } -}; - -template -struct field_traits::value>::type> -{ - static field_type type() { return field_type::String; } -}; - -template -struct field_traits::value>::type> -{ - static field_type type() { return field_type::Boolean; } -}; - -template -struct field_traits::value>::type> -{ - static field_type type() { return field_type::Null; } -}; - -template -struct field_traits::value>::type> -{ - static field_type type() { return field_type::Blob; } -}; - class field { public: @@ -70,7 +22,7 @@ public: , size_(size) , index_(index) , value_(value) - , type_(field_traits::type()) {} + , type_(data_type_traits::builtin_type(size)) {} field(std::string name, data_type_t data_type, size_t size = 0, int index = -1); field(const field &x) = default; field& operator=(const field &x) = default; @@ -80,7 +32,7 @@ public: template field& operator=(Type value) { value_ = std::move(value); - type_ = field_traits::type(); + type_ = data_type_traits::builtin_type(-1); return *this; } @@ -102,8 +54,10 @@ public: [[nodiscard]] bool is_floating_point() const; [[nodiscard]] bool is_bool() const; [[nodiscard]] bool is_string() const; + [[nodiscard]] bool is_varchar() const; [[nodiscard]] bool is_blob() const; [[nodiscard]] bool is_null() const; + [[nodiscard]] bool is_unknown() const; friend std::ostream& operator<<(std::ostream &out, const field &col); @@ -111,19 +65,17 @@ private: template void process(Operator &op) { - op.on_attribute(name_.c_str(), value_, field_type_to_data_type(type_), size_); + op.on_attribute(name_.c_str(), value_, type_, size_); } private: friend class record; - static data_type_t field_type_to_data_type(field_type type); - std::string name_; int index_{-1}; any_type value_; size_t size_{}; - field_type type_; + data_type_t type_; }; } diff --git a/src/sql/column_definition.cpp b/src/sql/column_definition.cpp index fdc6862..5e83272 100644 --- a/src/sql/column_definition.cpp +++ b/src/sql/column_definition.cpp @@ -33,7 +33,7 @@ const std::string &column_definition::name() const return name_; } -size_t column_definition::index() const +int column_definition::index() const { return index_; } diff --git a/src/sql/field.cpp b/src/sql/field.cpp index 8b163ee..229b9c1 100644 --- a/src/sql/field.cpp +++ b/src/sql/field.cpp @@ -7,13 +7,14 @@ namespace matador::sql { field::field(std::string name) : name_(std::move(name)) , value_(nullptr) - , type_(field_traits::type()) + , type_(data_type_t::type_unknown) {} field::field(std::string name, data_type_t data_type, size_t size, int index) -{ - -} + : name_(std::move(name)) + , size_(size) + , index_(index) + , type_(data_type) {} field::field(field &&x) noexcept : name_(std::move(x.name_)) @@ -23,7 +24,7 @@ field::field(field &&x) noexcept { x.value_ = nullptr; x.index_ = -1; - x.type_ = field_type::Null; + x.type_ = data_type_t::type_unknown; } field &field::operator=(field &&x) noexcept @@ -34,7 +35,7 @@ field &field::operator=(field &&x) noexcept type_ = x.type_; x.index_ = -1; x.value_ = nullptr; - x.type_ = field_type::Null; + x.type_ = data_type_t::type_unknown; return *this; } @@ -67,52 +68,42 @@ std::string field::str() const bool field::is_integer() const { - return type_ == field_type::Integer; + return type_ >= data_type_t::type_char && type_ <= data_type_t::type_unsigned_long_long; } bool field::is_floating_point() const { - return type_ == field_type::FloatingPoint; + return type_ == data_type_t::type_float || type_ == data_type_t::type_double; } bool field::is_bool() const { - return type_ == field_type::Boolean; + return type_ == data_type_t::type_bool; } bool field::is_string() const { - return type_ == field_type::String; + return type_ == data_type_t::type_text; +} + +bool field::is_varchar() const +{ + return type_ == data_type_t::type_varchar; } bool field::is_blob() const { - return type_ == field_type::Blob; + return type_ == data_type_t::type_blob; } bool field::is_null() const { - return type_ == field_type::Null; + return type_ == data_type_t::type_null; } -data_type_t field::field_type_to_data_type(field_type type) +bool field::is_unknown() const { - switch (type) { - case field_type::Integer: - return data_type_t::type_long_long; - case field_type::FloatingPoint: - return data_type_t::type_double; - case field_type::String: - return data_type_t::type_varchar; - case field_type::Boolean: - return data_type_t::type_bool; - case field_type::Blob: - return data_type_t::type_blob; - case field_type::Null: - return data_type_t::type_null; - default: - return data_type_t::type_unknown; - } + return type_ == data_type_t::type_unknown; } } \ No newline at end of file diff --git a/src/sql/query_result.cpp b/src/sql/query_result.cpp index 6ab68b3..46bbf82 100644 --- a/src/sql/query_result.cpp +++ b/src/sql/query_result.cpp @@ -8,9 +8,9 @@ template<> record *create_prototype(const std::vector &prototype) { auto result = std::make_unique(); -// for (const auto &col: prototype) { -// result->append({}) -// } + for (const auto &col: prototype) { + result->append({col.name(), col.type(), col.attributes().size(), col.index()}); + } return result.release(); } diff --git a/test/ColumnTest.cpp b/test/ColumnTest.cpp index ab607b7..fb14b48 100644 --- a/test/ColumnTest.cpp +++ b/test/ColumnTest.cpp @@ -8,7 +8,7 @@ TEST_CASE("Create empty column", "[column]") { column_definition c("name"); REQUIRE(c.name() == "name"); - REQUIRE(c.index() == 0); + REQUIRE(c.index() == -1); REQUIRE(c.type() == data_type_t::type_unknown); REQUIRE(c.ref_table().empty()); REQUIRE(c.ref_column().empty()); @@ -28,7 +28,7 @@ TEST_CASE("Copy and move column", "[column]") { column_definition c("name"); c.set(std::string{"george"}, 255); REQUIRE(c.name() == "name"); - REQUIRE(c.index() == 0); + REQUIRE(c.index() == -1); REQUIRE(c.ref_table().empty()); REQUIRE(c.ref_column().empty()); REQUIRE(c.type() == data_type_t::type_varchar); @@ -37,7 +37,7 @@ TEST_CASE("Copy and move column", "[column]") { auto c2 = c; REQUIRE(c2.name() == "name"); - REQUIRE(c2.index() == 0); + REQUIRE(c2.index() == -1); REQUIRE(c2.ref_table().empty()); REQUIRE(c2.ref_column().empty()); REQUIRE(c2.type() == data_type_t::type_varchar); @@ -46,7 +46,7 @@ TEST_CASE("Copy and move column", "[column]") { auto c3 = std::move(c2); REQUIRE(c3.name() == "name"); - REQUIRE(c3.index() == 0); + REQUIRE(c3.index() == -1); REQUIRE(c3.ref_table().empty()); REQUIRE(c3.ref_column().empty()); REQUIRE(c3.type() == data_type_t::type_varchar); @@ -54,7 +54,7 @@ TEST_CASE("Copy and move column", "[column]") { REQUIRE(c3.attributes().size() == 255); REQUIRE(c2.name().empty()); - REQUIRE(c2.index() == 0); + REQUIRE(c2.index() == -1); REQUIRE(c2.ref_table().empty()); REQUIRE(c2.ref_column().empty()); REQUIRE(c2.type() == data_type_t::type_varchar); diff --git a/test/FieldTest.cpp b/test/FieldTest.cpp index f434723..b645d53 100644 --- a/test/FieldTest.cpp +++ b/test/FieldTest.cpp @@ -9,7 +9,8 @@ TEST_CASE("Field test", "[field]") { REQUIRE(f.name() == "name"); REQUIRE(f.index() == -1); - REQUIRE(f.is_null()); + REQUIRE(f.is_unknown()); + REQUIRE(!f.is_null()); REQUIRE(!f.is_integer()); REQUIRE(!f.is_floating_point()); REQUIRE(!f.is_blob());