fixed field class and all tests

This commit is contained in:
Sascha Kuehl 2024-03-19 19:49:17 +01:00
parent 12919ba372
commit b15b8da31f
9 changed files with 43 additions and 99 deletions

View File

@ -209,7 +209,7 @@ TEST_CASE_METHOD(QueryRecordFixture, "Execute update record statement", "[sessio
REQUIRE(i.at(0).is_integer()); REQUIRE(i.at(0).is_integer());
REQUIRE(i.at(0).as<long long>() == 7); REQUIRE(i.at(0).as<long long>() == 7);
REQUIRE(i.at(1).name() == "name"); REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).is_string()); REQUIRE(i.at(1).is_varchar());
REQUIRE(i.at(1).as<std::string>() == "jane"); REQUIRE(i.at(1).as<std::string>() == "jane");
REQUIRE(i.at(2).name() == "age"); REQUIRE(i.at(2).name() == "age");
REQUIRE(i.at(2).is_integer()); REQUIRE(i.at(2).is_integer());

View File

@ -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).is_integer());
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).is_string()); REQUIRE(i.at(1).is_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).is_integer()); 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).is_integer());
REQUIRE(i.at(0).as<unsigned long>() == 7); REQUIRE(i.at(0).as<unsigned long>() == 7);
REQUIRE(i.at(1).name() == "name"); REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).is_string()); REQUIRE(i.at(1).is_varchar());
REQUIRE(i.at(1).as<std::string>() == "george"); REQUIRE(i.at(1).as<std::string>() == "george");
REQUIRE(i.at(2).name() == "color"); REQUIRE(i.at(2).name() == "color");
REQUIRE(i.at(2).is_string()); REQUIRE(i.at(2).is_varchar());
REQUIRE(i.at(2).as<std::string>() == "green"); REQUIRE(i.at(2).as<std::string>() == "green");
} }

View File

@ -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); 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]] const std::string& name() const;
[[nodiscard]] size_t index() const; [[nodiscard]] int index() const;
[[nodiscard]] const utils::field_attributes& attributes() const; [[nodiscard]] const utils::field_attributes& attributes() const;
[[nodiscard]] bool is_nullable() const; [[nodiscard]] bool is_nullable() const;
[[nodiscard]] data_type_t type() const; [[nodiscard]] data_type_t type() const;
@ -111,7 +111,7 @@ private:
static const data_type_index data_type_index_; static const data_type_index data_type_index_;
std::string name_; std::string name_;
size_t index_{}; int index_{-1};
utils::field_attributes attributes_; utils::field_attributes attributes_;
null_option null_option_{null_option::NOT_NULL}; null_option null_option_{null_option::NOT_NULL};
data_type_t type_{data_type_t::type_unknown}; data_type_t type_{data_type_t::type_unknown};

View File

