Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members

# coldata.h

Go to the documentation of this file.

    00001
    00002
    00003
    00004
    00005
    00006
    00007
    00008 /***********************************************************************
    00009  Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
    00010  MySQL AB, and (c) 2004, 2005 by Educational Technology Resources, Inc.
    00011  Others may also hold copyrights on code in this file.  See the CREDITS
    00012  file in the top directory of the distribution for details.
    00013
    00014  This file is part of MySQL++.
    00015
    00016  MySQL++ is free software; you can redistribute it and/or modify it
    00017  under the terms of the GNU Lesser General Public License as published
    00018  by the Free Software Foundation; either version 2.1 of the License, or
    00019  (at your option) any later version.
    00020
    00021  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
    00022  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    00023  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
    00024  License for more details.
    00025
    00026  You should have received a copy of the GNU Lesser General Public
    00027  License along with MySQL++; if not, write to the Free Software
    00028  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00029  USA
    00030 ***********************************************************************/
    00031
    00032 #ifndef MYSQLPP_COLDATA_H
    00033 #define MYSQLPP_COLDATA_H
    00034
    00035 #include "platform.h"
    00036
    00037 #include "const_string.h"
    00038 #include "convert.h"
    00039 #include "defs.h"
    00040 #include "exceptions.h"
    00041 #include "null.h"
    00042 #include "string_util.h"
    00043 #include "type_info.h"
    00044
    00045 #include <mysql.h>
    00046
    00047 #include <typeinfo>
    00048 #include <string>
    00049
    00050 #include <stdlib.h>
    00051
    00052 namespace mysqlpp {
    00053
    00084
    00085 template <class Str>
    00086 class ColData_Tmpl : public Str
    00087 {
    00088 public:
    00096         ColData_Tmpl() :
    00097         null_(false)
    00098         {
    00099         }
    00100
    00106         explicit ColData_Tmpl(bool n,
    00107                         mysql_type_info t = mysql_type_info::string_type) :
    00108         type_(t),
    00109         null_(n)
    00110         {
    00111         }
    00112
    00118         explicit ColData_Tmpl(const char* str,
    00119                         mysql_type_info t = mysql_type_info::string_type,
    00120                         bool n = false) :
    00121         Str(str),
    00122         type_(t),
    00123         buf_(str),
    00124         null_(n)
    00125         {
    00126         }
    00127
    00129         mysql_type_info type() const
    00130         {
    00131                 return type_;
    00132         }
    00133
    00136         bool quote_q() const
    00137         {
    00138                 return type_.quote_q();
    00139         }
    00140
    00143         bool escape_q() const
    00144         {
    00145                 return type_.escape_q();
    00146         }
    00147
    00149         template <class Type> Type conv(Type dummy) const;
    00150
    00152         void it_is_null() { null_ = true; }
    00153
    00155         inline const bool is_null() const { return null_; }
    00156
    00158         inline const std::string& get_string() const
    00159         {
    00160                 return buf_;
    00161         }
    00162
    00165         operator cchar*() const { return buf_.c_str(); }
    00166
    00168         operator signed char() const
    00169                         { return conv(static_cast<signed char>(0)); }
    00170
    00172         operator unsigned char() const
    00173                         { return conv(static_cast<unsigned char>(0)); }
    00174
    00176         operator int() const
    00177                         { return conv(static_cast<int>(0)); }
    00178
    00180         operator unsigned int() const
    00181                         { return conv(static_cast<unsigned int>(0)); }
    00182
    00184         operator short int() const
    00185                         { return conv(static_cast<short int>(0)); }
    00186
    00189         operator unsigned short int() const
    00190                         { return conv(static_cast<unsigned short int>(0)); }
    00191
    00193         operator long int() const
    00194                         { return conv(static_cast<long int>(0)); }
    00195
    00198         operator unsigned long int() const
    00199                         { return conv(static_cast<unsigned long int>(0)); }
    00200
    00201 #if !defined(NO_LONG_LONGS)
    00204         operator longlong() const
    00205                         { return conv(static_cast<longlong>(0)); }
    00206
    00209         operator ulonglong() const
    00210                         { return conv(static_cast<ulonglong>(0)); }
    00211 #endif
    00212
    00214         operator float() const
    00215                         { return conv(static_cast<float>(0)); }
    00216
    00218         operator double() const
    00219                         { return conv(static_cast<double>(0)); }
    00220
    00222         operator bool() const { return conv(0); }
    00223
    00224         template <class T, class B> operator Null<T, B>() const;
    00225
    00226 private:
    00227         mysql_type_info type_;
    00228         std::string buf_;
    00229         bool null_;
    00230 };
    00231
    00234 typedef ColData_Tmpl<const_string> ColData;
    00235
    00238 typedef ColData_Tmpl<std::string> MutableColData;
    00239
    00240
    00241 #if !defined(NO_BINARY_OPERS) && !defined(DOXYGEN_IGNORE)
    00242 // Ignore this section is NO_BINARY_OPERS is defined, or if this section
    00243 // is being parsed by Doxygen.  In the latter case, it's ignored because
    00244 // Doxygen doesn't understand it correctly, and we can't be bothered to
    00245 // explain it to Doxygen.
    00246
    00247 #define oprsw(opr, other, conv) \
    00248   template<class Str> \
    00249   inline other operator opr (ColData_Tmpl<Str> x, other y) \
    00250     {return static_cast<conv>(x) opr y;} \
    00251   template<class Str> \
    00252   inline other operator opr (other x, ColData_Tmpl<Str> y) \
    00253     {return x opr static_cast<conv>(y);}
    00254
    00255 #define operator_binary(other, conv) \
    00256   oprsw(+, other, conv) \
    00257   oprsw(-, other, conv) \
    00258   oprsw(*, other, conv) \
    00259   oprsw(/, other, conv)
    00260
    00261 #define operator_binary_int(other, conv) \
    00262   operator_binary(other, conv) \
    00263   oprsw(%, other, conv) \
    00264   oprsw(&, other, conv) \
    00265   oprsw(^, other, conv) \
    00266   oprsw(|, other, conv) \
    00267   oprsw(<<, other, conv) \
    00268   oprsw(>>, other, conv)
    00269
    00270 operator_binary(float, double)
    00271 operator_binary(double, double)
    00272
    00273 operator_binary_int(char, long int)
    00274 operator_binary_int(int, long int)
    00275 operator_binary_int(short int, long int)
    00276 operator_binary_int(long int, long int)
    00277
    00278 operator_binary_int(unsigned char, unsigned long int)
    00279 operator_binary_int(unsigned int, unsigned long int)
    00280 operator_binary_int(unsigned short int, unsigned long int)
    00281 operator_binary_int(unsigned long int, unsigned long int)
    00282
    00283 #if !defined(NO_LONG_LONGS)
    00284 operator_binary_int(longlong, longlong)
    00285 operator_binary_int(ulonglong, ulonglong)
    00286 #endif
    00287 #endif // NO_BINARY_OPERS
    00288
    00290
    00296 template <class Str> template<class T, class B>
    00297 ColData_Tmpl<Str>::operator Null<T, B>() const
    00298 {
    00299         if ((Str::size() == 4) &&
    00300                         (*this)[0] == 'N' &&
    00301                         (*this)[1] == 'U' &&
    00302                         (*this)[2] == 'L' &&
    00303                         (*this)[3] == 'L') {
    00304                 return Null<T, B>(null);
    00305         }
    00306         else {
    00307                 return Null<T, B>(conv(T()));
    00308         }
    00309 }
    00310
    00311 template <class Str> template <class Type>
    00312 Type ColData_Tmpl<Str>::conv(Type /* dummy */) const
    00313 {
    00314         std::string strbuf = buf_;
    00315         strip_all_blanks(strbuf);
    00316         size_t len = strbuf.size();
    00317         const char* str = strbuf.c_str();
    00318         const char* end = str;
    00319         Type num = mysql_convert<Type>(str, end);
    00320
    00321         if (*end == '.') {
    00322                 ++end;
    00323                 for (; *end == '0'; ++end) ;
    00324         }
    00325
    00326         if (*end != '\0' && end != 0) {
    00327                 throw BadConversion(typeid(Type).name(), Str::c_str(),
    00328                                 end - str, len);
    00329         }
    00330
    00331         return num;
    00332 }
    00333
    00334 } // end namespace mysqlpp
    00335
    00336 #endif