/** @class gatsDBpp::ColData @brief Embodies the ColData class. This class mostly performs conversions to and from char* strings. Every ColData constructor converts the input type to a char* string. The asXXXXX methods will then convert the string to the appropriate data type. @date $Date$ @version $Rev$ @author - Lance Deaver @copyright (©) Copyright 2006 by GATS Inc. 11864 Canon Blvd., Suite 101, Newport News, VA 23606 All Rights Reserved. No part of this software or publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise without the prior written permission of GATS Inc. @see ColData.h @see GATS_DB_Exception.h @see GATS_Utilties.hpp @todo perhaps add a public setter method for the _quote data member. @todo overloaded assignment operators? Column C = "fred flintstone" @todo overloaded << operator? Column C << 10; @todo Add an EXPORT define so windows users can build DLLs. @bug None known @attention Unless noted, the default behaviour of the ColData constructors is to set the quoteNeeded value to false. Meaning that the data will not be escaped and quoted before it is sent to the Database API. $Id$ */ #include #include #include "ColData.h" #include "GATS_Utilities.hpp" #include "GATS_DB_Exception.h" using namespace gatsDBpp; using namespace GATS_Utilities; /** * This constructor will create a default NULL ColData object or if provided * a char*, create a static copy of the string and operate * on this copy. You would use this constructor if you wanted to work with * blobs of data. * * @exception none * * @param[in] cc A const char* referring to a char* string or anything that has been * recast as a char* string. * @param[in] len The length of the data in bytes. If provided, the constructor will make an internal copy of len bytes of memory * begining at the address of cc. If not provided or set to zero, the constructor will use a strcpy * method and copy up to the terminating NULL. * @param[in] quotedata If true, this class will inform the Database API that this data needs to be quoted * and have special characters escaped before it used by the DataBase. Blob data will need this set to true as * blob data may have NULL and other special characters embedded in mid string. * If false (the default), the data will be treated as a normal NULL terminated string. */ ColData::ColData(const char* cc, const unsigned long len, const bool quotedata) : _null(false), _quote(quotedata) { if(cc != NULL) { _length = (len == 0) ? strlen(cc) : len ; _data.reset(new char[_length+1]); memcpy(_data.get(),cc, _length); _data[_length] = '\0'; } else { _null = true; _length = 0; _data.reset(new char[5] ); strcpy(_data.get(), "NULL" ); } } /** * @exception none * * @param[in] s A std::string * @param[in] quotedata The default is true, meaning this class will inform the Database API that this data needs * quoted and have special characters escaped before it can be used by the database. If set to false, the data will be * treated as a normal NULL terminated string. */ ColData::ColData(const std::string& s, const bool quotedata) : _null(false),_quote(quotedata), _length(s.size() ) { _data.reset(new char[_length+1]); strcpy(_data.get(), s.c_str() ); } /** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] i An int value. */ ColData::ColData(const int i) : _null(false),_quote(false) { std::string s=ConvertToString(i); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } /** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] l A long value. */ ColData::ColData(const long int l) : _null(false),_quote(false) { std::string s=ConvertToString(l); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } /** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] ll A long long int value. */ ColData::ColData(const long long int ll) : _null(false),_quote(false) { std::string s=ConvertToString(ll); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } /** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] ll An unsigned long long int value. */ ColData::ColData(const unsigned long long int ll) : _null(false),_quote(false) { std::string s=ConvertToString(ll); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } /** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] is A short int value. */ ColData::ColData(const short int is) : _null(false),_quote(false) { std::string s=ConvertToString(is); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } /** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] is An unsigned int value. */ ColData::ColData(const unsigned int is) : _null(false),_quote(false) { std::string s=ConvertToString(is); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } /*** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] il An unsigned long value. */ ColData::ColData(const unsigned long int il) : _null(false),_quote(false) { std::string s=ConvertToString(il); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } /** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] il An unsigned short int value. */ ColData::ColData(const unsigned short int il) : _null(false),_quote(false) { std::string s=ConvertToString(il); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } /** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] f A float value. * @param[in] prec The number of precision digits to maintain in the conversion from float to string. The default * is pretty good so leave this alone unless you know what you're doing. */ ColData::ColData(const float f, const unsigned int prec) : _null(false),_quote(false) { std::string s=ConvertToStringPrec(f, prec); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } /** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] d A double value. * @param[in] prec The number of precision digits to maintain in the conversion from double to string. The default * is pretty good so leave this alone unless you know what you're doing. */ ColData::ColData(const double d, const unsigned int prec) : _null(false),_quote(false) { std::string s=ConvertToStringPrec(d, prec); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } /** * @exception BadTypeConversion Input value could not be converted to a string. * * @param[in] d A long double value. * @param[in] prec The number of precision digits to maintain in the conversion from long double to string. The default * is pretty good so leave this alone unless you know what you're doing. */ ColData::ColData(const long double d, const unsigned int prec) : _null(false),_quote(false) { std::string s=ConvertToStringPrec(d, prec); //can throw a BadTypeConversion object ColData C(s.c_str(), strlen(s.c_str() ) ); _length=C._length; _data=C._data; } ColData::~ColData() { } unsigned long int ColData::Length() const { return _length; } bool ColData::isNull() const { return _null; } bool ColData::needsQuote() const { return _quote; } bool ColData::isEmpty() const { return (_length == 0); } /** * @exception ColDataIsNull ColData is NULL. */ std::string ColData::asString() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return std::string(_data.get() ); } /** * @exception BadTypeConversion ColData could not be converted to a double. * @exception ColDataIsNull ColData is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid double representations. The default is true. */ double ColData::asDouble(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::scientific, failIfLeftoverChars); } /** * @exception BadTypeConversion ColData could not be converted to a long. * @exception ColDataIsNull ColData is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid long representations. The default is true. */ long int ColData::asLong(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::dec, failIfLeftoverChars); } /** * @exception BadTypeConversion ColData could not be converted to an unsigned long. * @exception ColDataIsNull ColData is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid unsigned long representations. The default is true. */ unsigned long int ColData::asUlong(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::dec, failIfLeftoverChars); } /** * @exception BadTypeConversion ColData could not be converted to an int. * @exception ColDataIsNull ColData is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid int representations. The default is true. */ int ColData::asInt(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::dec, failIfLeftoverChars); } /** * @exception BadTypeConversion ColData could not be converted to an unsigned int. * @exception ColDataIsNull ColData object is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid unsigned int representations. The default is true. */ unsigned int ColData::asUint(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::dec, failIfLeftoverChars); } /** * @exception BadTypeConversion ColData could not be converted to a long double. * @exception ColDataIsNull ColData object is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid long double representations. The default is true. */ long double ColData::asLongDouble(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::scientific, failIfLeftoverChars); } /** * @exception BadTypeConversion ColData could not be converted to a long long. * @exception ColDataIsNull ColData Object is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid long long representations. The default is true. */ long long int ColData::asLongLong(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::dec, failIfLeftoverChars); } /** * @exception BadTypeConversion ColData could not be converted to an unsigned long long. * @exception ColDataIsNull ColData object is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid unsigned long long representations. The default is true. */ unsigned long long int ColData::asUlongLong(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::dec, failIfLeftoverChars); } /** * @exception BadTypeConversion ColData could not be converted to a float. * @exception ColDataIsNull ColData object is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid float representations. The default is true. */ float ColData::asFloat(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::scientific, failIfLeftoverChars); } /** * @exception BadTypeConversion ColData could not be converted to a short. * @exception ColDataIsNull ColData object is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid short int representations. The default is true. */ short int ColData::asShort(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::dec, failIfLeftoverChars); } /** * @exception BadTypeConversion ColData could not be converted to an unsigned short. * @exception ColDataIsNull ColData Object is NULL. * * @param[in] failIfLeftoverChars Informs the class to throw a BadConversion object if the ColData object * contains any characters which are not valid unsigned short int representations. The default is true. */ unsigned short int ColData::asUshort(const bool failIfLeftoverChars) const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertFromString(std::string(_data.get() ), std::dec, failIfLeftoverChars); } /** * @exception ColDataIsNull ColData is a NULL object * * @retval const char* Returns a pointer to the the objects internal data as a NULL terminated char* string. * @warning Do not delete this pointer. It works just like the c_str() method of string.. If you delete[] it, * you've gathered more than enough rope to hang yourself. */ const char* ColData::asC_str() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return _data.get(); } /** * @exception BadTypeConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecDouble() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * @exception BadTypeConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecLong() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * @exception BadTypeConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecInt() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * @exception BadTypeConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecFloat() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * @exception BadTypeConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecUlong() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * @exception BadTypeConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecUint() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * @exception BadTypeConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecShort() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * @exception BadTypeConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecUshort() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * @exception BadConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecLongDouble() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * @exception BadTypeConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecLongLong() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * @exception BadTypeConversion ColData could not be converted to a vector. * @exception ColDataIsNull ColData object is NULL. */ std::vector ColData::asVecUlongLong() const { if(_null) { THROW_GATS_EXCEPTION(ColDataIsNull, "ColData Object is NULL" ); } return ConvertToVector(_data.get(), _length) ; } /** * Executes the ColData.C_str() method and puts the results in a std::ostream method. */ std::ostream& gatsDBpp::operator<<(std::ostream& os, const ColData& C) { return os << C.asC_str(); }