session and insert query builder progress
This commit is contained in:
parent
d734d647ed
commit
f144b26dbb
|
|
@ -114,10 +114,24 @@ utils::result<object::object_ptr<Type>, utils::error> session::insert(object::ob
|
|||
|
||||
// Execute all steps; for Identity steps read RETURNING and write pk back into the object
|
||||
for (query::insert_step &step : *steps) {
|
||||
if (step.pk_generator == utils::generator_type::Sequence || step.pk_generator == utils::generator_type::Table) {
|
||||
auto result = it->second.pk_generator().next_id(*this);
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
step.apply_primary_key(utils::identifier{*result});
|
||||
}
|
||||
|
||||
auto stmt = cache_.acquire(step.ctx);
|
||||
if (!stmt.is_ok()) {
|
||||
return utils::failure(stmt.err());
|
||||
}
|
||||
|
||||
auto result = step.acquire_and_bind(cache_);
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
if (step.pk_generator == utils::generator_type::Identity) {
|
||||
// insert and read RETURNING
|
||||
auto record = stmt->fetch_one();
|
||||
|
|
@ -128,20 +142,7 @@ utils::result<object::object_ptr<Type>, utils::error> session::insert(object::ob
|
|||
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to insert object and retrieve identity."));
|
||||
}
|
||||
step.apply_returning(*record.value());
|
||||
} else if (step.pk_generator == utils::generator_type::Sequence || step.pk_generator == utils::generator_type::Table) {
|
||||
auto result = it->second.pk_generator().next_id(*this);
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
step.apply_primary_key(utils::identifier{*result});
|
||||
}
|
||||
|
||||
auto result = step.acquire_and_bind(cache_);
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
if (const auto exec_result = result->execute(); !exec_result.is_ok()) {
|
||||
} else if (const auto exec_result = result->execute(); !exec_result.is_ok()) {
|
||||
return utils::failure(exec_result.err());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ public:
|
|||
visited_.clear();
|
||||
|
||||
ptr_ = ptr;
|
||||
build_for(ptr);
|
||||
build_for(ptr, steps_);
|
||||
ptr_.reset();
|
||||
|
||||
// relation inserts must run after all entity inserts were collected
|
||||
|
|
@ -119,9 +119,8 @@ public:
|
|||
has_many_linker<ObjectType> linker(ptr_, join_column);
|
||||
for (auto &obj : objects) {
|
||||
if (obj.is_transient()) {
|
||||
build_for(obj);
|
||||
build_for(obj, relation_steps_);
|
||||
}
|
||||
// obj.is_persistent() ? build_for(obj) : on_foreign_object(obj, attr);
|
||||
|
||||
access::process(linker, *obj);
|
||||
}
|
||||
|
|
@ -154,7 +153,7 @@ public:
|
|||
// Ensure target exists as dependency (deps first)
|
||||
if (!obj.is_persistent()) {
|
||||
using dep_t = std::remove_reference_t<decltype(*obj)>;
|
||||
build_for<dep_t>(obj);
|
||||
build_for<dep_t>(obj, steps_);
|
||||
}
|
||||
|
||||
// Extract FK value from the foreign object
|
||||
|
|
@ -192,7 +191,7 @@ private:
|
|||
};
|
||||
|
||||
template<class EntityType>
|
||||
void build_for(const object::object_ptr<EntityType> &ptr) {
|
||||
void build_for(const object::object_ptr<EntityType> &ptr, std::vector<insert_step> &steps) {
|
||||
if (!ptr) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -239,7 +238,7 @@ private:
|
|||
step.pk_accessor.set(*ptr, id);
|
||||
};
|
||||
}
|
||||
steps_.push_back(std::move(step));
|
||||
steps.push_back(std::move(step));
|
||||
}
|
||||
|
||||
template<class Pointer>
|
||||
|
|
@ -254,7 +253,7 @@ private:
|
|||
}
|
||||
|
||||
using dep_t = std::remove_reference_t<decltype(*obj)>;
|
||||
build_for<dep_t>(obj);
|
||||
build_for<dep_t>(obj, steps_);
|
||||
}
|
||||
private:
|
||||
const basic_schema &schema_;
|
||||
|
|
|
|||
Loading…
Reference in New Issue