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 <mysql.h>
00037
00038 #include <map>
00039 #include <typeinfo>
00040
00041 namespace mysqlpp {
00042
00043 #if !defined(DOXYGEN_IGNORE)
00044
00045
00046 class MYSQLPP_EXPORT mysql_type_info;
00047 class MYSQLPP_EXPORT mysql_ti_sql_type_info_lookup;
00048
00049 class MYSQLPP_EXPORT mysql_ti_sql_type_info
00050 {
00051 private:
00052 friend class mysql_type_info;
00053 friend class mysql_ti_sql_type_info_lookup;
00054
00055 mysql_ti_sql_type_info& operator=(
00056 const mysql_ti_sql_type_info& b);
00057
00058
00059
00060 mysql_ti_sql_type_info() :
00061 sql_name_(0),
00062 c_type_(0),
00063 base_type_(0),
00064 default_(false)
00065 {
00066 }
00067
00068 mysql_ti_sql_type_info(const char* s,
00069 const std::type_info& t, const unsigned char bt = 0,
00070 const bool d = false) :
00071 sql_name_(s),
00072 c_type_(&t),
00073 base_type_(bt),
00074 default_(d)
00075 {
00076 }
00077
00078 const char* sql_name_;
00079 const std::type_info* c_type_;
00080 const unsigned char base_type_;
00081 const bool default_;
00082 };
00083
00084
00085 struct type_info_cmp
00086 {
00087 bool operator() (const std::type_info* lhs,
00088 const std::type_info* rhs) const
00089 {
00090 return lhs->before(*rhs) != 0;
00091 }
00092 };
00093
00094 class MYSQLPP_EXPORT mysql_ti_sql_type_info_lookup
00095 {
00096 private:
00097 friend class mysql_type_info;
00098
00099 typedef mysql_ti_sql_type_info sql_type_info;
00100
00101 mysql_ti_sql_type_info_lookup(
00102 const sql_type_info types[], const int size);
00103
00104 const unsigned char& operator [](
00105 const std::type_info& ti) const
00106 {
00107 return map_.find(&ti)->second;
00108 }
00109
00110 std::map<const std::type_info*, unsigned char, type_info_cmp> map_;
00111 };
00112
00113 #endif // !defined(DOXYGEN_IGNORE)
00114
00115
00119 class MYSQLPP_EXPORT mysql_type_info
00120 {
00121 public:
00132 mysql_type_info(unsigned char n = static_cast<unsigned char>(-1)) :
00133 _length(0),
00134 _max_length(0),
00135 num_(n)
00136 {
00137 }
00138
00144 mysql_type_info(enum_field_types t, bool _unsigned, bool _null) :
00145 _length(0),
00146 _max_length(0),
00147 num_(type(t, _unsigned, _null))
00148 {
00149 }
00150
00154 mysql_type_info(const MYSQL_FIELD& f) :
00155 _length(f.length),
00156 _max_length(f.max_length),
00157 num_(type(f.type, (f.flags & UNSIGNED_FLAG) != 0,
00158 (f.flags & NOT_NULL_FLAG) == 0))
00159 {
00160 }
00161
00163 mysql_type_info(const mysql_type_info& t) :
00164 _length(0),
00165 _max_length(0),
00166 num_(t.num_)
00167 {
00168 }
00169
00174 mysql_type_info(const std::type_info& t) :
00175 num_(lookups[t])
00176 {
00177 }
00178
00184 mysql_type_info& operator =(unsigned char n)
00185 {
00186 num_ = n;
00187 return *this;
00188 }
00189
00191 mysql_type_info& operator =(const mysql_type_info& t)
00192 {
00193 num_ = t.num_;
00194 return *this;
00195 }
00196
00201 mysql_type_info& operator =(const std::type_info& t)
00202 {
00203 num_ = lookups[t];
00204 return *this;
00205 }
00206
00211 const char* name() const { return deref().c_type_->name(); }
00212
00216 const char* sql_name() const { return deref().sql_name_; }
00217
00222 const std::type_info& c_type() const { return *deref().c_type_; }
00223
00228 const unsigned int length() const { return _length; }
00229
00234 const unsigned int max_length() const { return _max_length; }
00235
00241 const mysql_type_info base_type() const
00242 {
00243 return mysql_type_info(deref().base_type_);
00244 }
00245
00251 int id() const
00252 {
00253 return num_;
00254 }
00255
00261 bool quote_q() const;
00262
00268 bool escape_q() const;
00269
00274 bool before(mysql_type_info& b)
00275 {
00276 return num_ < b.num_;
00277 }
00278
00283 static const unsigned char string_type = 20;
00284
00285 unsigned int _length;
00286 unsigned int _max_length;
00287
00288 private:
00289 typedef mysql_ti_sql_type_info sql_type_info;
00290 typedef mysql_ti_sql_type_info_lookup sql_type_info_lookup;
00291
00292 static const sql_type_info types[62];
00293
00294 static const unsigned char offset = 0;
00295 static const unsigned char unsigned_offset = 21;
00296 static const unsigned char null_offset = 31;
00297 static const unsigned char unsigned_null_offset = 52;
00298
00299 static const sql_type_info_lookup lookups;
00300
00316 static unsigned char type(enum_field_types t,
00317 bool _unsigned, bool _null = false);
00318
00319 const sql_type_info& deref() const
00320 {
00321 return types[num_];
00322 }
00323
00324 unsigned char num_;
00325 };
00326
00328 inline bool operator ==(const mysql_type_info& a, const mysql_type_info& b)
00329 {
00330 return a.id() == b.id();
00331 }
00332
00334 inline bool operator !=(const mysql_type_info& a, const mysql_type_info& b)
00335 {
00336 return a.id() != b.id();
00337 }
00338
00341 inline bool operator ==(const std::type_info& a, const mysql_type_info& b)
00342 {
00343 return a == b.c_type();
00344 }
00345
00348 inline bool operator !=(const std::type_info& a, const mysql_type_info& b)
00349 {
00350 return a != b.c_type();
00351 }
00352
00355 inline bool operator ==(const mysql_type_info& a, const std::type_info& b)
00356 {
00357 return a.c_type() == b;
00358 }
00359
00362 inline bool operator !=(const mysql_type_info& a, const std::type_info& b)
00363 {
00364 return a.c_type() != b;
00365 }
00366
00367 }
00368
00369 #endif
00370