@ -12,54 +12,6 @@
namespace matador::sql { namespace matador::sql {
enum class field_type {
Integer,
FloatingPoint,
String,
Boolean,
Blob,
Null
};
template < typename Type, typename Enable = void >
struct field_traits;
template<typename Type>
struct field_traits<Type, typename std::enable_if<std::is_integral<Type>::value && !std::is_same<Type, bool>::value>::type>
{
static field_type type() { return field_type::Integer; }
};
template<typename Type>
struct field_traits<Type, typename std::enable_if<std::is_floating_point<Type>::value>::type>
{
static field_type type() { return field_type::FloatingPoint; }
};
template<typename Type>
struct field_traits<Type, typename std::enable_if<std::is_same<Type, std::string>::value>::type>
{
static field_type type() { return field_type::String; }
};
template<typename Type>
struct field_traits<Type, typename std::enable_if<std::is_same<Type, bool>::value>::type>
{
static field_type type() { return field_type::Boolean; }
};
template<typename Type>
struct field_traits<Type, typename std::enable_if<std::is_same<Type, nullptr_t>::value>::type>
{
static field_type type() { return field_type::Null; }
};
template<typename Type>
struct field_traits<Type, typename std::enable_if<std::is_same<Type, utils::blob>::value>::type>
{
static field_type type() { return field_type::Blob; }
};
class field class field
{ {
public: public:
@ -70,7 +22,7 @@ public:
, size_(size) , size_(size)
, index_(index) , index_(index)
, value_(value) , value_(value)
, type_(field_traits<Type>::type()) {} , type_(data_type_traits<Type>::builtin_type(size)) {}
field(std::string name, data_type_t data_type, size_t size = 0, int index = -1); field(std::string name, data_type_t data_type, size_t size = 0, int index = -1);
field(const field &x) = default; field(const field &x) = default;
field& operator=(const field &x) = default; field& operator=(const field &x) = default;
@ -80,7 +32,7 @@ public:
template<typename Type> template<typename Type>
field& operator=(Type value) { field& operator=(Type value) {
value_ = std::move(value); value_ = std::move(value);
type_ = field_traits<Type>::type(); type_ = data_type_traits<Type>::builtin_type(-1);
return *this; return *this;
} }
@ -102,8 +54,10 @@ public:
[[nodiscard]] bool is_floating_point() const; [[nodiscard]] bool is_floating_point() const;
[[nodiscard]] bool is_bool() const; [[nodiscard]] bool is_bool() const;
[[nodiscard]] bool is_string() const; [[nodiscard]] bool is_string() const;
[[nodiscard]] bool is_varchar() const;
[[nodiscard]] bool is_blob() const; [[nodiscard]] bool is_blob() const;
[[nodiscard]] bool is_null() const; [[nodiscard]] bool is_null() const;
[[nodiscard]] bool is_unknown() const;
friend std::ostream& operator<<(std::ostream &out, const field &col); friend std::ostream& operator<<(std::ostream &out, const field &col);
@ -111,19 +65,17 @@ private:
template<class Operator> template<class Operator>
void process(Operator &op) 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: private:
friend class record; friend class record;
static data_type_t field_type_to_data_type(field_type type);
std::string name_; std::string name_;
int index_{-1}; int index_{-1};
any_type value_; any_type value_;
size_t size_{}; size_t size_{};
field_type type_; data_type_t type_;
}; };
} }

View File

@ -33,7 +33,7 @@ const std::string &column_definition::name() const
return name_; return name_;
} }
size_t column_definition::index() const int column_definition::index() const
{ {
return index_; return index_;
} }

View File

@ -7,13 +7,14 @@ namespace matador::sql {
field::field(std::string name) field::field(std::string name)
: name_(std::move(name)) : name_(std::move(name))
, value_(nullptr) , value_(nullptr)
, type_(field_traits<nullptr_t>::type()) , type_(data_type_t::type_unknown)
{} {}
field::field(std::string name, data_type_t data_type, size_t size, int index) 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 field::field(field &&x) noexcept
: name_(std::move(x.name_)) : name_(std::move(x.name_))
@ -23,7 +24,7 @@ field::field(field &&x) noexcept
{ {
x.value_ = nullptr; x.value_ = nullptr;
x.index_ = -1; x.index_ = -1;
x.type_ = field_type::Null; x.type_ = data_type_t::type_unknown;
} }
field &field::operator=(field &&x) noexcept field &field::operator=(field &&x) noexcept
@ -34,7 +35,7 @@ field &field::operator=(field &&x) noexcept
type_ = x.type_; type_ = x.type_;
x.index_ = -1; x.index_ = -1;
x.value_ = nullptr; x.value_ = nullptr;
x.type_ = field_type::Null; x.type_ = data_type_t::type_unknown;
return *this; return *this;
} }
@ -67,52 +68,42 @@ std::string field::str() const
bool field::is_integer() 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 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 bool field::is_bool() const
{ {
return type_ == field_type::Boolean; return type_ == data_type_t::type_bool;
} }
bool field::is_string() const 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 bool field::is_blob() const
{ {
return type_ == field_type::Blob; return type_ == data_type_t::type_blob;
} }
bool field::is_null() const 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) { return type_ == data_type_t::type_unknown;
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;
}
} }
} }

View File

