Go to the documentation of this file.
29 #if !defined(MYSQLPP_MYSTRING_H)
30 #define MYSQLPP_MYSTRING_H
48 #if !defined(DOXYGEN_IGNORE)
53 template<typename T, bool is_signed = std::numeric_limits<T>::is_signed>
54 struct conv_promotion;
57 struct conv_promotion<float>
63 struct conv_promotion<double>
68 # if !defined(NO_LONG_LONGS)
70 struct conv_promotion<unsigned long long>
72 typedef unsigned long long type;
76 struct conv_promotion<long long>
78 typedef long long type;
84 struct conv_promotion<char>
92 struct conv_promotion<T, true>
98 struct conv_promotion<T, false>
100 typedef unsigned long type;
104 class MYSQLPP_EXPORT SQLTypeAdapter;
105 #endif // !defined(DOXYGEN_IGNORE)
139 class MYSQLPP_EXPORT String
144 typedef const char value_type;
147 typedef size_t size_type;
150 typedef const char* const_iterator;
154 typedef const_iterator iterator;
156 #if !defined(DOXYGEN_IGNORE)
158 typedef int difference_type;
159 typedef const char* const_pointer;
160 typedef const_pointer pointer;
161 #endif // !defined(DOXYGEN_IGNORE)
180 buffer_(other.buffer_)
196 explicit String(
const char* str, size_type len,
198 bool is_null =
false) :
199 buffer_(new
SQLBuffer(str, len, type, is_null))
210 explicit String(
const std::string& str,
212 bool is_null =
false) :
225 explicit String(
const char* str,
227 bool is_null =
false) :
228 buffer_(new
SQLBuffer(str, static_cast<size_type>(strlen(str)),
242 void assign(
const char* str, size_type len,
244 bool is_null =
false)
246 buffer_ =
new SQLBuffer(str, len, type, is_null);
255 void assign(
const std::string& str,
257 bool is_null =
false)
259 buffer_ =
new SQLBuffer(str.data(),
260 static_cast<size_type
>(str.length()), type, is_null);
269 void assign(
const char* str,
271 bool is_null =
false)
281 char at(size_type pos)
const;
285 const_iterator begin()
const {
return data(); }
288 const char* c_str()
const {
return data(); }
290 #if defined(MYSQLPP_PLATFORM_VISUAL_CPP)
292 # pragma warning(disable: 4244)
297 template <
class Type>
298 Type conv(Type)
const
303 typedef typename detail::conv_promotion<Type>::type conv_type;
304 return do_conv<conv_type>(
typeid(Type).name());
307 #if defined(MYSQLPP_PLATFORM_VISUAL_CPP)
308 # pragma warning(default: 4244)
317 template <
class T,
class B>
324 return Null<T, B>(conv(T()));
333 int compare(
const String& other)
const;
340 int compare(
const std::string& other)
const;
350 int compare(size_type pos, size_type num, std::string& other)
const;
357 int compare(
const char* other)
const;
369 int compare(size_type pos, size_type num,
const char* other)
const;
373 const char* data()
const;
376 bool empty()
const {
return size() == 0; }
380 const_iterator end()
const;
384 bool escape_q()
const;
387 bool is_null()
const;
400 size_type length()
const;
407 size_type max_size()
const {
return size(); }
411 bool quote_q()
const;
417 size_type size()
const {
return length(); }
421 void strip_leading_blanks(std::string& s)
const
423 const char* pc = data();
425 size_type n = length();
426 while (n && (*pc ==
' ')) {
445 void to_string(std::string& s)
const;
448 mysql_type_info type()
const
454 String& operator =(
const std::string& rhs)
457 static_cast<size_type
>(rhs.length()),
467 String& operator =(
const char* str)
470 static_cast<size_type
>(strlen(str)),
481 String& operator =(
const String& other)
483 buffer_ = other.buffer_;
492 template <
typename T>
493 bool operator ==(
const T& rhs)
const
495 return compare(rhs) == 0;
510 template <
typename T>
511 bool operator !=(
const T& rhs)
const
513 return compare(rhs) != 0;
530 char operator [](size_type pos)
const {
return at(pos); }
533 operator const char*()
const {
return data(); }
536 operator signed char()
const
537 {
return conv(
static_cast<signed char>(0)); }
540 operator unsigned char()
const
541 {
return conv(
static_cast<unsigned char>(0)); }
545 {
return conv(
static_cast<int>(0)); }
548 operator unsigned int()
const
549 {
return conv(
static_cast<unsigned int>(0)); }
552 operator short int()
const
553 {
return conv(
static_cast<short int>(0)); }
557 operator unsigned short int()
const
558 {
return conv(
static_cast<unsigned short int>(0)); }
561 operator long int()
const
562 {
return conv(
static_cast<long int>(0)); }
566 operator unsigned long int()
const
567 {
return conv(
static_cast<unsigned long int>(0)); }
569 #if !defined(NO_LONG_LONGS)
570 operator longlong()
const
573 {
return conv(
static_cast<longlong
>(0)); }
577 operator ulonglong()
const
578 {
return conv(
static_cast<ulonglong
>(0)); }
582 operator float()
const
583 {
return conv(
static_cast<float>(0)); }
586 operator double()
const
587 {
return conv(
static_cast<double>(0)); }
590 operator bool()
const {
return buffer_ ? atoi(c_str()) :
false; }
593 operator Date()
const {
return buffer_ ?
Date(*
this) :
Date(); }
600 operator Time()
const {
return buffer_ ?
Time(*
this) :
Time(); }
605 template <
class T,
class B>
610 template <
class Type>
611 Type do_conv(
const char* type_name)
const
614 std::stringstream buf;
615 buf.write(data(),
static_cast<std::streamsize
>(length()));
616 buf.imbue(std::locale::classic());
628 (
typeid(Type) !=
typeid(
float)) &&
629 (
typeid(Type) !=
typeid(
double))) {
635 while (buf >> c && c ==
'0') ;
636 if (buf.eof() && c ==
'0') {
641 else if (buf.eof()) {
657 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& o,
661 #if !defined(MYSQLPP_NO_BINARY_OPERS) && !defined(DOXYGEN_IGNORE)
667 #define oprsw(opr, other, conv) \
668 inline other operator opr (String x, other y) \
669 { return static_cast<conv>(x) opr y; } \
670 inline other operator opr (other x, String y) \
671 { return x opr static_cast<conv>(y); }
673 #define operator_binary(other, conv) \
674 oprsw(+, other, conv) \
675 oprsw(-, other, conv) \
676 oprsw(*, other, conv) \
677 oprsw(/, other, conv)
679 #define operator_binary_int(other, conv) \
680 operator_binary(other, conv) \
681 oprsw(%, other, conv) \
682 oprsw(&, other, conv) \
683 oprsw(^, other, conv) \
684 oprsw(|, other, conv) \
685 oprsw(<<, other, conv) \
686 oprsw(>>, other, conv)
689 #if defined(MYSQLPP_PLATFORM_VISUAL_CPP)
690 # pragma warning(disable: 4244)
693 operator_binary(
float,
double)
694 operator_binary(
double,
double)
696 operator_binary_int(
char,
long int)
697 operator_binary_int(
int,
long int)
698 operator_binary_int(
short int,
long int)
699 operator_binary_int(
long int,
long int)
701 operator_binary_int(
unsigned char,
unsigned long int)
702 operator_binary_int(
unsigned int,
unsigned long int)
703 operator_binary_int(
unsigned short int,
unsigned long int)
704 operator_binary_int(
unsigned long int,
unsigned long int)
706 #if defined(MYSQLPP_PLATFORM_VISUAL_CPP)
707 # pragma warning(default: 4244)
710 #if !defined(NO_LONG_LONGS)
711 operator_binary_int(longlong, longlong)
712 operator_binary_int(ulonglong, ulonglong)
713 #endif // !defined(NO_LONG_LONGS)
714 #endif // !defined(MYSQLPP_NO_BINARY_OPERS) && !defined(DOXYGEN_IGNORE)
717 #if !defined(DOXYGEN_IGNORE)
726 template <> MYSQLPP_EXPORT
bool String::conv(
bool)
const;
740 template <> MYSQLPP_EXPORT String
String::conv(String)
const;
743 template <> MYSQLPP_EXPORT std::string
String::conv(std::string)
const;
750 template <> MYSQLPP_EXPORT Date
String::conv(Date)
const;
757 template <> MYSQLPP_EXPORT DateTime
String::conv(DateTime)
const;
764 template <> MYSQLPP_EXPORT Time
String::conv(Time)
const;
766 #endif // !defined(DOXYGEN_IGNORE)
770 #endif // !defined(MYSQLPP_MYSTRING_H)
const mysql_type_info & type() const
Return the SQL type of the data held in the buffer.
Definition: sql_buffer.h:157
const typedef char * const_iterator
Type of iterators.
Definition: mystring.h:194
const char * data() const
Return pointer to raw data buffer.
Definition: sql_buffer.h:124
const char * c_str() const
Return a const pointer to the string data.
Definition: mystring.h:332
Type conv(Type) const
Template for converting the column data to most any numeric data type.
Definition: mystring.h:342
size_t size_type
Type of "size" integers.
Definition: mystring.h:191
void to_string(std::string &s) const
Copies this object's data into a C++ string.
Definition: mystring.cpp:236
const_iterator end() const
Return iterator pointing to one past the last character of the string.
Definition: mystring.cpp:187
Declares the MySQL++-specific exception classes.
size_type length() const
Return number of bytes in data buffer.
Definition: sql_buffer.h:136
void set_null()
Sets the internal SQL null flag.
Definition: sql_buffer.h:154
size_type length() const
Return number of bytes in the string.
Definition: mystring.cpp:220
size_type size() const
Return number of bytes in string.
Definition: mystring.h:461
C++ form of SQL's DATE type.
Definition: datetime.h:247
Exception thrown when a bad type conversion is attempted.
Definition: exceptions.h:113
Holds SQL data in string form plus type information for use in converting the string to compatible C+...
Definition: sql_buffer.h:63
bool escape_q() const
Returns true if data of this type should be escaped, false otherwise.
Definition: mystring.cpp:194
A class for building and executing SQL queries.
Definition: query.h:143
void it_is_null()
Set a flag indicating that this object is a SQL null.
Definition: mystring.cpp:208
Defines a class for building and executing SQL queries.
Converts many different data types to strings suitable for use in SQL queries.
Definition: stadapter.h:95
bool escape_q() const
Returns true if the SQL type is of a type that needs to be escaped.
Definition: type_info.cpp:283
The type of the global mysqlpp::null object.
Definition: null.h:71
Declares classes that implement SQL "null" semantics within C++'s type system.
bool is_null() const
Returns true if this object is a SQL null.
Definition: mystring.cpp:201
bool quote_q() const
Returns true if the SQL type is of a type that needs to be quoted.
Definition: type_info.cpp:268
C++ form of SQL's TIME type.
Definition: datetime.h:369
const char * data() const
Raw access to the underlying buffer, with no C string interpretation.
Definition: mystring.cpp:180
Class for holding data from a SQL column with the NULL attribute.
Definition: null.h:192
char at(size_type pos) const
Return a character within the string.
Definition: mystring.cpp:62
This file includes top-level definitions for use both internal to the library, and outside it....
A std::string work-alike that can convert itself from SQL text data formats to C++ data types.
Definition: mystring.h:161
bool is_null() const
Return true if buffer's contents represent a SQL null.
Definition: sql_buffer.h:147
int compare(const String &other) const
Lexically compare this string to another.
Definition: mystring.cpp:74
bool quote_q() const
Returns true if data of this type should be quoted, false otherwise.
Definition: mystring.cpp:227
SQL field type information.
Definition: type_info.h:170
C++ form of SQL's DATETIME type.
Definition: datetime.h:70
static const enum_field_types string_type
The internal constant we use for our string type.
Definition: type_info.h:314
Declares String class, MySQL++'s generic std::string-like class, used for holding data received from ...
Declares the SQLBuffer class.
Declares classes to add SQL-compatible date and time types to C++'s type system.