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> class ColData_Tmpl : public Str
    00086 {
    00087 private:
    00088         mysql_type_info _type;
    00089         std::string buf;
    00090         bool _null;
    00091
    00092 public:
    00100         ColData_Tmpl() :
    00101         _null(false)
    00102         {
    00103         }
    00104
    00110         explicit ColData_Tmpl(bool n,
    00111                         mysql_type_info t = mysql_type_info::string_type) :
    00112         _type(t),
    00113         _null(n)
    00114         {
    00115         }
    00116
    00122         explicit ColData_Tmpl(const char* str,
    00123                         mysql_type_info t = mysql_type_info::string_type,
    00124                         bool n = false) :
    00125         Str(str),
    00126         _type(t),
    00127         _null(n)
    00128         {
    00129                 buf = str;
    00130         }
    00131
    00133         mysql_type_info type() const
    00134         {
    00135                 return _type;
    00136         }
    00137
    00140         bool quote_q() const
    00141         {
    00142                 return _type.quote_q();
    00143         }
    00144
    00147         bool escape_q() const
    00148         {
    00149                 return _type.escape_q();
    00150         }
    00151
    00153         template <class Type> Type conv(Type dummy) const;
    00154
    00156         void it_is_null() { _null = true; }
    00157
    00159         inline const bool is_null() const { return _null; }
    00160
    00162         inline const std::string& get_string() const { return buf; }
    00163
    00166         operator cchar*() const { return buf.c_str(); }
    00167
    00169         operator signed char() const { return conv(static_cast<signed char>(0)); }
    00170
    00172         operator unsigned char() const { return conv(static_cast<unsigned char>(0)); }
    00173
    00175         operator int() const { return conv(static_cast<int>(0)); }
    00176
    00178         operator unsigned int() const { return conv(static_cast<unsigned int>(0)); }
    00179
    00181         operator short int() const { return conv(static_cast<short int>(0)); }
    00182
    00185         operator unsigned short int() const { return conv(static_cast<unsigned short int>(0)); }
    00186
    00188         operator long int() const { return conv(static_cast<long int>(0)); }
    00189
    00192         operator unsigned long int() const { return conv(static_cast<unsigned long int>(0)); }
    00193
    00196         operator longlong() const { return conv(static_cast<longlong>(0)); }
    00197
    00200         operator ulonglong() const { return conv(static_cast<ulonglong>(0)); }
    00201
    00203         operator float() const { return conv(static_cast<float>(0)); }
    00204
    00206         operator double() const { return conv(static_cast<double>(0)); }
    00207
    00208         template <class T, class B> operator Null<T, B>() const;
    00209 };
    00210
    00213 typedef ColData_Tmpl < const_string > ColData;
    00214
    00217 typedef ColData_Tmpl < std::string > MutableColData;
    00218
    00219
    00220 #if !defined(NO_BINARY_OPERS) && !defined(DOXYGEN_IGNORE)
    00221 // Ignore this section is NO_BINARY_OPERS is defined, or if this section
    00222 // is being parsed by Doxygen.  In the latter case, it's ignored because
    00223 // Doxygen doesn't understand it correctly, and we can't be bothered to
    00224 // explain it to Doxygen.
    00225
    00226 #define oprsw(opr, other, conv) \
    00227   template<class Str> \
    00228   inline other operator opr (ColData_Tmpl<Str> x, other y) \
    00229     {return static_cast<conv>(x) opr y;} \
    00230   template<class Str> \
    00231   inline other operator opr (other x, ColData_Tmpl<Str> y) \
    00232     {return x opr static_cast<conv>(y);}
    00233
    00234 #define operator_binary(other, conv) \
    00235   oprsw(+, other, conv) \
    00236   oprsw(-, other, conv) \
    00237   oprsw(*, other, conv) \
    00238   oprsw(/, other, conv)
    00239
    00240 #define operator_binary_int(other, conv) \
    00241   operator_binary(other, conv) \
    00242   oprsw(%, other, conv) \
    00243   oprsw(&, other, conv) \
    00244   oprsw(^, other, conv) \
    00245   oprsw(|, other, conv) \
    00246   oprsw(<<, other, conv) \
    00247   oprsw(>>, other, conv)
    00248
    00249 operator_binary(float, double)
    00250 operator_binary(double, double)
    00251
    00252 operator_binary_int(char, long int)
    00253 operator_binary_int(int, long int)
    00254 operator_binary_int(short int, long int)
    00255 operator_binary_int(long int, long int)
    00256
    00257 operator_binary_int(unsigned char, unsigned long int)
    00258 operator_binary_int(unsigned int, unsigned long int)
    00259 operator_binary_int(unsigned short int, unsigned long int)
    00260 operator_binary_int(unsigned long int, unsigned long int)
    00261
    00262 operator_binary_int(longlong, longlong)
    00263 operator_binary_int(ulonglong, ulonglong)
    00264 #endif // NO_BINARY_OPERS
    00265
    00267
    00273 template <class Str> template<class T, class B>
    00274 ColData_Tmpl<Str>::operator Null<T, B>() const
    00275 {
    00276         if ((*this)[0] == 'N' && (*this)[1] == 'U' && (*this)[2] == 'L' &&
    00277                         (*this)[3] == 'L' && Str::size() == 4) {
    00278                 return Null<T, B>(null);
    00279         }
    00280         else {
    00281                 return Null<T, B>(conv(T()));
    00282         }
    00283 }
    00284
    00285 template <class Str> template <class Type>
    00286 Type ColData_Tmpl<Str>::conv(Type /* dummy */) const
    00287 {
    00288         std::string strbuf = buf;
    00289         strip_all_blanks(strbuf);
    00290         size_t len = strbuf.size();
    00291         const char *str = strbuf.c_str();
    00292         const char *end = str;
    00293         Type num = mysql_convert < Type > (str, end);
    00294         if (*end == '.') {
    00295                 end++;
    00296                 for (; *end == '0'; end++) ;
    00297         } if (*end != '\0' && end != NULL) {
    00298                 throw BadConversion(typeid(Type).name(), Str::c_str(),
    00299                                                         end - str, len);
    00300         }
    00301         return num;
    00302 }
    00303
    00304 } // end namespace mysqlpp
    00305
    00306 #endif