Add query params execution

This commit is contained in:
Josip Mrden 2024-03-01 13:19:14 +01:00
parent 34f2a1e10b
commit ed3a8a9328
4 changed files with 25 additions and 12 deletions

View File

@ -851,8 +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 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 mgp_execution_result *execute_query(mgp_graph *graph, const char *query, mgp_map *params, mgp_memory *memory) {
return MgInvoke<mgp_execution_result *>(mgp_execute_query, graph, memory, query, params);
}
inline mgp_execution_headers *fetch_execution_headers(mgp_execution_result *exec_result) {

View File

@ -1810,7 +1810,7 @@ enum mgp_error mgp_execution_headers_size(struct mgp_execution_headers *headers,
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);
struct mgp_map *params, 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

@ -1600,12 +1600,13 @@ class ExecutionHeaders {
};
class QueryExecution {
private:
friend class Map;
mgp_graph *graph_;
public:
QueryExecution(mgp_graph *graph);
ExecutionResult ExecuteQuery(std::string_view query) const;
private:
mgp_graph *graph_;
ExecutionResult ExecuteQuery(std::string_view query, Map params = Map()) const;
};
class ExecutionRow {
@ -4389,8 +4390,8 @@ inline std::string ExecutionHeaders::At(size_t index) const {
inline QueryExecution::QueryExecution(mgp_graph *graph) : graph_(graph) {}
inline ExecutionResult QueryExecution::ExecuteQuery(std::string_view query) const {
return ExecutionResult(mgp::MemHandlerCallback(execute_query, graph_, query.data()), graph_);
inline ExecutionResult QueryExecution::ExecuteQuery(std::string_view query, mgp::Map params) const {
return ExecutionResult(mgp::MemHandlerCallback(execute_query, graph_, query.data(), params.ptr_), graph_);
}
inline ExecutionResult::ExecutionResult(mgp_execution_result *result, mgp_graph *graph)

View File

@ -4072,9 +4072,19 @@ struct MgProcedureResultStream final {
}
};
mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *query, mgp_execution_result **result) {
std::map<std::string, memgraph::storage::PropertyValue> CreateQueryParams(mgp_map *params) {
std::map<std::string, memgraph::storage::PropertyValue> query_params;
for (auto &[k, v] : params->items) {
query_params.emplace(k, ToPropertyValue(v));
}
return query_params;
}
mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *query, mgp_map *params,
mgp_execution_result **result) {
return WrapExceptions(
[query, graph, memory]() {
[query, params, graph, memory]() {
auto query_string = std::string(query);
auto *instance = memgraph::query::InterpreterContext::getInstance();
@ -4087,7 +4097,9 @@ mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *qu
instance->interpreters.WithLock([&interpreter](auto &interpreters) { interpreters.erase(&interpreter); });
});
auto prepare_query_result = interpreter.Prepare(query_string, {}, {});
const auto query_params = CreateQueryParams(params);
auto prepare_query_result = interpreter.Prepare(query_string, query_params, {});
MgProcedureResultStream stream(memory);
interpreter.Pull(&stream, {}, prepare_query_result.qid);