# mystring.h

Go to the documentation of this file.

    00001
    00002
    00003
    00004
    00005 /***********************************************************************
    00006  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
    00007  (c) 2004-2008 by Educational Technology Resources, Inc.  Others may
    00008  also hold copyrights on code in this file.  See the CREDITS file in
    00009  the top directory of the distribution for details.
    00010
    00011  This file is part of MySQL++.
    00012
    00013  MySQL++ is free software; you can redistribute it and/or modify it
    00014  under the terms of the GNU Lesser General Public License as published
    00015  by the Free Software Foundation; either version 2.1 of the License, or
    00016  (at your option) any later version.
    00017
    00018  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
    00019  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    00020  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
    00021  License for more details.
    00022
    00023  You should have received a copy of the GNU Lesser General Public
    00024  License along with MySQL++; if not, write to the Free Software
    00025  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00026  USA
    00027 ***********************************************************************/
    00028
    00029 #if !defined(MYSQLPP_MYSTRING_H)
    00030 #define MYSQLPP_MYSTRING_H
    00031
    00032 #include "common.h"
    00033
    00034 #include "datetime.h"
    00035 #include "exceptions.h"
    00036 #include "null.h"
    00037 #include "sql_buffer.h"
    00038
    00039 #include <string>
    00040 #include <sstream>
    00041 #include <limits>
    00042
    00043 #include <stdlib.h>
    00044 #include <string.h>
    00045
    00046 namespace mysqlpp {
    00047
    00048 #if !defined(DOXYGEN_IGNORE)
    00049 // Doxygen will not generate documentation for this section.
    00050
    00051 namespace detail
    00052 {
    00053         template<typename T, bool is_signed = std::numeric_limits<T>::is_signed>
    00054         struct conv_promotion;
    00055
    00056         template<>
    00057         struct conv_promotion<float>
    00058         {
    00059                 typedef double type;
    00060         };
    00061
    00062         template<>
    00063         struct conv_promotion<double>
    00064         {
    00065                 typedef double type;
    00066         };
    00067
    00068 #       if !defined(NO_LONG_LONGS)
    00069         template<>
    00070         struct conv_promotion<unsigned long long>
    00071         {
    00072                 typedef unsigned long long type;
    00073         };
    00074
    00075         template<>
    00076         struct conv_promotion<long long>
    00077         {
    00078                 typedef long long type;
    00079         };
    00080 #       endif
    00081
    00082         // preserve existing behaviour, char converted as signed long
    00083         template<>
    00084         struct conv_promotion<char>
    00085         {
    00086                 typedef long type;
    00087         };
    00088
    00089         // all other types use signed/unsigned long
    00090
    00091         template<typename T>
    00092         struct conv_promotion<T, true>
    00093         {
    00094                 typedef long type;
    00095         };
    00096
    00097         template<typename T>
    00098         struct conv_promotion<T, false>
    00099         {
    00100                 typedef unsigned long type;
    00101         };
    00102 } // namespace detail
    00103
    00104 class MYSQLPP_EXPORT SQLTypeAdapter;
    00105 #endif // !defined(DOXYGEN_IGNORE)
    00106
    00138
    00139 class MYSQLPP_EXPORT String
    00140 {
    00141 public:
    00144         typedef const char value_type;
    00145
    00147         typedef unsigned int size_type;
    00148
    00150         typedef const char* const_iterator;
    00151
    00154         typedef const_iterator iterator;
    00155
    00156 #if !defined(DOXYGEN_IGNORE)
    00157 // Doxygen will not generate documentation for this section.
    00158         typedef int difference_type;
    00159         typedef const char* const_pointer;
    00160         typedef const_pointer pointer;
    00161 #endif // !defined(DOXYGEN_IGNORE)
    00162
    00167         String() :
    00168         buffer_()
    00169         {
    00170         }
    00171
    00179         String(const String& other) :
    00180         buffer_(other.buffer_)
    00181         {
    00182         }
    00183
    00196         explicit String(const char* str, size_type len,
    00197                         mysql_type_info type = mysql_type_info::string_type,
    00198                         bool is_null = false) :
    00199         buffer_(new SQLBuffer(str, len, type, is_null))
    00200         {
    00201         }
    00202
    00210         explicit String(const std::string& str,
    00211                         mysql_type_info type = mysql_type_info::string_type,
    00212                         bool is_null = false) :
    00213         buffer_(new SQLBuffer(str.data(), static_cast<size_type>(str.length()),
    00214                         type, is_null))
    00215         {
    00216         }
    00217
    00225         explicit String(const char* str,
    00226                         mysql_type_info type = mysql_type_info::string_type,
    00227                         bool is_null = false) :
    00228         buffer_(new SQLBuffer(str, static_cast<size_type>(strlen(str)),
    00229                         type, is_null))
    00230         {
    00231         }
    00232
    00234         ~String() { }
    00235
    00242         void assign(const char* str, size_type len,
    00243                         mysql_type_info type = mysql_type_info::string_type,
    00244                         bool is_null = false)
    00245         {
    00246                 buffer_ = new SQLBuffer(str, len, type, is_null);
    00247         }
    00248
    00255         void assign(const std::string& str,
    00256                         mysql_type_info type = mysql_type_info::string_type,
    00257                         bool is_null = false)
    00258         {
    00259                 buffer_ = new SQLBuffer(str.data(),
    00260                                 static_cast<size_type>(str.length()), type, is_null);
    00261         }
    00262
    00269         void assign(const char* str,
    00270                         mysql_type_info type = mysql_type_info::string_type,
    00271                         bool is_null = false)
    00272         {
    00273                 buffer_ = new SQLBuffer(str, static_cast<size_type>(strlen(str)),
    00274                                 type, is_null);
    00275         }
    00276
    00281         char at(size_type pos) const;
    00282
    00285         const_iterator begin() const { return data(); }
    00286
    00288         const char* c_str() const { return data(); }
    00289
    00290 #if defined(MYSQLPP_PLATFORM_VISUAL_CPP)
    00291 // Squish VC++ warning about "possible loss of data" for these conversions
    00292 #   pragma warning(disable: 4244)
    00293 #endif
    00294
    00297         template <class Type>
    00298         Type conv(Type) const
    00299         {
    00300                 // Conversions are done using one of double/long/ulong/llong/ullong
    00301                 // so we call a helper function to do the work using that type.
    00302                 // This reduces the amount of template code instantiated.
    00303                 typedef typename detail::conv_promotion<Type>::type conv_type;
    00304                 return do_conv<conv_type>(typeid(Type).name());
    00305         }
    00306
    00307 #if defined(MYSQLPP_PLATFORM_VISUAL_CPP)
    00308 #   pragma warning(default: 4244)
    00309 #endif
    00310
    00317         template <class T, class B>
    00318         Null<T, B> conv(Null<T, B>) const
    00319         {
    00320                 if (is_null()) {
    00321                         return Null<T, B>(null);
    00322                 }
    00323                 else {
    00324                         return Null<T, B>(conv(T()));
    00325                 }
    00326         }
    00327
    00333         int compare(const String& other) const;
    00334
    00340         int compare(const std::string& other) const;
    00341
    00350         int compare(size_type pos, size_type num, std::string& other) const;
    00351
    00357         int compare(const char* other) const;
    00358
    00369         int compare(size_type pos, size_type num, const char* other) const;
    00370
    00373         const char* data() const;
    00374
    00377         const_iterator end() const;
    00378
    00381         bool escape_q() const;
    00382
    00384         bool is_null() const;
    00385
    00387         void it_is_null();
    00388
    00390         size_type length() const;
    00391
    00397         size_type max_size() const { return size(); }
    00398
    00401         bool quote_q() const;
    00402
    00404         size_type size() const { return length(); }
    00405
    00408     void strip_leading_blanks(std::string& s) const
    00409     {
    00410         const char* pc = data();
    00411         if (pc) {
    00412             size_type n = length();
    00413             while (n && (*pc == ' ')) {
    00414                 ++pc;
    00415                 --n;
    00416             }
    00417
    00418             s.assign(pc, n);
    00419         }
    00420         else {
    00421             s.clear();
    00422         }
    00423     }
    00424
    00432         void to_string(std::string& s) const;
    00433
    00435         mysql_type_info type() const
    00436         {
    00437                 return buffer_ ? buffer_->type() : mysql_type_info::string_type;
    00438         }
    00439
    00441         String& operator =(const std::string& rhs)
    00442         {
    00443                 buffer_ = new SQLBuffer(rhs.data(),
    00444                                 static_cast<size_type>(rhs.length()),
    00445                                 mysql_type_info::string_type, false);
    00446
    00447                 return *this;
    00448         }
    00449
    00454         String& operator =(const char* str)
    00455         {
    00456                 buffer_ = new SQLBuffer(str,
    00457                                 static_cast<size_type>(strlen(str)),
    00458                                 mysql_type_info::string_type, false);
    00459
    00460                 return *this;
    00461         }
    00462
    00468         String& operator =(const String& other)
    00469         {
    00470                 buffer_ = other.buffer_;
    00471
    00472                 return *this;
    00473         }
    00474
    00479         template <typename T>
    00480         bool operator ==(const T& rhs) const
    00481         {
    00482                 return compare(rhs) == 0;
    00483         }
    00484
    00488         bool operator ==(const mysqlpp::null_type&) const
    00489         {
    00490                 return is_null();
    00491         }
    00492
    00497         template <typename T>
    00498         bool operator !=(const T& rhs) const
    00499         {
    00500                 return compare(rhs) != 0;
    00501         }
    00502
    00506         bool operator !=(const mysqlpp::null_type&) const
    00507         {
    00508                 return !is_null();
    00509         }
    00510
    00515         char operator [](size_type pos) const;
    00516
    00518         operator const char*() const { return data(); }
    00519
    00521         operator signed char() const
    00522                         { return conv(static_cast<signed char>(0)); }
    00523
    00525         operator unsigned char() const
    00526                         { return conv(static_cast<unsigned char>(0)); }
    00527
    00529         operator int() const
    00530                         { return conv(static_cast<int>(0)); }
    00531
    00533         operator unsigned int() const
    00534                         { return conv(static_cast<unsigned int>(0)); }
    00535
    00537         operator short int() const
    00538                         { return conv(static_cast<short int>(0)); }
    00539
    00542         operator unsigned short int() const
    00543                         { return conv(static_cast<unsigned short int>(0)); }
    00544
    00546         operator long int() const
    00547                         { return conv(static_cast<long int>(0)); }
    00548
    00551         operator unsigned long int() const
    00552                         { return conv(static_cast<unsigned long int>(0)); }
    00553
    00554 #if !defined(NO_LONG_LONGS)
    00557         operator longlong() const
    00558                         { return conv(static_cast<longlong>(0)); }
    00559
    00562         operator ulonglong() const
    00563                         { return conv(static_cast<ulonglong>(0)); }
    00564 #endif
    00565
    00567         operator float() const
    00568                         { return conv(static_cast<float>(0)); }
    00569
    00571         operator double() const
    00572                         { return conv(static_cast<double>(0)); }
    00573
    00575         operator bool() const { return buffer_ ? atoi(c_str()) : false; }
    00576
    00578         operator Date() const { return buffer_ ? Date(*this) : Date(); }
    00579
    00581         operator DateTime() const
    00582                         { return buffer_ ? DateTime(*this) : DateTime(); }
    00583
    00585         operator Time() const { return buffer_ ? Time(*this) : Time(); }
    00586
    00590         template <class T, class B>
    00591         operator Null<T, B>() const { return conv(Null<T, B>()); }
    00592
    00593 private:
    00595         template <class Type>
    00596         Type do_conv(const char* type_name) const
    00597         {
    00598                 if (buffer_) {
    00599                         std::stringstream buf;
    00600                         buf.write(data(), length());
    00601                         buf.imbue(std::locale::classic()); // "C" locale
    00602                         Type num = Type();
    00603
    00604                         if (buf >> num) {
    00605                                 char c;
    00606                                 if (!(buf >> c)) {
    00607                                         // Nothing left in buffer, so conversion complete,
    00608                                         // and thus successful.
    00609                                         return num;
    00610                                 }
    00611
    00612                                 if (c == '.' &&
    00613                                                 (typeid(Type) != typeid(float)) &&
    00614                                                 (typeid(Type) != typeid(double))) {
    00615                                         // Conversion stopped on a decimal point -- locale
    00616                                         // doesn't matter to MySQL -- so only way to succeed
    00617                                         // is if it's an integer and everything following
    00618                                         // the decimal is inconsequential.
    00619                                         c = '0';        // handles '.' at end of string
    00620                                         while (buf >> c && c == '0') /* spin */ ;
    00621                                         if (buf.eof() && c == '0') {
    00622                                                 return num;  // only zeros after decimal point
    00623                                         }
    00624                                 }
    00625                         }
    00626                         else if (buf.eof()) {
    00627                                 return num;  // nothing to convert, return default value
    00628                         }
    00629
    00630                         throw BadConversion(type_name, data(), 0, length());
    00631                 }
    00632                 else {
    00633                         return 0;
    00634                 }
    00635         }
    00636
    00637         RefCountedBuffer buffer_;
    00638
    00639         friend class SQLTypeAdapter;
    00640 };
    00641
    00642 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& o,
    00643                 const String& in);
    00644
    00645
    00646 #if !defined(MYSQLPP_NO_BINARY_OPERS) && !defined(DOXYGEN_IGNORE)
    00647 // Ignore this section is MYSQLPP_NO_BINARY_OPERS is defined, or if this
    00648 // section is being parsed by Doxygen.  In the latter case, it's ignored
    00649 // because Doxygen doesn't understand it correctly, and we can't be
    00650 // bothered to explain it to Doxygen.
    00651
    00652 #define oprsw(opr, other, conv) \
    00653         inline other operator opr (String x, other y) \
    00654                         { return static_cast<conv>(x) opr y; } \
    00655         inline other operator opr (other x, String y) \
    00656                         { return x opr static_cast<conv>(y); }
    00657
    00658 #define operator_binary(other, conv) \
    00659   oprsw(+, other, conv) \
    00660   oprsw(-, other, conv) \
    00661   oprsw(*, other, conv) \
    00662   oprsw(/, other, conv)
    00663
    00664 #define operator_binary_int(other, conv) \
    00665   operator_binary(other, conv) \
    00666   oprsw(%, other, conv) \
    00667   oprsw(&, other, conv) \
    00668   oprsw(^, other, conv) \
    00669   oprsw(|, other, conv) \
    00670   oprsw(<<, other, conv) \
    00671   oprsw(>>, other, conv)
    00672
    00673 // Squish more complaints about possible loss of data
    00674 #if defined(MYSQLPP_PLATFORM_VISUAL_CPP)
    00675 #       pragma warning(disable: 4244)
    00676 #endif
    00677
    00678 operator_binary(float, double)
    00679 operator_binary(double, double)
    00680
    00681 operator_binary_int(char, long int)
    00682 operator_binary_int(int, long int)
    00683 operator_binary_int(short int, long int)
    00684 operator_binary_int(long int, long int)
    00685
    00686 operator_binary_int(unsigned char, unsigned long int)
    00687 operator_binary_int(unsigned int, unsigned long int)
    00688 operator_binary_int(unsigned short int, unsigned long int)
    00689 operator_binary_int(unsigned long int, unsigned long int)
    00690
    00691 #if defined(MYSQLPP_PLATFORM_VISUAL_CPP)
    00692 #       pragma warning(default: 4244)
    00693 #endif
    00694
    00695 #if !defined(NO_LONG_LONGS)
    00696 operator_binary_int(longlong, longlong)
    00697 operator_binary_int(ulonglong, ulonglong)
    00698 #endif // !defined(NO_LONG_LONGS)
    00699 #endif // !defined(MYSQLPP_NO_BINARY_OPERS) && !defined(DOXYGEN_IGNORE)
    00700
    00701
    00702 #if !defined(DOXYGEN_IGNORE)
    00703 // Doxygen isn't smart enough to recognize these template
    00704 // specializations.  Maybe it's the MYSQLPP_EXPORT tags?
    00705
    00711 template <> MYSQLPP_EXPORT bool String::conv(bool) const;
    00712
    00725 template <> MYSQLPP_EXPORT String String::conv(String) const;
    00726
    00728 template <> MYSQLPP_EXPORT std::string String::conv(std::string) const;
    00729
    00735 template <> MYSQLPP_EXPORT Date String::conv(Date) const;
    00736
    00742 template <> MYSQLPP_EXPORT DateTime String::conv(DateTime) const;
    00743
    00749 template <> MYSQLPP_EXPORT Time String::conv(Time) const;
    00750
    00751 #endif // !defined(DOXYGEN_IGNORE)
    00752
    00753 } // end namespace mysqlpp
    00754
    00755 #endif // !defined(MYSQLPP_MYSTRING_H)