1/*
2 Copyright (c) 2018, 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/*
21 LEX_CSTRING with comparison semantics.
22*/
23
24// db and table names: case sensitive (or insensitive) in table_alias_charset
25struct Compare_table_names
26{
27 int operator()(const LEX_CSTRING& a, const LEX_CSTRING& b) const
28 {
29 DBUG_ASSERT(a.str[a.length] == 0);
30 DBUG_ASSERT(b.str[b.length] == 0);
31 return my_strnncoll(table_alias_charset,
32 (uchar*)a.str, a.length,
33 (uchar*)b.str, b.length);
34 }
35};
36
37// column names and other identifiers: case insensitive in system_charset_info
38struct Compare_identifiers
39{
40 int operator()(const LEX_CSTRING& a, const LEX_CSTRING& b) const
41 {
42 DBUG_ASSERT(a.str[a.length] == 0);
43 DBUG_ASSERT(b.str[b.length] == 0);
44 return my_strcasecmp(system_charset_info, a.str, b.str);
45 }
46};
47
48class Lex_cstring : public LEX_CSTRING
49{
50 public:
51 Lex_cstring()
52 {
53 str= NULL;
54 length= 0;
55 }
56 Lex_cstring(const char *_str, size_t _len)
57 {
58 str= _str;
59 length= _len;
60 }
61 void set(const char *_str, size_t _len)
62 {
63 str= _str;
64 length= _len;
65 }
66};
67
68template <class Compare>
69struct Lex_cstring_with_compare : public Lex_cstring
70{
71public:
72 Lex_cstring_with_compare() {}
73 Lex_cstring_with_compare(const char *_str, size_t _len) :
74 Lex_cstring(_str, _len)
75 { }
76 Lex_cstring_with_compare(const LEX_STRING src) :
77 Lex_cstring(src.str, src.length)
78 { }
79 Lex_cstring_with_compare(const LEX_CSTRING src) : Lex_cstring(src.str, src.length)
80 { }
81 Lex_cstring_with_compare(const char *_str) : Lex_cstring(_str, strlen(_str))
82 { }
83 bool streq(const Lex_cstring_with_compare& b) const
84 {
85 return Lex_cstring::length == b.length && 0 == Compare()(*this, b);
86 }
87 operator const char* () const
88 {
89 return str;
90 }
91 operator bool () const
92 {
93 return str != NULL;
94 }
95};
96
97typedef Lex_cstring_with_compare<Compare_identifiers> Lex_ident;
98typedef Lex_cstring_with_compare<Compare_table_names> Lex_table_name;
99
100#define XSTRING_WITH_LEN(X) (X).ptr(), (X).length()
101#define DB_WITH_LEN(X) (X).db.str, (X).db.length
102#define TABLE_NAME_WITH_LEN(X) (X).table_name.str, (X).table_name.length
103
104#endif // VERS_STRING_INCLUDED
105