Created query execution result
This commit is contained in:
parent
2b30ba3fef
commit
2c0fc680de
@ -851,6 +851,8 @@ 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 void execute_query(mgp_graph *graph, const char *query) { MgInvokeVoid(mgp_execute_query, graph, query); }
|
||||
inline void execute_query(mgp_graph *graph, const char *query, mgp_memory *memory) {
|
||||
MgInvokeVoid(mgp_execute_query, graph, memory, query);
|
||||
}
|
||||
|
||||
} // namespace mgp
|
||||
|
@ -1801,7 +1801,10 @@ 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);
|
||||
|
||||
enum mgp_error mgp_execute_query(mgp_graph *graph, const char *query);
|
||||
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);
|
||||
|
||||
/// @}
|
||||
|
||||
|
@ -94,6 +94,7 @@ struct MapItem;
|
||||
class Duration;
|
||||
class Value;
|
||||
class QueryExecution;
|
||||
class QueryExecutionResult;
|
||||
|
||||
struct StealType {};
|
||||
inline constexpr StealType steal{};
|
||||
@ -1554,12 +1555,19 @@ class Return {
|
||||
class QueryExecution {
|
||||
public:
|
||||
QueryExecution(mgp_graph *graph);
|
||||
void ExecuteQuery(std::string_view query) const;
|
||||
QueryExecutionResult ExecuteQuery(std::string_view query) const;
|
||||
|
||||
private:
|
||||
mgp_graph *graph_;
|
||||
};
|
||||
|
||||
class QueryExecutionResult {
|
||||
mgp_query_execution_result *result_;
|
||||
|
||||
public:
|
||||
QueryExecutionResult(mgp_query_execution_result *result);
|
||||
};
|
||||
|
||||
enum class ProcedureType : uint8_t {
|
||||
Read,
|
||||
Write,
|
||||
@ -4298,7 +4306,11 @@ inline mgp_type *Return::GetMGPType() const {
|
||||
|
||||
inline QueryExecution::QueryExecution(mgp_graph *graph) : graph_(graph) {}
|
||||
|
||||
inline void QueryExecution::ExecuteQuery(std::string_view query) const { mgp::execute_query(graph_, query.data()); }
|
||||
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) {}
|
||||
|
||||
// do not enter
|
||||
namespace detail {
|
||||
|
@ -28,7 +28,6 @@
|
||||
#include "mg_procedure.h"
|
||||
#include "module.hpp"
|
||||
#include "query/db_accessor.hpp"
|
||||
#include "query/discard_value_stream.hpp"
|
||||
#include "query/frontend/ast/ast.hpp"
|
||||
#include "query/interpreter.hpp"
|
||||
#include "query/interpreter_context.hpp"
|
||||
@ -4026,8 +4025,35 @@ mgp_error mgp_untrack_current_thread_allocations(mgp_graph *graph) {
|
||||
});
|
||||
}
|
||||
|
||||
mgp_error mgp_execute_query(mgp_graph *graph, const char *query) {
|
||||
return WrapExceptions([&]() {
|
||||
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;
|
||||
for (auto &row : tv_rows) {
|
||||
std::vector<mgp_value> vals;
|
||||
for (auto &val : row) {
|
||||
vals.emplace_back(val, graph, memory->impl);
|
||||
}
|
||||
mgp_rows.emplace_back(std::move(vals));
|
||||
}
|
||||
|
||||
return mgp_rows;
|
||||
}
|
||||
|
||||
mgp_query_execution_result::mgp_query_execution_result(std::vector<std::vector<memgraph::query::TypedValue>> tv_rows,
|
||||
mgp_graph *graph, mgp_memory *memory)
|
||||
: 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) {
|
||||
return WrapExceptions(
|
||||
[&]() {
|
||||
auto query_string = std::string(query);
|
||||
auto *instance = memgraph::query::InterpreterContext::getInstance();
|
||||
|
||||
@ -4041,7 +4067,10 @@ mgp_error mgp_execute_query(mgp_graph *graph, const char *query) {
|
||||
});
|
||||
|
||||
auto results = interpreter.Prepare(query_string, {}, {});
|
||||
memgraph::query::DiscardValueResultStream stream;
|
||||
MgProcedureResultStream stream;
|
||||
interpreter.Pull(&stream, {}, results.qid);
|
||||
});
|
||||
|
||||
return NewRawMgpObject<mgp_query_execution_result>(memory, std::move(stream.rows), graph, memory);
|
||||
},
|
||||
result);
|
||||
}
|
||||
|
@ -995,8 +995,11 @@ bool ContainsDeleted(const mgp_value *val);
|
||||
|
||||
memgraph::query::TypedValue ToTypedValue(const mgp_value &val, memgraph::utils::MemoryResource *memory);
|
||||
|
||||
struct mgp_query_execution {
|
||||
explicit mgp_query_execution(){};
|
||||
struct mgp_query_execution_result {
|
||||
explicit mgp_query_execution_result(std::vector<std::vector<memgraph::query::TypedValue>> tv_rows, mgp_graph *graph,
|
||||
mgp_memory *memory);
|
||||
|
||||
~mgp_query_execution() = default;
|
||||
~mgp_query_execution_result() = default;
|
||||
|
||||
std::vector<std::vector<mgp_value>> rows;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user