insert_query_builder progress

This commit is contained in:
2026-02-17 16:19:14 +01:00
parent 5668f1060b
commit 38dc42ade8
3 changed files with 173 additions and 4 deletions
+46
View File
@@ -96,6 +96,52 @@ private:
const std::unordered_map<std::string, sql::statement> &statements_per_column_;
};
/*
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);
} */
class session final : public sql::executor {
public:
session(session_context &&ctx, const query::schema &scm);