sqlstring.h 6.1 KB

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