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

mysqlpp::ColData_Tmpl< Str > Class Template Reference

Template for string data that can convert itself to any standard C data type. More...

#include <coldata.h>

Collaboration diagram for mysqlpp::ColData_Tmpl< Str >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ColData_Tmpl ()
 Default constructor.

 ColData_Tmpl (const ColData_Tmpl< Str > &cd)
 Copy ctor.

 ColData_Tmpl (bool n, mysql_type_info t=mysql_type_info::string_type)
 Constructor allowing you to set the null flag and the type data.

 ColData_Tmpl (const std::string &str, mysql_type_info t=mysql_type_info::string_type, bool n=false)
 C++ string version of full ctor.

 ColData_Tmpl (const char *str, mysql_type_info t=mysql_type_info::string_type, bool n=false)
 Null-terminated C string version of full ctor.

 ColData_Tmpl (const char *str, typename Str::size_type len, mysql_type_info t=mysql_type_info::string_type, bool n=false)
 Full constructor.

mysql_type_info type () const
 Get this object's current MySQL type.

bool quote_q () const
 Returns true if data of this type should be quoted, false otherwise.

bool escape_q () const
 Returns true if data of this type should be escaped, false otherwise.

template<class Type> Type conv (Type dummy) const
 Template for converting data from one type to another.

void it_is_null ()
 Set a flag indicating that this object is a SQL null.

const bool is_null () const
 Returns true if this object is a SQL null.

const std::string & get_string () const
 Returns this object's data in C++ string form.

 operator cchar * () const
 Returns a const char pointer to the object's raw data.

 operator signed char () const
 Converts this object's string data to a signed char.

 operator unsigned char () const
 Converts this object's string data to an unsigned char.

 operator int () const
 Converts this object's string data to an int.

 operator unsigned int () const
 Converts this object's string data to an unsigned int.

 operator short int () const
 Converts this object's string data to a short int.

 operator unsigned short int () const
 Converts this object's string data to an unsigned short int.

 operator long int () const
 Converts this object's string data to a long int.

 operator unsigned long int () const
 Converts this object's string data to an unsigned long int.

 operator longlong () const
 Converts this object's string data to the platform- specific 'longlong' type, usually a 64-bit integer.

 operator ulonglong () const
 Converts this object's string data to the platform- specific 'ulonglong' type, usually a 64-bit unsigned integer.

 operator float () const
 Converts this object's string data to a float.

 operator double () const
 Converts this object's string data to a double.

 operator bool () const
 Converts this object's string data to a bool.

template<class T, class B>  operator Null () const
 Converts this object to a SQL null.


Detailed Description

template<class Str>
class mysqlpp::ColData_Tmpl< Str >

Template for string data that can convert itself to any standard C data type.

Do not use this class directly. Use the typedef ColData or MutableColData instead. ColData is a ColData_Tmpl<const std::string> and MutableColData is a ColData_Tmpl<std::string>.

The ColData types add to the C++ string type the ability to automatically convert the string data to any of the basic C types. This is important with SQL, because all data coming from the database is in string form. MySQL++ uses this class internally to hold the data it receives from the server, so you can use it naturally, because it does the conversions implicitly:

 ColData("12.86") + 2.0 

That works fine, but be careful. If you had said this instead:

 ColData("12.86") + 2 

the result would be 14 because 2 is an integer, and C++'s type conversion rules put the ColData object in an integer context.

If these automatic conversions scare you, define the macro NO_BINARY_OPERS to disable this behavior.

This class also has some basic information about the type of data stored in it, to allow it to do the conversions more intelligently than a trivial implementation would allow.


Constructor & Destructor Documentation

template<class Str>
mysqlpp::ColData_Tmpl< Str >::ColData_Tmpl  )  [inline]
 

Default constructor.

Null flag is set to false, type data is not set, and string data is left empty.

It's probably a bad idea to use this ctor, becuase there's no way to set the type data once the object's constructed.

template<class Str>
mysqlpp::ColData_Tmpl< Str >::ColData_Tmpl const ColData_Tmpl< Str > &  cd  )  [inline]
 

Copy ctor.

Parameters:
cd the other ColData_Tmpl object

template<class Str>
mysqlpp::ColData_Tmpl< Str >::ColData_Tmpl bool  n,
mysql_type_info  t = mysql_type_info::string_type
[inline, explicit]
 

Constructor allowing you to set the null flag and the type data.

Parameters:
n if true, data is a SQL null
t MySQL type information for data being stored

template<class Str>
mysqlpp::ColData_Tmpl< Str >::ColData_Tmpl const std::string &  str,
mysql_type_info  t = mysql_type_info::string_type,
bool  n = false
[inline, explicit]
 

C++ string version of full ctor.

Parameters:
str the string this object represents
t MySQL type information for data within str
n if true, str is a SQL null

template<class Str>
mysqlpp::ColData_Tmpl< Str >::ColData_Tmpl const char *  str,
mysql_type_info  t = mysql_type_info::string_type,
bool  n = false
[inline, explicit]
 

Null-terminated C string version of full ctor.

Parameters:
str the string this object represents
t MySQL type information for data within str
n if true, str is a SQL null

template<class Str>
mysqlpp::ColData_Tmpl< Str >::ColData_Tmpl const char *  str,
typename Str::size_type  len,
mysql_type_info  t = mysql_type_info::string_type,
bool  n = false
[inline, explicit]
 

Full constructor.

Parameters:
str the string this object represents
len the length of the string; embedded nulls are legal
t MySQL type information for data within str
n if true, str is a SQL null


Member Function Documentation

template<class Str>
const std::string& mysqlpp::ColData_Tmpl< Str >::get_string  )  const [inline]
 

Returns this object's data in C++ string form.

This method is inefficient, and not recommended. It makes a duplicate copy of the string that lives as long as the ColData object itself.

If you are using the MutableColData typedef for this template, you can avoid the duplicate copy entirely. You can pass a MutableColData object to anything expecting a std::string and get the right result. (This didn't work reliably prior to v2.3.)

This method is arguably useful with plain ColData objects, but there are more efficient alternatives. If you know your data is a null-terminated C string, just cast this object to a const char* or call the data() method. This gives you a pointer to our internal buffer, so the copy isn't needed. If the ColData can contain embedded null characters, you do need to make a copy, but it's better to make your own copy of the string, instead of calling get_string(), so you can better control its lifetime:

            ColData cd = ...;
            std::string s(cd.data(), cd.length());

template<class Str>
template<class T, class B>
mysqlpp::ColData_Tmpl< Str >::operator Null< T, B >  )  const
 

Converts this object to a SQL null.

Returns a copy of the global null object if the string data held by the object is exactly equal to "NULL". Else, it constructs an empty object of type T and tries to convert it to Null<T, B>.


The documentation for this class was generated from the following file:
Generated on Wed Jul 11 15:35:09 2007 for MySQL++ by doxygen 1.3.5