MySQL++ Reference Manual

MySQL++: type_info.h Source File

MySQL++ 3.2.5

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 private:
53 // For use with flags_ bitset
54 enum {
55 tf_default = 1,
56 tf_null = 2,
57 tf_unsigned = 4
58 };
59
60 friend class mysql_type_info;
61 friend class mysql_ti_sql_type_info_lookup;
62
63 mysql_ti_sql_type_info& operator=(
64 const mysql_ti_sql_type_info& b);
65
66 // Not initting _base_type and _default because only mysql_type_info
67 // can create them. There *must* be only one copy of each.
68 mysql_ti_sql_type_info() :
69 sql_name_(0),
70 c_type_(0),
71 base_type_(
72 #if MYSQL_VERSION_ID > 40000
73 MYSQL_TYPE_NULL
74 #else
75 FIELD_TYPE_NULL
76 #endif
77 ),
78 flags_(0)
79 {
80 }
81
82 mysql_ti_sql_type_info(const char* s,
83 const std::type_info& t, const enum_field_types bt,
84 const unsigned int flags = 0) :
85 sql_name_(s),
86 c_type_(&t),
87 base_type_(bt),
88 flags_(flags)
89 {
90 }
91
92 bool is_default() const { return flags_ & tf_default; }
93 bool is_null() const { return flags_ & tf_null; }
94 bool is_unsigned() const { return flags_ & tf_unsigned; }
95
96 const char* sql_name_;
97 const std::type_info* c_type_;
98 const enum_field_types base_type_;
99 const unsigned int flags_;
100 };
101
102
103 struct type_info_cmp
104 {
105 bool operator() (const std::type_info* lhs,
106 const std::type_info* rhs) const
107 {
108 return lhs->before(*rhs) != 0;
109 }
110 };
111
112 class MYSQLPP_EXPORT mysql_ti_sql_type_info_lookup
113 {
114 private:
115 friend class mysql_type_info;
116
117 typedef mysql_ti_sql_type_info sql_type_info;
118 typedef std::map<const std::type_info*, unsigned char, type_info_cmp>
119 map_type;
120
121 mysql_ti_sql_type_info_lookup(const sql_type_info types[],
122 const int size);
123
124 const unsigned char& operator [](
125 const std::type_info& ti) const
126 {
127 map_type::const_iterator it = map_.find(&ti);
128 if (it != map_.end()) {
129 return it->second;
130 }
131 else {
132 std::ostringstream outs;
133 outs << "Failed to find MySQL C API type ID for " << ti.name();
134 throw TypeLookupFailed(outs.str());
135 }
136 }
137
138 map_type map_;
139 };
140
141 #endif // !defined(DOXYGEN_IGNORE)
142
143
148 class MYSQLPP_EXPORT mysql_type_info
149 {
150 public:
158 mysql_type_info() :
159 num_(static_cast<unsigned char>(-1))
160 {
161 }
162
168 mysql_type_info(enum_field_types t, bool _unsigned = false,
169 bool _null = false) :
170 num_(type(t, _unsigned, _null))
171 {
172 }
173
175 mysql_type_info(const mysql_type_info& t) :
176 num_(t.num_)
177 {
178 }
179
184 mysql_type_info(const std::type_info& t) :
185 num_(lookups[t])
186 {
187 }
188
190 mysql_type_info& operator =(const mysql_type_info& t)
191 {
192 num_ = t.num_;
193 return *this;
194 }
195
200 mysql_type_info& operator =(const std::type_info& t)
201 {
202 num_ = lookups[t];
203 return *this;
204 }
205
210 const char* name() const { return deref().c_type_->name(); }
211
215 const char* sql_name() const { return deref().sql_name_; }
216
221 const std::type_info& c_type() const { return *deref().c_type_; }
222
228 const mysql_type_info base_type() const
229 {
230 return mysql_type_info(deref().base_type_);
231 }
232
238 int id() const
239 {
240 return num_;
241 }
242
248 bool quote_q() const;
249
255 bool escape_q() const;
256
261 bool before(mysql_type_info& b)
262 {
263 return num_ < b.num_;
264 }
265
270 static const enum_field_types string_type =
271 #if MYSQL_VERSION_ID > 40000
272 MYSQL_TYPE_STRING;
273 #else
274 FIELD_TYPE_STRING;
275 #endif
276
277 private:
278 typedef mysql_ti_sql_type_info sql_type_info;
279 typedef mysql_ti_sql_type_info_lookup sql_type_info_lookup;
280
281 static const sql_type_info types[];
282 static const int num_types;
283
284 static const sql_type_info_lookup lookups;
285
304 static unsigned char type(enum_field_types t,
305 bool _unsigned, bool _null = false);
306
307 const sql_type_info& deref() const
308 {
309 return types[num_];
310 }
311
312 unsigned char num_;
313 };
314
316 inline bool operator ==(const mysql_type_info& a, const mysql_type_info& b)
317 {
318 return a.id() == b.id();
319 }
320
322 inline bool operator !=(const mysql_type_info& a, const mysql_type_info& b)
323 {
324 return a.id() != b.id();
325 }
326
329 inline bool operator ==(const std::type_info& a, const mysql_type_info& b)
330 {
331 return a == b.c_type();
332 }
333
336 inline bool operator !=(const std::type_info& a, const mysql_type_info& b)
337 {
338 return a != b.c_type();
339 }
340
343 inline bool operator ==(const mysql_type_info& a, const std::type_info& b)
344 {
345 return a.c_type() == b;
346 }
347
350 inline bool operator !=(const mysql_type_info& a, const std::type_info& b)
351 {
352 return a.c_type() != b;
353 }
354
355 } // end namespace mysqlpp
356
357 #endif // !defined(MYSQLPP_TYPE_INFO_H)
358
mysqlpp::mysql_type_info::sql_name
const char * sql_name() const
Returns the name of the SQL type.
Definition: type_info.h:215
mysqlpp::operator !=
bool operator !=(const mysql_type_info &a, const mysql_type_info &b)
Returns true if two mysql_type_info objects are not equal.
Definition: type_info.h:322
mysqlpp::mysql_type_info
SQL field type information.
Definition: type_info.h:148
mysqlpp::mysql_type_info::id
int id() const
Returns the ID of the SQL type.
Definition: type_info.h:238
mysqlpp::mysql_type_info::base_type
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:228
exceptions.h
Declares the MySQL++-specific exception classes.
mysqlpp::mysql_type_info::name
const char * name() const
Returns an implementation-defined name of the C++ type.
Definition: type_info.h:210
mysqlpp::mysql_type_info::mysql_type_info
mysql_type_info(const std::type_info &t)
Create object from a C++ type_info object.
Definition: type_info.h:184
common.h
This file includes top-level definitions for use both internal to the library, and outside it....
mysqlpp::mysql_type_info::mysql_type_info
mysql_type_info()
Default constructor.
Definition: type_info.h:158
mysqlpp::mysql_type_info::c_type
const std::type_info & c_type() const
Returns the type_info for the C++ type associated with the SQL type.
Definition: type_info.h:221
mysqlpp::mysql_type_info::mysql_type_info
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:168
mysqlpp::mysql_type_info::mysql_type_info
mysql_type_info(const mysql_type_info &t)
Create object as a copy of another.
Definition: type_info.h:175
mysqlpp::mysql_type_info::before
bool before(mysql_type_info &b)
Provides a way to compare two types for sorting.
Definition: type_info.h:261