00001 #ifndef MYSQLPP_COLDATA_H
00002 #define MYSQLPP_COLDATA_H
00003
00010
00011 #include "platform.h"
00012
00013 #include "const_string.h"
00014 #include "convert.h"
00015 #include "defs.h"
00016 #include "exceptions.h"
00017 #include "null.h"
00018 #include "string_util.h"
00019 #include "type_info.h"
00020
00021 #include <mysql.h>
00022
00023 #include <typeinfo>
00024 #include <string>
00025
00026 #include <stdlib.h>
00027
00028 namespace mysqlpp {
00029
00058
00059 template <class Str> class ColData_Tmpl : public Str
00060 {
00061 private:
00062 mysql_type_info _type;
00063 std::string buf;
00064 bool _null;
00065
00066 public:
00067 explicit ColData_Tmpl(bool n,
00068 mysql_type_info t = mysql_type_info::string_type) :
00069 _type(t),
00070 _null(n)
00071 {
00072 }
00073
00074 explicit ColData_Tmpl(const char* str,
00075 mysql_type_info t = mysql_type_info::string_type,
00076 bool n = false) :
00077 Str(str),
00078 _type(t),
00079 _null(n)
00080 {
00081 buf = str;
00082 }
00083
00085 ColData_Tmpl() :
00086 _null(false)
00087 {
00088 }
00089
00091 mysql_type_info type() const
00092 {
00093 return _type;
00094 }
00095
00098 bool quote_q() const
00099 {
00100 return _type.quote_q();
00101 }
00102
00105 bool escape_q() const
00106 {
00107 return _type.escape_q();
00108 }
00109
00111 template <class Type> Type conv(Type dummy) const;
00112
00114 void it_is_null() { _null = true; }
00115
00117 inline const bool is_null() const { return _null; }
00118
00120 inline const std::string& get_string() const { return buf; }
00121
00124 operator cchar*() const { return buf.c_str(); }
00125
00127 operator signed char() const { return conv(static_cast<signed char>(0)); }
00128
00130 operator unsigned char() const { return conv(static_cast<unsigned char>(0)); }
00131
00133 operator int() const { return conv(static_cast<int>(0)); }
00134
00136 operator unsigned int() const { return conv(static_cast<unsigned int>(0)); }
00137
00139 operator short int() const { return conv(static_cast<short int>(0)); }
00140
00143 operator unsigned short int() const { return conv(static_cast<unsigned short int>(0)); }
00144
00146 operator long int() const { return conv(static_cast<long int>(0)); }
00147
00150 operator unsigned long int() const { return conv(static_cast<unsigned long int>(0)); }
00151
00154 operator longlong() const { return conv(static_cast<longlong>(0)); }
00155
00158 operator ulonglong() const { return conv(static_cast<ulonglong>(0)); }
00159
00161 operator float() const { return conv(static_cast<float>(0)); }
00162
00164 operator double() const { return conv(static_cast<double>(0)); }
00165
00166 template <class T, class B> operator Null<T, B>() const;
00167 };
00168
00171 typedef ColData_Tmpl < const_string > ColData;
00172
00175 typedef ColData_Tmpl < std::string > MutableColData;
00176
00177
00178 #ifndef NO_BINARY_OPERS
00179
00180 #define oprsw(opr, other, conv) \
00181 template<class Str> \
00182 inline other operator opr (ColData_Tmpl<Str> x, other y) \
00183 {return static_cast<conv>(x) opr y;} \
00184 template<class Str> \
00185 inline other operator opr (other x, ColData_Tmpl<Str> y) \
00186 {return x opr static_cast<conv>(y);}
00187
00188 #define operator_binary(other, conv) \
00189 oprsw(+, other, conv) \
00190 oprsw(-, other, conv) \
00191 oprsw(*, other, conv) \
00192 oprsw(/, other, conv)
00193
00194 #define operator_binary_int(other, conv) \
00195 operator_binary(other, conv) \
00196 oprsw(%, other, conv) \
00197 oprsw(&, other, conv) \
00198 oprsw(^, other, conv) \
00199 oprsw(|, other, conv) \
00200 oprsw(<<, other, conv) \
00201 oprsw(>>, other, conv)
00202
00203 operator_binary(float, double)
00204 operator_binary(double, double)
00205
00206 operator_binary_int(char, long int)
00207 operator_binary_int(int, long int)
00208 operator_binary_int(short int, long int)
00209 operator_binary_int(long int, long int)
00210
00211 operator_binary_int(unsigned char, unsigned long int)
00212 operator_binary_int(unsigned int, unsigned long int)
00213 operator_binary_int(unsigned short int, unsigned long int)
00214 operator_binary_int(unsigned long int, unsigned long int)
00215
00216 operator_binary_int(longlong, longlong)
00217 operator_binary_int(ulonglong, ulonglong)
00218 #endif
00219
00220 template <class Str> template<class T, class B>
00221 ColData_Tmpl<Str>::operator Null<T, B>() const
00222 {
00223 if ((*this)[0] == 'N' && (*this)[1] == 'U' && (*this)[2] == 'L' &&
00224 (*this)[3] == 'L' && Str::size() == 4) {
00225 return Null<T, B>(null);
00226 }
00227 else {
00228 return Null<T, B>(conv(T()));
00229 }
00230 }
00231
00232 template <class Str> template <class Type>
00233 Type ColData_Tmpl<Str>::conv(Type ) const
00234 {
00235 std::string strbuf = buf;
00236 strip_all_blanks(strbuf);
00237 size_t len = strbuf.size();
00238 const char *str = strbuf.c_str();
00239 const char *end = str;
00240 Type num = mysql_convert < Type > (str, end);
00241 if (*end == '.') {
00242 end++;
00243 for (; *end == '0'; end++) ;
00244 } if (*end != '\0' && end != NULL) {
00245 throw BadConversion(typeid(Type).name(), Str::c_str(),
00246 end - str, len);
00247 }
00248 return num;
00249 }
00250
00251 }
00252
00253 #endif