#include #include #include "matador/sql/column.hpp" #include "matador/sql/condition.hpp" #include "matador/sql/connection_info.hpp" #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; template class StatementTestFixture { public: StatementTestFixture() : pool_(Type::dns, 4), session_(pool_) { session_.create() .table("airplane") .execute(); } ~StatementTestFixture() { session_.drop().table("airplane").execute(); } matador::sql::session &session() { return session_; } std::vector> &planes() { return planes_; } private: matador::sql::connection_pool pool_; matador::sql::session session_; std::vector> planes_{ make_entity(1, "Airbus", "A380"), make_entity(2, "Boeing", "707"), make_entity(3, "Boeing", "747") }; }; TEMPLATE_TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]", Sqlite, Postgres) { auto &s = StatementTestFixture::session(); auto &planes = StatementTestFixture::planes(); SECTION("Insert with prepared statement and placeholder") { auto stmt = s.insert() .template into("airplane") .template values().prepare(); for (const auto &plane: planes) { auto res = stmt.bind(*plane).execute(); REQUIRE(res == 1); stmt.reset(); } auto result = s.template select().from("airplane").template fetch_all(); size_t index{0}; for (const auto &i: result) { REQUIRE(i.id == planes[index]->id); REQUIRE(i.brand == planes[index]->brand); REQUIRE(i.model == planes[index++]->model); } } SECTION("Select with prepared statement") { for (const auto &plane: planes) { auto res = s.insert().template into("airplane").values(*plane).execute(); REQUIRE(res == 1); } auto stmt = s.template select().from("airplane").where("brand"_col == _).prepare(); stmt.bind(0, "Airbus"); auto result = stmt.template fetch(); for (const auto &i: result) { REQUIRE(i.id == planes[0]->id); REQUIRE(i.brand == planes[0]->brand); REQUIRE(i.model == planes[0]->model); } stmt.reset(); stmt.bind(0, "Boeing"); result = stmt.template fetch(); size_t index{1}; for (const auto &i: result) { REQUIRE(i.id == planes[index]->id); REQUIRE(i.brand == planes[index]->brand); REQUIRE(i.model == planes[index++]->model); } } }