collations.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
  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 also distributed with certain software (including
  9. * but not limited to OpenSSL) that is licensed under separate terms,
  10. * as designated in a particular file or component or in included license
  11. * documentation. The authors of MySQL hereby grant you an
  12. * additional permission to link the program and your derivative works
  13. * with the separately licensed software that they have included with
  14. * MySQL.
  15. *
  16. * Without limiting anything contained in the foregoing, this file,
  17. * which is part of MySQL Connector/C++, is also subject to the
  18. * Universal FOSS Exception, version 1.0, a copy of which can be found at
  19. * http://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. #ifndef MYSQLX_COLLATIONS_H
  31. #define MYSQLX_COLLATIONS_H
  32. #include "common.h"
  33. /*
  34. Import Lists of built-in character sets and collations supported by MySQL
  35. Server.
  36. */
  37. #include "mysql_charsets.h"
  38. #include "mysql_collations.h"
  39. namespace mysqlx {
  40. /*
  41. Enumeration of known character sets. For each character set CS listed
  42. in CDK_CS_LIST() macro, there is CharacterSet::CS constant in this
  43. enumeration.
  44. */
  45. enum class CharacterSet : unsigned short
  46. {
  47. #undef CS
  48. #define CS_ENUM(CS) CS,
  49. CDK_CS_LIST(CS_ENUM)
  50. };
  51. #define CS_NAME_SWITCH(CS) case CharacterSet::CS: return #CS;
  52. /**
  53. Returns name of a character set given by its id.
  54. @ingroup devapi_res
  55. */
  56. inline
  57. const char* characterSetName(CharacterSet id)
  58. {
  59. switch (id)
  60. {
  61. CDK_CS_LIST(CS_NAME_SWITCH)
  62. default:
  63. THROW("Unknown character set id");
  64. }
  65. }
  66. /**
  67. Structure that provides information about character set collation.
  68. For each known collation COLL over character set CS, there is static object
  69. `Collation<CS>::%COLL` of type `CollationInfo` which describes
  70. collation COLL. For example `Collation<CharacterSet::latin1>::%swedish_ci` is
  71. an `CollationInfo` object which describes the `swedish_ci` collation over
  72. `latin1` character set.
  73. @ingroup devapi_res
  74. */
  75. struct CollationInfo
  76. {
  77. /// Numeric collation id, as used by MySQL server.
  78. unsigned id() const { return m_id; }
  79. /// String name of a collation, such as "latin1_generic_ci".
  80. const char *getName() const { return m_name; }
  81. /// Id of the character set of this collation.
  82. CharacterSet getCharacterSet() const { return m_cs; }
  83. /**
  84. Returns true if given collation is case sensitive. Binary
  85. collations are assumed to be case sensitive.
  86. */
  87. bool isCaseSensitive() const { return m_case != case_ci; }
  88. /// Returns true if this is binary collation.
  89. bool isBinary() const { return m_case == case_bin; }
  90. bool operator==(const CollationInfo &other) const
  91. {
  92. return m_id == other.m_id;
  93. }
  94. bool operator!=(const CollationInfo &other) const
  95. {
  96. return !operator==(other);
  97. }
  98. private:
  99. enum coll_case { case_bin, case_ci, case_cs };
  100. CharacterSet m_cs;
  101. unsigned m_id;
  102. coll_case m_case;
  103. const char *m_name;
  104. public:
  105. struct Access;
  106. friend Access;
  107. };
  108. template <CharacterSet CS> struct Collation;
  109. /*
  110. The Collation<CS>::COLL constants are generated from information provided by
  111. COLLATION_XXX() macros.Each line X(CS, ID, COLL, CASE) in the expansion of
  112. a COLLATION_XXX() macro declares collation with name COLL for character set
  113. CS. ID is the MySQL id number for the collation. CASE is one of ci, cs or bin
  114. and indicates whether it is case insensitive, sensitive or binary collation,
  115. respectively.
  116. */
  117. /*
  118. Generate declarations of Collation<CS>::COLL constants using
  119. COLLATINS_XXX() macros. The list CS_LIST() is processed using
  120. COLL_DECL() macro, which for each character set CS declares
  121. specialization Collation<CS> of the Collation<> template and
  122. declares collation constants within this specialization.
  123. The collation constant declarations are generated by processing
  124. COLLATIONS_CS() list with COLL_CONST() macro. The name of each
  125. constant is generated by COLL_CONST_NAME() macro. The convention
  126. used is that case insensitive and case sensitive collations get
  127. a _ci or _cs suffix, respectively, while binary collation constant
  128. have no suffix to the collation name.
  129. Collation constants declared here are defined in file result.cc.
  130. */
  131. #define COLL_DECL(CS) \
  132. template<> struct Collation<CharacterSet::CS> \
  133. { COLLATIONS_##CS(COLL_CONST) }; \
  134. #define COLL_CONST(CS,ID,COLL,CASE) \
  135. static PUBLIC_API const CollationInfo COLL_CONST_NAME(COLL,CASE);
  136. #define COLL_CONST_NAME(COLL,CASE) COLL_CONST_NAME_##CASE(COLL)
  137. #define COLL_CONST_NAME_bin(COLL) COLL
  138. #define COLL_CONST_NAME_ci(COLL) COLL##_ci
  139. #define COLL_CONST_NAME_cs(COLL) COLL##_cs
  140. CDK_CS_LIST(COLL_DECL)
  141. } // mysqlx
  142. #endif