|
| 1 | +/* Copyright (c) 2000, 2019, Oracle and/or its affiliates. |
| 2 | + Copyright (c) 2010, 2024, MariaDB Corporation. |
| 3 | +
|
| 4 | + This program is free software; you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation; version 2 of the License. |
| 7 | +
|
| 8 | + This program is distributed in the hope that it will be useful, |
| 9 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + GNU General Public License for more details. |
| 12 | +
|
| 13 | + You should have received a copy of the GNU General Public License |
| 14 | + along with this program; if not, write to the Free Software |
| 15 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ |
| 16 | + |
| 17 | + |
| 18 | +#ifndef LEX_IDENT_CLI |
| 19 | +#define LEX_IDENT_CLI |
| 20 | + |
| 21 | +#include "my_global.h" |
| 22 | +#include "m_ctype.h" |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + A string with metadata. Usually points to a string in the client |
| 27 | + character set, but unlike Lex_ident_cli_st (see below) it does not |
| 28 | + necessarily point to a query fragment. It can also point to memory |
| 29 | + of other kinds (e.g. an additional THD allocated memory buffer |
| 30 | + not overlapping with the current query text). |
| 31 | +
|
| 32 | + We'll add more flags here eventually, to know if the string has, e.g.: |
| 33 | + - multi-byte characters |
| 34 | + - bad byte sequences |
| 35 | + - backslash escapes: 'a\nb' |
| 36 | + and reuse the original query fragments instead of making the string |
| 37 | + copy too early, in Lex_input_stream::get_text(). |
| 38 | + This will allow to avoid unnecessary copying, as well as |
| 39 | + create more optimal Item types in sql_yacc.yy |
| 40 | +*/ |
| 41 | +struct Lex_string_with_metadata_st: public LEX_CSTRING |
| 42 | +{ |
| 43 | +private: |
| 44 | + bool m_is_8bit; // True if the string has 8bit characters |
| 45 | + char m_quote; // Quote character, or 0 if not quoted |
| 46 | +public: |
| 47 | + void set_8bit(bool is_8bit) { m_is_8bit= is_8bit; } |
| 48 | + void set_metadata(bool is_8bit, char quote) |
| 49 | + { |
| 50 | + m_is_8bit= is_8bit; |
| 51 | + m_quote= quote; |
| 52 | + } |
| 53 | + void set(const char *s, size_t len, bool is_8bit, char quote) |
| 54 | + { |
| 55 | + str= s; |
| 56 | + length= len; |
| 57 | + set_metadata(is_8bit, quote); |
| 58 | + } |
| 59 | + void set(const LEX_CSTRING *s, bool is_8bit, char quote) |
| 60 | + { |
| 61 | + ((LEX_CSTRING &)*this)= *s; |
| 62 | + set_metadata(is_8bit, quote); |
| 63 | + } |
| 64 | + bool is_8bit() const { return m_is_8bit; } |
| 65 | + bool is_quoted() const { return m_quote != '\0'; } |
| 66 | + char quote() const { return m_quote; } |
| 67 | + // Get string repertoire by the 8-bit flag and the character set |
| 68 | + my_repertoire_t repertoire(CHARSET_INFO *cs) const |
| 69 | + { |
| 70 | + return !m_is_8bit && my_charset_is_ascii_based(cs) ? |
| 71 | + MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30; |
| 72 | + } |
| 73 | + // Get string repertoire by the 8-bit flag, for ASCII-based character sets |
| 74 | + my_repertoire_t repertoire() const |
| 75 | + { |
| 76 | + return !m_is_8bit ? MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30; |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | + |
| 81 | +/* |
| 82 | + Used to store identifiers in the client character set. |
| 83 | + Points to a query fragment. |
| 84 | +*/ |
| 85 | +struct Lex_ident_cli_st: public Lex_string_with_metadata_st |
| 86 | +{ |
| 87 | +public: |
| 88 | + void set_keyword(const char *s, size_t len) |
| 89 | + { |
| 90 | + set(s, len, false, '\0'); |
| 91 | + } |
| 92 | + void set_ident(const char *s, size_t len, bool is_8bit) |
| 93 | + { |
| 94 | + set(s, len, is_8bit, '\0'); |
| 95 | + } |
| 96 | + void set_ident_quoted(const char *s, size_t len, bool is_8bit, char quote) |
| 97 | + { |
| 98 | + set(s, len, is_8bit, quote); |
| 99 | + } |
| 100 | + void set_unquoted(const LEX_CSTRING *s, bool is_8bit) |
| 101 | + { |
| 102 | + set(s, is_8bit, '\0'); |
| 103 | + } |
| 104 | + const char *pos() const { return str - is_quoted(); } |
| 105 | + const char *end() const { return str + length + is_quoted(); } |
| 106 | +}; |
| 107 | + |
| 108 | + |
| 109 | +class Lex_ident_cli: public Lex_ident_cli_st |
| 110 | +{ |
| 111 | +public: |
| 112 | + Lex_ident_cli(const LEX_CSTRING *s, bool is_8bit) |
| 113 | + { |
| 114 | + set_unquoted(s, is_8bit); |
| 115 | + } |
| 116 | + Lex_ident_cli(const char *s, size_t len) |
| 117 | + { |
| 118 | + set_ident(s, len, false); |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +#endif // LEX_IDENT_CLI |
0 commit comments