type_info.h
Go to the documentation of this file.
/***********************************************************************
Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
(c) 2004-2008 by Educational Technology Resources, Inc. Others may
also hold copyrights on code in this file. See the CREDITS.txt file
in the top directory of the distribution for details.
This file is part of MySQL++.
MySQL++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
MySQL++ is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with MySQL++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
***********************************************************************/
#if !defined(MYSQLPP_TYPE_INFO_H)
#define MYSQLPP_TYPE_INFO_H
#include "common.h"
#include "exceptions.h"
#include <map>
#include <sstream>
#include <typeinfo>
namespace mysqlpp {
#if !defined(DOXYGEN_IGNORE)
// Doxygen will not generate documentation for this section.
class MYSQLPP_EXPORT mysql_type_info;
class MYSQLPP_EXPORT mysql_ti_sql_type_info_lookup;
class MYSQLPP_EXPORT mysql_ti_sql_type_info
{
private:
// For use with flags_ bitset
enum {
tf_default = 1,
tf_null = 2,
tf_unsigned = 4
};
friend class mysql_type_info;
friend class mysql_ti_sql_type_info_lookup;
mysql_ti_sql_type_info& operator=(
const mysql_ti_sql_type_info& b);
// Not initting _base_type and _default because only mysql_type_info
// can create them. There *must* be only one copy of each.
mysql_ti_sql_type_info() :
sql_name_(0),
c_type_(0),
base_type_(
#if MYSQL_VERSION_ID > 40000
MYSQL_TYPE_NULL
#else
FIELD_TYPE_NULL
#endif
),
flags_(0)
{
}
mysql_ti_sql_type_info(const char* s,
const std::type_info& t, const enum_field_types bt,
const unsigned int flags = 0) :
sql_name_(s),
c_type_(&t),
base_type_(bt),
flags_(flags)
{
}
bool is_default() const { return flags_ & tf_default; }
bool is_null() const { return flags_ & tf_null; }
bool is_unsigned() const { return flags_ & tf_unsigned; }
const char* sql_name_;
const std::type_info* c_type_;
const enum_field_types base_type_;
const unsigned int flags_;
};
struct type_info_cmp
{
bool operator() (const std::type_info* lhs,
const std::type_info* rhs) const
{
return lhs->before(*rhs) != 0;
}
};
class MYSQLPP_EXPORT mysql_ti_sql_type_info_lookup
{
private:
friend class mysql_type_info;
typedef mysql_ti_sql_type_info sql_type_info;
typedef std::map<const std::type_info*, unsigned char, type_info_cmp>
map_type;
mysql_ti_sql_type_info_lookup(const sql_type_info types[],
const int size);
const unsigned char& operator [](
const std::type_info& ti) const
{
map_type::const_iterator it = map_.find(&ti);
if (it != map_.end()) {
return it->second;
}
else {
std::ostringstream outs;
outs << "Failed to find MySQL C API type ID for " << ti.name();
throw TypeLookupFailed(outs.str());
}
}
map_type map_;
};
#endif // !defined(DOXYGEN_IGNORE)
class MYSQLPP_EXPORT mysql_type_info
{
public:
mysql_type_info() :
num_(static_cast<unsigned char>(-1))
{
}
mysql_type_info(enum_field_types t, bool _unsigned = false,
bool _null = false) :
num_(type(t, _unsigned, _null))
{
}
mysql_type_info(const mysql_type_info& t) :
num_(t.num_)
{
}
mysql_type_info(const std::type_info& t) :
num_(lookups[t])
{
}
mysql_type_info& operator =(const mysql_type_info& t)
{
num_ = t.num_;
return *this;
}
mysql_type_info& operator =(const std::type_info& t)
{
num_ = lookups[t];
return *this;
}
const char* name() const { return deref().c_type_->name(); }
const char* sql_name() const { return deref().sql_name_; }
const std::type_info& c_type() const { return *deref().c_type_; }
const mysql_type_info base_type() const
{
return mysql_type_info(deref().base_type_);
}
int id() const
{
return num_;
}
bool quote_q() const;
bool escape_q() const;
bool before(mysql_type_info& b)
{
return num_ < b.num_;
}
static const enum_field_types string_type =
#if MYSQL_VERSION_ID > 40000
MYSQL_TYPE_STRING;
#else
FIELD_TYPE_STRING;
#endif
private:
typedef mysql_ti_sql_type_info sql_type_info;
typedef mysql_ti_sql_type_info_lookup sql_type_info_lookup;
static const sql_type_info types[];
static const int num_types;
static const sql_type_info_lookup lookups;
static unsigned char type(enum_field_types t,
bool _unsigned, bool _null = false);
const sql_type_info& deref() const
{
return types[num_];
}
unsigned char num_;
};
inline bool operator ==(const mysql_type_info& a, const mysql_type_info& b)
{
return a.id() == b.id();
}
inline bool operator !=(const mysql_type_info& a, const mysql_type_info& b)
{
return a.id() != b.id();
}
inline bool operator ==(const std::type_info& a, const mysql_type_info& b)
{
return a == b.c_type();
}
inline bool operator !=(const std::type_info& a, const mysql_type_info& b)
{
return a != b.c_type();
}
inline bool operator ==(const mysql_type_info& a, const std::type_info& b)
{
return a.c_type() == b;
}
inline bool operator !=(const mysql_type_info& a, const std::type_info& b)
{
return a.c_type() != b;
}
} // end namespace mysqlpp
#endif // !defined(MYSQLPP_TYPE_INFO_H)
mysqlpp::mysql_type_info::id
int id() const
Returns the ID of the SQL type.
Definition: type_info.h:238
mysqlpp::mysql_type_info
SQL field type information.
Definition: type_info.h:148
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
exceptions.h
Declares the MySQL++-specific exception classes.
mysqlpp::mysql_type_info::sql_name
const char * sql_name() const
Returns the name of the SQL type.
Definition: type_info.h:215
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
mysqlpp::mysql_type_info::name
const char * name() const
Returns an implementation-defined name of the C++ type.
Definition: type_info.h:210
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::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
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