114 lines
3.0 KiB
C++
114 lines
3.0 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include "matador/sql/column_definition.hpp"
|
|
#include "matador/sql/condition.hpp"
|
|
#include "matador/sql/connection.hpp"
|
|
#include "matador/sql/query.hpp"
|
|
|
|
#include "matador/object/object_ptr.hpp"
|
|
|
|
#include "QueryFixture.hpp"
|
|
|
|
#include "models/airplane.hpp"
|
|
|
|
using namespace matador::sql;
|
|
using namespace matador::test;
|
|
|
|
namespace matador::test::detail {
|
|
template<class Type, typename... Args>
|
|
[[maybe_unused]] object_ptr<Type> make_object_ptr(Args&&... args)
|
|
{
|
|
return object_ptr(new Type(std::forward<Args>(args)...));
|
|
}
|
|
}
|
|
|
|
class StatementTestFixture : public QueryFixture
|
|
{
|
|
public:
|
|
StatementTestFixture()
|
|
{
|
|
const auto res = query::create()
|
|
.table<airplane>("airplane", schema)
|
|
.execute(db);
|
|
REQUIRE(res.is_ok());
|
|
tables_to_drop.emplace("airplane");
|
|
}
|
|
|
|
protected:
|
|
std::vector<matador::object_ptr<airplane>> planes{
|
|
matador::test::detail::make_object_ptr<airplane>(1, "Airbus", "A380"),
|
|
matador::test::detail::make_object_ptr<airplane>(2, "Boeing", "707"),
|
|
matador::test::detail::make_object_ptr<airplane>(3, "Boeing", "747")
|
|
};
|
|
};
|
|
|
|
TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]")
|
|
{
|
|
using namespace matador::utils;
|
|
schema.attach<airplane>("airplane");
|
|
table ap{"airplane"};
|
|
SECTION("Insert with prepared statement and placeholder") {
|
|
auto stmt = query::insert()
|
|
.into("airplane", column_generator::generate<airplane>(schema, true))
|
|
.values<airplane>()
|
|
.prepare(db);
|
|
|
|
for (const auto &plane: planes) {
|
|
auto res = stmt.bind(*plane).execute();
|
|
REQUIRE(res.is_ok());
|
|
REQUIRE(*res == 1);
|
|
stmt.reset();
|
|
}
|
|
|
|
auto result = query::select(column_generator::generate<airplane>(schema, true))
|
|
.from(ap)
|
|
.fetch_all<airplane>(db);
|
|
|
|
REQUIRE(result.is_ok());
|
|
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 = query::insert()
|
|
.into("airplane", column_generator::generate<airplane>(schema, true))
|
|
.values(*plane)
|
|
.execute(db);
|
|
REQUIRE(res.is_ok());
|
|
REQUIRE(*res == 1);
|
|
}
|
|
|
|
auto stmt = query::select(column_generator::generate<airplane>(schema, true))
|
|
.from(ap)
|
|
.where("brand"_col == _)
|
|
.prepare(db);
|
|
|
|
auto result = stmt.bind(0, "Airbus")
|
|
.fetch<airplane>();
|
|
|
|
REQUIRE(result.is_ok());
|
|
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();
|
|
|
|
result = stmt.bind(0, "Boeing")
|
|
.fetch<airplane>();
|
|
|
|
size_t index{1};
|
|
REQUIRE(result.is_ok());
|
|
for (const auto &i: *result) {
|
|
REQUIRE(i.id == planes[index]->id);
|
|
REQUIRE(i.brand == planes[index]->brand);
|
|
REQUIRE(i.model == planes[index++]->model);
|
|
}
|
|
}
|
|
} |