added data binding traits

This commit is contained in:
2023-12-11 17:20:42 +01:00
parent 8ad13076c8
commit 1c92e07173
17 changed files with 378 additions and 146 deletions
+6 -15
View File
@@ -7,25 +7,16 @@
using namespace matador;
class TestConnection
{
public:
explicit TestConnection(sql::connection_info info)
: info_(std::move(info)) {}
void open() {}
private:
sql::connection_info info_;
};
TEST_CASE("Acquire prepared statement", "[statement cache]") {
sql::statement_cache cache;
sql::connection_pool<TestConnection> pool("sqlite://sqlite.db", 4);
sql::connection_pool<sql::connection> pool("sqlite://sqlite.db", 4);
// sql::session s(pool);
// auto conn = pool.acquire();
sql::session s(pool);
auto conn = pool.acquire();
std::string sql = R"(SELECT * FROM person WHERE name = 'george')";
// auto stmt = cache.acquire(sql, conn);
// auto &stmt = cache.acquire(sql, *conn);
}
+71 -10
View File
@@ -4,6 +4,8 @@
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/session.hpp"
#include "matador/utils/enum_mapper.hpp"
#include "Databases.hpp"
#include "models/location.hpp"
@@ -19,13 +21,13 @@ class TypeTraitsTestFixture
{
public:
TypeTraitsTestFixture()
: pool_(Type::dns, 4), session_(pool_)
: pool_(Type::dns, 4), session_(pool_)
{
auto res = session_.create()
.table<location>("location")
.execute();
.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)))");
REQUIRE(res.second == R"(CREATE TABLE "location" ("id" BIGINT, "name" VARCHAR(255), "coordinate_x" INTEGER, "coordinate_y" INTEGER, "coordinate_z" INTEGER, "color" TEXT, CONSTRAINT PK_location PRIMARY KEY (id)))");
}
~TypeTraitsTestFixture()
@@ -41,19 +43,78 @@ private:
matador::sql::session session_;
};
static const matador::utils::enum_mapper<Color> color_enum({
{Color::Green, "green"},
{Color::Red, "red"},
{Color::Blue, "blue"},
{Color::Yellow, "yellow"},
{Color::Black, "black"},
{Color::White, "white"},
{Color::Brown, "brown"}
});
namespace matador::sql {
template<>
struct data_type_traits<Color, void>
{
inline static data_type_t builtin_type(std::size_t size)
{ return data_type_traits<std::string>::builtin_type(size); }
static void read_value(query_result_reader &reader, const char *id, size_t index, Color &value)
{
std::string enum_string;
reader.read_value(id, index, enum_string, 64);
auto enum_opt = color_enum.to_enum(enum_string);
if (enum_opt) {
value = enum_opt.value();
}
}
static any_type create_value(Color &value)
{
return color_enum.to_string(value);
}
static void bind_value(parameter_binder &binder, size_t index, Color &value)
{
binder.bind(index, color_enum.to_string(value));
}
};
}
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};
SECTION("Insert and select with direct execution") {
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 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>();
auto result = s.template select<location>().from("location").
template fetch_all<location>();
for (const auto &l : result) {
REQUIRE(l.name == "center");
for (const auto &l: result) {
REQUIRE(l.name == "center");
}
}
SECTION("Insert and select with prepared statement") {
location loc{1, "center", {1, 2, 3}, Color::Black};
auto stmt = s.insert().template into<location>("location").template values<location>().prepare();
auto res = stmt.bind(loc).execute();
REQUIRE(res == 1);
auto result = s.template select<location>().from("location").
template fetch_all<location>();
for (const auto &l: result) {
REQUIRE(l.name == "center");
}
}
}
+1
View File
@@ -23,4 +23,5 @@ void attribute(Operator &op, const char *id, test::coordinate &value, const fiel
}
}
#endif //QUERY_COORDINATE_HPP