fixed backend test compilation

This commit is contained in:
Sascha Kühl 2025-02-12 16:01:52 +01:00
parent 42acaf24ce
commit 5d41a592bb
36 changed files with 727 additions and 575 deletions

View File

@ -1,16 +1,38 @@
#ifndef OBJECT_PTR_HPP
#define OBJECT_PTR_HPP
#include "matador/utils/identifier.hpp"
#include <memory>
namespace matador::object {
template <typename Type>
class object_ptr {
public:
using value_type = Type;
Type* operator->() { return ptr; };
Type& operator*() { return *ptr; };
object_ptr() = default;
object_ptr(Type *obj) : ptr_(obj) {}
explicit object_ptr(std::shared_ptr<Type> obj) : ptr_(obj) {}
object_ptr(const object_ptr &other) : ptr_(other.ptr_) {}
object_ptr(object_ptr &&other) noexcept : ptr_(std::move(other.ptr_)) {}
object_ptr &operator=(const object_ptr &other) = default;
object_ptr &operator=(object_ptr &&other) = default;
using value_type = Type;
Type* operator->() { return ptr_.get(); }
Type& operator*() const { return *ptr_; }
[[nodiscard]] bool empty() const { return ptr_ == nullptr; }
Type* get() { return ptr_.get(); }
const Type* get() const { return ptr_.get(); }
operator bool() { return valid(); }
bool valid() { return ptr_ != nullptr; }
[[nodiscard]] const utils::identifier& primary_key() const { return pk_; }
void primary_key(const utils::identifier &pk) { pk_ = pk; }
private:
Type* ptr{nullptr};
std::shared_ptr<Type> ptr_{};
utils::identifier pk_;
};
}

View File

@ -0,0 +1,33 @@
#ifndef ERROR_CODE_HPP
#define ERROR_CODE_HPP
#include <cstdint>
#include <system_error>
namespace matador::orm {
enum class error_code : uint8_t {
Ok = 0,
NoConnectionAvailable,
UnknownType,
FailedToBuildQuery,
FailedToFindObject
};
class orm_category_impl final : public std::error_category
{
public:
[[nodiscard]] const char* name() const noexcept override;
[[nodiscard]] std::string message(int ev) const override;
};
const std::error_category& orm_category();
std::error_code make_error_code(error_code e);
std::error_condition make_error_condition(error_code e);
}
template <>
struct std::is_error_code_enum<matador::orm::error_code> : true_type {};
#endif //ERROR_CODE_HPP

View File

