| 1 | #include "duckdb/parser/keyword_helper.hpp" |
| 2 | #include "duckdb/parser/parser.hpp" |
| 3 | #include "duckdb/common/string_util.hpp" |
| 4 | |
| 5 | namespace duckdb { |
| 6 | |
| 7 | bool KeywordHelper::IsKeyword(const string &text) { |
| 8 | return Parser::IsKeyword(text); |
| 9 | } |
| 10 | |
| 11 | bool KeywordHelper::RequiresQuotes(const string &text, bool allow_caps) { |
| 12 | for (size_t i = 0; i < text.size(); i++) { |
| 13 | if (i > 0 && (text[i] >= '0' && text[i] <= '9')) { |
| 14 | continue; |
| 15 | } |
| 16 | if (text[i] >= 'a' && text[i] <= 'z') { |
| 17 | continue; |
| 18 | } |
| 19 | if (allow_caps) { |
| 20 | if (text[i] >= 'A' && text[i] <= 'Z') { |
| 21 | continue; |
| 22 | } |
| 23 | } |
| 24 | if (text[i] == '_') { |
| 25 | continue; |
| 26 | } |
| 27 | return true; |
| 28 | } |
| 29 | return IsKeyword(text); |
| 30 | } |
| 31 | |
| 32 | string KeywordHelper::EscapeQuotes(const string &text, char quote) { |
| 33 | return StringUtil::Replace(source: text, from: string(1, quote), to: string(2, quote)); |
| 34 | } |
| 35 | |
| 36 | string KeywordHelper::WriteQuoted(const string &text, char quote) { |
| 37 | // 1. Escapes all occurences of 'quote' by doubling them (escape in SQL) |
| 38 | // 2. Adds quotes around the string |
| 39 | return string(1, quote) + EscapeQuotes(text) + string(1, quote); |
| 40 | } |
| 41 | |
| 42 | string KeywordHelper::WriteOptionallyQuoted(const string &text, char quote, bool allow_caps) { |
| 43 | if (!RequiresQuotes(text, allow_caps)) { |
| 44 | return text; |
| 45 | } |
| 46 | return WriteQuoted(text, quote); |
| 47 | } |
| 48 | |
| 49 | } // namespace duckdb |
| 50 | |