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 "common.h"
00035
00036 #include <map>
00037 #include <typeinfo>
00038
00039 namespace mysqlpp {
00040
00041 #if !defined(DOXYGEN_IGNORE)
00042
00043
00044 class MYSQLPP_EXPORT mysql_type_info;
00045 class MYSQLPP_EXPORT mysql_ti_sql_type_info_lookup;
00046
00047 class MYSQLPP_EXPORT mysql_ti_sql_type_info
00048 {
00049 private:
00050 friend class mysql_type_info;
00051 friend class mysql_ti_sql_type_info_lookup;
00052
00053 mysql_ti_sql_type_info& operator=(
00054 const mysql_ti_sql_type_info& b);
00055
00056
00057
00058 mysql_ti_sql_type_info() :
00059 sql_name_(0),
00060 c_type_(0),
00061 base_type_(0),
00062 default_(false)
00063 {
00064 }
00065
00066 mysql_ti_sql_type_info(const char* s,
00067 const std::type_info& t, const unsigned char bt = 0,
00068 const bool d = false) :
00069 sql_name_(s),
00070 c_type_(&t),
00071 base_type_(bt),
00072 default_(d)
00073 {
00074 }
00075
00076 const char* sql_name_;
00077 const std::type_info* c_type_;
00078 const unsigned char base_type_;
00079 const bool default_;
00080 };
00081
00082
00083 struct type_info_cmp
00084 {
00085 bool operator() (const std::type_info* lhs,
00086 const std::type_info* rhs) const
00087 {
00088 return lhs->before(*rhs) != 0;
00089 }
00090 };
00091
00092 class MYSQLPP_EXPORT mysql_ti_sql_type_info_lookup
00093 {
00094 private:
00095 friend class mysql_type_info;
00096
00097 typedef mysql_ti_sql_type_info sql_type_info;
00098
00099 mysql_ti_sql_type_info_lookup(
00100 const sql_type_info types[], const int size);
00101
00102 const unsigned char& operator [](
00103 const std::type_info& ti) const
00104 {
00105 return map_.find(&ti)->second;
00106 }
00107
00108 std::map<const std::type_info*, unsigned char, type_info_cmp> map_;
00109 };
00110
00111 #endif // !defined(DOXYGEN_IGNORE)
00112
00113
00117 class MYSQLPP_EXPORT mysql_type_info
00118 {
00119 public:
00130 mysql_type_info(unsigned char n = static_cast<unsigned char>(-1)) :
00131 _length(0),
00132 _max_length(0),
00133 num_(n)
00134 {
00135 }
00136
00142 mysql_type_info(enum_field_types t, bool _unsigned, bool _null) :
00143 _length(0),
00144 _max_length(0),
00145 num_(type(t, _unsigned, _null))
00146 {
00147 }
00148
00152 mysql_type_info(const MYSQL_FIELD& f) :
00153 _length(f.length),
00154 _max_length(f.max_length),
00155 num_(type(f.type, (f.flags & UNSIGNED_FLAG) != 0,
00156 (f.flags & NOT_NULL_FLAG) == 0))
00157 {
00158 }
00159
00161 mysql_type_info(const mysql_type_info& t) :
00162 _length(0),
00163 _max_length(0),
00164 num_(t.num_)
00165 {
00166 }
00167
00172 mysql_type_info(const std::type_info& t) :
00173 num_(lookups[t])
00174 {
00175 }
00176
00182 mysql_type_info& operator =(unsigned char n)
00183 {
00184 num_ = n;
00185 return *this;
00186 }
00187
00189 mysql_type_info& operator =(const mysql_type_info& t)
00190 {
00191 num_ = t.num_;
00192 return *this;
00193 }
00194
00199 mysql_type_info& operator =(const std::type_info& t)
00200 {
00201 num_ = lookups[t];
00202 return *this;
00203 }
00204
00209 const char* name() const { return deref().c_type_->name(); }
00210
00214 const char* sql_name() const { return deref().sql_name_; }
00215
00220 const std::type_info& c_type() const { return *deref().c_type_; }
00221
00226 const unsigned int length() const { return _length; }
00227
00232 const unsigned int max_length() const { return _max_length; }
00233
00239 const mysql_type_info base_type() const
00240 {
00241 return mysql_type_info(deref().base_type_);
00242 }
00243
00249 int id() const
00250 {
00251 return num_;
00252 }
00253
00259 bool quote_q() const;
00260
00266 bool escape_q() const;
00267
00272 bool before(mysql_type_info& b)
00273 {
00274 return num_ < b.num_;
00275 }
00276
00281 static const unsigned char string_type = 20;
00282
00283 unsigned int _length;
00284 unsigned int _max_length;
00285
00286 private:
00287 typedef mysql_ti_sql_type_info sql_type_info;
00288 typedef mysql_ti_sql_type_info_lookup sql_type_info_lookup;
00289
00290 static const sql_type_info types[62];
00291
00292 static const unsigned char offset = 0;
00293 static const unsigned char unsigned_offset = 21;
00294 static const unsigned char null_offset = 31;
00295 static const unsigned char unsigned_null_offset = 52;
00296
00297 static const sql_type_info_lookup lookups;
00298
00314 static unsigned char type(enum_field_types t,
00315 bool _unsigned, bool _null = false);
00316
00317 const sql_type_info& deref() const
00318 {
00319 return types[num_];
00320 }
00321
00322 unsigned char num_;
00323 };
00324
00326 inline bool operator ==(const mysql_type_info& a, const mysql_type_info& b)
00327 {
00328 return a.id() == b.id();
00329 }
00330
00332 inline bool operator !=(const mysql_type_info& a, const mysql_type_info& b)
00333 {
00334 return a.id() != b.id();
00335 }
00336
00339 inline bool operator ==(const std::type_info& a, const mysql_type_info& b)
00340 {
00341 return a == b.c_type();
00342 }
00343
00346 inline bool operator !=(const std::type_info& a, const mysql_type_info& b)
00347 {
00348 return a != b.c_type();
00349 }
00350
00353 inline bool operator ==(const mysql_type_info& a, const std::type_info& b)
00354 {
00355 return a.c_type() == b;
00356 }
00357
00360 inline bool operator !=(const mysql_type_info& a, const std::type_info& b)
00361 {
00362 return a.c_type() != b;
00363 }
00364
00365 }
00366
00367 #endif
00368