@ -1,6 +1,7 @@
#ifndef QUERY_SESSION_HPP
#define QUERY_SESSION_HPP
#include "matador/orm/error_code.hpp"
#include "matador/orm/session_query_builder.hpp"
#include "matador/query/query.hpp"
@ -18,16 +19,9 @@
namespace matador::orm {
enum class session_error {
Ok = 0,
NoConnectionAvailable,
UnknownType,
FailedToBuildQuery,
FailedToFindObject
};
utils::error make_error(error_code ec, const std::string &msg);
class session
{
class session final {
public:
explicit session(sql::connection_pool<sql::connection> &pool);
@ -37,81 +31,81 @@ public:
utils::result<void, utils::error> create_schema() const;
template<typename Type>
object::object_ptr<Type> insert(Type *obj);
utils::result<object::object_ptr<Type>, utils::error> insert(Type *obj);
template< class Type, typename... Args >
object::object_ptr<Type> insert(Args&&... args) {
utils::result<object::object_ptr<Type>, utils::error> insert(Args&&... args) {
return insert(new Type(std::forward<Args>(args)...));
}
template<typename Type, typename PrimaryKeyType>
utils::result<object::object_ptr<Type>, session_error> find(const PrimaryKeyType &pk) {
utils::result<object::object_ptr<Type>, utils::error> find(const PrimaryKeyType &pk) {
auto c = pool_.acquire();
if (!c.valid()) {
return utils::failure(session_error::NoConnectionAvailable);
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
}
auto info = schema_->info<Type>();
if (!info) {
return utils::failure(session_error::UnknownType);
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
}
session_query_builder eqb(*schema_);
auto data = eqb.build<Type>(pk);
if (!data.is_ok()) {
return utils::failure(session_error::FailedToBuildQuery);
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
}
auto obj = build_select_query(c, data.release()).template fetch_one<Type>();
auto obj = build_select_query(data.release()).template fetch_one<Type>(*c);
if (!obj) {
return utils::failure(session_error::FailedToFindObject);
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to find object of type " + info->get().name() + " with primary key " + std::to_string(pk) + "."));
}
return utils::ok(object::object_ptr<Type>{ obj.release() });
}
template<typename Type>
utils::result<sql::query_result<Type>, session_error> find() {
utils::result<sql::query_result<Type>, utils::error> find() {
auto c = pool_.acquire();
if (!c.valid()) {
return utils::failure(session_error::NoConnectionAvailable);
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
}
auto info = schema_->info<Type>();
if (!info) {
return utils::failure(session_error::UnknownType);
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
}
session_query_builder eqb(*schema_);
auto data = eqb.build<Type>();
if (!data.is_ok()) {
return utils::failure(session_error::FailedToBuildQuery);
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
}
return utils::ok(build_select_query(c, data.release()).template fetch_all<Type>());
return build_select_query(data.release()).template fetch_all<Type>(*c);
}
template<typename Type>
utils::result<query::query_from_intermediate, session_error> select() {
utils::result<query::query_from_intermediate, utils::error> select() {
auto c = pool_.acquire();
if (!c.valid()) {
return utils::failure(session_error::NoConnectionAvailable);
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
}
auto info = schema_->info<Type>();
if (!info) {
return utils::failure(session_error::UnknownType);
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
}
session_query_builder eqb(*schema_);
auto data = eqb.build<Type>();
if (!data.is_ok()) {
return utils::failure(session_error::FailedToBuildQuery);
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
}
return utils::ok(build_select_query(c, data.release()).template fetch_all<Type>());
return utils::ok(build_select_query(data.release()).template fetch_all<Type>(*c));
}
template<typename Type>
void drop_table();
void drop_table(const std::string &table_name);
void drop_table(const std::string &table_name) const;
[[nodiscard]] utils::result<sql::query_result<sql::record>, utils::error> fetch(const sql::query_context &q) const;
// [[nodiscard]] query_result<record> fetch(const std::string &sql) const;
@ -128,7 +122,7 @@ private:
// [[nodiscard]] std::unique_ptr<sql::query_result_impl> fetch(const std::string &sql) const;
static query::fetchable_query build_select_query(sql::connection_ptr<sql::connection> &conn, entity_query_data &&data);
static query::fetchable_query build_select_query(entity_query_data &&data);
private:
sql::connection_pool<sql::connection> &pool_;
@ -145,20 +139,23 @@ template<typename Type>
}
template<typename Type>
object::object_ptr<Type> session::insert(Type *obj)
utils::result<object::object_ptr<Type>, utils::error> session::insert(Type *obj)
{
auto c = pool_.acquire();
auto info = schema_->info<Type>();
if (!info) {
return {};
return utils::failure(info.err());
}
query::query::insert()
.into(info->name, sql::column_generator::generate<Type>(*schema_, true))
auto res = query::query::insert()
.into(info->get().name(), sql::column_generator::generate<Type>(*schema_, true))
.values(*obj)
.execute();
.execute(*c);
if (!res) {
return utils::failure(res.err());
}
return object::object_ptr{obj};
return utils::ok(object::object_ptr{obj});
}
template<typename Type>

View File

@ -121,9 +121,9 @@ public:
}
pk_ = nullptr;
table_info_stack_.push(info.value());
entity_query_data_ = { info->name() };
entity_query_data_ = { info->get().name() };
try {
access::process(*this, info->prototype());
access::process(*this, info->get().prototype());
return {utils::ok(std::move(entity_query_data_))};
} catch (const query_builder_exception &ex) {

View File

@ -25,11 +25,10 @@ protected:
public:
template < class Type >
utils::result<sql::query_result<Type>, utils::error> fetch_all(sql::executor &exec)
{
utils::result<sql::query_result<Type>, utils::error> fetch_all(sql::executor &exec) {
auto result = fetch(exec);
if (!result.is_ok()) {
return utils::error(result.err());
return utils::failure(result.err());
}
return utils::ok(sql::query_result<Type>(result.release()));
@ -41,7 +40,7 @@ public:
{
auto result = fetch(exec);
if (!result.is_ok()) {
return utils::error(result.err());
return utils::failure(result.err());
}
auto objects = sql::query_result<Type>(result.release());

View File

@ -5,6 +5,8 @@
#include "matador/query/intermediates/executable_query.hpp"
#include "matador/object/attribute_definition_generator.hpp"
namespace matador::query {
class query_create_intermediate : public query_intermediate
@ -14,11 +16,10 @@ public:
executable_query table(const sql::table &table, std::initializer_list<object::attribute_definition> columns);
executable_query table(const sql::table &table, const std::vector<object::attribute_definition> &columns);
// template<class Type>
// executable_query table(const sql::table &table, const sql::schema &schema)
// {
// return this->table(table, column_definition_generator::generate<Type>(schema));
// }
template<class Type>
executable_query table(const sql::table &table, const object::schema &schema) {
return this->table(table, object::attribute_definition_generator::generate<Type>(schema));
}
};
}

View File

@ -2,20 +2,21 @@
#define QUERY_INSERT_INTERMEDIATE_HPP
#include "matador/query/intermediates/query_intermediate.hpp"
#include "matador/query/intermediates/query_into_intermediate.hpp"
#include "matador/sql/column_generator.hpp"
namespace matador::query {
class query_into_intermediate;
class query_insert_intermediate : public query_intermediate
{
public:
query_insert_intermediate();
// template<class Type>
// query_into_intermediate into(const sql::table &table, const sql::schema &schema) {
// return into(table, column_generator::generate<Type>(schema));
// }
template<class Type>
query_into_intermediate into(const sql::table &table, const object::schema &schema) {
return into(table, sql::column_generator::generate<Type>(schema));
}
query_into_intermediate into(const sql::table &table, std::initializer_list<sql::column> columns);
query_into_intermediate into(const sql::table &table, std::vector<sql::column> &&columns);
query_into_intermediate into(const sql::table &table, const std::vector<std::string> &column_names);

View File

@ -9,15 +9,6 @@
namespace matador::query {
// template < class Type >
// std::vector<utils::any_type> as_placeholder(const Type &obj)
// {
// placeholder_generator generator;
// access::process(generator, obj);
// return generator.placeholder_values;
// \}
class query_into_intermediate : public query_intermediate
{
public:
@ -25,16 +16,15 @@ public:
executable_query values(std::initializer_list<std::variant<utils::placeholder, utils::database_type>> values);
executable_query values(std::vector<std::variant<utils::placeholder, utils::database_type>> &&values);
// template<class Type>
// executable_query values()
// {
// Type obj;
// return values(std::move(as_placeholder(obj)));
// }
executable_query values(std::vector<utils::database_type> &&values);
template<class Type>
executable_query values(const Type &obj)
{
return values(std::move(value_extractor::extract(obj)));
executable_query values() {
Type obj;
return values(obj);
}
template<class Type>
executable_query values(const Type &obj) {
return values(value_extractor::extract(obj));
}
};

View File

@ -11,31 +11,19 @@
namespace matador::query {
// template < class Type >
// std::vector<internal::key_value_pair> as_key_value_placeholder(const Type &obj)
// {
// placeholder_key_value_generator generator;
// access::process(generator, obj);
//
// return generator.placeholder_values;
// }
class query_update_intermediate : public query_intermediate
{
class query_update_intermediate : public query_intermediate {
public:
explicit query_update_intermediate(const sql::table& table);
query_set_intermediate set(std::initializer_list<internal::key_value_pair> columns);
query_set_intermediate set(std::vector<internal::key_value_pair> &&columns);
// template<class Type>
// query_set_intermediate set()
// {
// Type obj;
// return set(std::move(as_key_value_placeholder(obj)));
// }
template<class Type>
query_set_intermediate set(const Type &obj)
{
query_set_intermediate set() {
Type obj;
return set(obj);
}
template<class Type>
query_set_intermediate set(const Type &obj) {
return set(key_value_generator::generate(obj));
}
};

View File

@ -3,6 +3,8 @@
#include "matador/query/query_intermediates.hpp"
#include "matador/sql/column_generator.hpp"
namespace matador::sql {
class connection;
}
@ -22,10 +24,10 @@ public:
[[nodiscard]] static query_select_intermediate select(const std::vector<sql::column>& columns);
[[nodiscard]] static query_select_intermediate select(const std::vector<std::string> &column_names);
[[nodiscard]] static query_select_intermediate select(std::vector<sql::column> columns, std::initializer_list<sql::column> additional_columns);
// template<class Type>
// [[nodiscard]] static query_select_intermediate select(const sql::schema &schema) {
// return select(sql::column_generator::generate<Type>(schema));
// }
template<class Type>
[[nodiscard]] static query_select_intermediate select(const object::schema &schema) {
return select(sql::column_generator::generate<Type>(schema));
}
[[nodiscard]] static query_insert_intermediate insert();
[[nodiscard]] static query_update_intermediate update(const sql::table &table);
[[nodiscard]] static query_delete_intermediate remove();

View File

@ -17,8 +17,7 @@ private:
public:
template < class Type >
static std::vector<utils::database_type> extract(const Type &type)
{
static std::vector<utils::database_type> extract(const Type &type) {
std::vector<utils::database_type> values;
value_extractor gen(values);
access::process(gen, type);

View File

@ -1,8 +1,9 @@
#ifndef QUERY_CONNECTION_HPP
#define QUERY_CONNECTION_HPP
#include "matador/sql/abstract_sql_logger.hpp"
#include "matador/object/attribute_definition.hpp"
#include "matador/sql/abstract_sql_logger.hpp"
#include "matador/sql/connection_info.hpp"
#include "matador/sql/executor.hpp"
#include "matador/sql/statement.hpp"
@ -51,7 +52,7 @@ public:
*
* @param x The connection to copy move
*/
connection(connection &&x) noexcept = default;
connection(connection &&x) noexcept;
/**
* Assigns moves from the given connection
*

View File

@ -29,8 +29,7 @@ struct connection_info {
std::string driver{}; /**< Driver to use. This is used by th mssql/odbc backends. */
template < class Operator >
void process(Operator &op)
{
void process(Operator &op) {
namespace field = matador::access;
field::attribute(op, "type", type);
field::attribute(op, "user", user);

View File

@ -81,7 +81,8 @@ public:
connection_repo_.emplace_back(count, info_);
auto &conn = connection_repo_.back();
idle_connections_.emplace(conn.first, &conn);
conn.second.open();
// Todo: handle result
std::ignore = conn.second.open();
--count;
}
}

View File

@ -33,8 +33,7 @@ public:
}
template < class Type >
void bind(const size_t pos, Type &val)
{
void bind(const size_t pos, Type &val) {
utils::data_type_traits<Type>::bind_value(binder(), adjust_index(pos), val);
}

View File

@ -9,13 +9,11 @@ library::library(std::string lib)
: lib_(std::move(lib))
{}
library::~library()
{
library::~library() {
unload();
}
bool library::load()
{
bool library::load() {
auto path = os::getenv("MATADOR_BACKENDS_PATH");
#if defined(_MSC_VER) || defined(__MINGW32__)
auto cookie = AddDllDirectory(std::wstring(path.begin(), path.end()).c_str());

View File

@ -40,6 +40,7 @@ add_library(matador-orm STATIC
../../include/matador/query/query_intermediates.hpp
../../include/matador/query/query_part.hpp
../../include/matador/query/value_extractor.hpp
../../include/matador/orm/error_code.hpp
../../include/matador/orm/session.hpp
../../include/matador/orm/session_query_builder.hpp
../../include/matador/sql/abstract_sql_logger.hpp
@ -104,6 +105,7 @@ add_library(matador-orm STATIC
query/query_part.cpp
query/query_update_intermediate.cpp
query/value_extractor.cpp
orm/error_code.cpp
orm/session.cpp
orm/session_query_builder.cpp
sql/backend_provider.cpp

View File

@ -0,0 +1,39 @@
#include "matador/orm/error_code.hpp"
namespace matador::orm {
const char * orm_category_impl::name() const noexcept {
return "orm";
}
std::string orm_category_impl::message(const int ev) const {
switch (static_cast<error_code>(ev)) {
case error_code::Ok:
return "OK";
case error_code::NoConnectionAvailable:
return "No connection available";
case error_code::UnknownType:
return "Unknown type";
case error_code::FailedToBuildQuery:
return "Failed to build query";
case error_code::FailedToFindObject:
return "Failed to find object";
default:
return "Unknown error";
}
}
const std::error_category & orm_category() {
static orm_category_impl instance;
return instance;
}
std::error_code make_error_code(error_code e) {
return {static_cast<int>(e), orm_category()};
}
std::error_condition make_error_condition(error_code e) {
return {static_cast<int>(e), orm_category()};
}
}

View File

@ -8,6 +8,10 @@
namespace matador::orm {
utils::error make_error( const error_code ec, const std::string& msg ) {
return utils::error(ec, msg);
}
session::session(sql::connection_pool<sql::connection> &pool)
: pool_(pool)
, dialect_(sql::backend_provider::instance().connection_dialect(pool_.info().type))
@ -26,8 +30,7 @@ utils::result<void, utils::error> session::create_schema() const {
return utils::ok<void>();
}
void session::drop_table(const std::string &table_name)
{
void session::drop_table(const std::string &table_name) const {
auto c = pool_.acquire();
if (!c.valid()) {
throw std::logic_error("no database connection available");
@ -114,7 +117,7 @@ const class sql::dialect &session::dialect() const
// return c->fetch(sql);
// }
query::fetchable_query session::build_select_query(sql::connection_ptr<sql::connection> &conn, entity_query_data &&data) {
query::fetchable_query session::build_select_query(entity_query_data &&data) {
return query::query::select(data.columns)
.from(data.root_table_name)
.join_left(data.joins)

View File

@ -14,4 +14,11 @@ executable_query query_into_intermediate::values(std::vector<std::variant<utils:
return {context_};
}
executable_query query_into_intermediate::values( std::vector<utils::database_type>&& values ) {
std::vector<std::variant<utils::placeholder, utils::database_type>> transformed_values;
for (auto&& val : values) {
transformed_values.emplace_back(val);
}
return this->values(std::move(transformed_values));
}
} // namespace matador::query

View File

@ -45,6 +45,11 @@ connection &connection::operator=(const connection &x) {
return *this;
}
connection::connection( connection&& x ) noexcept
: connection_info_(std::move(x.connection_info_))
, connection_(std::move(x.connection_))
, logger_(std::move(x.logger_)) {}
connection & connection::operator=(connection &&x) noexcept {
connection_info_ = std::move(x.connection_info_);
connection_ = std::move(x.connection_);

View File

@ -5,9 +5,7 @@
namespace matador::utils {
void data_type_traits<test::Color, void>::read_value(attribute_reader &reader, const char *id, size_t index,
test::Color &value)
{
void data_type_traits<test::Color>::read_value(attribute_reader &reader, const char *id, const size_t index, test::Color &value) {
std::string enum_string;
reader.read_value(id, index, enum_string, 64);
if (const auto enum_opt = color_enum.to_enum(enum_string)) {
@ -15,8 +13,7 @@ void data_type_traits<test::Color, void>::read_value(attribute_reader &reader, c
}
}
void data_type_traits<test::Color, void>::bind_value(attribute_writer &binder, const size_t index, const test::Color &value)
{
void data_type_traits<test::Color>::bind_value(attribute_writer &binder, const size_t index, const test::Color &value) {
binder.write_value(index, color_enum.to_string(value));
}

View File

@ -1,13 +1,15 @@
#include "catch2/catch_test_macros.hpp"
#include "catch2/matchers/catch_matchers_string.hpp"
#include "matador/sql/column_definition.hpp"
#include "matador/object/attribute_definition.hpp"
#include "matador/object/schema.hpp"
#include "matador/sql/connection.hpp"
#include "matador/sql/column_generator.hpp"
#include "matador/query/condition.hpp"
#include "matador/query/query.hpp"
#include "matador/sql/schema.hpp"
#include "matador/utils/basic_types.hpp"
#include "matador/utils/string.hpp"
@ -15,12 +17,15 @@
#include "QueryFixture.hpp"
#include <iostream>
using namespace matador::test;
using namespace matador::sql;
using namespace matador::object;
using namespace matador::query;
TEST_CASE_METHOD( QueryFixture, "Insert and select basic datatypes", "[query][datatypes]" ) {
schema.attach<types>("types");
REQUIRE(schema.attach<types>("types"));
auto res = query::create()
.table<types>("types", schema)
.execute(db);
@ -30,18 +35,15 @@ TEST_CASE_METHOD( QueryFixture, "Insert and select basic datatypes", "[query][da
float float_value = 2.44557f;
double double_value = 11111.23433345;
char cval = 'c';
int8_t cval = 'c';
short sval = (std::numeric_limits<short>::min)();
int ival = (std::numeric_limits<int>::min)();
long lval = (std::numeric_limits<long>::min)();
long long llval = (std::numeric_limits<long long>::max)();
unsigned char ucval = (std::numeric_limits<unsigned char>::max)();
unsigned short usval = (std::numeric_limits<unsigned short>::max)();
unsigned int uival = (std::numeric_limits<unsigned int>::max)();
unsigned long ulval = (std::numeric_limits<unsigned long>::max)();
unsigned long long ullval = (std::numeric_limits<unsigned long long>::max)();
if (db.type() == "sqlite" || db.type() == "postgres") {
ulval = (std::numeric_limits<long>::max)();
ullval = (std::numeric_limits<long long>::max)();
}
bool bval = true;
@ -55,19 +57,19 @@ TEST_CASE_METHOD( QueryFixture, "Insert and select basic datatypes", "[query][da
"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. "
"At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd "
"gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
matador::date date_val(15, 3, 2015);
auto time_val = matador::time(2015, 3, 15, 13, 56, 23, 123);
// matador::date date_val(15, 3, 2015);
// auto time_val = matador::time(2015, 3, 15, 13, 56, 23, 123);
matador::utils::blob blob_val {1,2,3,4,5,6,7,8};
types t {
1,
cval, sval, ival, lval, llval,
ucval, usval, uival, ulval, ullval,
cval, sval, ival, llval,
ucval, usval, uival, ullval,
float_value, double_value,
bval,
"Armer schwarzer Kater",
strval, varcharval,
date_val, time_val,
// date_val, time_val,
blob_val
};
@ -88,12 +90,10 @@ TEST_CASE_METHOD( QueryFixture, "Insert and select basic datatypes", "[query][da
REQUIRE((*result)->char_ == cval);
REQUIRE((*result)->short_ == sval);
REQUIRE((*result)->int_ == ival);
REQUIRE((*result)->long_ == lval);
REQUIRE((*result)->long64_ == llval);
REQUIRE((*result)->unsigned_char_ == ucval);
REQUIRE((*result)->unsigned_short_ == usval);
REQUIRE((*result)->unsigned_int_ == uival);
REQUIRE((*result)->unsigned_long_ == ulval);
REQUIRE((*result)->unsigned_long64_ == ullval);
REQUIRE((*result)->float_ == float_value);
REQUIRE((*result)->double_ == double_value);
@ -101,8 +101,8 @@ TEST_CASE_METHOD( QueryFixture, "Insert and select basic datatypes", "[query][da
REQUIRE((*result)->bool_ == bval);
REQUIRE((*result)->varchar_ == varcharval);
REQUIRE((*result)->string_ == strval);
REQUIRE((*result)->date_ == date_val);
REQUIRE((*result)->time_ == time_val);
// REQUIRE((*result)->date_ == date_val);
// REQUIRE((*result)->time_ == time_val);
REQUIRE((*result)->binary_ == blob_val);
}
@ -121,7 +121,7 @@ TEST_CASE_METHOD( QueryFixture, "Test quoted identifier", "[query][quotes][ident
// check table description
std::vector<std::string> column_names = { "from", "to"};
std::vector<matador::data_type> types = {matador::data_type::type_varchar, matador::data_type::type_varchar};
std::vector<matador::utils::basic_type> types = {matador::utils::basic_type::type_varchar, matador::utils::basic_type::type_varchar};
const auto columns = db.describe("quotes");
REQUIRE(columns.is_ok());
@ -194,7 +194,7 @@ TEST_CASE_METHOD( QueryFixture, "Test quoted column names", "[query][quotes][col
for (const auto &col : *columns) {
REQUIRE(col.name() == name);
REQUIRE(col.type() == matador::data_type::type_varchar);
REQUIRE(col.type() == matador::utils::basic_type::type_varchar);
}
res = query::drop()
@ -278,7 +278,7 @@ TEST_CASE_METHOD(QueryFixture, "Test quoted literals", "[query][quotes][literals
TEST_CASE_METHOD(QueryFixture, "Test describe table", "[query][describe][table]") {
using namespace matador::sql;
schema.attach<types>("types");
REQUIRE(schema.attach<types>("types"));
const auto res = query::create()
.table<types>("types", schema)
.execute(db);
@ -293,29 +293,30 @@ TEST_CASE_METHOD(QueryFixture, "Test describe table", "[query][describe][table]"
"val_char", "val_float", "val_double", "val_short",
"val_int", "val_long", "val_long_long", "val_unsigned_char",
"val_unsigned_short", "val_unsigned_int", "val_unsigned_long", "val_unsigned_long_long",
"val_bool", "val_cstr", "val_string", "val_varchar", "val_date", "val_time",
"val_bool", "val_cstr", "val_string", "val_varchar",
// "val_date", "val_time",
"val_binary"};
const std::vector<std::function<bool (const column_definition&)>> type_check = {
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_floating_point(); },
[](const column_definition &cf) { return cf.is_floating_point(); },
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_integer(); },
[](const column_definition &cf) { return cf.is_bool(); },
[](const column_definition &cf) { return cf.is_varchar(); },
[](const column_definition &cf) { return cf.is_string(); },
[](const column_definition &cf) { return cf.is_varchar(); },
[](const column_definition &cf) { return cf.is_date(); },
[](const column_definition &cf) { return cf.is_time(); },
[](const column_definition &cf) { return cf.is_blob(); }
const std::vector<std::function<bool (const attribute_definition&)>> type_check = {
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_floating_point(); },
[](const attribute_definition &cf) { return cf.is_floating_point(); },
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_integer(); },
[](const attribute_definition &cf) { return cf.is_bool(); },
[](const attribute_definition &cf) { return cf.is_varchar(); },
[](const attribute_definition &cf) { return cf.is_string(); },
[](const attribute_definition &cf) { return cf.is_varchar(); },
// [](const attribute_definition &cf) { return cf.is_date(); },
// [](const attribute_definition &cf) { return cf.is_time(); },
[](const attribute_definition &cf) { return cf.is_blob(); }
};
const auto &cols = columns.value();
@ -341,7 +342,7 @@ struct pk {
matador::access::attribute(op, "name", name, 255);
}
unsigned long id{};
uint32_t id{};
std::string name;
};
}
@ -350,7 +351,7 @@ TEST_CASE_METHOD(QueryFixture, "Test primary key", "[query][primary key]") {
using namespace matador::test::temporary;
using namespace matador::sql;
schema.attach<pk>("pk");
REQUIRE(schema.attach<pk>("pk"));
auto res = query::create()
.table<pk>("pk", schema)
.execute(db);
@ -379,7 +380,7 @@ TEST_CASE_METHOD(QueryFixture, "Test primary key prepared", "[query][primary key
using namespace matador::test::temporary;
using namespace matador::sql;
schema.attach<pk>("pk");
REQUIRE(schema.attach<pk>("pk"));
auto res = query::create()
.table<pk>("pk", schema)
.execute(db);
@ -393,8 +394,9 @@ TEST_CASE_METHOD(QueryFixture, "Test primary key prepared", "[query][primary key
.into("pk", column_generator::generate<pk>(schema))
.values<pk>()
.prepare(db);
REQUIRE(stmt);
res = stmt.bind(pk1)
res = stmt->bind(pk1)
.execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
@ -402,150 +404,151 @@ TEST_CASE_METHOD(QueryFixture, "Test primary key prepared", "[query][primary key
stmt = query::select<pk>(schema)
.from("pk")
.prepare(db);
REQUIRE(stmt);
auto row = stmt.fetch_one<pk>();
auto row = stmt->fetch_one<pk>();
REQUIRE(row.is_ok());
REQUIRE(*row != nullptr);
REQUIRE(row.value()->id > 0);
REQUIRE(row.value()->name == "george");
}
namespace matador::test::temporary {
struct appointment
{
unsigned long id{};
std::string name;
matador::time time_point{};
matador::date date_point{};
template < class Operator >
void process(Operator &op)
{
matador::access::primary_key(op, "id", id);
matador::access::attribute(op, "name", name, 255);
matador::access::attribute(op, "time_point", time_point);
matador::access::attribute(op, "date_point", date_point);
}
};
}
TEST_CASE_METHOD(QueryFixture, "Test select time and date", "[query][select][time]") {
using namespace matador::test::temporary;
using namespace matador::sql;
schema.attach<appointment>("appointment");
auto res = query::create()
.table<appointment>("appointment", schema)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
tables_to_drop.emplace("appointment");
auto dinner = appointment{ 1, "dinner" };
auto time_str = matador::utils::to_string(dinner.time_point);
auto date_str = matador::utils::to_string(dinner.date_point);
res = query::insert()
.into("appointment", column_generator::generate<appointment>(schema))
.values(dinner)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
auto row = query::select<appointment>(schema)
.from("appointment")
.fetch_one<appointment>(db);
REQUIRE(row.is_ok());
REQUIRE(*row != nullptr);
REQUIRE(matador::utils::to_string(row.value()->time_point) == time_str);
REQUIRE(matador::utils::to_string(row.value()->date_point) == date_str);
}
TEST_CASE_METHOD(QueryFixture, "Test null column", "[query][select][null]") {
using namespace matador::sql;
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("first_name", 255, null_option::NULLABLE),
make_column<std::string>("last_name", 255, null_option::NULLABLE)
})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
tables_to_drop.emplace("person");
res = query::insert()
.into("person", {"id", "first_name"})
.values({1, "george"})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
res = query::insert()
.into("person", {"id", "last_name"})
.values({2, "clooney"})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
auto result = query::select({"id", "first_name", "last_name"})
.from("person")
.fetch_all(db);
REQUIRE(result.is_ok());
std::vector<std::string> expected_first_names{"george", ""};
std::vector<std::string> expected_last_names{"", "clooney"};
size_t index{0};
for (const auto& row : *result) {
auto first_name = row.at<std::string>("first_name");
auto last_name = row.at<std::string>("last_name");
std::cout << "first name " << first_name.value() << " last name " << last_name.value() << std::endl;
REQUIRE(first_name == expected_first_names[index]);
REQUIRE(last_name == expected_last_names[index++]);
}
}
TEST_CASE_METHOD(QueryFixture, "Test null column prepared", "[query][select][null][prepared]") {
using namespace matador::sql;
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("first_name", 255, null_option::NULLABLE),
make_column<std::string>("last_name", 255, null_option::NULLABLE)
})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
tables_to_drop.emplace("person");
res = query::insert()
.into("person", {"id", "first_name"})
.values({1, "george"})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
res = query::insert()
.into("person", {"id", "last_name"})
.values({2, "clooney"})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
auto result = query::select({"id", "first_name", "last_name"})
.from("person")
.fetch_all(db);
REQUIRE(result.is_ok());
std::vector<std::string> expected_first_names{"george", ""};
std::vector<std::string> expected_last_names{"", "clooney"};
size_t index{0};
for (const auto& row : *result) {
auto first_name = row.at("first_name").as<std::string>();
auto last_name = row.at("last_name").as<std::string>();
REQUIRE(first_name == expected_first_names[index]);
REQUIRE(last_name == expected_last_names[index++]);
}
}
// namespace matador::test::temporary {
// struct appointment
// {
// unsigned long id{};
// std::string name;
// matador::time time_point{};
// matador::date date_point{};
//
// template < class Operator >
// void process(Operator &op)
// {
// matador::access::primary_key(op, "id", id);
// matador::access::attribute(op, "name", name, 255);
// matador::access::attribute(op, "time_point", time_point);
// matador::access::attribute(op, "date_point", date_point);
// }
// };
//
// }
// TEST_CASE_METHOD(QueryFixture, "Test select time and date", "[query][select][time]") {
// using namespace matador::test::temporary;
// using namespace matador::sql;
// REQUIRE(schema.attach<appointment>("appointment"));
// auto res = query::create()
// .table<appointment>("appointment", schema)
// .execute(db);
// REQUIRE(res.is_ok());
// REQUIRE(*res == 0);
// tables_to_drop.emplace("appointment");
//
// auto dinner = appointment{ 1, "dinner" };
// auto time_str = matador::utils::to_string(dinner.time_point);
// auto date_str = matador::utils::to_string(dinner.date_point);
//
// res = query::insert()
// .into("appointment", column_generator::generate<appointment>(schema))
// .values(dinner)
// .execute(db);
// REQUIRE(res.is_ok());
// REQUIRE(*res == 1);
//
// auto row = query::select<appointment>(schema)
// .from("appointment")
// .fetch_one<appointment>(db);
// REQUIRE(row.is_ok());
//
// REQUIRE(*row != nullptr);
// REQUIRE(matador::utils::to_string(row.value()->time_point) == time_str);
// REQUIRE(matador::utils::to_string(row.value()->date_point) == date_str);
// }
//
// TEST_CASE_METHOD(QueryFixture, "Test null column", "[query][select][null]") {
// using namespace matador::sql;
//
// auto res = query::create()
// .table("person", {
// make_pk_column<unsigned long>("id"),
// make_column<std::string>("first_name", 255, null_option::NULLABLE),
// make_column<std::string>("last_name", 255, null_option::NULLABLE)
// })
// .execute(db);
// REQUIRE(res.is_ok());
// REQUIRE(*res == 0);
// tables_to_drop.emplace("person");
//
// res = query::insert()
// .into("person", {"id", "first_name"})
// .values({1, "george"})
// .execute(db);
// REQUIRE(res.is_ok());
// REQUIRE(*res == 1);
//
// res = query::insert()
// .into("person", {"id", "last_name"})
// .values({2, "clooney"})
// .execute(db);
// REQUIRE(res.is_ok());
// REQUIRE(*res == 1);
//
// auto result = query::select({"id", "first_name", "last_name"})
// .from("person")
// .fetch_all(db);
// REQUIRE(result.is_ok());
//
// std::vector<std::string> expected_first_names{"george", ""};
// std::vector<std::string> expected_last_names{"", "clooney"};
// size_t index{0};
// for (const auto& row : *result) {
// auto first_name = row.at<std::string>("first_name");
// auto last_name = row.at<std::string>("last_name");
// std::cout << "first name " << first_name.value() << " last name " << last_name.value() << std::endl;
// REQUIRE(first_name == expected_first_names[index]);
// REQUIRE(last_name == expected_last_names[index++]);
// }
// }
//
// TEST_CASE_METHOD(QueryFixture, "Test null column prepared", "[query][select][null][prepared]") {
// using namespace matador::sql;
//
// auto res = query::create()
// .table("person", {
// make_pk_column<unsigned long>("id"),
// make_column<std::string>("first_name", 255, null_option::NULLABLE),
// make_column<std::string>("last_name", 255, null_option::NULLABLE)
// })
// .execute(db);
// REQUIRE(res.is_ok());
// REQUIRE(*res == 0);
// tables_to_drop.emplace("person");
//
// res = query::insert()
// .into("person", {"id", "first_name"})
// .values({1, "george"})
// .execute(db);
// REQUIRE(res.is_ok());
// REQUIRE(*res == 1);
//
// res = query::insert()
// .into("person", {"id", "last_name"})
// .values({2, "clooney"})
// .execute(db);
// REQUIRE(res.is_ok());
// REQUIRE(*res == 1);
//
// auto result = query::select({"id", "first_name", "last_name"})
// .from("person")
// .fetch_all(db);
// REQUIRE(result.is_ok());
//
// std::vector<std::string> expected_first_names{"george", ""};
// std::vector<std::string> expected_last_names{"", "clooney"};
// size_t index{0};
// for (const auto& row : *result) {
// auto first_name = row.at("first_name").as<std::string>();
// auto last_name = row.at("last_name").as<std::string>();
// REQUIRE(first_name == expected_first_names[index]);
// REQUIRE(last_name == expected_last_names[index++]);
// }
// }

View File

@ -1,6 +1,8 @@
#include "QueryFixture.hpp"
#include "matador/sql/query.hpp"
#include "matador/query/query.hpp"
#include "matador/sql/dialect.hpp"
#include "catch2/catch_test_macros.hpp"
@ -10,7 +12,7 @@ QueryFixture::QueryFixture()
: db(connection::dns)
, schema(db.dialect().default_schema_name())
{
db.open();
REQUIRE(db.open());
}
QueryFixture::~QueryFixture() {
@ -37,7 +39,7 @@ void QueryFixture::check_table_not_exists(const std::string &table_name) const
void QueryFixture::drop_table_if_exists(const std::string &table_name) const {
const auto result = db.exists(table_name).and_then([&table_name, this](bool exists) {
if (exists) {
if (sql::query::drop()
if (query::query::drop()
.table(table_name)
.execute(db).is_ok()) {
this->check_table_not_exists(table_name);

View File

@ -21,8 +21,8 @@ public:
protected:
sql::connection db;
sql::schema schema;
std::stack <std::string> tables_to_drop;
object::schema schema;
private:
void drop_table_if_exists(const std::string &table_name) const;

View File

@ -1,9 +1,10 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/column.hpp"
#include "matador/sql/condition.hpp"
#include "matador/sql/connection.hpp"
#include "matador/sql/query.hpp"
#include "matador/query/condition.hpp"
#include "matador/query/query.hpp"
#include "matador/utils/types.hpp"
#include "matador/utils/string.hpp"
@ -13,6 +14,8 @@
#include <list>
#include <algorithm>
using namespace matador::object;
using namespace matador::query;
using namespace matador::sql;
using namespace matador::test;
@ -21,24 +24,22 @@ TEST_CASE_METHOD(QueryFixture, "Test all data types for record", "[query][record
auto res = query::create()
.table("types", {
make_pk_column<unsigned long>("id"),
make_column<char>("val_char"),
make_column<short>("val_short"),
make_column<int>("val_int"),
make_column<long>("val_long"),
make_column<long long>("val_long_long"),
make_column<unsigned char>("val_uchar"),
make_column<unsigned short>("val_ushort"),
make_column<unsigned int>("val_uint"),
make_column<unsigned long>("val_ulong"),
make_column<unsigned long long>("val_ulong_long"),
make_pk_column<uint32_t>("id"),
make_column<int8_t>("val_char"),
make_column<int16_t>("val_short"),
make_column<int32_t>("val_int"),
make_column<int64_t>("val_long_long"),
make_column<uint8_t>("val_uchar"),
make_column<uint16_t>("val_ushort"),
make_column<uint32_t>("val_uint"),
make_column<uint64_t>("val_ulong_long"),
make_column<bool>("val_bool"),
make_column<float>("val_float"),
make_column<double>("val_double"),
make_column<std::string>("val_string"),
make_column<std::string>("val_varchar", 63),
make_column<matador::date>("val_date"),
make_column<matador::time>("val_time"),
// make_column<matador::date>("val_date"),
// make_column<matador::time>("val_time"),
make_column<matador::utils::blob>("val_blob"),
})
.execute(db);
@ -54,7 +55,8 @@ TEST_CASE_METHOD(QueryFixture, "Test all data types for record", "[query][record
"val_bool",
"val_float", "val_double",
"val_string", "val_varchar",
"val_date", "val_time", "val_blob"};
// "val_date", "val_time",
"val_blob"};
const auto fields = db.describe("types");
REQUIRE(fields.is_ok());
@ -62,29 +64,27 @@ TEST_CASE_METHOD(QueryFixture, "Test all data types for record", "[query][record
REQUIRE(std::find(cols.begin(), cols.end(), fld.name()) != cols.end());
}
unsigned long id{1};
char c{-11};
short s{-256};
int i{-123456};
long l{-9876543};
long long ll{-987654321};
unsigned char uc{13};
unsigned short us{1024};
unsigned int ui{654321};
unsigned long ul{12345678};
unsigned long long ull{1234567890};
uint32_t id{1};
int8_t c{-11};
int16_t s{-256};
int32_t i{-123456};
int64_t ll{-987654321};
uint8_t uc{13};
uint16_t us{1024};
uint32_t ui{654321};
uint64_t ull{1234567890};
bool b{true};
float f{3.1415f};
double d{2.71828};
std::string str{"long text"};
std::string varchar{"good day"};
auto md{matador::date()};
auto mt{matador::time::now()};
// auto md{matador::date()};
// auto mt{matador::time::now()};
matador::utils::blob bin{0x01,0x02,0x03,0x04};
res = query::insert()
.into("types", cols)
.values({id, c, s, i, l, ll, uc, us, ui, ul, ull, b, f, d, str, varchar, md, mt, bin})
.values({id, c, s, i, ll, uc, us, ui, ull, b, f, d, str, varchar, /*md, mt, */bin})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(res.value() == 1);
@ -96,24 +96,22 @@ TEST_CASE_METHOD(QueryFixture, "Test all data types for record", "[query][record
REQUIRE(row.is_ok());
REQUIRE(row.value().has_value());
REQUIRE(id == (*row)->at<unsigned long>("id"));
REQUIRE(c == (*row)->at<char>("val_char"));
REQUIRE(s == (*row)->at<short>("val_short"));
REQUIRE(i == (*row)->at<int>("val_int"));
REQUIRE(l == (*row)->at<long>("val_long"));
REQUIRE(ll == (*row)->at<long long>("val_long_long"));
REQUIRE(uc == (*row)->at<unsigned char>("val_uchar"));
REQUIRE(id == (*row)->at<uint32_t>("id"));
REQUIRE(c == (*row)->at<int8_t>("val_char"));
REQUIRE(s == (*row)->at<int16_t>("val_short"));
REQUIRE(i == (*row)->at<int32_t>("val_int"));
REQUIRE(ll == (*row)->at<int64_t>("val_long_long"));
REQUIRE(uc == (*row)->at<uint8_t>("val_uchar"));
REQUIRE(us == (*row)->at<unsigned short>("val_ushort"));
REQUIRE(ui == (*row)->at<unsigned int>("val_uint"));
REQUIRE(ul == (*row)->at<unsigned long>("val_ulong"));
REQUIRE(ull == (*row)->at<unsigned long long>("val_ulong_long"));
REQUIRE(ull == (*row)->at<uint64_t>("val_ulong_long"));
REQUIRE((*row)->at<bool>("val_bool"));
REQUIRE(f == (*row)->at<float>("val_float"));
REQUIRE(d == (*row)->at<double>("val_double"));
REQUIRE(str == (*row)->at<std::string>("val_string"));
REQUIRE(varchar == (*row)->at<std::string>("val_varchar"));
REQUIRE(md == (*row)->at<matador::date>("val_date"));
REQUIRE(mt == (*row)->at<matador::time>("val_time"));
// REQUIRE(md == (*row)->at<matador::date>("val_date"));
// REQUIRE(mt == (*row)->at<matador::time>("val_time"));
REQUIRE(bin == (*row)->at<matador::utils::blob>("val_blob"));
}
@ -122,7 +120,7 @@ TEST_CASE_METHOD(QueryFixture, "Create and drop table statement", "[query][recor
check_table_not_exists("person");
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
@ -146,7 +144,7 @@ TEST_CASE_METHOD(QueryFixture, "Create and drop table statement with foreign key
{
auto res = query::create()
.table("airplane", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("brand", 255),
make_column<std::string>("model", 255),
})
@ -159,8 +157,8 @@ TEST_CASE_METHOD(QueryFixture, "Create and drop table statement with foreign key
res = query::create()
.table("flight", {
make_pk_column<unsigned long>("id"),
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
make_pk_column<uint32_t>("id"),
make_fk_column<uint32_t>("airplane_id", "airplane", "id"),
make_column<std::string>("pilot_name", 255),
})
.execute(db);
@ -191,7 +189,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert record statement", "[query][recor
{
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
@ -230,7 +228,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert record statement with foreign key
{
auto res = query::create()
.table("airplane", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("brand", 255),
make_column<std::string>("model", 255),
})
@ -241,8 +239,8 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert record statement with foreign key
res = query::create()
.table("flight", {
make_pk_column<unsigned long>("id"),
make_fk_column<unsigned long>("airplane_id", "airplane", "id"),
make_pk_column<uint32_t>("id"),
make_fk_column<uint32_t>("airplane_id", "airplane", "id"),
make_column<std::string>("pilot_name", 255),
})
.execute(db);
@ -250,7 +248,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert record statement with foreign key
REQUIRE(res.value() == 0);
tables_to_drop.emplace("flight");
std::vector<std::vector<matador::utils::any_type>> values_list{
std::vector<std::vector<matador::utils::database_type>> values_list{
{1, "Airbus", "A380"},
{2, "Boeing", "707"},
{3, "Boeing", "747"}
@ -284,7 +282,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute update record statement", "[query][recor
{
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
@ -333,7 +331,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement", "[query][record]")
{
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
@ -342,7 +340,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement", "[query][record]")
REQUIRE(res.value() == 0);
tables_to_drop.emplace("person");
std::vector<std::vector<matador::utils::any_type>> values_list{
std::vector<std::vector<matador::utils::database_type>> values_list{
{1, "george", 45},
{2, "jane", 32},
{3, "michael", 67},
@ -389,7 +387,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with order by", "[query
{
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
@ -398,7 +396,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with order by", "[query
REQUIRE(res.value() == 0);
tables_to_drop.emplace("person");
std::vector<std::vector<matador::utils::any_type>> values_list{
std::vector<std::vector<matador::utils::database_type>> values_list{
{1, "george", 45},
{2, "jane", 32},
{3, "michael", 67},
@ -432,7 +430,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with group by and order
{
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
@ -441,7 +439,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with group by and order
REQUIRE(*res == 0);
tables_to_drop.emplace("person");
std::vector<std::vector<matador::utils::any_type>> values_list{
std::vector<std::vector<matador::utils::database_type>> values_list{
{1, "george", 45},
{2, "jane", 45},
{3, "joe", 45},
@ -482,7 +480,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute delete statement", "[query][record]")
{
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
}).execute(db);
@ -537,9 +535,9 @@ TEST_CASE_METHOD(QueryFixture, "Test quoted identifier record", "[query][record]
// check table description
std::vector<std::string> columns = { "from", "to"};
std::vector<matador::data_type> types = {
matador::data_type::type_varchar,
matador::data_type::type_varchar
std::vector<matador::utils::basic_type> types = {
matador::utils::basic_type::type_varchar,
matador::utils::basic_type::type_varchar
};
auto fields = db.describe("quotes");
REQUIRE(fields.is_ok());
@ -585,7 +583,7 @@ TEST_CASE_METHOD(QueryFixture, "Test create record", "[query][record][create]")
check_table_not_exists("person");
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
@ -608,7 +606,7 @@ TEST_CASE_METHOD(QueryFixture, "Test insert record", "[query][record][insert]")
check_table_not_exists("person");
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
@ -632,7 +630,7 @@ TEST_CASE_METHOD(QueryFixture, "Test insert record", "[query][record][insert]")
REQUIRE(row.is_ok());
REQUIRE(row->has_value());
REQUIRE((*row)->at("id").as<unsigned long>() == 1);
REQUIRE((*row)->at("id").as<uint32_t>() == 1);
REQUIRE((*row)->at("name").as<std::string>() == "hans");
REQUIRE((*row)->at("age").as<unsigned short>() == 45);
}
@ -641,7 +639,7 @@ TEST_CASE_METHOD(QueryFixture, "Test update record", "[query][record][update]")
check_table_not_exists("person");
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
@ -665,7 +663,7 @@ TEST_CASE_METHOD(QueryFixture, "Test update record", "[query][record][update]")
REQUIRE(row.is_ok());
REQUIRE(row->has_value());
REQUIRE((*row)->at("id").as<unsigned long>() == 1);
REQUIRE((*row)->at("id").as<uint32_t>() == 1);
REQUIRE((*row)->at("name").as<std::string>() == "hans");
REQUIRE((*row)->at("age").as<unsigned short>() == 45);
@ -682,7 +680,7 @@ TEST_CASE_METHOD(QueryFixture, "Test update record", "[query][record][update]")
REQUIRE(row.is_ok());
REQUIRE(row->has_value());
REQUIRE((*row)->at("id").as<unsigned long>() == 1);
REQUIRE((*row)->at("id").as<uint32_t>() == 1);
REQUIRE((*row)->at("name").as<std::string>() == "jane");
REQUIRE((*row)->at("age").as<unsigned short>() == 47);
}
@ -691,14 +689,15 @@ TEST_CASE_METHOD(QueryFixture, "Test prepared record statement", "[query][record
check_table_not_exists("person");
auto stmt = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<unsigned short>("age")
})
.prepare(db);
REQUIRE(stmt.is_ok());
tables_to_drop.emplace("person");
auto res = stmt.execute();
auto res = stmt->execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
check_table_exists("person");
@ -716,7 +715,7 @@ TEST_CASE_METHOD(QueryFixture, "Test scalar result", "[query][record][scalar][re
check_table_not_exists("person");
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
})
.execute(db);
REQUIRE(res.is_ok());
@ -725,7 +724,7 @@ TEST_CASE_METHOD(QueryFixture, "Test scalar result", "[query][record][scalar][re
check_table_exists("person");
tables_to_drop.emplace("person");
std::vector<unsigned long> ids({ 1,2,3,4 });
std::vector<uint32_t> ids({ 1,2,3,4 });
for(auto id : ids) {
res = query::insert()
@ -740,33 +739,34 @@ TEST_CASE_METHOD(QueryFixture, "Test scalar result", "[query][record][scalar][re
.from("person")
.order_by("id"_col).asc()
.prepare(db);
REQUIRE(stmt);
auto rows = stmt.fetch();
auto rows = stmt->fetch();
REQUIRE(rows.is_ok());
size_t index{0};
for (const auto &row : *rows) {
REQUIRE(row.at("id").as<unsigned long>() == ids[index]);
REQUIRE(row.at("id").as<uint32_t>() == ids[index]);
++index;
}
REQUIRE(index == 4);
stmt.reset();
stmt->reset();
rows = stmt.fetch();
rows = stmt->fetch();
REQUIRE(rows.is_ok());
index = 0;
for (const auto &row : *rows) {
REQUIRE(row.at("id").as<unsigned long>() == ids[index]);
REQUIRE(row.at("id").as<uint32_t>() == ids[index]);
++index;
}
REQUIRE(index == 4);
stmt.reset();
stmt->reset();
auto row = stmt.fetch_one();
auto row = stmt->fetch_one();
REQUIRE(row.is_ok());
REQUIRE(row->has_value());
REQUIRE(row.value()->at("id").as<unsigned long>() == ids[0]);
REQUIRE(row.value()->at("id").as<uint32_t>() == ids[0]);
}

View File

@ -1,9 +1,10 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/column.hpp"
#include "matador/sql/condition.hpp"
#include "matador/sql/connection.hpp"
#include "matador/sql/query.hpp"
#include "matador/query/condition.hpp"
#include "matador/query/query.hpp"
#include "models/person.hpp"
@ -13,15 +14,18 @@
#include <vector>
using namespace matador::sql;
using namespace matador::object;
using namespace matador::query;
using namespace matador::test;
TEST_CASE_METHOD(QueryFixture, "Test create statement", "[query][statement][create]") {
schema.attach<matador::test::person>("person");
REQUIRE(schema.attach<matador::test::person>("person"));
auto stmt = query::create()
.table<matador::test::person>("person", schema)
.prepare(db);
REQUIRE(stmt);
auto res = stmt.execute();
auto res = stmt->execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
tables_to_drop.emplace("person");
@ -39,12 +43,13 @@ TEST_CASE_METHOD(QueryFixture, "Test create statement", "[query][statement][crea
TEST_CASE_METHOD(QueryFixture, "Test insert statement", "[query][statement][insert]") {
using namespace matador::test;
schema.attach<person>("person");
REQUIRE(schema.attach<person>("person"));
auto stmt = query::create()
.table<person>("person", schema)
.prepare(db);
REQUIRE(stmt);
auto res = stmt.execute();
auto res = stmt->execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
tables_to_drop.emplace("person");
@ -57,8 +62,9 @@ TEST_CASE_METHOD(QueryFixture, "Test insert statement", "[query][statement][inse
.into<person>("person", schema)
.values<person>()
.prepare(db);
REQUIRE(stmt);
res = stmt.bind(george)
res = stmt->bind(george)
.execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
@ -79,12 +85,13 @@ TEST_CASE_METHOD(QueryFixture, "Test update statement", "[query][statement][upda
using namespace matador::test;
using namespace matador::utils;
schema.attach<matador::test::person>("person");
REQUIRE(schema.attach<matador::test::person>("person"));
auto stmt = query::create()
.table<matador::test::person>("person", schema)
.prepare(db);
REQUIRE(stmt);
auto res = stmt.execute();
auto res = stmt->execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
tables_to_drop.emplace("person");
@ -97,8 +104,9 @@ TEST_CASE_METHOD(QueryFixture, "Test update statement", "[query][statement][upda
.into<person>("person", schema)
.values<person>()
.prepare(db);
REQUIRE(stmt);
res = stmt.bind(george)
res = stmt->bind(george)
.execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
@ -120,8 +128,9 @@ TEST_CASE_METHOD(QueryFixture, "Test update statement", "[query][statement][upda
.set<person>()
.where("id"_col == _)
.prepare(db);
REQUIRE(stmt);
res = stmt.bind(george)
res = stmt->bind(george)
.bind(4, george.id)
.execute();
REQUIRE(res.is_ok());
@ -142,12 +151,13 @@ TEST_CASE_METHOD(QueryFixture, "Test update statement", "[query][statement][upda
TEST_CASE_METHOD(QueryFixture, "Test delete statement", "[query][statement][delete]") {
using namespace matador::test;
schema.attach<matador::test::person>("person");
REQUIRE(schema.attach<matador::test::person>("person"));
auto stmt = query::create()
.table<matador::test::person>("person", schema)
.prepare(db);
REQUIRE(stmt);
auto res = stmt.execute();
auto res = stmt->execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
tables_to_drop.emplace("person");
@ -158,6 +168,7 @@ TEST_CASE_METHOD(QueryFixture, "Test delete statement", "[query][statement][dele
.into<person>("person", schema)
.values<person>()
.prepare(db);
REQUIRE(stmt);
std::vector<person> peoples {
{1,"george", 45, {1,2,3,4}},
@ -167,20 +178,20 @@ TEST_CASE_METHOD(QueryFixture, "Test delete statement", "[query][statement][dele
};
for (const auto &p : peoples) {
res = stmt.bind(p)
res = stmt->bind(p)
.execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
stmt.reset();
stmt->reset();
}
auto select_stmt = query::select<person>(schema)
.from("person")
.where("name"_col == matador::utils::_)
.prepare(db);
REQUIRE(select_stmt);
auto rows = select_stmt
.bind(0, "jane")
auto rows = select_stmt->bind(0, "jane")
.fetch<person>();
REQUIRE(rows.is_ok());
@ -196,30 +207,28 @@ TEST_CASE_METHOD(QueryFixture, "Test delete statement", "[query][statement][dele
.from("person")
.where("name"_col == matador::utils::_)
.prepare(db);
REQUIRE(stmt);
res = stmt.bind(0, "jane")
res = stmt->bind(0, "jane")
.execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
select_stmt.reset();
auto row = select_stmt
.bind(0, "jane")
select_stmt->reset();
auto row = select_stmt->bind(0, "jane")
.fetch_one<person>();
REQUIRE(row.is_ok());
REQUIRE(*row == nullptr);
stmt.reset();
res = stmt
.bind(0, "merlin")
stmt->reset();
res = stmt->bind(0, "merlin")
.execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
select_stmt.reset();
row = select_stmt
.bind(0, "merlin")
select_stmt->reset();
row = select_stmt->bind(0, "merlin")
.fetch_one<person>();
REQUIRE(row.is_ok());
@ -229,12 +238,13 @@ TEST_CASE_METHOD(QueryFixture, "Test delete statement", "[query][statement][dele
TEST_CASE_METHOD(QueryFixture, "Test reuse prepared statement", "[query][statement][reuse]") {
using namespace matador::test;
schema.attach<matador::test::person>("person");
REQUIRE(schema.attach<matador::test::person>("person"));
auto stmt = query::create()
.table<matador::test::person>("person", schema)
.prepare(db);
REQUIRE(stmt);
auto res = stmt.execute();
auto res = stmt->execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 0);
tables_to_drop.emplace("person");
@ -245,6 +255,7 @@ TEST_CASE_METHOD(QueryFixture, "Test reuse prepared statement", "[query][stateme
.into<person>("person", schema)
.values<person>()
.prepare(db);
REQUIRE(stmt);
std::vector<person> peoples {
{1,"george", 45, {1,2,3,4}},
@ -254,18 +265,19 @@ TEST_CASE_METHOD(QueryFixture, "Test reuse prepared statement", "[query][stateme
};
for (const auto &p : peoples) {
res = stmt.bind(p)
res = stmt->bind(p)
.execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
stmt.reset();
stmt->reset();
}
stmt = query::select<person>(schema)
.from("person")
.prepare(db);
REQUIRE(stmt);
auto rows = stmt.fetch<person>();
auto rows = stmt->fetch<person>();
REQUIRE(rows.is_ok());
size_t index = 0;
@ -277,9 +289,9 @@ TEST_CASE_METHOD(QueryFixture, "Test reuse prepared statement", "[query][stateme
++index;
}
stmt.reset();
stmt->reset();
rows = stmt.fetch<person>();
rows = stmt->fetch<person>();
REQUIRE(rows.is_ok());
index = 0;

View File

@ -2,6 +2,8 @@
#include "matador/object/attribute_definition.hpp"
#include "matador/sql/column_generator.hpp"
#include "matador/query/condition.hpp"
#include "matador/query/query.hpp"
@ -12,13 +14,16 @@
#include "models/person.hpp"
#include "models/recipe.hpp"
using namespace matador::object;
using namespace matador::query;
using namespace matador::sql;
using namespace matador::test;
using namespace matador::query;
TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") {
schema.attach<airplane>("airplane");
schema.attach<flight>("flight");
auto result = schema.attach<airplane>("airplane")
.and_then( [this] { return schema.attach<flight>("flight");} );
REQUIRE(result.is_ok());
auto res = query::create()
.table<airplane>("airplane", schema)
.execute(db);
@ -38,9 +43,8 @@ TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query
tables_to_drop.emplace("flight");
}
TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[query][where]")
{
schema.attach<person>("person");
TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[query][where]") {
REQUIRE(schema.attach<person>("person"));
auto res = query::create()
.table<person>("person", schema)
.execute(db);
@ -94,11 +98,10 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[q
}
}
TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]")
{
TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]") {
auto res = query::create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_pk_column<uint32_t>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("color", 63)
})
@ -127,7 +130,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]")
REQUIRE(i.size() == 3);
REQUIRE(i.at(0).name() == "id");
REQUIRE(i.at(0).is_integer());
REQUIRE(i.at(0).as<unsigned long>() == 7);
REQUIRE(i.at(0).as<uint32_t>() == 7);
REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).is_varchar());
REQUIRE(i.at(1).as<std::string>() == "george");
@ -137,10 +140,11 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]")
}
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][foreign]")
{
schema.attach<airplane>("airplane");
schema.attach<flight>("flight");
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][foreign]") {
auto result = schema.attach<airplane>("airplane")
.and_then( [this] { return schema.attach<flight>("flight");} );
REQUIRE(result.is_ok());
auto res = query::create()
.table<airplane>("airplane", schema)
.execute(db);
@ -159,10 +163,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
REQUIRE(db.exists("flight"));
tables_to_drop.emplace("flight");
std::vector<matador::object_ptr<airplane>> planes{
matador::object_ptr<airplane>(new airplane{1, "Airbus", "A380"}),
matador::object_ptr<airplane>(new airplane{2, "Boeing", "707"}),
matador::object_ptr<airplane>(new airplane{3, "Boeing", "747"})
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
};
for (const auto &plane: planes) {
@ -194,15 +198,16 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
.fetch_one(db);
REQUIRE(f.is_ok());
REQUIRE(f.value()->at(0).as<unsigned long>() == 4);
REQUIRE(f.value()->at(1).as<unsigned long>() == 2);
REQUIRE(f.value()->at(0).as<uint32_t>() == 4);
REQUIRE(f.value()->at(1).as<uint32_t>() == 2);
REQUIRE(f.value()->at(2).as<std::string>() == "hans");
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left", "[query][foreign][join_left]")
{
schema.attach<airplane>("airplane");
schema.attach<flight>("flight");
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left", "[query][foreign][join_left]") {
auto result = schema.attach<airplane>("airplane")
.and_then( [this] { return schema.attach<flight>("flight");} );
REQUIRE(result.is_ok());
auto res = query::create()
.table<airplane>("airplane", schema)
.execute(db);
@ -221,10 +226,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
REQUIRE(db.exists("flight"));
tables_to_drop.emplace("flight");
std::vector<matador::object_ptr<airplane>> planes{
matador::object_ptr<airplane>(new airplane{1, "Airbus", "A380"}),
matador::object_ptr<airplane>(new airplane{2, "Boeing", "707"}),
matador::object_ptr<airplane>(new airplane{3, "Boeing", "747"})
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
};
for (const auto &plane: planes) {
@ -241,11 +246,11 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
.fetch_value<int>(db).value();
REQUIRE(count == 3);
std::vector<matador::object_ptr<flight>> flights{
matador::object_ptr<flight>(new flight{4, planes.at(0), "hans"}),
matador::object_ptr<flight>(new flight{5, planes.at(0), "otto"}),
matador::object_ptr<flight>(new flight{6, planes.at(1), "george"}),
matador::object_ptr<flight>(new flight{7, planes.at(2), "paul"})
std::vector flights{
object_ptr(new flight{4, planes.at(0), "hans"}),
object_ptr(new flight{5, planes.at(0), "otto"}),
object_ptr(new flight{6, planes.at(1), "george"}),
object_ptr(new flight{7, planes.at(2), "paul"})
};
for (const auto &f: flights) {
@ -262,35 +267,37 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
.fetch_one(db);
REQUIRE(f.is_ok());
REQUIRE(f->has_value());
REQUIRE(f.value()->at(0).as<unsigned long>() == 4);
REQUIRE(f.value()->at(1).as<unsigned long>() == 1);
REQUIRE(f.value()->at(0).as<uint32_t>() == 4);
REQUIRE(f.value()->at(1).as<uint32_t>() == 1);
REQUIRE(f.value()->at(2).as<std::string>() == "hans");
auto result = query::select({"f.id", "ap.brand", "ap.model", "f.pilot_name"})
auto select_result = query::select({"f.id", "ap.brand", "ap.model", "f.pilot_name"})
.from({"flight", "f"})
.join_left({"airplane", "ap"})
.on("f.airplane_id"_col == "ap.id"_col)
.order_by("f.id").asc()
.fetch_all(db);
REQUIRE(result.is_ok());
REQUIRE(select_result.is_ok());
std::vector<std::pair<unsigned long, std::string>> expected_result {
std::vector<std::pair<uint32_t, std::string>> expected_result {
{4, "hans"},
{5, "otto"},
{6, "george"},
{7, "paul"}
};
size_t index{0};
for (const auto &r: *result) {
for (const auto &r: *select_result) {
REQUIRE(r.size() == 4);
REQUIRE(r.at(0).as<unsigned long>() == expected_result[index].first);
REQUIRE(r.at(0).as<uint32_t>() == expected_result[index].first);
REQUIRE(r.at(3).as<std::string>() == expected_result[index++].second);
}
}
TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single entity", "[query][join_left][find]") {
schema.attach<airplane>("airplane");
schema.attach<flight>("flight");
auto result = schema.attach<airplane>("airplane")
.and_then( [this] { return schema.attach<flight>("flight");} );
REQUIRE(result.is_ok());
auto res = query::create()
.table<airplane>("airplane", schema)
.execute(db);
@ -309,10 +316,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(db.exists("flight"));
tables_to_drop.emplace("flight");
std::vector<matador::object_ptr<airplane>> planes{
matador::object_ptr<airplane>(new airplane{1, "Airbus", "A380"}),
matador::object_ptr<airplane>(new airplane{2, "Boeing", "707"}),
matador::object_ptr<airplane>(new airplane{3, "Boeing", "747"})
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
};
for (const auto &plane: planes) {
@ -331,11 +338,11 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(count->has_value());
REQUIRE(count->value() == 3);
std::vector<matador::object_ptr<flight>> flights{
matador::object_ptr<flight>(new flight{4, planes.at(0), "hans"}),
matador::object_ptr<flight>(new flight{5, planes.at(0), "otto"}),
matador::object_ptr<flight>(new flight{6, planes.at(1), "george"}),
matador::object_ptr<flight>(new flight{7, planes.at(2), "paul"})
std::vector flights{
object_ptr(new flight{4, planes.at(0), "hans"}),
object_ptr(new flight{5, planes.at(0), "otto"}),
object_ptr(new flight{6, planes.at(1), "george"}),
object_ptr(new flight{7, planes.at(2), "paul"})
};
for (const auto &f: flights) {
@ -352,11 +359,11 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
.fetch_one(db);
REQUIRE(f.is_ok());
REQUIRE(f->has_value());
REQUIRE(f.value()->at(0).as<unsigned long>() == 4);
REQUIRE(f.value()->at(1).as<unsigned long>() == 1);
REQUIRE(f.value()->at(0).as<uint32_t>() == 4);
REQUIRE(f.value()->at(1).as<uint32_t>() == 1);
REQUIRE(f.value()->at(2).as<std::string>() == "hans");
auto result = query::select({"f.id", "f.airplane_id", "ap.brand", "ap.model", "f.pilot_name"})
auto select_result = query::select({"f.id", "f.airplane_id", "ap.brand", "ap.model", "f.pilot_name"})
.from({"flight", "f"})
.join_left({"airplane", "ap"})
.on("f.airplane_id"_col == "ap.id"_col)
@ -365,20 +372,22 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
auto expected_flight = flights[0];
REQUIRE(result.is_ok());
REQUIRE(*result);
REQUIRE(result.value()->id == expected_flight->id);
REQUIRE(result.value()->pilot_name == expected_flight->pilot_name);
REQUIRE(result.value()->airplane.get());
REQUIRE(result.value()->airplane->id == 1);
REQUIRE(result.value()->airplane->model == "A380");
REQUIRE(result.value()->airplane->brand == "Airbus");
REQUIRE(select_result.is_ok());
REQUIRE(*select_result);
auto f1 = select_result.release();
REQUIRE(f1->id == expected_flight->id);
REQUIRE(f1->pilot_name == expected_flight->pilot_name);
REQUIRE(f1->airplane.get());
REQUIRE(f1->airplane->id == 1);
REQUIRE(f1->airplane->model == "A380");
REQUIRE(f1->airplane->brand == "Airbus");
}
TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship", "[query][join][many_to_many]") {
schema.attach<recipe>("recipes");
schema.attach<ingredient>("ingredients");
schema.attach<recipe_ingredient>("recipe_ingredients");
auto result = schema.attach<recipe>("recipes")
.and_then( [this] { return schema.attach<ingredient>("ingredients"); } )
.and_then( [this] { return schema.attach<recipe_ingredient>("recipe_ingredients"); } );
auto res = query::create()
.table<recipe>("recipes", schema)
.execute(db);
@ -460,14 +469,14 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
REQUIRE(*res == 1);
}
auto result = query::select({"r.id", "r.name", "ri.ingredient_id"})
auto select_result = query::select({"r.id", "r.name", "ri.ingredient_id"})
.from({"recipes", "r"})
.join_left({"recipe_ingredients", "ri"})
.on("r.id"_col == "ri.recipe_id"_col)
.fetch_all(db);
REQUIRE(result.is_ok());
REQUIRE(select_result.is_ok());
std::vector<std::tuple<unsigned long, std::string, unsigned long>> expected_result_one_join {
std::vector<std::tuple<uint32_t, std::string, uint32_t>> expected_result_one_join {
{7, "Apple Crumble", 1},
{7, "Apple Crumble", 4},
{7, "Apple Crumble", 5},
@ -478,22 +487,22 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
{9, "Fruit Salad", 3}
};
size_t index{0};
for (const auto &r: *result) {
for (const auto &r: *select_result) {
REQUIRE(r.size() == 3);
REQUIRE(r.at(0).as<unsigned long>().value() == std::get<0>(expected_result_one_join[index]));
REQUIRE(r.at(0).as<uint32_t>().value() == std::get<0>(expected_result_one_join[index]));
REQUIRE(r.at(1).as<std::string>().value() == std::get<1>(expected_result_one_join[index]));
REQUIRE(r.at(2).as<unsigned long>().value() == std::get<2>(expected_result_one_join[index]));
REQUIRE(r.at(2).as<uint32_t>().value() == std::get<2>(expected_result_one_join[index]));
++index;
}
result = query::select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
select_result = query::select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
.from({"recipes", "r"})
.join_left({"recipe_ingredients", "ri"}).on("r.id"_col == "ri.recipe_id"_col)
.join_left({"ingredients", "i"}).on("ri.ingredient_id"_col == "i.id"_col)
.fetch_all(db);
REQUIRE(result.is_ok());
std::vector<std::tuple<unsigned long, std::string, unsigned long, std::string>> expected_result_two_joins {
std::vector<std::tuple<uint32_t, std::string, uint32_t, std::string>> expected_result_two_joins {
{7, "Apple Crumble", 1, "Apple"},
{7, "Apple Crumble", 4, "Sugar"},
{7, "Apple Crumble", 5, "Flour"},
@ -504,16 +513,16 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
{9, "Fruit Salad", 3, "Pineapple"}
};
index = 0;
for (const auto &r: *result) {
for (const auto &r: *select_result) {
REQUIRE(r.size() == 4);
REQUIRE(r.at(0).as<unsigned long>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(r.at(0).as<uint32_t>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(r.at(1).as<std::string>().value() == std::get<1>(expected_result_two_joins[index]));
REQUIRE(r.at(2).as<unsigned long>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(r.at(2).as<uint32_t>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(r.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
}
result = query::select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
select_result = query::select({"r.id", "r.name", "ri.ingredient_id", "i.name"})
.from({"recipes", "r"})
.join_left({"recipe_ingredients", "ri"}).on("r.id"_col == "ri.recipe_id"_col)
.join_left({"ingredients", "i"}).on("ri.ingredient_id"_col == "i.id"_col)
@ -522,11 +531,11 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
REQUIRE(result.is_ok());
index = 3;
for (const auto &r: *result) {
for (const auto &r: *select_result) {
REQUIRE(r.size() == 4);
REQUIRE(r.at(0).as<unsigned long>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(r.at(0).as<uint32_t>().value() == std::get<0>(expected_result_two_joins[index]));
REQUIRE(r.at(1).as<std::string>().value() == std::get<1>(expected_result_two_joins[index]));
REQUIRE(r.at(2).as<unsigned long>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(r.at(2).as<uint32_t>().value() == std::get<2>(expected_result_two_joins[index]));
REQUIRE(r.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
}

View File

@ -7,52 +7,62 @@
#include "models/book.hpp"
#include "models/flight.hpp"
#include <iostream>
using namespace matador;
using namespace matador::object;
using namespace matador::test;
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
ses.attach<airplane>("airplanes");
ses.attach<flight>("flights");
ses.create_schema();
const auto result = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.attach<flight>("flights"); } )
.and_then([this] { return ses.create_schema(); } );
REQUIRE(result.is_ok());
tables_to_drop.emplace("airplanes");
tables_to_drop.emplace("flights");
auto plane = ses.insert<airplane>(1, "Boeing", "A380");
auto f = ses.insert<flight>(2, plane, "sully");
REQUIRE(plane.is_ok());
auto f = ses.insert<flight>(2, *plane, "sully");
REQUIRE(f.is_ok());
const auto result = ses.find<flight>(2);
REQUIRE(result.is_ok());
REQUIRE(result->get()->id == f->id);
REQUIRE(result->get()->pilot_name == f->pilot_name);
REQUIRE(result->get()->airplane);
REQUIRE(result->get()->airplane->id == plane->id);
REQUIRE(result->get()->airplane->brand == plane->brand);
REQUIRE(result->get()->airplane->model == plane->model);
const auto res = ses.find<flight>(2);
REQUIRE(res.is_ok());
auto rf = *res;
REQUIRE(rf->id == (*f)->id);
REQUIRE(rf->pilot_name == (*f)->pilot_name);
REQUIRE(rf->airplane);
REQUIRE(rf->airplane->id == (*plane)->id);
REQUIRE(rf->airplane->brand == (*plane)->brand);
REQUIRE(rf->airplane->model == (*plane)->model);
}
TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session][find]") {
ses.attach<airplane>("airplanes");
ses.create_schema();
const auto result = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.create_schema(); } );
REQUIRE(result.is_ok());
tables_to_drop.emplace("airplanes");
auto a380 = ses.insert<airplane>(1, "Boeing", "A380");
REQUIRE(a380.is_ok());
auto result = ses.find<airplane>(2);
auto res = ses.find<airplane>(2);
REQUIRE(!result.is_ok());
REQUIRE((result.err().ec() == sql::session_error_code::FailedToFindObject));
REQUIRE((result.err().ec() == orm::error_code::FailedToFindObject));
result = ses.find<airplane>(1);
auto find_result = ses.find<airplane>(1);
REQUIRE(result);
auto read_a380 = result.value();
REQUIRE(a380->id == read_a380->id);
REQUIRE(find_result);
auto read_a380 = find_result.value();
REQUIRE((*a380)->id == read_a380->id);
}
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][find]") {
ses.attach<airplane>("airplanes");
ses.create_schema();
const auto result = ses.attach<airplane>("airplanes")
.and_then([this] { return ses.create_schema(); } );
REQUIRE(result.is_ok());
tables_to_drop.emplace("airplanes");
@ -62,18 +72,19 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][f
planes.emplace_back(new airplane(3, "Boeing", "747"));
for (auto &&plane: planes) {
ses.insert(plane.release());
auto res = ses.insert(plane.release());
REQUIRE(res.is_ok());
}
auto result = ses.find<airplane>();
auto find_result = ses.find<airplane>();
std::vector<std::tuple<unsigned long, std::string, std::string>> expected_result {
{1, "Airbus", "A380"},
{2, "Boeing", "707"},
{3, "Boeing", "747"}
};
REQUIRE(result);
auto all_planes = result.release();
REQUIRE(find_result);
auto all_planes = find_result.release();
size_t index {0};
for (const auto &i: all_planes) {
REQUIRE(i.id == std::get<0>(expected_result[index]));
@ -84,25 +95,25 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][f
}
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many relation", "[session][find][one-to-many]") {
ses.attach<author>("authors");
ses.attach<book>("books");
auto result = ses.attach<author>("authors")
.and_then( [this] { return ses.attach<book>("books"); } )
.and_then( [this] { return ses.create_schema(); } );
tables_to_drop.emplace("authors");
tables_to_drop.emplace("books");
ses.create_schema();
std::vector<std::unique_ptr<author>> authors;
authors.emplace_back(new author{1, "Michael", "Crichton", "23.10.1942", 1975, true, {}});
authors.emplace_back(new author{ 2, "Steven", "King", "21.9.1947", 1956, false, {}});
for (auto &&a: authors) {
ses.insert(a.release());
auto res = ses.insert(a.release());
REQUIRE(res.is_ok());
}
auto result = ses.find<author>();
REQUIRE(result.is_ok());
auto all_authors = result.release();
auto find_result = ses.find<author>();
REQUIRE(find_result.is_ok());
auto all_authors = find_result.release();
std::vector<object_ptr<author>> author_repo;
for (auto it = all_authors.begin(); it != all_authors.end(); ++it) {
std::cout << "author: " << it->first_name << " (books: " << it->books.size() << ")\n";
@ -123,13 +134,14 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
books.emplace_back( new book{12, "The Dark Tower: The Gunslinger", author_repo[1], 1982} );
for (auto &&b: books) {
ses.insert(b.release());
auto res = ses.insert(b.release());
REQUIRE(res.is_ok());
}
result = ses.find<author>();
REQUIRE(result);
find_result = ses.find<author>();
REQUIRE(find_result);
all_authors = result.release();
all_authors = find_result.release();
for (auto it = all_authors.begin(); it != all_authors.end(); ++it) {
std::cout << "author: " << it->first_name << " (books: " << it->books.size() << ")\n";
}

View File

@ -2,8 +2,9 @@
#include "matador/sql/connection_info.hpp"
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/session.hpp"
#include "matador/sql/statement_cache.hpp"
#include "matador/orm/session.hpp"
// #include "matador/sql/statement_cache.hpp"
#include "connection.hpp"
#include "matador/sql/dialect_builder.hpp"
@ -14,13 +15,13 @@ class StatementCacheFixture
{
public:
StatementCacheFixture()
: pool(matador::test::connection::dns, 4), ses(pool)
: pool(test::connection::dns, 4), ses(pool)
{}
~StatementCacheFixture() = default;
protected:
matador::sql::connection_pool<matador::sql::connection> pool;
matador::sql::session ses;
sql::connection_pool<matador::sql::connection> pool;
orm::session ses;
};
TEST_CASE_METHOD(StatementCacheFixture, "Acquire prepared statement", "[statement cache]") {

View File

@ -1,9 +1,12 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/column_definition.hpp"
#include "matador/sql/condition.hpp"
#include "matador/object/attribute_definition.hpp"
#include "matador/sql/connection.hpp"
#include "matador/sql/query.hpp"
#include "matador/sql/column_generator.hpp"
#include "matador/query/condition.hpp"
#include "matador/query/query.hpp"
#include "matador/object/object_ptr.hpp"
@ -12,6 +15,8 @@
#include "models/airplane.hpp"
using namespace matador::sql;
using namespace matador::object;
using namespace matador::query;
using namespace matador::test;
namespace matador::test::detail {
@ -35,29 +40,29 @@ public:
}
protected:
std::vector<matador::object_ptr<airplane>> planes{
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")
};
};
TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]")
{
TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]") {
using namespace matador::utils;
schema.attach<airplane>("airplane");
REQUIRE(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);
REQUIRE(stmt.is_ok());
for (const auto &plane: planes) {
auto res = stmt.bind(*plane).execute();
auto res = stmt->bind(*plane).execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
stmt.reset();
stmt->reset();
}
auto result = query::select(column_generator::generate<airplane>(schema, true))
@ -87,8 +92,9 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
.from(ap)
.where("brand"_col == _)
.prepare(db);
REQUIRE(stmt.is_ok());
auto result = stmt.bind(0, "Airbus")
auto result = stmt->bind(0, "Airbus")
.fetch<airplane>();
REQUIRE(result.is_ok());
@ -98,9 +104,9 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
REQUIRE(i.model == planes[0]->model);
}
stmt.reset();
stmt->reset();
result = stmt.bind(0, "Boeing")
result = stmt->bind(0, "Boeing")
.fetch<airplane>();
size_t index{1};

View File

@ -4,21 +4,21 @@
#include "matador/sql/connection.hpp"
#include "matador/sql/column_generator.hpp"
#include "matador/sql/query.hpp"
#include "matador/query/query.hpp"
#include "QueryFixture.hpp"
#include "models/location.hpp"
using namespace matador::query;
using namespace matador::sql;
using namespace matador::test;
class TypeTraitsTestFixture : public QueryFixture
{
public:
TypeTraitsTestFixture()
{
db.open();
TypeTraitsTestFixture() {
REQUIRE(db.open());
const auto res = query::create()
.table<location>("location", schema)
.execute(db);
@ -26,9 +26,8 @@ public:
}
};
TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with type traits", "[typetraits]")
{
schema.attach<location>("location");
TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with type traits", "[typetraits]") {
REQUIRE(schema.attach<location>("location"));
SECTION("Insert and select with direct execution") {
location loc{1, "center", {1, 2, 3}, Color::Black};
@ -58,9 +57,8 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
.into("location", column_generator::generate<location>(schema, true))
.values<location>()
.prepare(db);
auto res = stmt
.bind(loc)
REQUIRE(stmt);
auto res = stmt->bind(loc)
.execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 1);

View File

@ -14,8 +14,8 @@ struct author;
struct book {
unsigned int id{};
matador::object::object_ptr<author> book_author;
std::string title;
object::object_ptr<author> book_author;
unsigned short published_in{};
template<typename Operator>

View File

@ -21,7 +21,7 @@ struct flight
: id(id), airplane(plane), pilot_name(std::move(name)) {}
unsigned int id{};
object::object_ptr<test::airplane> airplane;
object::object_ptr<airplane> airplane;
std::string pilot_name;
template<class Operator>

30
test/models/person.hpp Normal file
View File

@ -0,0 +1,30 @@
#ifndef QUERY_PERSON_HPP
#define QUERY_PERSON_HPP
#include "matador/utils/access.hpp"
#include "matador/utils/field_attributes.hpp"
#include "matador/utils/types.hpp"
#include <string>
namespace matador::test {
struct person {
unsigned int id{};
std::string name;
unsigned int age{};
utils::blob image;
template<class Operator>
void process(Operator &op) {
namespace field = matador::access;
using namespace matador::utils;
field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255);
field::attribute(op, "age", age);
field::attribute(op, "image", image);
}
};
}
#endif //QUERY_PERSON_HPP

View File

@ -11,15 +11,13 @@ struct types
enum { CSTR_LEN=255 };
unsigned int id_ = 0;
char char_ = 'c';
int8_t char_ = 'c';
short short_ = -127;
int int_ = -65000;
long long_ = -128000;
long long long64_ = -1234567890;
unsigned char unsigned_char_ = 'H';
unsigned short unsigned_short_ = 128;
unsigned int unsigned_int_ = 65000;
unsigned long unsigned_long_ = 128000;
unsigned long long unsigned_long64_ = 1234567890;
float float_ = 3.1415f;
double double_ = 1.1414;
@ -42,19 +40,17 @@ struct types
field::attribute(op, "val_double", double_);
field::attribute(op, "val_short", short_);
field::attribute(op, "val_int", int_);
field::attribute(op, "val_long", long_);
field::attribute(op, "val_long_long", long64_);
field::attribute(op, "val_unsigned_char", unsigned_char_);
field::attribute(op, "val_unsigned_short", unsigned_short_);
field::attribute(op, "val_unsigned_int", unsigned_int_);
field::attribute(op, "val_unsigned_long", unsigned_long_);
field::attribute(op, "val_unsigned_long_long", unsigned_long64_);
field::attribute(op, "val_bool", bool_);
field::attribute(op, "val_cstr", cstr_, CSTR_LEN);
field::attribute(op, "val_string", string_);
field::attribute(op, "val_varchar", varchar_, 63);
field::attribute(op, "val_date", date_);
field::attribute(op, "val_time", time_);
// field::attribute(op, "val_date", date_);
// field::attribute(op, "val_time", time_);
field::attribute(op, "val_binary", binary_);
}
};