implemented specialization of data types via type traits and handling of embedded non-trivial types
This commit is contained in:
+5
-1
@@ -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
|
||||
|
||||
@@ -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
@@ -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
|
||||
{
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user