@ -8,9 +8,9 @@ template<>
record *create_prototype<record>(const std::vector<column_definition> &prototype) record *create_prototype<record>(const std::vector<column_definition> &prototype)
{ {
auto result = std::make_unique<record>(); auto result = std::make_unique<record>();
// for (const auto &col: prototype) { for (const auto &col: prototype) {
// result->append({}) result->append({col.name(), col.type(), col.attributes().size(), col.index()});
// } }
return result.release(); return result.release();
} }

View File

@ -8,7 +8,7 @@ TEST_CASE("Create empty column", "[column]") {
column_definition c("name"); column_definition c("name");
REQUIRE(c.name() == "name"); REQUIRE(c.name() == "name");
REQUIRE(c.index() == 0); REQUIRE(c.index() == -1);
REQUIRE(c.type() == data_type_t::type_unknown); REQUIRE(c.type() == data_type_t::type_unknown);
REQUIRE(c.ref_table().empty()); REQUIRE(c.ref_table().empty());
REQUIRE(c.ref_column().empty()); REQUIRE(c.ref_column().empty());
@ -28,7 +28,7 @@ TEST_CASE("Copy and move column", "[column]") {
column_definition c("name"); column_definition c("name");
c.set(std::string{"george"}, 255); c.set(std::string{"george"}, 255);
REQUIRE(c.name() == "name"); REQUIRE(c.name() == "name");
REQUIRE(c.index() == 0); REQUIRE(c.index() == -1);
REQUIRE(c.ref_table().empty()); REQUIRE(c.ref_table().empty());
REQUIRE(c.ref_column().empty()); REQUIRE(c.ref_column().empty());
REQUIRE(c.type() == data_type_t::type_varchar); REQUIRE(c.type() == data_type_t::type_varchar);
@ -37,7 +37,7 @@ TEST_CASE("Copy and move column", "[column]") {
auto c2 = c; auto c2 = c;
REQUIRE(c2.name() == "name"); REQUIRE(c2.name() == "name");
REQUIRE(c2.index() == 0); REQUIRE(c2.index() == -1);
REQUIRE(c2.ref_table().empty()); REQUIRE(c2.ref_table().empty());
REQUIRE(c2.ref_column().empty()); REQUIRE(c2.ref_column().empty());
REQUIRE(c2.type() == data_type_t::type_varchar); REQUIRE(c2.type() == data_type_t::type_varchar);
@ -46,7 +46,7 @@ TEST_CASE("Copy and move column", "[column]") {
auto c3 = std::move(c2); auto c3 = std::move(c2);
REQUIRE(c3.name() == "name"); REQUIRE(c3.name() == "name");
REQUIRE(c3.index() == 0); REQUIRE(c3.index() == -1);
REQUIRE(c3.ref_table().empty()); REQUIRE(c3.ref_table().empty());
REQUIRE(c3.ref_column().empty()); REQUIRE(c3.ref_column().empty());
REQUIRE(c3.type() == data_type_t::type_varchar); 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(c3.attributes().size() == 255);
REQUIRE(c2.name().empty()); REQUIRE(c2.name().empty());
REQUIRE(c2.index() == 0); REQUIRE(c2.index() == -1);
REQUIRE(c2.ref_table().empty()); REQUIRE(c2.ref_table().empty());
REQUIRE(c2.ref_column().empty()); REQUIRE(c2.ref_column().empty());
REQUIRE(c2.type() == data_type_t::type_varchar); REQUIRE(c2.type() == data_type_t::type_varchar);

View File

@ -9,7 +9,8 @@ TEST_CASE("Field test", "[field]") {
REQUIRE(f.name() == "name"); REQUIRE(f.name() == "name");
REQUIRE(f.index() == -1); REQUIRE(f.index() == -1);
REQUIRE(f.is_null()); REQUIRE(f.is_unknown());
REQUIRE(!f.is_null());
REQUIRE(!f.is_integer()); REQUIRE(!f.is_integer());
REQUIRE(!f.is_floating_point()); REQUIRE(!f.is_floating_point());
REQUIRE(!f.is_blob()); REQUIRE(!f.is_blob());