-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathtryConvertStringLiterals.cpp
139 lines (129 loc) · 3.89 KB
/
tryConvertStringLiterals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <Databases/MySQL/tryConvertStringLiterals.h>
#include <Core/MySQL/MySQLCharset.h>
#include <IO/ReadHelpers.h>
#include <IO/ReadBufferFromMemory.h>
#include <Parsers/CommonParsers.h>
#include <Common/quoteString.h>
#include <Poco/String.h>
namespace DB
{
/// Reads current string literal using also double quotes
/// https://dev.mysql.com/doc/refman/8.0/en/string-literals.html
static bool tryReadLiteral(
IParser::Pos & pos,
String & to)
{
bool ret = false;
try
{
if (pos->type == TokenType::StringLiteral ||
(pos->type == TokenType::QuotedIdentifier && *pos->begin == '"'))
{
ReadBufferFromMemory buf(pos->begin, pos->size());
if (*pos->begin == '"')
readDoubleQuotedStringWithSQLStyle(to, buf);
else
readQuotedStringWithSQLStyle(to, buf);
ret = true;
}
}
catch (...) // NOLINT(bugprone-empty-catch)
{
/// Ignore parsing errors
}
return ret;
}
/// Reads charset introducers before string literal
/// https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html
static bool tryReadCharset(
IParser::Pos & pos,
String & to)
{
bool ret = false;
if (pos->type == TokenType::BareWord &&
*pos->begin == '_' &&
pos->size() > 1)
{
String charset_name(pos->begin + 1, pos->size() - 1);
charset_name = Poco::toLower(charset_name);
if (MySQLCharset::isCharsetAvailable(charset_name))
{
to = charset_name;
ret = true;
}
}
return ret;
}
bool tryConvertStringLiterals(String & query)
{
Tokens tokens(query.data(), query.data() + query.size());
IParser::Pos pos(tokens, DBMS_DEFAULT_MAX_PARSER_DEPTH, DBMS_DEFAULT_MAX_PARSER_BACKTRACKS);
Expected expected;
String rewritten_query;
rewritten_query.reserve(query.size());
MySQLCharset charset;
String charset_name;
auto charset_pos = pos;
bool ret = false;
const auto * copy_from = query.data();
for (;pos->type != TokenType::EndOfStream; ++pos)
{
if (tryReadCharset(pos, charset_name))
{
charset_pos = pos;
continue;
}
String literal;
if (tryReadLiteral(pos, literal))
{
if (!charset_name.empty())
{
try
{
bool need_convert = MySQLCharset::needConvert(charset_name);
String to;
if (!literal.empty() &&
need_convert &&
charset.convert(charset_name, to, literal) == 0)
{
literal = to;
}
/// Skip found charset from the query and spaces after
if (need_convert ||
charset_name == "utf8mb4" ||
charset_name == "utf8mb3")
{
rewritten_query.append(copy_from, charset_pos->begin - copy_from);
copy_from = pos->begin;
ret = true;
}
}
catch (...) // NOLINT(bugprone-empty-catch)
{
/// Ignore unsupported charsets
}
charset_name.clear();
}
/// The query should be rewritten
if (!ret)
ret = *pos->begin == '"';
if (ret)
{
rewritten_query.append(copy_from, pos->begin - copy_from).append(quoteString(literal));
copy_from = pos->end;
}
}
else
{
charset_name.clear();
}
}
if (ret)
{
if (copy_from < pos->end)
rewritten_query.append(copy_from, pos->begin - copy_from);
query = rewritten_query;
}
return ret;
}
}