Add support for headers

This commit is contained in:
Josip Mrden 2024-02-28 23:23:15 +01:00
parent 8fdc199d2b
commit 1145ea87ad
5 changed files with 97 additions and 43 deletions

View File

@ -851,12 +851,20 @@ inline void func_result_set_value(mgp_func_result *res, mgp_value *value, mgp_me
MgInvokeVoid(mgp_func_result_set_value, res, value, memory);
}
inline mgp_query_execution_result *execute_query(mgp_graph *graph, const char *query, mgp_memory *memory) {
return MgInvoke<mgp_query_execution_result *>(mgp_execute_query, graph, memory, query);
inline mgp_execution_result *execute_query(mgp_graph *graph, const char *query, mgp_memory *memory) {
return MgInvoke<mgp_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);
inline mgp_execution_headers *mgp_fetch_execution_headers(mgp_execution_result *exec_result) {
return MgInvoke<mgp_execution_headers *>(mgp_fetch_execution_headers, exec_result);
}
inline size_t execution_headers_size(mgp_execution_headers *headers) {
return MgInvoke<size_t>(mgp_execution_headers_size, headers);
}
inline const char *execution_headers_at(mgp_execution_headers *headers, size_t index) {
return MgInvoke<const char *>(mgp_execution_headers_at, headers, index);
}
} // namespace mgp

View File

