2017-03-10 17:52:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Types.h>
|
2017-03-10 17:52:36 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Transforms the LIKE expression into regexp re2. For example, abc%def -> ^abc.*def$
|
2017-03-10 17:52:36 +00:00
|
|
|
inline String likePatternToRegexp(const String & pattern)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
String res;
|
|
|
|
res.reserve(pattern.size() * 2);
|
|
|
|
const char * pos = pattern.data();
|
|
|
|
const char * end = pos + pattern.size();
|
2017-03-10 17:52:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (pos < end && *pos == '%')
|
|
|
|
++pos;
|
|
|
|
else
|
|
|
|
res = "^";
|
2017-03-10 17:52:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (pos < end)
|
|
|
|
{
|
|
|
|
switch (*pos)
|
|
|
|
{
|
|
|
|
case '^': case '$': case '.': case '[': case '|': case '(': case ')': case '?': case '*': case '+': case '{':
|
|
|
|
res += '\\';
|
|
|
|
res += *pos;
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
if (pos + 1 != end)
|
|
|
|
res += ".*";
|
|
|
|
else
|
|
|
|
return res;
|
|
|
|
break;
|
|
|
|
case '_':
|
|
|
|
res += ".";
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
++pos;
|
|
|
|
if (pos == end)
|
|
|
|
res += "\\\\";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*pos == '%' || *pos == '_')
|
|
|
|
res += *pos;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res += '\\';
|
|
|
|
res += *pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res += *pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
2017-03-10 17:52:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res += '$';
|
|
|
|
return res;
|
2017-03-10 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|