2017-03-08 21:11:05 +08:00
|
|
|
//
|
|
|
|
// Copyright 2017 Memgraph
|
|
|
|
// Created by Florijan Stamenkovic on 08.03.17.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef MEMGRAPH_PARAMETERS_HPP
|
|
|
|
#define MEMGRAPH_PARAMETERS_HPP
|
|
|
|
|
2017-06-15 00:53:02 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-04-10 18:22:48 +08:00
|
|
|
#include "query/typed_value.hpp"
|
2017-03-08 21:11:05 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encapsulates user provided parameters (and stripped literals)
|
2017-06-15 00:53:02 +08:00
|
|
|
* and provides ways of obtaining them by position.
|
2017-03-08 21:11:05 +08:00
|
|
|
*/
|
|
|
|
struct Parameters {
|
|
|
|
public:
|
|
|
|
/**
|
2017-06-15 00:53:02 +08:00
|
|
|
* Adds a value to the stripped arguments under a token position.
|
2017-03-08 21:11:05 +08:00
|
|
|
*
|
2017-06-15 00:53:02 +08:00
|
|
|
* @param position Token position in query of value.
|
2017-03-08 21:11:05 +08:00
|
|
|
* @param value
|
|
|
|
*/
|
2017-06-15 00:53:02 +08:00
|
|
|
void Add(int position, const query::TypedValue &value) {
|
|
|
|
storage_.emplace_back(position, value);
|
2017-03-08 21:11:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-15 00:53:02 +08:00
|
|
|
* Returns the value found for the given token position.
|
2017-03-08 21:11:05 +08:00
|
|
|
*
|
2017-06-15 00:53:02 +08:00
|
|
|
* @param position Token position in query of value.
|
|
|
|
* @return Value for the given token position.
|
2017-03-08 21:11:05 +08:00
|
|
|
*/
|
2017-06-15 00:53:02 +08:00
|
|
|
const query::TypedValue &AtTokenPosition(int position) const {
|
|
|
|
auto found = std::find_if(storage_.begin(), storage_.end(),
|
|
|
|
[&](const std::pair<int, query::TypedValue> a) {
|
|
|
|
return a.first == position;
|
|
|
|
});
|
2017-03-08 21:11:05 +08:00
|
|
|
permanent_assert(found != storage_.end(),
|
2017-06-15 00:53:02 +08:00
|
|
|
"Token position must be present in container");
|
2017-03-08 21:11:05 +08:00
|
|
|
return found->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the position-th stripped value. Asserts that this
|
|
|
|
* container has at least (position + 1) elements.
|
|
|
|
*
|
|
|
|
* @param position Which stripped param is sought.
|
2017-06-15 00:53:02 +08:00
|
|
|
* @return Token position and value for sought param.
|
2017-03-08 21:11:05 +08:00
|
|
|
*/
|
2017-06-15 00:53:02 +08:00
|
|
|
const std::pair<int, query::TypedValue> &At(int position) const {
|
|
|
|
permanent_assert(position < static_cast<int>(storage_.size()),
|
|
|
|
"Invalid position");
|
|
|
|
return storage_[position];
|
2017-03-08 21:11:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the number of arguments in this container */
|
2017-06-15 00:53:02 +08:00
|
|
|
int size() const { return storage_.size(); }
|
2017-03-08 21:11:05 +08:00
|
|
|
|
|
|
|
private:
|
2017-06-15 00:53:02 +08:00
|
|
|
std::vector<std::pair<int, query::TypedValue>> storage_;
|
2017-03-08 21:11:05 +08:00
|
|
|
};
|
|
|
|
|
2017-06-08 00:28:31 +08:00
|
|
|
#endif // MEMGRAPH_PARAMETERS_HPP
|