/////////////////////////////////////////////////////////////////////// // $Id: IDbiOutRowStream.cxx,v 1.1 2011/01/18 05:49:20 finch Exp $ // // COMET::IDbiOutRowStream #include #include "IDbiFieldType.hxx" #include "IDbiOutRowStream.hxx" #include "IDbiTableMetaData.hxx" #include #include using std::endl; #include "UtilString.hxx" #include "IVldTimeStamp.hxx" ClassImp(COMET::IDbiOutRowStream) #define OUT(t,v) \ if ( ! StoreDefaultIfInvalid(t) ) { \ ostringstream out; \ out << setprecision(16)<< v; \ Store(out.str()); \ } \ // If writing unsigned dat as signed, convert bit pattern to signed, // extending sign bit if necessary. // For BIGINT (size 8) make an exception. It's used only as // an alternative to unsigned int so can written without conversion. #define OUT2(t,v) \ const COMET::IDbiFieldType& fType = this->ColFieldType(this->CurColNum()); \ if ( fType.IsSigned() && fType.GetSize() != 8 ) { \ Int_t v_signed = (Int_t) v; \ if ( fType.GetType() == IDbi::kTiny && v & 0x80 ) v_signed |= 0xffffff00; \ if ( fType.GetType() == IDbi::kShort && v & 0x8000 ) v_signed |= 0xffff0000; \ OUT(IDbi::kInt,v_signed); } \ else { \ OUT(t,v); \ } \ // Definition of static data members // ********************************* // Definition of all member functions (static or otherwise) // ******************************************************* // // - ordered: ctors, dtor, operators then in alphabetical order. //..................................................................... COMET::IDbiOutRowStream::IDbiOutRowStream(const COMET::IDbiTableMetaData* metaData) : COMET::IDbiRowStream(metaData), fBadData(kFALSE) { // // // Purpose: Default constructor // // Arguments: // metaData in Meta data for table to be written to.. COMETTrace( "Creating COMET::IDbiOutRowStream" << " "); } //..................................................................... COMET::IDbiOutRowStream::~IDbiOutRowStream() { // // // Purpose: Destructor COMETTrace( "Destroying COMET::IDbiOutRowStream" << " "); } //..................................................................... COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(Bool_t src) { OUT(IDbi::kBool,src); return *this;} COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(Char_t src) { OUT(IDbi::kChar,src); return *this;} COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(const Char_t* src) { OUT(IDbi::kString,src); return *this;} COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(Short_t src) { OUT(IDbi::kShort,src); return *this;} COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(UShort_t src) { OUT2(IDbi::kUShort,src); return *this;} COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(Int_t src) { OUT(IDbi::kInt,src); return *this;} COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(UInt_t src) { OUT2(IDbi::kUInt,src); return *this;} COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(Float_t src) { OUT(IDbi::kFloat,src); return *this;} COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(Double_t src) { OUT(IDbi::kDouble,src); return *this;} COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(const string& src) { OUT(IDbi::kString,src); return *this;} COMET::IDbiOutRowStream& COMET::IDbiOutRowStream::operator<<(const COMET::IVldTimeStamp& src) { if ( ! StoreDefaultIfInvalid(IDbi::kDate) ) Store(IDbi::MakeDateTimeString(src).c_str()); return *this; } //..................................................................... Bool_t COMET::IDbiOutRowStream::StoreDefaultIfInvalid(IDbi::DataTypes type) { // // // Purpose: Store default value if illegal type supplied. // // Arguments: // type in Type of supplied value. // // Return: kTRUE if illegal type supplied. // // Contact: N. West // // Specification:- // ============= // // o If type of supplied value is incompatible with current column // type store default value, report error and return kTRUE, otherwise // return kFALSE. // Program Notes:- // ============= // None. COMET::IDbiFieldType typeSupplied(type); COMET::IDbiFieldType typeRequired(CurColFieldType()); if ( typeSupplied.IsCompatible(typeRequired) ) return kFALSE; string udef = typeRequired.UndefinedValue(); COMETSevere( "In table " << TableNameTc() << " column "<< CurColNum() << " (" << CurColName() << ")" << " of type " << typeRequired.AsString() << " is incompatible with user type " << typeSupplied.AsString() << ", value \"" << udef << "\" will be substituted." << " "); Store(udef.c_str()); fBadData = kTRUE; return kTRUE; } //..................................................................... void COMET::IDbiOutRowStream::Store(const string& str) { // // // Purpose: Store string value as comma separated values but exclude SeqNo. // // Arguments: // str in Value to be stored. // // Return: // // Contact: N. West // // Specification:- // ============= // // o Store string value in CSV with separator from // previous value and increment fNumValues. If required // enclose value in quotes. If storing the SeqNo column then // store a single '?' instead which will be replace during // SQL generation - see // Program Notes:- // ============= // None. UInt_t concept = CurColFieldType().GetConcept(); string delim = ""; if ( concept == IDbi::kString || concept == IDbi::kDate || concept == IDbi::kChar ) delim = "\'"; if ( CurColNum()> 1 ) fCSV += ','; fCSV += delim; if ( concept != IDbi::kString ) fCSV += str; // When exporting strings, take care of special characters. else { COMET::UtilString::MakePrintable(str.c_str(),fCSV); } fCSV += delim; IncrementCurCol(); }