1 /*
2    Copyright (c) 2018, 2020, 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-1301  USA */
16 
17 #ifndef VERS_STRING_INCLUDED
18 #define VERS_STRING_INCLUDED
19 
20 #include "lex_string.h"
21 
22 /*
23   LEX_CSTRING with comparison semantics.
24 */
25 
26 // db and table names: case sensitive (or insensitive) in table_alias_charset
27 struct Compare_table_names
28 {
operatorCompare_table_names29   int operator()(const LEX_CSTRING& a, const LEX_CSTRING& b) const
30   {
31     DBUG_ASSERT(a.str[a.length] == 0);
32     DBUG_ASSERT(b.str[b.length] == 0);
33     return table_alias_charset->strnncoll(a.str, a.length,
34                                           b.str, b.length);
35   }
36 };
37 
38 // column names and other identifiers: case insensitive in system_charset_info
39 struct Compare_identifiers
40 {
operatorCompare_identifiers41   int operator()(const LEX_CSTRING& a, const LEX_CSTRING& b) const
42   {
43     DBUG_ASSERT(a.str[a.length] == 0);
44     DBUG_ASSERT(b.str[b.length] == 0);
45     return my_strcasecmp(system_charset_info, a.str, b.str);
46   }
47 };
48 
49 
50 template <class Compare>
51 struct Lex_cstring_with_compare : public Lex_cstring
52 {
53 public:
Lex_cstring_with_compareLex_cstring_with_compare54   Lex_cstring_with_compare() {}
Lex_cstring_with_compareLex_cstring_with_compare55   Lex_cstring_with_compare(const char *_str, size_t _len) :
56     Lex_cstring(_str, _len)
57   { }
Lex_cstring_with_compareLex_cstring_with_compare58   Lex_cstring_with_compare(const LEX_STRING src) :
59     Lex_cstring(src.str, src.length)
60   { }
Lex_cstring_with_compareLex_cstring_with_compare61   Lex_cstring_with_compare(const LEX_CSTRING src) : Lex_cstring(src.str, src.length)
62   { }
Lex_cstring_with_compareLex_cstring_with_compare63   Lex_cstring_with_compare(const char *_str) : Lex_cstring(_str, strlen(_str))
64   { }
streqLex_cstring_with_compare65   bool streq(const Lex_cstring_with_compare& b) const
66   {
67     return Lex_cstring::length == b.length && 0 == Compare()(*this, b);
68   }
69   operator const char* () const
70   {
71     return str;
72   }
73   operator bool () const
74   {
75     return str != NULL;
76   }
77 };
78 
79 typedef Lex_cstring_with_compare<Compare_identifiers> Lex_ident;
80 typedef Lex_cstring_with_compare<Compare_table_names> Lex_table_name;
81 
82 #endif // VERS_STRING_INCLUDED
83