@ -1801,15 +1801,19 @@ enum mgp_error mgp_func_result_set_error_msg(struct mgp_func_result *result, con
enum mgp_error mgp_func_result_set_value(struct mgp_func_result *result, struct mgp_value *value,
struct mgp_memory *memory);
struct mgp_query_execution_result;
struct mgp_execution_headers;
enum mgp_error mgp_execute_query(mgp_graph *graph, struct mgp_memory *memory, const char *query,
struct mgp_query_execution_result **result);
enum mgp_error mgp_execution_headers_at(struct mgp_execution_headers *headers, size_t index, const char **result);
struct mgp_query_execution_headers;
enum mgp_error mgp_execution_headers_size(struct mgp_execution_headers *headers, size_t *result);
enum mgp_error mgp_get_query_execution_headers(struct mgp_query_execution_result *query_execution,
struct mgp_query_execution_headers **result);
struct mgp_execution_result;
enum mgp_error mgp_execute_query(struct mgp_graph *graph, struct mgp_memory *memory, const char *query,
struct mgp_execution_result **result);
enum mgp_error mgp_fetch_execution_headers(struct mgp_execution_result *exec_result,
struct mgp_execution_headers **headers);
/// @}

View File

@ -1552,6 +1552,16 @@ class Return {
mgp_type *GetMGPType() const;
};
class ExecutionHeaders {
public:
ExecutionHeaders(mgp_execution_headers *headers);
size_t Size() const;
std::string At(size_t index) const;
private:
mgp_execution_headers *headers_;
};
class QueryExecution {
public:
QueryExecution(mgp_graph *graph);
@ -1562,10 +1572,10 @@ class QueryExecution {
};
class QueryExecutionResult {
mgp_query_execution_result *result_;
mgp_execution_result *result_;
public:
QueryExecutionResult(mgp_query_execution_result *result);
QueryExecutionResult(mgp_execution_result *result);
std::vector<std::string> Headers() const;
};
@ -4305,13 +4315,21 @@ inline mgp_type *Return::GetMGPType() const {
return util::ToMGPType(type_);
}
inline ExecutionHeaders::ExecutionHeaders(mgp_execution_headers *headers) : headers_(headers) {}
inline size_t ExecutionHeaders::Size() const { return mgp::execution_headers_size(headers_); }
inline std::string ExecutionHeaders::At(size_t index) const {
return std::string(mgp::execution_headers_at(headers_, index));
}
inline QueryExecution::QueryExecution(mgp_graph *graph) : graph_(graph) {}
inline QueryExecutionResult QueryExecution::ExecuteQuery(std::string_view query) const {
mgp::MemHandlerCallback(execute_query, graph_, query.data());
}
inline QueryExecutionResult::QueryExecutionResult(mgp_query_execution_result *result) : result_(result) {}
inline QueryExecutionResult::QueryExecutionResult(mgp_execution_result *result) : result_(result) {}
inline std::vector<std::string> QueryExecutionResult::Headers() const {
return mgp::get_query_execution_headers(result_);

View File

@ -4025,13 +4025,6 @@ mgp_error mgp_untrack_current_thread_allocations(mgp_graph *graph) {
});
}
struct MgProcedureResultStream final {
using Row = std::vector<memgraph::query::TypedValue>;
using Rows = std::vector<Row>;
Rows rows;
void Result(const Row &row) { rows.push_back(row); }
};
std::vector<std::vector<mgp_value>> GetMgpRowsFromTypedValues(
std::vector<std::vector<memgraph::query::TypedValue>> tv_rows, mgp_graph *graph, mgp_memory *memory) {
std::vector<std::vector<mgp_value>> mgp_rows;
@ -4046,17 +4039,41 @@ std::vector<std::vector<mgp_value>> GetMgpRowsFromTypedValues(
return mgp_rows;
}
mgp_query_execution_headers::mgp_query_execution_headers(std::vector<std::string> headers) : headers(headers){};
mgp_execution_headers::mgp_execution_headers(memgraph::utils::pmr::vector<memgraph::utils::pmr::string> &&storage)
: headers(std::move(storage)){};
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)
: headers(headers), rows(GetMgpRowsFromTypedValues(std::move(tv_rows), graph, memory)) {}
mgp_error mgp_execution_headers_size(mgp_execution_headers *headers, size_t *result) {
static_assert(noexcept(headers->headers.size()));
*result = headers->headers.size();
return mgp_error::MGP_ERROR_NO_ERROR;
}
mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *query,
mgp_query_execution_result **result) {
mgp_error mgp_execution_headers_at(mgp_execution_headers *headers, size_t index, const char **result) {
return WrapExceptions(
[&]() {
[headers, index] {
if (index >= Call<size_t>(mgp_execution_headers_size, headers)) {
throw std::out_of_range("Header cannot be retrieved, because index exceeds headers' size!");
}
return headers->headers[index].data();
},
result);
}
mgp_execution_result::mgp_execution_result(mgp_execution_headers &&headers,
std::vector<std::vector<memgraph::query::TypedValue>> tv_rows,
mgp_graph *graph, mgp_memory *memory)
: headers(std::move(headers)), rows(GetMgpRowsFromTypedValues(std::move(tv_rows), graph, memory)) {}
struct MgProcedureResultStream final {
using Row = std::vector<memgraph::query::TypedValue>;
using Rows = std::vector<Row>;
Rows rows;
void Result(const Row &row) { rows.push_back(row); }
};
mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *query, mgp_execution_result **result) {
return WrapExceptions(
[query, graph, memory]() {
auto query_string = std::string(query);
auto *instance = memgraph::query::InterpreterContext::getInstance();
@ -4073,13 +4090,17 @@ mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *qu
MgProcedureResultStream stream;
interpreter.Pull(&stream, {}, prepare_query_result.qid);
return NewRawMgpObject<mgp_query_execution_result>(memory, std::move(prepare_query_result.headers),
std::move(stream.rows), graph, memory);
memgraph::utils::pmr::vector<memgraph::utils::pmr::string> headers(memory->impl);
for (auto header : prepare_query_result.headers) {
headers.emplace_back(header);
}
return NewRawMgpObject<mgp_execution_result>(memory, mgp_execution_headers{std::move(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);
mgp_error mgp_fetch_execution_headers(mgp_execution_result *exec_result, mgp_execution_headers **result) {
return WrapExceptions([exec_result]() { return &exec_result->headers; }, result);
}

View File

@ -995,20 +995,23 @@ 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;
struct mgp_execution_headers {
using allocator_type = memgraph::utils::Allocator<mgp_execution_headers>;
using storage_type = memgraph::utils::pmr::vector<memgraph::utils::pmr::string>;
explicit mgp_execution_headers(storage_type &&storage);
std::vector<std::string> headers;
~mgp_execution_headers() = default;
storage_type headers;
};
struct mgp_query_execution_result {
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);
struct mgp_execution_result {
explicit mgp_execution_result(mgp_execution_headers &&headers,
std::vector<std::vector<memgraph::query::TypedValue>> tv_rows, mgp_graph *graph,
mgp_memory *memory);
~mgp_query_execution_result() = default;
~mgp_execution_result() = default;
mgp_query_execution_headers headers;
mgp_execution_headers headers;
std::vector<std::vector<mgp_value>> rows;
};