Compare commits
3 Commits
54e6786ceb
...
d9f9712487
| Author | SHA1 | Date |
|---|---|---|
|
|
d9f9712487 | |
|
|
c1620d620e | |
|
|
cf977a32aa |
|
|
@ -328,7 +328,7 @@ utils::result<object::object_ptr<Type>, utils::error> session::find(const Primar
|
|||
"Failed to build query for type " + info.name() + "."));
|
||||
}
|
||||
|
||||
auto res = build_select_query(data.release()).prepare(*this);
|
||||
auto res = data->prepare(*this);
|
||||
|
||||
if (!res) {
|
||||
return utils::failure(res.err());
|
||||
|
|
@ -357,7 +357,7 @@ utils::result<sql::query_result<Type>, utils::error> session::find(query::criter
|
|||
"Failed to build query for type " + it->second.name() + "."));
|
||||
}
|
||||
|
||||
auto result = build_select_query(data.release()).prepare(*this);
|
||||
auto result = data->prepare(*this);
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include "matador/query/basic_schema.hpp"
|
||||
#include "matador/query/intermediates/executable_query.hpp"
|
||||
#include "matador/query/query.hpp"
|
||||
#include "matador/query/query_builder_exception.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
class insert_query_builder {
|
||||
|
|
@ -10,25 +12,22 @@ public:
|
|||
explicit insert_query_builder(const basic_schema &schema);
|
||||
|
||||
template<class EntityType>
|
||||
utils::result<executable_query, utils::error> build(const object::object_ptr<EntityType> &ptr) {
|
||||
utils::result<std::vector<executable_query>, query_build_error> build(const object::object_ptr<EntityType> &ptr) {
|
||||
const auto it = schema_.find(typeid(EntityType));
|
||||
if (it == schema_.end()) {
|
||||
return utils::failure(query_build_error::UnknownType);
|
||||
}
|
||||
|
||||
executable_query q = query::query::insert().into(it->second.table()).values(*ptr);
|
||||
return utils::ok(std::vector{q});
|
||||
}
|
||||
|
||||
template < class V >
|
||||
void on_primary_key(const char *id, V &, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {
|
||||
push(id);
|
||||
if (!is_root_entity()) {
|
||||
return;
|
||||
}
|
||||
entity_query_data_.pk_column_name = id;
|
||||
}
|
||||
|
||||
void on_revision(const char *id, uint64_t &/*rev*/);
|
||||
static void on_primary_key(const char *id, V &, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {}
|
||||
static void on_revision(const char *id, uint64_t &/*rev*/);
|
||||
|
||||
template<typename Type>
|
||||
void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes)
|
||||
{
|
||||
push(id);
|
||||
}
|
||||
static void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
||||
|
||||
template<class Pointer>
|
||||
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||
|
|
|
|||
|
|
@ -249,7 +249,7 @@ public:
|
|||
);
|
||||
}
|
||||
|
||||
const select_query_data &query_data() const;
|
||||
[[nodiscard]] const select_query_data &query_data() const;
|
||||
|
||||
private:
|
||||
template<class Pointer>
|
||||
|
|
|
|||
|
|
@ -93,7 +93,9 @@ public:
|
|||
* @return True if the object matches the type, false otherwise.
|
||||
*/
|
||||
template<typename MessageType>
|
||||
[[nodiscard]] bool is() const { return type_ == std::type_index(typeid(MessageType)); }
|
||||
[[nodiscard]] bool is() const {
|
||||
return type_ == std::type_index(typeid(MessageType));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Accesses the stored object as a typed reference.
|
||||
|
|
@ -105,10 +107,14 @@ public:
|
|||
*/
|
||||
template<typename MessageType>
|
||||
const MessageType& get() const {
|
||||
if (!is<MessageType>()) throw std::bad_cast();
|
||||
const void* p = raw_ptr();
|
||||
if (!p) throw std::runtime_error("AnyMessage: empty pointer");
|
||||
return *static_cast<const MessageType*>(p);
|
||||
if (!is<MessageType>()) {
|
||||
throw std::bad_cast();
|
||||
}
|
||||
const void* ptr = raw_ptr();
|
||||
if (!ptr) {
|
||||
throw std::runtime_error("AnyMessage: empty pointer");
|
||||
}
|
||||
return *static_cast<const MessageType*>(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -209,26 +215,24 @@ public:
|
|||
* @return A subscription object to manage the handler's registration.
|
||||
*/
|
||||
template<typename MessageType>
|
||||
subscription subscribe(std::function<void(const MessageType&)> handler,
|
||||
Filter<MessageType> filter = nullptr)
|
||||
{
|
||||
subscription subscribe(std::function<void(const MessageType&)> handler, Filter<MessageType> filter = nullptr) {
|
||||
auto id = next_id_.fetch_add(1, std::memory_order_relaxed);
|
||||
std::unique_lock writeLock(mutex_);
|
||||
std::unique_lock write_lock(mutex_);
|
||||
|
||||
auto &vec = handlers_[std::type_index(typeid(MessageType))];
|
||||
Entry e;
|
||||
e.id = id;
|
||||
e.handler = [h = std::move(handler)](const void* p) {
|
||||
Entry entry;
|
||||
entry.id = id;
|
||||
entry.handler = [h = std::move(handler)](const void* p) {
|
||||
h(*static_cast<const MessageType*>(p));
|
||||
};
|
||||
if (filter) {
|
||||
e.filter = [f = std::move(filter)](const void* p) -> bool {
|
||||
entry.filter = [f = std::move(filter)](const void* p) -> bool {
|
||||
return f(*static_cast<const MessageType*>(p));
|
||||
};
|
||||
} else {
|
||||
e.filter = nullptr;
|
||||
entry.filter = nullptr;
|
||||
}
|
||||
vec.emplace_back(std::move(e));
|
||||
vec.emplace_back(std::move(entry));
|
||||
return {this, std::type_index(typeid(MessageType)), id};
|
||||
}
|
||||
|
||||
|
|
@ -245,8 +249,7 @@ public:
|
|||
template<typename MessageType, typename CallerClass>
|
||||
subscription subscribe(CallerClass* instance,
|
||||
MemberHandler<MessageType, CallerClass> memberFn,
|
||||
Filter<MessageType> filter = nullptr)
|
||||
{
|
||||
Filter<MessageType> filter = nullptr) {
|
||||
auto fn = [instance, memberFn](const MessageType& m) { (instance->*memberFn)(m); };
|
||||
return subscribe<MessageType>(std::function<void(const MessageType&)>(fn), std::move(filter));
|
||||
}
|
||||
|
|
@ -257,29 +260,28 @@ public:
|
|||
* @tparam MessageType The type of the message to subscribe to.
|
||||
* @tparam CallerClass The class of the shared_ptr instance.
|
||||
* @param instance A shared pointer to the instance.
|
||||
* @param memberFn A pointer to the member function to execute.
|
||||
* @param member_func A pointer to the member function to execute.
|
||||
* @param filter An optional filter function.
|
||||
* @return A subscription object to manage the handler's registration.
|
||||
*/
|
||||
template<typename MessageType, typename CallerClass>
|
||||
subscription subscribe(std::shared_ptr<CallerClass> instance,
|
||||
MemberHandler<MessageType, CallerClass> memberFn,
|
||||
Filter<MessageType> filter = nullptr)
|
||||
{
|
||||
std::weak_ptr<CallerClass> w = instance;
|
||||
auto handler = [w, memberFn](const MessageType& m) {
|
||||
if (auto s = w.lock()) {
|
||||
(s.get()->*memberFn)(m);
|
||||
MemberHandler<MessageType, CallerClass> member_func,
|
||||
Filter<MessageType> filter = nullptr) {
|
||||
std::weak_ptr<CallerClass> caller_ptr = instance;
|
||||
auto handler = [caller_ptr, member_func](const MessageType& msg) {
|
||||
if (auto caller = caller_ptr.lock()) {
|
||||
(caller.get()->*member_func)(msg);
|
||||
}
|
||||
};
|
||||
|
||||
std::function<bool(const MessageType&)> local_filter = nullptr;
|
||||
if (filter) {
|
||||
local_filter = [w, filter = std::move(filter)](const MessageType& m) -> bool {
|
||||
if (w.expired()) {
|
||||
local_filter = [caller_ptr, filter = std::move(filter)](const MessageType& msg) -> bool {
|
||||
if (caller_ptr.expired()) {
|
||||
return false;
|
||||
}
|
||||
return filter(m);
|
||||
return filter(msg);
|
||||
};
|
||||
}
|
||||
return subscribe<MessageType>(std::move(handler), std::move(local_filter));
|
||||
|
|
@ -321,7 +323,7 @@ public:
|
|||
void publish(const MessageType& msg) const {
|
||||
std::vector<Entry> snapshot;
|
||||
{
|
||||
std::shared_lock readLock(mutex_);
|
||||
std::shared_lock read_lock(mutex_);
|
||||
const auto it = handlers_.find(std::type_index(typeid(MessageType)));
|
||||
if (it == handlers_.end()) {
|
||||
return;
|
||||
|
|
@ -329,7 +331,9 @@ public:
|
|||
snapshot = it->second; // copy list to avoid holding lock during callbacks
|
||||
}
|
||||
for (const auto &e : snapshot) {
|
||||
if (!e.filter || e.filter(&msg)) e.handler(&msg);
|
||||
if (!e.filter || e.filter(&msg)) {
|
||||
e.handler(&msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ add_library(matador-orm STATIC
|
|||
../../include/matador/query/criteria_evaluator.hpp
|
||||
../../include/matador/query/fk_value_extractor.hpp
|
||||
../../include/matador/query/generator.hpp
|
||||
../../include/matador/query/insert_query_builder.hpp
|
||||
../../include/matador/query/intermediates/executable_query.hpp
|
||||
../../include/matador/query/intermediates/fetchable_query.hpp
|
||||
../../include/matador/query/intermediates/query_alter_intermediate.hpp
|
||||
|
|
@ -115,6 +116,7 @@ add_library(matador-orm STATIC
|
|||
query/criteria/not_criteria.cpp
|
||||
query/criteria_evaluator.cpp
|
||||
query/generator.cpp
|
||||
query/insert_query_builder.cpp
|
||||
query/intermediates/executable_query.cpp
|
||||
query/intermediates/fetchable_query.cpp
|
||||
query/intermediates/query_alter_intermediate.cpp
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
#include "matador/query/insert_query_builder.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
insert_query_builder::insert_query_builder(const basic_schema& schema)
|
||||
: schema_(schema) {}
|
||||
|
||||
void insert_query_builder::on_revision(const char* /*id*/, uint64_t&) {}
|
||||
} // namespace matador::query
|
||||
|
|
@ -17,6 +17,7 @@ add_executable(OrmTests
|
|||
query/ColumnGeneratorTest.cpp
|
||||
query/CriteriaTests.cpp
|
||||
query/GeneratorTests.cpp
|
||||
query/InsertQueryBuilderTest.cpp
|
||||
query/QueryBuilderTest.cpp
|
||||
query/QueryFixture.cpp
|
||||
query/QueryFixture.hpp
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include "matador/sql/backend_provider.hpp"
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/interface/connection_impl.hpp"
|
||||
|
||||
#include "matador/query/schema.hpp"
|
||||
|
||||
#include "matador/query/insert_query_builder.hpp"
|
||||
|
||||
#include "../backend/test_backend_service.hpp"
|
||||
|
||||
#include "../../models/airplane.hpp"
|
||||
#include "../../models/flight.hpp"
|
||||
|
||||
using namespace matador::object;
|
||||
using namespace matador::sql;
|
||||
using namespace matador::query;
|
||||
using namespace matador::utils;
|
||||
|
||||
TEST_CASE("insert query builder test", "[query][insert_query_builder]") {
|
||||
using namespace matador::test;
|
||||
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
||||
connection db("noop://noop.db");
|
||||
|
||||
schema scm;
|
||||
auto result = scm.attach<airplane>("airplanes")
|
||||
.and_then( [&scm] { return scm.attach<flight>("flights"); } );
|
||||
REQUIRE(result);
|
||||
|
||||
insert_query_builder iqb(scm);
|
||||
|
||||
const auto a380 = object_ptr(std::make_shared<airplane>(1, "Boeing", "A380" ));
|
||||
auto build_result = iqb.build<airplane>(a380);
|
||||
REQUIRE(build_result.is_ok());
|
||||
|
||||
const auto& stmts = *build_result;
|
||||
REQUIRE(stmts.size() == 1);
|
||||
|
||||
const auto sql = stmts.front().str(db);
|
||||
REQUIRE(sql == R"(INSERT INTO "airplanes" ("id", "brand", "model") VALUES (1, 'Boeing', 'A380'))");
|
||||
}
|
||||
Loading…
Reference in New Issue