Add functionality for bypassing MySQL whitespace/comments

This functionality is needed both in the query classifier, where
it was first created, and in the cache.
This commit is contained in:
Johan Wikman
2017-03-17 10:02:27 +02:00
parent 6db3cc380b
commit 7469c5be52
3 changed files with 166 additions and 0 deletions

View File

@ -1213,3 +1213,111 @@ char* modutil_get_canonical(GWBUF* querybuf)
return querystr;
}
char* modutil_MySQL_bypass_whitespace(char* sql, size_t len)
{
char *i = sql;
char *end = i + len;
while (i != end)
{
if (isspace(*i))
{
++i;
}
else if (*i == '/') // Might be a comment
{
if ((i + 1 != end) && (*(i + 1) == '*')) // Indeed it was
{
i += 2;
while (i != end)
{
if (*i == '*') // Might be the end of the comment
{
++i;
if (i != end)
{
if (*i == '/') // Indeed it was
{
++i;
break; // Out of this inner while.
}
}
}
else
{
// It was not the end of the comment.
++i;
}
}
}
else
{
// Was not a comment, so we'll bail out.
break;
}
}
else if (*i == '-') // Might be the start of a comment to the end of line
{
bool is_comment = false;
if (i + 1 != end)
{
if (*(i + 1) == '-') // Might be, yes.
{
if (i + 2 != end)
{
if (isspace(*(i + 2))) // Yes, it is.
{
is_comment = true;
i += 3;
while ((i != end) && (*i != '\n'))
{
++i;
}
if (i != end)
{
ss_dassert(*i == '\n');
++i;
}
}
}
}
}
if (!is_comment)
{
break;
}
}
else if (*i == '#')
{
++i;
while ((i != end) && (*i != '\n'))
{
++i;
}
if (i != end)
{
ss_dassert(*i == '\n');
++i;
}
break;
}
else
{
// Neither whitespace not start of a comment, so we bail out.
break;
}
}
return i;
}