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