1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionsStringArray.h>
3
4namespace DB
5{
6
7class ExtractURLParametersImpl
8{
9private:
10 Pos pos;
11 Pos end;
12 bool first;
13
14public:
15 static constexpr auto name = "extractURLParameters";
16 static String getName() { return name; }
17
18 static size_t getNumberOfArguments() { return 1; }
19
20 static void checkArguments(const DataTypes & arguments)
21 {
22 if (!isString(arguments[0]))
23 throw Exception("Illegal type " + arguments[0]->getName() + " of first argument of function " + getName() + ". Must be String.",
24 ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
25 }
26
27 void init(Block & /*block*/, const ColumnNumbers & /*arguments*/) {}
28
29 /// Returns the position of the argument that is the column of rows
30 size_t getStringsArgumentPosition()
31 {
32 return 0;
33 }
34
35 /// Called for each next string.
36 void set(Pos pos_, Pos end_)
37 {
38 pos = pos_;
39 end = end_;
40 first = true;
41 }
42
43 /// Get the next token, if any, or return false.
44 bool get(Pos & token_begin, Pos & token_end)
45 {
46 if (pos == nullptr)
47 return false;
48
49 if (first)
50 {
51 first = false;
52 pos = find_first_symbols<'?', '#'>(pos, end);
53 if (pos + 1 >= end)
54 return false;
55 ++pos;
56 }
57
58 while (true)
59 {
60 token_begin = pos;
61 pos = find_first_symbols<'=', '&', '#', '?'>(pos, end);
62 if (pos == end)
63 return false;
64
65 if (*pos == '?')
66 {
67 ++pos;
68 continue;
69 }
70
71 break;
72 }
73
74 if (*pos == '&' || *pos == '#')
75 {
76 token_end = pos++;
77 }
78 else
79 {
80 ++pos;
81 pos = find_first_symbols<'&', '#'>(pos, end);
82 if (pos == end)
83 token_end = end;
84 else
85 token_end = pos++;
86 }
87
88 return true;
89 }
90};
91
92struct NameExtractURLParameters { static constexpr auto name = "extractURLParameters"; };
93using FunctionExtractURLParameters = FunctionTokens<ExtractURLParametersImpl>;
94
95void registerFunctionExtractURLParameters(FunctionFactory & factory)
96{
97 factory.registerFunction<FunctionExtractURLParameters>();
98}
99
100}
101