primary key generator progress: added primary_key_generator_finder
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "matador/query/select_query_builder.hpp"
|
||||
#include "matador/query/criteria.hpp"
|
||||
#include "matador/query/insert_query_builder.hpp"
|
||||
#include "matador/query/query.hpp"
|
||||
#include "matador/query/generator.hpp"
|
||||
#include "matador/query/schema.hpp"
|
||||
@@ -153,12 +154,8 @@ public:
|
||||
* @param obj Object to insert
|
||||
* @return Inserted object
|
||||
*/
|
||||
// template<typename Type>
|
||||
// utils::result<object::object_ptr<Type>, utils::error> insert(Type *obj);
|
||||
template<typename Type>
|
||||
utils::result<object::object_ptr<Type>, utils::error> insert(object::object_ptr<Type> obj);
|
||||
// template<class Type, typename... Args>
|
||||
// utils::result<object::object_ptr<Type>, utils::error> insert(Args &&... args);
|
||||
|
||||
template<typename Type>
|
||||
utils::result<object::object_ptr<Type>, utils::error> update(const object::object_ptr<Type> &obj);
|
||||
@@ -205,6 +202,54 @@ private:
|
||||
|
||||
template<typename Type>
|
||||
utils::result<object::object_ptr<Type>, utils::error> session::insert(object::object_ptr<Type> obj) {
|
||||
const auto it = schema_.find(typeid(Type));
|
||||
if (it == schema_.end()) {
|
||||
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
}
|
||||
|
||||
// Build dependency-ordered insert steps (deps first, root last)
|
||||
query::insert_query_builder iqb(schema_);
|
||||
auto steps = iqb.build(obj);
|
||||
if (!steps.is_ok()) {
|
||||
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build insert dependency queries."));
|
||||
}
|
||||
|
||||
// Execute all steps; for Identity steps read RETURNING and write pk back into the object
|
||||
for (auto &step : *steps) {
|
||||
if (!step.has_returning) {
|
||||
const auto exec_res = step.query.execute(*this);
|
||||
if (!exec_res.is_ok()) {
|
||||
return utils::failure(exec_res.err());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
auto stmt_res = step.query.prepare(*this);
|
||||
if (!stmt_res.is_ok()) {
|
||||
return utils::failure(stmt_res.err());
|
||||
}
|
||||
|
||||
// RETURNING produces a result set; fetch the first row (single-row insert)
|
||||
auto rec_res = stmt_res->fetch_one(); // const overload => std::optional<sql::record>
|
||||
if (!rec_res.is_ok()) {
|
||||
return utils::failure(rec_res.err());
|
||||
}
|
||||
if (!rec_res.value().has_value()) {
|
||||
return utils::failure(make_error(error_code::FailedToFindObject, "INSERT ... RETURNING did not return a row."));
|
||||
}
|
||||
|
||||
if (step.apply_returning) {
|
||||
step.apply_returning(*rec_res.value());
|
||||
}
|
||||
}
|
||||
|
||||
return utils::ok(obj);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const auto it = schema_.find(typeid(Type));
|
||||
if (it == schema_.end()) {
|
||||
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
|
||||
Reference in New Issue
Block a user