125 lines
3.4 KiB
C++
125 lines
3.4 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include "matador/object/attribute_definition.hpp"
|
|
|
|
#include "matador/sql/connection.hpp"
|
|
#include "matador/sql/column_generator.hpp"
|
|
|
|
#include "matador/query/criteria.hpp"
|
|
#include "matador/query/query.hpp"
|
|
|
|
#include "matador/object/object_ptr.hpp"
|
|
|
|
#include "QueryFixture.hpp"
|
|
|
|
#include "models/airplane.hpp"
|
|
|
|
using namespace matador::sql;
|
|
using namespace matador::object;
|
|
using namespace matador::query;
|
|
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", repo)
|
|
.execute(db);
|
|
REQUIRE(res.is_ok());
|
|
tables_to_drop.emplace("airplane");
|
|
}
|
|
|
|
protected:
|
|
std::vector<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")
|
|
};
|
|
};
|
|
|
|
// template<class Type>
|
|
// std::vector<matador::utils::placeholder> to_placeholder() {
|
|
// return placeholder_generator::generate<Type>();
|
|
// }
|
|
TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]") {
|
|
using namespace matador::utils;
|
|
REQUIRE(repo.attach<airplane>("airplane"));
|
|
table ap{"airplane"};
|
|
SECTION("Insert with prepared statement and placeholder") {
|
|
auto stmt = query::insert()
|
|
.into("airplane", generator::placeholder<airplane>())
|
|
// .into("airplane", column_generator::generate<airplane>(repo, true))
|
|
.values<airplane>()
|
|
.prepare(db);
|
|
REQUIRE(stmt.is_ok());
|
|
|
|
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>(repo, 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>(repo, true))
|
|
.values(*plane)
|
|
.execute(db);
|
|
REQUIRE(res.is_ok());
|
|
REQUIRE(*res == 1);
|
|
}
|
|
|
|
auto stmt = query::select(column_generator::generate<airplane>(repo, true))
|
|
.from(ap)
|
|
.where("brand"_col == _)
|
|
.prepare(db);
|
|
REQUIRE(stmt.is_ok());
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |