MySQL++  3.3.0
type_info.h
Go to the documentation of this file.
1 
7 /***********************************************************************
8  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
9  (c) 2004-2008 by Educational Technology Resources, Inc. Others may
10  also hold copyrights on code in this file. See the CREDITS.txt file
11  in the top directory of the distribution for details.
12 
13  This file is part of MySQL++.
14 
15  MySQL++ is free software; you can redistribute it and/or modify it
16  under the terms of the GNU Lesser General Public License as published
17  by the Free Software Foundation; either version 2.1 of the License, or
18  (at your option) any later version.
19 
20  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
23  License for more details.
24 
25  You should have received a copy of the GNU Lesser General Public
26  License along with MySQL++; if not, write to the Free Software
27  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
28  USA
29 ***********************************************************************/
30 
31 #if !defined(MYSQLPP_TYPE_INFO_H)
32 #define MYSQLPP_TYPE_INFO_H
33 
34 #include "common.h"
35 
36 #include "exceptions.h"
37 
38 #include <map>
39 #include <sstream>
40 #include <typeinfo>
41 
42 namespace mysqlpp {
43 
44 #if !defined(DOXYGEN_IGNORE)
45 // Doxygen will not generate documentation for this section.
46 
47 class MYSQLPP_EXPORT mysql_type_info;
48 class MYSQLPP_EXPORT mysql_ti_sql_type_info_lookup;
49 
50 class MYSQLPP_EXPORT mysql_ti_sql_type_info
51 {
52 public:
53 #if __cplusplus >= 201103L
54  // Do not allow copy by assignment. (C++11+ version.)
55  mysql_ti_sql_type_info& operator=(const mysql_ti_sql_type_info&) =
56  delete;
57 #endif
58 
59 private:
60  // For use with flags_ bitset
61  enum {
62  tf_default = 1,
63  tf_null = 2,
64  tf_unsigned = 4
65  };
66 
67  friend class mysql_type_info;
68  friend class mysql_ti_sql_type_info_lookup;
69 
70 #if __cplusplus < 201103L
71  // Pre-C++11 alternative to no-assign declaration above.
72  mysql_ti_sql_type_info& operator=(
73  const mysql_ti_sql_type_info& b);
74 #endif
75 
76  // Not initting _base_type and _default because only mysql_type_info
77  // can create them. There *must* be only one copy of each.
78  mysql_ti_sql_type_info() :
79  sql_name_(0),
80  c_type_(0),
81  base_type_(
82 #if MYSQL_VERSION_ID > 40000
83  MYSQL_TYPE_NULL
84 #else
85  FIELD_TYPE_NULL
86 #endif
87  ),
88  flags_(0)
89  {
90  }
91 
92  mysql_ti_sql_type_info(const char* s,
93  const std::type_info& t, const enum_field_types bt,
94  const unsigned int flags = 0) :
95  sql_name_(s),
96  c_type_(&t),
97  base_type_(bt),
98  flags_(flags)
99  {
100  }
101 
102  bool is_default() const { return flags_ & tf_default; }
103  bool is_null() const { return flags_ & tf_null; }
104  bool is_unsigned() const { return flags_ & tf_unsigned; }
105 
106  const char* sql_name_;
107  const std::type_info* c_type_;
108  const enum_field_types base_type_;
109  const unsigned int flags_;
110 };
111 
112 
113 struct type_info_cmp
114 {
115  bool operator() (const std::type_info* lhs,
116  const std::type_info* rhs) const
117  {
118  return lhs->before(*rhs) != 0;
119  }
120 };
121 
122 class MYSQLPP_EXPORT mysql_ti_sql_type_info_lookup
123 {
124 private:
125  friend class mysql_type_info;
126 
127  typedef mysql_ti_sql_type_info sql_type_info;
128  typedef std::map<const std::type_info*, unsigned char, type_info_cmp>
129  map_type;
130 
131  mysql_ti_sql_type_info_lookup(const sql_type_info types[],
132  const int size);
133 
134  const unsigned char& operator [](
135  const std::type_info& ti) const
136  {
137  map_type::const_iterator it = map_.find(&ti);
138  if (it != map_.end()) {
139  return it->second;
140  }
141  else {
142  std::ostringstream outs;
143  outs << "Failed to find MySQL C API type ID for " << ti.name();
144  throw TypeLookupFailed(outs.str());
145  }
146  }
147 
148  map_type map_;
149 };
150 
151 #endif // !defined(DOXYGEN_IGNORE)
152 
153 
158 class MYSQLPP_EXPORT mysql_type_info
159 {
160 public:
169  num_(static_cast<unsigned char>(-1))
170  {
171  }
172 
178  mysql_type_info(enum_field_types t, bool _unsigned = false,
179  bool _null = false) :
180  num_(type(t, _unsigned, _null))
181  {
182  }
183 
186  num_(t.num_)
187  {
188  }
189 
194  mysql_type_info(const std::type_info& t) :
195  num_(lookups[t])
196  {
197  }
198 
200  mysql_type_info& operator =(const mysql_type_info& t)
201  {
202  num_ = t.num_;
203  return *this;
204  }
205 
210  mysql_type_info& operator =(const std::type_info& t)
211  {
212  num_ = lookups[t];
213  return *this;
214  }
215 
220  const char* name() const { return deref().c_type_->name(); }
221 
225  const char* sql_name() const { return deref().sql_name_; }
226 
231  const std::type_info& c_type() const { return *deref().c_type_; }
232 
239  {
240  return mysql_type_info(deref().base_type_);
241  }
242 
248  int id() const
249  {
250  return num_;
251  }
252 
258  bool quote_q() const;
259 
265  bool escape_q() const;
266 
272  {
273  return num_ < b.num_;
274  }
275 
280  static const enum_field_types string_type =
281 #if MYSQL_VERSION_ID > 40000
282  MYSQL_TYPE_STRING;
283 #else
284  FIELD_TYPE_STRING;
285 #endif
286 
287 private:
288  typedef mysql_ti_sql_type_info sql_type_info;
289  typedef mysql_ti_sql_type_info_lookup sql_type_info_lookup;
290 
291  static const sql_type_info types[];
292  static const int num_types;
293 
294  static const sql_type_info_lookup lookups;
295 
314  static unsigned char type(enum_field_types t,
315  bool _unsigned, bool _null = false);
316 
317  const sql_type_info& deref() const
318  {
319  return types[num_];
320  }
321 
322  unsigned char num_;
323 };
324 
326 inline bool operator ==(const mysql_type_info& a, const mysql_type_info& b)
327 {
328  return a.id() == b.id();
329 }
330 
332 inline bool operator !=(const mysql_type_info& a, const mysql_type_info& b)
333 {
334  return a.id() != b.id();
335 }
336 
339 inline bool operator ==(const std::type_info& a, const mysql_type_info& b)
340 {
341  return a == b.c_type();
342 }
343 
346 inline bool operator !=(const std::type_info& a, const mysql_type_info& b)
347 {
348  return a != b.c_type();
349 }
350 
353 inline bool operator ==(const mysql_type_info& a, const std::type_info& b)
354 {
355  return a.c_type() == b;
356 }
357 
360 inline bool operator !=(const mysql_type_info& a, const std::type_info& b)
361 {
362  return a.c_type() != b;
363 }
364 
365 } // end namespace mysqlpp
366 
367 #endif // !defined(MYSQLPP_TYPE_INFO_H)
368 
SQL field type information.
Definition: type_info.h:159
mysql_type_info(const mysql_type_info &t)
Create object as a copy of another.
Definition: type_info.h:185
int id() const
Returns the ID of the SQL type.
Definition: type_info.h:248
const mysql_type_info base_type() const
Returns the type_info for the C++ type inside of the mysqlpp::Null type.
Definition: type_info.h:238
mysql_type_info(enum_field_types t, bool _unsigned=false, bool _null=false)
Create object from MySQL C API type info.
Definition: type_info.h:178
mysql_type_info()
Default constructor.
Definition: type_info.h:168
const std::type_info & c_type() const
Returns the type_info for the C++ type associated with the SQL type.
Definition: type_info.h:231
mysql_type_info(const std::type_info &t)
Create object from a C++ type_info object.
Definition: type_info.h:194
bool before(mysql_type_info &b)
Provides a way to compare two types for sorting.
Definition: type_info.h:271
const char * name() const
Returns an implementation-defined name of the C++ type.
Definition: type_info.h:220
const char * sql_name() const
Returns the name of the SQL type.
Definition: type_info.h:225
This file includes top-level definitions for use both internal to the library, and outside it....
Declares the MySQL++-specific exception classes.