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;
156 #if !defined(DOXYGEN_IGNORE)
158 typedef int difference_type;
159 typedef const char* const_pointer;
160 typedef const_pointer pointer;
180 buffer_(other.buffer_)
198 bool is_null =
false) :
199 buffer_(new
SQLBuffer(str, len, type, is_null))
212 bool is_null =
false) :
227 bool is_null =
false) :
244 bool is_null =
false)
246 buffer_ =
new SQLBuffer(str, len, type, is_null);
257 bool is_null =
false)
260 static_cast<size_type>(str.length()), type, is_null);
271 bool is_null =
false)
281 char at(size_type pos)
const;
288 const char*
c_str()
const {
return data(); }
290 #if defined(MYSQLPP_PLATFORM_VISUAL_CPP)
292 # pragma warning(disable: 4244)
297 template <
class Type>
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>
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;
411 bool quote_q()
const;
423 const char* pc = data();
426 while (n && (*pc ==
' ')) {
445 void to_string(std::string& s)
const;
454 String& operator =(
const std::string& rhs)
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)
572 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; }
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()) {
645 throw BadConversion(type_name, data(), 0, length());
654 friend class SQLTypeAdapter;
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)
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;
C++ form of SQL's DATETIME type.
Definition: datetime.h:49
C++ form of SQL's DATE type.
Definition: datetime.h:226
Class for holding data from a SQL column with the NULL attribute.
Definition: null.h:171
Holds SQL data in string form plus type information for use in converting the string to compatible C+...
Definition: sql_buffer.h:42
A std::string work-alike that can convert itself from SQL text data formats to C++ data types.
Definition: mystring.h:140
void assign(const char *str, size_type len, mysql_type_info type=mysql_type_info::string_type, bool is_null=false)
Assign raw data to this object.
Definition: mystring.h:242
void assign(const std::string &str, mysql_type_info type=mysql_type_info::string_type, bool is_null=false)
Assign a C++ string to this object.
Definition: mystring.h:255
void assign(const char *str, mysql_type_info type=mysql_type_info::string_type, bool is_null=false)
Assign a C string to this object.
Definition: mystring.h:269
size_type size() const
Return number of bytes in string.
Definition: mystring.h:417
~String()
Destroy string.
Definition: mystring.h:234
size_type max_size() const
Return the maximum number of characters in the string.
Definition: mystring.h:407
String(const String &other)
Copy ctor.
Definition: mystring.h:179
bool empty() const
Returns true if size() == 0.
Definition: mystring.h:376
const char * const_iterator
Type of iterators.
Definition: mystring.h:150
const char * c_str() const
Return a const pointer to the string data.
Definition: mystring.h:288
Null< T, B > conv(Null< T, B >) const
Overload of conv() for types wrapped with Null<>
Definition: mystring.h:318
Type conv(Type) const
Template for converting the column data to most any numeric data type.
Definition: mystring.h:298
const_iterator begin() const
Return iterator pointing to the first character of the string.
Definition: mystring.h:285
const_iterator iterator
Same as const_iterator because the data cannot be changed.
Definition: mystring.h:154
void strip_leading_blanks(std::string &s) const
Returns a copy of our internal string without leading blanks.
Definition: mystring.h:421
String()
Default constructor.
Definition: mystring.h:167
mysql_type_info type() const
Get this object's current MySQL type.
Definition: mystring.h:448
const char value_type
Type of the data stored in this object, when it is not equal to SQL null.
Definition: mystring.h:144
size_t size_type
Type of "size" integers.
Definition: mystring.h:147
String(const char *str, size_type len, mysql_type_info type=mysql_type_info::string_type, bool is_null=false)
Full constructor.
Definition: mystring.h:196
String(const std::string &str, mysql_type_info type=mysql_type_info::string_type, bool is_null=false)
C++ string version of full ctor.
Definition: mystring.h:210
String(const char *str, mysql_type_info type=mysql_type_info::string_type, bool is_null=false)
Null-terminated C string version of full ctor.
Definition: mystring.h:225
C++ form of SQL's TIME type.
Definition: datetime.h:348
SQL field type information.
Definition: type_info.h:159
static const enum_field_types string_type
The internal constant we use for our string type.
Definition: type_info.h:280
The type of the global mysqlpp::null object.
Definition: null.h:50
This file includes top-level definitions for use both internal to the library, and outside it....
Declares classes to add SQL-compatible date and time types to C++'s type system.
Declares the MySQL++-specific exception classes.
Declares classes that implement SQL "null" semantics within C++'s type system.
Declares the SQLBuffer class.
RefCountedPointer< SQLBuffer > RefCountedBuffer
Reference-counted version of SQLBuffer.
Definition: sql_buffer.h:136