-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Query parameters in interactive mode (:param) #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ignition
wants to merge
2
commits into
master
Choose a base branch
from
feat/param-command
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # git apply needs LF context lines to match an LF-checked-out source tree; | ||
| # Windows autocrlf would otherwise rewrite these to CRLF and break the patch. | ||
| *.patch text eol=lf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright (C) 2016-2023 Memgraph Ltd. [https://memgraph.com] | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| #include "parameters.hpp" | ||
|
|
||
| namespace query::params { | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr const char *kWhitespace = " \t\r\n"; | ||
|
|
||
| std::string Trim(const std::string &s) { | ||
| const auto begin = s.find_first_not_of(kWhitespace); | ||
| if (begin == std::string::npos) return ""; | ||
| const auto end = s.find_last_not_of(kWhitespace); | ||
| return s.substr(begin, end - begin + 1); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| ParamParse ParseParamCommand(const std::string &line) { | ||
| const std::string trimmed = Trim(line); | ||
| const auto head_end = trimmed.find_first_of(kWhitespace); | ||
| const std::string head = trimmed.substr(0, head_end); | ||
| const std::string rest = head_end == std::string::npos ? "" : Trim(trimmed.substr(head_end)); | ||
|
|
||
| if (head == ":params") { | ||
| if (rest.empty()) { | ||
| ParamCommand command; | ||
| command.kind = ParamCommand::Kind::kList; | ||
| return {.is_param_command = true, .command = command, .error = ""}; | ||
| } | ||
| if (rest == "clear") { | ||
| ParamCommand command; | ||
| command.kind = ParamCommand::Kind::kClear; | ||
| return {.is_param_command = true, .command = command, .error = ""}; | ||
| } | ||
| return {.is_param_command = true, .command = std::nullopt, .error = "expected ':params' or ':params clear'"}; | ||
| } | ||
|
|
||
| if (head != ":param") return {}; // not a parameter command | ||
|
|
||
| const auto name_end = rest.find_first_of(kWhitespace); | ||
| ParamCommand command; | ||
| command.kind = ParamCommand::Kind::kSet; | ||
| command.name = rest.substr(0, name_end); | ||
| command.expression = name_end == std::string::npos ? "" : Trim(rest.substr(name_end)); | ||
|
|
||
| if (command.name.empty() || command.expression.empty()) { | ||
| return {.is_param_command = true, .command = std::nullopt, .error = "expected ':param <name> <expression>'"}; | ||
| } | ||
|
|
||
| return {.is_param_command = true, .command = command, .error = ""}; | ||
| } | ||
|
|
||
| bool ParamStore::Empty() const { return params_.empty(); } | ||
|
|
||
| std::size_t ParamStore::Size() const { return params_.size(); } | ||
|
|
||
| void ParamStore::Set(const std::string &name, const mg_value *value) { | ||
| params_.insert_or_assign(name, mg_memory::MakeCustomUnique<mg_value>(mg_value_copy(value))); | ||
| } | ||
|
|
||
| const mg_value *ParamStore::Get(const std::string &name) const { | ||
| const auto it = params_.find(name); | ||
| return it == params_.end() ? nullptr : it->second.get(); | ||
| } | ||
|
|
||
| std::vector<std::string> ParamStore::Names() const { | ||
| std::vector<std::string> names; | ||
| names.reserve(params_.size()); | ||
| for (const auto &[name, value] : params_) names.push_back(name); | ||
| return names; // std::map keeps keys sorted | ||
| } | ||
|
|
||
| void ParamStore::Clear() { params_.clear(); } | ||
|
|
||
| mg_memory::MgMapPtr ParamStore::AsMap() const { | ||
| auto map = mg_memory::MakeCustomUnique<mg_map>(mg_map_make_empty(static_cast<uint32_t>(params_.size()))); | ||
| for (const auto &[name, value] : params_) { | ||
| // mg_map_insert copies the key and takes ownership of the value copy. | ||
| mg_map_insert(map.get(), name.c_str(), mg_value_copy(value.get())); | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
| } // namespace query::params | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright (C) 2016-2023 Memgraph Ltd. [https://memgraph.com] | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <map> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "utils/mg_memory.hpp" | ||
|
|
||
| namespace query::params { | ||
|
|
||
| /// A parsed `:param` / `:params` interactive command. | ||
| struct ParamCommand { | ||
| enum class Kind { | ||
| kSet, ///< `:param <name> <expression>` | ||
| kList, ///< `:params` | ||
| kClear, ///< `:params clear` | ||
| }; | ||
|
|
||
| Kind kind; | ||
| std::string name; ///< populated for kSet | ||
| std::string expression; ///< populated for kSet | ||
| }; | ||
|
|
||
| /// Result of attempting to parse a line as a parameter command. | ||
| struct ParamParse { | ||
| /// True if the line is a `:param`/`:params` command (well-formed or not). | ||
| bool is_param_command{false}; | ||
| /// Set iff the command parsed successfully. | ||
| std::optional<ParamCommand> command{std::nullopt}; | ||
| /// Human-readable reason, set iff `is_param_command && !command`. | ||
| std::string error{}; | ||
| }; | ||
|
|
||
| /// Parses a single line as a parameter command. | ||
| /// | ||
| /// Returns `is_param_command == false` for anything that is not a | ||
| /// `:param`/`:params` command, so other command handlers can take over. | ||
| ParamParse ParseParamCommand(const std::string &line); | ||
|
|
||
| /// Holds the query parameters set via `:param`, owning a copy of each value, | ||
| /// and exposes them as an `mg_map` for `mg_session_run`. | ||
| class ParamStore { | ||
| public: | ||
| bool Empty() const; | ||
| std::size_t Size() const; | ||
|
|
||
| /// Stores a copy of `value` under `name`, overwriting any existing entry. | ||
| void Set(const std::string &name, const mg_value *value); | ||
| /// Returns the value stored under `name`, or nullptr if none. | ||
| const mg_value *Get(const std::string &name) const; | ||
| /// Returns all parameter names in sorted order. | ||
| std::vector<std::string> Names() const; | ||
| /// Removes all parameters. | ||
| void Clear(); | ||
| /// Builds an `mg_map` copy of all parameters, suitable for `mg_session_run`. | ||
| mg_memory::MgMapPtr AsMap() const; | ||
|
|
||
| private: | ||
| std::map<std::string, mg_memory::MgValuePtr> params_; | ||
| }; | ||
|
|
||
| } // namespace query::params |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include
\fand\vin here?