sqlstring.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2008, 2019, 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 _SQL_STRING_H_
  31. #define _SQL_STRING_H_
  32. #include <string>
  33. #include <algorithm>
  34. #include "build_config.h"
  35. #include <iostream>
  36. namespace sql
  37. {
  38. class SQLString
  39. {
  40. #ifdef _WIN32
  41. #pragma warning(push)
  42. #pragma warning(disable: 4251)
  43. #endif
  44. std::string realStr;
  45. #ifdef _WIN32
  46. #pragma warning(pop)
  47. #endif
  48. public:
  49. #ifdef _WIN32
  50. //TODO something less dirty-hackish.
  51. static const size_t npos = static_cast<std::string::size_type>(-1);
  52. #else
  53. static const size_t npos = std::string::npos;
  54. #endif
  55. ~SQLString() {}
  56. SQLString() {}
  57. SQLString(const SQLString & other) : realStr(other.realStr) {}
  58. SQLString(const std::string & other) : realStr(other) {}
  59. SQLString(const char other[]) : realStr(other) {}
  60. SQLString(const char * s, size_t n) : realStr(s, n) {}
  61. // Needed for stuff like SQLString str= "char * string constant"
  62. const SQLString & operator=(const char * s)
  63. {
  64. realStr = s;
  65. return *this;
  66. }
  67. const SQLString & operator=(const std::string & rhs)
  68. {
  69. realStr = rhs;
  70. return *this;
  71. }
  72. const SQLString & operator=(const SQLString & rhs)
  73. {
  74. realStr = rhs.realStr;
  75. return *this;
  76. }
  77. // Conversion to st::string. Comes in play for stuff like std::string str= SQLString_var;
  78. operator const std::string &() const
  79. {
  80. return realStr;
  81. }
  82. /** For access std::string methods. Not sure we need it. Makes it look like some smart ptr.
  83. possibly operator* - will look even more like smart ptr */
  84. std::string * operator ->()
  85. {
  86. return & realStr;
  87. }
  88. int compare(const SQLString& str) const
  89. {
  90. return realStr.compare(str.realStr);
  91. }
  92. int compare(const char * s) const
  93. {
  94. return realStr.compare(s);
  95. }
  96. int compare(size_t pos1, size_t n1, const char * s) const
  97. {
  98. return realStr.compare(pos1, n1, s);
  99. }
  100. int caseCompare(const SQLString &s) const
  101. {
  102. std::string tmp(realStr), str(s);
  103. std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
  104. std::transform(str.begin(), str.end(), str.begin(), ::tolower);
  105. return tmp.compare(str);
  106. }
  107. int caseCompare(const char * s) const
  108. {
  109. std::string tmp(realStr), str(s);
  110. std::transform(str.begin(), str.end(), str.begin(), ::tolower);
  111. std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
  112. return tmp.compare(str);
  113. }
  114. int caseCompare(size_t pos1, size_t n1, const char * s) const
  115. {
  116. std::string tmp(realStr.c_str() + pos1, n1), str(s);
  117. std::transform(str.begin(), str.end(), str.begin(), ::tolower);
  118. std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
  119. return tmp.compare(str);
  120. }
  121. const std::string & asStdString() const
  122. {
  123. return realStr;
  124. }
  125. const char * c_str() const
  126. {
  127. return realStr.c_str();
  128. }
  129. size_t length() const
  130. {
  131. return realStr.length();
  132. }
  133. SQLString & append(const std::string & str)
  134. {
  135. realStr.append(str);
  136. return *this;
  137. }
  138. SQLString & append(const char * s)
  139. {
  140. realStr.append(s);
  141. return *this;
  142. }
  143. const char& operator[](size_t pos) const
  144. {
  145. return realStr[pos];
  146. }
  147. size_t find(char c, size_t pos = 0) const
  148. {
  149. return realStr.find(c, pos);
  150. }
  151. size_t find(const SQLString & s, size_t pos = 0) const
  152. {
  153. return realStr.find(s.realStr, pos);
  154. }
  155. SQLString substr(size_t pos = 0, size_t n = npos) const
  156. {
  157. return realStr.substr(pos, n);
  158. }
  159. const SQLString& replace(size_t pos1, size_t n1, const SQLString & s)
  160. {
  161. realStr.replace(pos1, n1, s.realStr);
  162. return *this;
  163. }
  164. size_t find_first_of(char c, size_t pos = 0) const
  165. {
  166. return realStr.find_first_of(c, pos);
  167. }
  168. size_t find_last_of(char c, size_t pos = npos) const
  169. {
  170. return realStr.find_last_of(c, pos);
  171. }
  172. const SQLString & operator+=(const SQLString & op2)
  173. {
  174. realStr += op2.realStr;
  175. return *this;
  176. }
  177. };
  178. /*
  179. Operators that can and have to be not a member.
  180. */
  181. inline const SQLString operator+(const SQLString & op1, const SQLString & op2)
  182. {
  183. return sql::SQLString(op1.asStdString() + op2.asStdString());
  184. }
  185. inline bool operator ==(const SQLString & op1, const SQLString & op2)
  186. {
  187. return (op1.asStdString() == op2.asStdString());
  188. }
  189. inline bool operator !=(const SQLString & op1, const SQLString & op2)
  190. {
  191. return (op1.asStdString() != op2.asStdString());
  192. }
  193. inline bool operator <(const SQLString & op1, const SQLString & op2)
  194. {
  195. return op1.asStdString() < op2.asStdString();
  196. }
  197. }// namespace sql
  198. namespace std
  199. {
  200. // operator << for SQLString output
  201. inline ostream & operator << (ostream & os, const sql::SQLString & str )
  202. {
  203. return os << str.asStdString();
  204. }
  205. }
  206. #endif