Implement headers in mgp

This commit is contained in:
Josip Mrden 2024-02-28 17:08:17 +01:00
parent 112c4528d3
commit 8fdc199d2b
5 changed files with 39 additions and 7 deletions

View File

@ -855,4 +855,8 @@ inline mgp_query_execution_result *execute_query(mgp_graph *graph, const char *q
return MgInvoke<mgp_query_execution_result *>(mgp_execute_query, graph, memory, query);
}
inline std::vector<std::string> get_query_execution_headers(mgp_query_execution_result *query_execution) {
return MgInvoke<std::vector<std::string>>(mgp_get_query_execution_headers, query_execution);
}
} // namespace mgp

View File

@ -1806,6 +1806,11 @@ struct mgp_query_execution_result;
enum mgp_error mgp_execute_query(mgp_graph *graph, struct mgp_memory *memory, const char *query,
struct mgp_query_execution_result **result);
struct mgp_query_execution_headers;
enum mgp_error mgp_get_query_execution_headers(struct mgp_query_execution_result *query_execution,
struct mgp_query_execution_headers **result);
/// @}
#ifdef __cplusplus

View File

@ -1566,6 +1566,7 @@ class QueryExecutionResult {
public:
QueryExecutionResult(mgp_query_execution_result *result);
std::vector<std::string> Headers() const;
};
enum class ProcedureType : uint8_t {
@ -4312,6 +4313,10 @@ inline QueryExecutionResult QueryExecution::ExecuteQuery(std::string_view query)
inline QueryExecutionResult::QueryExecutionResult(mgp_query_execution_result *result) : result_(result) {}
inline std::vector<std::string> QueryExecutionResult::Headers() const {
return mgp::get_query_execution_headers(result_);
};
// do not enter
namespace detail {
inline void AddParamsReturnsToProc(mgp_proc *proc, std::vector<Parameter> &parameters,

View File

@ -4046,9 +4046,12 @@ std::vector<std::vector<mgp_value>> GetMgpRowsFromTypedValues(
return mgp_rows;
}
mgp_query_execution_result::mgp_query_execution_result(std::vector<std::vector<memgraph::query::TypedValue>> tv_rows,
mgp_query_execution_headers::mgp_query_execution_headers(std::vector<std::string> headers) : headers(headers){};
mgp_query_execution_result::mgp_query_execution_result(std::vector<std::string> headers,
std::vector<std::vector<memgraph::query::TypedValue>> tv_rows,
mgp_graph *graph, mgp_memory *memory)
: rows(GetMgpRowsFromTypedValues(std::move(tv_rows), graph, memory)) {}
: headers(headers), rows(GetMgpRowsFromTypedValues(std::move(tv_rows), graph, memory)) {}
mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *query,
mgp_query_execution_result **result) {
@ -4057,7 +4060,7 @@ mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *qu
auto query_string = std::string(query);
auto *instance = memgraph::query::InterpreterContext::getInstance();
memgraph::query::Interpreter interpreter(instance);
memgraph::query::Interpreter interpreter(instance, instance->dbms_handler->Get());
interpreter.SetUser(graph->ctx->user_or_role);
instance->interpreters.WithLock([&interpreter](auto &interpreters) { interpreters.insert(&interpreter); });
@ -4066,11 +4069,17 @@ mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *qu
instance->interpreters.WithLock([&interpreter](auto &interpreters) { interpreters.erase(&interpreter); });
});
auto results = interpreter.Prepare(query_string, {}, {});
auto prepare_query_result = interpreter.Prepare(query_string, {}, {});
MgProcedureResultStream stream;
interpreter.Pull(&stream, {}, results.qid);
interpreter.Pull(&stream, {}, prepare_query_result.qid);
return NewRawMgpObject<mgp_query_execution_result>(memory, std::move(stream.rows), graph, memory);
return NewRawMgpObject<mgp_query_execution_result>(memory, std::move(prepare_query_result.headers),
std::move(stream.rows), graph, memory);
},
result);
}
mgp_error mgp_get_query_execution_headers(mgp_query_execution_result *execution_result,
mgp_query_execution_headers **result) {
return WrapExceptions([&]() { return &execution_result->headers; }, result);
}

View File

@ -995,11 +995,20 @@ bool ContainsDeleted(const mgp_value *val);
memgraph::query::TypedValue ToTypedValue(const mgp_value &val, memgraph::utils::MemoryResource *memory);
struct mgp_query_execution_headers {
explicit mgp_query_execution_headers(std::vector<std::string> headers);
~mgp_query_execution_headers() = default;
std::vector<std::string> headers;
};
struct mgp_query_execution_result {
explicit mgp_query_execution_result(std::vector<std::vector<memgraph::query::TypedValue>> tv_rows, mgp_graph *graph,
explicit mgp_query_execution_result(std::vector<std::string> headers,
std::vector<std::vector<memgraph::query::TypedValue>> tv_rows, mgp_graph *graph,
mgp_memory *memory);
~mgp_query_execution_result() = default;
mgp_query_execution_headers headers;
std::vector<std::vector<mgp_value>> rows;
};