/*********************************************************************** stadapter.cpp - Implements the SQLTypeAdapter class. Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and (c) 2004-2009 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 ***********************************************************************/ #include "stadapter.h" #include "mystring.h" #include "refcounted.h" #include "stream2string.h" #include #include #include using namespace std; namespace mysqlpp { SQLTypeAdapter::SQLTypeAdapter() : is_processed_(false) { } SQLTypeAdapter::SQLTypeAdapter(const SQLTypeAdapter& other) : buffer_(other.buffer_), is_processed_(false) { } SQLTypeAdapter::SQLTypeAdapter(const String& other, bool processed) : buffer_(other.buffer_), is_processed_(processed) { } SQLTypeAdapter::SQLTypeAdapter(const std::string& str, bool processed) : buffer_(new SQLBuffer(str, mysql_type_info::string_type, false)), is_processed_(processed) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(const Null& str, bool processed) : buffer_(new SQLBuffer(str.is_null ? null_str : str.data, str.is_null ? typeid(void) : typeid(str.data), str.is_null)), is_processed_(processed) { } SQLTypeAdapter::SQLTypeAdapter(const Null& str, bool processed) : buffer_(new SQLBuffer( str.is_null ? null_str.c_str() : str.data.data(), str.is_null ? null_str.length() : str.data.length(), str.is_null ? typeid(void) : typeid(str.data), str.is_null)), is_processed_(processed) { } #endif SQLTypeAdapter::SQLTypeAdapter(const char* str, bool processed) : buffer_(new SQLBuffer(str, strlen(str), mysql_type_info::string_type, false)), is_processed_(processed) { } SQLTypeAdapter::SQLTypeAdapter(const char* str, int len, bool processed) : buffer_(new SQLBuffer(str, len, mysql_type_info::string_type, false)), is_processed_(processed) { } SQLTypeAdapter::SQLTypeAdapter(char c) : buffer_(new SQLBuffer(stream2string(c), mysql_type_info::string_type, false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null c) : buffer_(new SQLBuffer(c.is_null ? null_str : stream2string(c), c.is_null ? typeid(void) : typeid(c.data), c.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(tiny_int i) : buffer_(new SQLBuffer(stream2string(i), typeid(i), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null > i) : buffer_(new SQLBuffer(i.is_null ? null_str : stream2string(i), i.is_null ? typeid(void) : typeid(i.data), i.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(tiny_int i) : buffer_(new SQLBuffer(stream2string(i), typeid(i), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null > i) : buffer_(new SQLBuffer(i.is_null ? null_str : stream2string(i), i.is_null ? typeid(void) : typeid(i.data), i.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(short i) : buffer_(new SQLBuffer(stream2string(i), typeid(i), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null i) : buffer_(new SQLBuffer(i.is_null ? null_str : stream2string(i), i.is_null ? typeid(void) : typeid(i.data), i.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(unsigned short i) : buffer_(new SQLBuffer(stream2string(i), typeid(i), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null i) : buffer_(new SQLBuffer(i.is_null ? null_str : stream2string(i), i.is_null ? typeid(void) : typeid(i.data), i.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(int i) : buffer_(new SQLBuffer(stream2string(i), typeid(i), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null i) : buffer_(new SQLBuffer(i.is_null ? null_str : stream2string(i), i.is_null ? typeid(void) : typeid(i.data), i.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(unsigned i) : buffer_(new SQLBuffer(stream2string(i), typeid(i), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null i) : buffer_(new SQLBuffer(i.is_null ? null_str : stream2string(i), i.is_null ? typeid(void) : typeid(i.data), i.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(long i) : buffer_(new SQLBuffer(stream2string(i), typeid(i), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null i) : buffer_(new SQLBuffer(i.is_null ? null_str : stream2string(i), i.is_null ? typeid(void) : typeid(i.data), i.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(unsigned long i) : buffer_(new SQLBuffer(stream2string(i), typeid(i), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null i) : buffer_(new SQLBuffer(i.is_null ? null_str : stream2string(i), i.is_null ? typeid(void) : typeid(i.data), i.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(longlong i) : buffer_(new SQLBuffer(stream2string(i), typeid(i), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null i) : buffer_(new SQLBuffer(i.is_null ? null_str : stream2string(i), i.is_null ? typeid(void) : typeid(i.data), i.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(ulonglong i) : buffer_(new SQLBuffer(stream2string(i), typeid(i), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null i) : buffer_(new SQLBuffer(i.is_null ? null_str : stream2string(i), i.is_null ? typeid(void) : typeid(i.data), i.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(float f) : is_processed_(false) { typedef numeric_limits nlf; if ((nlf::has_infinity && (f == nlf::infinity())) || (nlf::has_quiet_NaN && (f == nlf::quiet_NaN())) || (nlf::has_signaling_NaN && (f == nlf::signaling_NaN()))) { // f isn't null-able, but it's infinite or NaN, so store it // as a 0. This at least prevents syntactically-invalid SQL. buffer_ = new SQLBuffer("0", typeid(f), true); } else { ostringstream outs; outs.precision(9); // max dec digits needed for IEEE 754 32-bit float outs << f; buffer_ = new SQLBuffer(outs.str(), typeid(f), false); } } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null f) : is_processed_(false) { typedef numeric_limits nlf; if (f.is_null || (nlf::has_infinity && (f.data == nlf::infinity())) || (nlf::has_quiet_NaN && (f.data == nlf::quiet_NaN())) || (nlf::has_signaling_NaN && (f.data == nlf::signaling_NaN()))) { // MySQL wants infinite and NaN FP values stored as SQL NULL buffer_ = new SQLBuffer(null_str, typeid(void), true); } else { ostringstream outs; outs.precision(9); // as above outs << f; buffer_ = new SQLBuffer(outs.str(), typeid(f.data), false); } } #endif SQLTypeAdapter::SQLTypeAdapter(double f) : is_processed_(false) { typedef numeric_limits nld; if ((nld::has_infinity && (f == nld::infinity())) || (nld::has_quiet_NaN && (f == nld::quiet_NaN())) || (nld::has_signaling_NaN && (f == nld::signaling_NaN()))) { // f isn't null-able, but it's infinite or NaN, so store it // as a 0. This at least prevents syntactically-invalid SQL. buffer_ = new SQLBuffer("0", typeid(f), true); } else { ostringstream outs; outs.precision(17); // max dec digits needed for IEEE 754 64-bit float outs << f; buffer_ = new SQLBuffer(outs.str(), typeid(f), false); } } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(Null f) : is_processed_(false) { typedef numeric_limits nld; if (f.is_null || (nld::has_infinity && (f.data == nld::infinity())) || (nld::has_quiet_NaN && (f.data == nld::quiet_NaN())) || (nld::has_signaling_NaN && (f.data == nld::signaling_NaN()))) { // MySQL wants infinite and NaN FP values stored as SQL NULL buffer_ = new SQLBuffer(null_str, typeid(void), true); } else { ostringstream outs; outs.precision(17); // as above outs << f; buffer_ = new SQLBuffer(outs.str(), typeid(f.data), false); } } #endif SQLTypeAdapter::SQLTypeAdapter(const Date& d) : buffer_(new SQLBuffer(stream2string(d), typeid(d), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(const Null& d) : buffer_(new SQLBuffer(d.is_null ? null_str : stream2string(d), d.is_null ? typeid(void) : typeid(d.data), d.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(const DateTime& dt) : buffer_(new SQLBuffer(stream2string(dt), typeid(dt), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(const Null& dt) : buffer_(new SQLBuffer(dt.is_null ? null_str : stream2string(dt), dt.is_null ? typeid(void) : typeid(dt.data), dt.is_null)), is_processed_(false) { } #endif SQLTypeAdapter::SQLTypeAdapter(const Time& t) : buffer_(new SQLBuffer(stream2string(t), typeid(t), false)), is_processed_(false) { } #if !defined(DOXYGEN_IGNORE) SQLTypeAdapter::SQLTypeAdapter(const Null