implemented specialization of data types via type traits and handling of embedded non-trivial types

This commit is contained in:
2023-12-10 22:40:01 +01:00
parent da423ea8bb
commit 8ad13076c8
17 changed files with 346 additions and 90 deletions
+5 -1
View File
@@ -27,7 +27,11 @@ add_executable(tests QueryBuilderTest.cpp
ColumnTest.cpp
SessionRecordTest.cpp
StatementCacheTest.cpp
StatementTest.cpp)
StatementTest.cpp
models/coordinate.hpp
models/location.hpp
TypeTraitsTest.cpp
Databases.hpp)
target_link_libraries(tests PRIVATE
Catch2::Catch2WithMain
matador
+15
View File
@@ -0,0 +1,15 @@
#ifndef QUERY_DATABASES_HPP
#define QUERY_DATABASES_HPP
struct Postgres
{
// constexpr static const char *dns{"postgres://test:test123@127.0.0.1:15432/test"};
constexpr static const char *dns{"postgres://test:test123@127.0.0.1:5432/matador_test"};
};
struct Sqlite
{
constexpr static const char *dns{"sqlite://sqlite.db"};
};
#endif //QUERY_DATABASES_HPP
+2 -11
View File
@@ -7,22 +7,13 @@
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/session.hpp"
#include "Databases.hpp"
#include "models/airplane.hpp"
using namespace matador::sql;
using namespace matador::test;
struct Postgres
{
// constexpr static const char *dns{"postgres://test:test123@127.0.0.1:15432/test"};
constexpr static const char *dns{"postgres://test:test123@127.0.0.1:5432/matador_test"};
};
struct Sqlite
{
constexpr static const char *dns{"sqlite://sqlite.db"};
};
template<class Type>
class StatementTestFixture
{
+59
View File
@@ -0,0 +1,59 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/session.hpp"
#include "Databases.hpp"
#include "models/location.hpp"
using namespace matador::sql;
using namespace matador::test;
using namespace matador::sql;
using namespace matador::test;
template<class Type>
class TypeTraitsTestFixture
{
public:
TypeTraitsTestFixture()
: pool_(Type::dns, 4), session_(pool_)
{
auto res = session_.create()
.table<location>("location")
.execute();
REQUIRE(res.first == 0);
REQUIRE(res.second == R"(CREATE TABLE "location" ("id" BIGINT, "name" VARCHAR(255), "coordinate_x" INTEGER, "coordinate_y" INTEGER, "coordinate_z" INTEGER, "color" INTEGER, CONSTRAINT PK_location PRIMARY KEY (id)))");
}
~TypeTraitsTestFixture()
{
session_.drop().table("location").execute();
}
matador::sql::session &session()
{ return session_; }
private:
matador::sql::connection_pool<matador::sql::connection> pool_;
matador::sql::session session_;
};
TEMPLATE_TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with type traits", "[typetraits]", Sqlite, Postgres)
{
auto &s = TypeTraitsTestFixture<TestType>::session();
location loc{1, "center", {1, 2, 3}, Color::Black};
auto res = s.insert().template into<location>("location").values(loc).execute();
REQUIRE(res.first == 1);
auto result = s.template select<location>().from("location").template fetch_all<location>();
for (const auto &l : result) {
REQUIRE(l.name == "center");
}
}
+26
View File
@@ -0,0 +1,26 @@
#ifndef QUERY_COORDINATE_HPP
#define QUERY_COORDINATE_HPP
#include "matador/utils/access.hpp"
namespace matador::test {
struct coordinate
{
int x;
int y;
int z;
};
}
namespace matador::utils::access {
template<class Operator>
void attribute(Operator &op, const char *id, test::coordinate &value, const field_attributes &attr = null_attributes) {
attribute(op, (std::string(id) + "_x").c_str(), value.x, attr);
attribute(op, (std::string(id) + "_y").c_str(), value.y, attr);
attribute(op, (std::string(id) + "_z").c_str(), value.z, attr);
}
}
#endif //QUERY_COORDINATE_HPP
+34
View File
@@ -0,0 +1,34 @@
#ifndef QUERY_LOCATION_HPP
#define QUERY_LOCATION_HPP
#include "matador/utils/access.hpp"
#include "coordinate.hpp"
namespace matador::test {
enum class Color : uint8_t {
Green, Red, Blue, Yellow, Black, White, Brown
};
struct location
{
unsigned long id;
std::string name;
coordinate coord;
Color color;
template < class Operator >
void process(Operator &op)
{
namespace field = matador::utils::access;
field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255);
field::attribute(op, "coordinate", coord);
field::attribute(op, "color", color);
}
};
}
#endif //QUERY_LOCATION_HPP