00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
00222
00223
00224
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
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 ) 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 }
00305
00306 #endif