MySQL Connector/C++ 9.1.0
MySQL connector library for C and C++ applications
collations.h
1/*
2 * Copyright (c) 2016, 2024, Oracle and/or its affiliates.
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, version 2.0, as
6 * published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms, as
10 * designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * Without limiting anything contained in the foregoing, this file,
17 * which is part of Connector/C++, is also subject to the
18 * Universal FOSS Exception, version 1.0, a copy of which can be found at
19 * https://oss.oracle.com/licenses/universal-foss-exception.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 * See the GNU General Public License, version 2.0, for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30
31#ifndef MYSQLX_COLLATIONS_H
32#define MYSQLX_COLLATIONS_H
33
34#include "common.h"
35
36/*
37 Import Lists of built-in character sets and collations supported by MySQL
38 Server.
39*/
40
41#include "mysql_charsets.h"
42#include "mysql_collations.h"
43
44
45namespace mysqlx {
46MYSQLX_ABI_BEGIN(2,0)
47
48/*
49 Enumeration of known character sets. For each character set CS listed
50 in CDK_CS_LIST() macro, there is CharacterSet::CS constant in this
51 enumeration.
52*/
53
54
55enum class CharacterSet : unsigned short
56{
57 #undef CS
58 #define CS_ENUM(CS) CS,
59 CDK_CS_LIST(CS_ENUM)
60};
61
62
63#define CS_NAME_SWITCH(CS) case CharacterSet::CS: return #CS;
64
71inline
72const char* characterSetName(CharacterSet id)
73{
74 switch (id)
75 {
76 CDK_CS_LIST(CS_NAME_SWITCH)
77 default:
78 THROW("Unknown character set id");
79 }
80}
81
82
95struct CollationInfo
96{
98
99 unsigned id() const { return m_id; }
100
102
103 const char *getName() const { return m_name; }
104
106
107 CharacterSet getCharacterSet() const { return m_cs; }
108
114 bool isCaseSensitive() const { return m_case != case_ci; }
115
117
118 bool isBinary() const { return m_case == case_bin; }
119
120
121 bool operator==(const CollationInfo &other) const
122 {
123 return m_id == other.m_id;
124 }
125
126 bool operator!=(const CollationInfo &other) const
127 {
128 return !operator==(other);
129 }
130
131private:
132
133 enum coll_case { case_bin, case_ci, case_cs };
134
135 CharacterSet m_cs;
136 unsigned m_id;
137 coll_case m_case;
138 const char *m_name;
139
140public:
141
142 struct Access;
143 friend Access;
144};
145
146
147template <CharacterSet CS> struct Collation;
148
149
150/*
151 The Collation<CS>::COLL constants are generated from information provided by
152 COLLATION_XXX() macros.Each line X(CS, ID, COLL, CASE) in the expansion of
153 a COLLATION_XXX() macro declares collation with name COLL for character set
154 CS. ID is the MySQL id number for the collation. CASE is one of ci, cs or bin
155 and indicates whether it is case insensitive, sensitive or binary collation,
156 respectively.
157*/
158
159/*
160 Generate declarations of Collation<CS>::COLL constants using
161 COLLATINS_XXX() macros. The list CS_LIST() is processed using
162 COLL_DECL() macro, which for each character set CS declares
163 specialization Collation<CS> of the Collation<> template and
164 declares collation constants within this specialization.
165
166 The collation constant declarations are generated by processing
167 COLLATIONS_CS() list with COLL_CONST() macro. The name of each
168 constant is generated by COLL_CONST_NAME() macro. The convention
169 used is that case insensitive and case sensitive collations get
170 a _ci or _cs suffix, respectively, while binary collation constant
171 have no suffix to the collation name.
172
173 Collation constants declared here are defined in file result.cc.
174*/
175
176#define COLL_DECL(CS) \
177template<> struct Collation<CharacterSet::CS> \
178{ COLLATIONS_##CS(COLL_CONST) }; \
179
180#define COLL_CONST(CS,ID,COLL,CASE) \
181static PUBLIC_API const CollationInfo COLL_CONST_NAME(COLL,CASE);
182
183#define COLL_CONST_NAME(COLL,CASE) COLL_CONST_NAME_##CASE(COLL)
184
185#define COLL_CONST_NAME_bin(COLL) COLL
186#define COLL_CONST_NAME_ci(COLL) COLL##_ci
187#define COLL_CONST_NAME_ai_ci(COLL) COLL##_ai_ci
188#define COLL_CONST_NAME_cs(COLL) COLL##_cs
189#define COLL_CONST_NAME_as_cs(COLL) COLL##_as_cs
190#define COLL_CONST_NAME_as_ci(COLL) COLL##_as_ci
191#define COLL_CONST_NAME_as_cs_ks(COLL) COLL##_as_cs_ks
192
193// Add utf8mb4 alias for bin collation for compatibility
194
195#undef COLLATIONS_utf8mb4_EXTRA
196#define COLLATIONS_utf8mb4_EXTRA \
197static PUBLIC_API const CollationInfo utf8mb4;
198
199CDK_CS_LIST(COLL_DECL)
200
201#undef COLLATIONS_utf8mb4_EXTRA
202#define COLLATIONS_utf8mb4_EXTRA
203
204
205MYSQLX_ABI_END(2,0)
206} // mysqlx
207
208#endif
const char * characterSetName(CharacterSet id)
Returns name of a character set given by its id.
Definition: collations.h:71
Structure that provides information about character set collation.
Definition: collations.h:94
bool isCaseSensitive() const
Returns true if given collation is case sensitive.
Definition: collations.h:112
bool isBinary() const
Returns true if this is binary collation.
Definition: collations.h:116