Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

type_info.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 /***********************************************************************
00008  Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
00009  MySQL AB, and (c) 2004-2006 by Educational Technology Resources, Inc.
00010  Others may also hold copyrights on code in this file.  See the CREDITS
00011  file in the top directory of the distribution for details.
00012 
00013  This file is part of MySQL++.
00014 
00015  MySQL++ is free software; you can redistribute it and/or modify it
00016  under the terms of the GNU Lesser General Public License as published
00017  by the Free Software Foundation; either version 2.1 of the License, or
00018  (at your option) any later version.
00019 
00020  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
00021  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00022  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00023  License for more details.
00024 
00025  You should have received a copy of the GNU Lesser General Public
00026  License along with MySQL++; if not, write to the Free Software
00027  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00028  USA
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 // Doxygen will not generate documentation for this section.
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         // Not initting _base_type and _default because only mysql_type_info
00057         // can create them.  There *must* be only one copy of each.
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 }                                                               // end namespace mysqlpp
00366 
00367 #endif
00368 

Generated on Wed Jul 11 15:34:34 2007 for MySQL++ by doxygen 1.3.5