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 #ifndef MYSQLPP_TYPE_INFO_H
00032 #define MYSQLPP_TYPE_INFO_H
00033
00034 #include "defs.h"
00035
00036 #include <mysql.h>
00037
00038 #include <typeinfo>
00039 #include <map>
00040
00041 namespace mysqlpp {
00042
00043 class mysql_type_info;
00044 class mysql_ti_sql_type_info_lookup;
00045
00046 #if !defined(DOXYGEN_IGNORE)
00047
00048
00049 class mysql_ti_sql_type_info
00050 {
00051 friend class mysql_type_info;
00052 friend class mysql_ti_sql_type_info_lookup;
00053
00054 private:
00055 const char* _sql_name;
00056 const std::type_info* _c_type;
00057 const unsigned char _base_type;
00058 const bool _default;
00059
00060 mysql_ti_sql_type_info& operator=(const mysql_ti_sql_type_info& b);
00061
00062
00063
00064 mysql_ti_sql_type_info() :
00065 _base_type(0),
00066 _default(false)
00067 {
00068 }
00069
00070 mysql_ti_sql_type_info(const char* s, const std::type_info& t,
00071 const unsigned char bt = 0, const bool d = false) :
00072 _sql_name(s),
00073 _c_type(&t),
00074 _base_type(bt),
00075 _default(d)
00076 {
00077 }
00078 };
00079
00080
00081 struct type_info_cmp
00082 {
00083 bool operator() (const std::type_info* lhs,
00084 const std::type_info* rhs) const
00085 {
00086 return lhs->before(*rhs) != 0;
00087 }
00088 };
00089
00090 class mysql_ti_sql_type_info_lookup
00091 {
00092 friend class mysql_type_info;
00093
00094 private:
00095 typedef mysql_ti_sql_type_info sql_type_info;
00096
00097 std::map<const std::type_info*, unsigned char, type_info_cmp> _map;
00098
00099 mysql_ti_sql_type_info_lookup(const sql_type_info types[],
00100 const int size);
00101
00102 const unsigned char& operator [](const std::type_info& ti) const
00103 {
00104 return _map.find(&ti)->second;
00105 }
00106 };
00107
00108 #endif // !defined(DOXYGEN_IGNORE)
00109
00110
00114 class mysql_type_info
00115 {
00116 private:
00117 typedef mysql_ti_sql_type_info sql_type_info;
00118 typedef mysql_ti_sql_type_info_lookup sql_type_info_lookup;
00119
00120 static const sql_type_info types[62];
00121
00122 static const unsigned char offset = 0;
00123 static const unsigned char unsigned_offset = 21;
00124 static const unsigned char null_offset = 31;
00125 static const unsigned char unsigned_null_offset = 52;
00126
00127 static const sql_type_info_lookup lookups;
00128
00144 static unsigned char type(enum_field_types t, bool _unsigned,
00145 bool _null = false);
00146
00147 public:
00152 static const unsigned char string_type = 20;
00153
00154 unsigned int _length;
00155 unsigned int _max_length;
00156
00157 private:
00158 unsigned char num;
00159 inline const sql_type_info& deref() const;
00160
00161 public:
00163 mysql_type_info()
00164 {
00165 }
00166
00173 mysql_type_info(unsigned char n) :
00174 num(n)
00175 {
00176 }
00177
00183 inline mysql_type_info(enum_field_types t, bool _unsigned, bool _null);
00184
00188 inline mysql_type_info(const MYSQL_FIELD& f);
00189
00191 mysql_type_info(const mysql_type_info& t) :
00192 num(t.num)
00193 {
00194 }
00195
00200 mysql_type_info(const std::type_info& t)
00201 {
00202 num = lookups[t];
00203 }
00204
00210 mysql_type_info& operator =(unsigned char n)
00211 {
00212 num = n;
00213 return *this;
00214 }
00215
00217 mysql_type_info& operator =(const mysql_type_info& t)
00218 {
00219 num = t.num;
00220 return *this;
00221 }
00222
00227 mysql_type_info& operator =(const std::type_info& t)
00228 {
00229 num = lookups[t];
00230 return *this;
00231 }
00232
00237 inline const char* name() const;
00238
00242 inline const char* sql_name() const;
00243
00248 inline const std::type_info& c_type() const;
00249
00254 inline const unsigned int length() const;
00255
00260 inline const unsigned int max_length() const;
00261
00267 inline const mysql_type_info base_type() const;
00268
00274 int id() const
00275 {
00276 return num;
00277 }
00278
00284 bool quote_q() const;
00285
00291 bool escape_q() const;
00292
00297 bool before(mysql_type_info& b)
00298 {
00299 return num < b.num;
00300 }
00301 };
00302
00303 inline const mysql_type_info::sql_type_info& mysql_type_info::deref() const
00304 {
00305 return types[num];
00306 }
00307
00308 inline const char* mysql_type_info::name() const
00309 {
00310 return deref()._c_type->name();
00311 }
00312
00313 inline const char* mysql_type_info::sql_name() const
00314 {
00315 return deref()._sql_name;
00316 }
00317
00318 inline const unsigned int mysql_type_info::length() const
00319 {
00320 return _length;
00321 }
00322
00323 inline const unsigned int mysql_type_info::max_length() const
00324 {
00325 return _max_length;
00326 }
00327
00328 inline const std::type_info& mysql_type_info::c_type() const
00329 {
00330 return *deref()._c_type;
00331 }
00332
00333 inline const mysql_type_info mysql_type_info::base_type() const
00334 {
00335 return mysql_type_info(deref()._base_type);
00336 }
00337
00338 inline mysql_type_info::mysql_type_info(enum_field_types t,
00339 bool _unsigned, bool _null)
00340 {
00341 num = type(t, _unsigned, _null);
00342 }
00343
00344 inline mysql_type_info::mysql_type_info(const MYSQL_FIELD& f)
00345 {
00346 num = type(f.type, (f.flags & UNSIGNED_FLAG) != 0,
00347 (f.flags & NOT_NULL_FLAG) == 0);
00348 _length = f.length;
00349 _max_length = f.max_length;
00350 }
00351
00353 inline bool operator ==(const mysql_type_info& a, const mysql_type_info& b)
00354 {
00355 return a.id() == b.id();
00356 }
00357
00359 inline bool operator !=(const mysql_type_info& a, const mysql_type_info& b)
00360 {
00361 return a.id() != b.id();
00362 }
00363
00366 inline bool operator ==(const std::type_info& a, const mysql_type_info& b)
00367 {
00368 return a == b.c_type();
00369 }
00370
00373 inline bool operator !=(const std::type_info& a, const mysql_type_info& b)
00374 {
00375 return a != b.c_type();
00376 }
00377
00380 inline bool operator ==(const mysql_type_info& a, const std::type_info& b)
00381 {
00382 return a.c_type() == b;
00383 }
00384
00387 inline bool operator !=(const mysql_type_info& a, const std::type_info& b)
00388 {
00389 return a.c_type() != b;
00390 }
00391
00392 }
00393
00394 #endif
00395