2016-02-03 07:18:20 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
2016-02-04 09:45:12 +08:00
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
2016-02-03 07:18:20 +08:00
|
|
|
|
|
|
|
#include "cypher/cypher.h"
|
2016-02-04 09:45:12 +08:00
|
|
|
#include "cypher/tokenizer/cypher_lexer.hpp"
|
|
|
|
#include "utils/variadic/variadic.hpp"
|
2016-02-03 07:18:20 +08:00
|
|
|
|
2016-02-06 20:45:45 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2016-02-04 09:45:12 +08:00
|
|
|
template<typename ...Ts>
|
2016-02-03 07:18:20 +08:00
|
|
|
class QueryStripper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-02-06 20:45:45 +08:00
|
|
|
QueryStripper(Ts&&... strip_types) :
|
|
|
|
lexer(std::make_unique<CypherLexer>()),
|
|
|
|
strip_types(std::make_tuple(std::forward<Ts>(strip_types)...))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
QueryStripper(QueryStripper& other) = delete;
|
|
|
|
QueryStripper(QueryStripper&& other) :
|
|
|
|
strip_types(std::move(other.strip_types)),
|
|
|
|
lexer(std::move(other.lexer))
|
|
|
|
{
|
|
|
|
}
|
2016-02-03 07:18:20 +08:00
|
|
|
|
2016-02-06 20:45:45 +08:00
|
|
|
decltype(auto) strip(const std::string& query)
|
2016-02-03 07:18:20 +08:00
|
|
|
{
|
2016-02-06 20:45:45 +08:00
|
|
|
auto tokenizer = lexer->tokenize(query);
|
2016-02-03 07:18:20 +08:00
|
|
|
std::string stripped = "";
|
|
|
|
int counter = 0;
|
2016-02-04 09:45:12 +08:00
|
|
|
constexpr auto size = std::tuple_size<decltype(strip_types)>::value;
|
2016-02-03 07:18:20 +08:00
|
|
|
while (auto token = tokenizer.lookup())
|
|
|
|
{
|
2016-02-04 09:45:12 +08:00
|
|
|
if (_or(token.id, strip_types, std::make_index_sequence<size>{})) {
|
2016-02-03 07:18:20 +08:00
|
|
|
stripped += "@" + std::to_string(counter++);
|
|
|
|
} else {
|
|
|
|
stripped += token.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stripped;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-02-04 09:45:12 +08:00
|
|
|
std::tuple<Ts...> strip_types;
|
2016-02-06 20:45:45 +08:00
|
|
|
CypherLexer::uptr lexer;
|
2016-02-04 09:45:12 +08:00
|
|
|
|
|
|
|
template<typename Value, typename Tuple, std::size_t ...index>
|
|
|
|
bool _or(Value&& value, Tuple&& tuple, std::index_sequence<index...>)
|
|
|
|
{
|
|
|
|
return or_vargs(std::forward<Value>(value),
|
|
|
|
std::get<index>(std::forward<Tuple>(tuple))...);
|
|
|
|
}
|
2016-02-03 07:18:20 +08:00
|
|
|
};
|
2016-02-06 20:45:45 +08:00
|
|
|
|
|
|
|
template<typename ...Ts>
|
|
|
|
decltype(auto) make_query_stripper(Ts&&... ts) {
|
|
|
|
return QueryStripper<Ts...>(std::forward<Ts>(ts)...);
|
|
|
|
}
|