Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DbiOutRowStream.cxx

Go to the documentation of this file.
00001 
00002 // $Id: DbiOutRowStream.cxx,v 1.19 2006/08/08 10:51:32 west Exp $
00003 //
00004 // DbiOutRowStream
00005 
00006 #include <sstream>
00007 
00008 #include "DatabaseInterface/DbiFieldType.h"
00009 #include "DatabaseInterface/DbiOutRowStream.h"
00010 #include "DatabaseInterface/DbiTableMetaData.h"
00011 #include "LeakChecker/Lea.h"
00012 #include "MessageService/MsgService.h"
00013 #include "Util/UtilString.h"
00014 #include "Validity/VldTimeStamp.h"
00015 
00016 ClassImp(DbiOutRowStream)
00017 
00018 #define OUT(t,v)                         \
00019   if ( ! StoreDefaultIfInvalid(t) ) {    \
00020     ostringstream out;                   \
00021     out << setprecision(16)<< v;         \
00022     Store(out.str());                    \
00023   }                                      \
00024 
00025 // If writing unsigned dat as signed, convert bit pattern to signed, 
00026 // extending sign bit if necessary.                                  
00027 // For BIGINT (size 8) make an exception.  It's used only as
00028 // an alternative to unsigned int so can written without conversion.
00029 #define OUT2(t,v)                         \
00030   const DbiFieldType& fType = this->ColFieldType(this->CurColNum());             \
00031   if ( fType.IsSigned() && fType.GetSize() != 8 ) {                              \
00032     Int_t v_signed = (Int_t) v;                                                  \
00033     if ( fType.GetType() == Dbi::kTiny  && v & 0x80   ) v_signed |= 0xffffff00;  \
00034     if ( fType.GetType() == Dbi::kShort && v & 0x8000 ) v_signed |= 0xffff0000;  \
00035     OUT(Dbi::kInt,v_signed); }                                                   \
00036   else {                                                                         \
00037     OUT(t,v);                                                                    \
00038   }                                                                              \
00039 
00040 //   Definition of static data members
00041 //   *********************************
00042 
00043 CVSID("$Id: DbiOutRowStream.cxx,v 1.19 2006/08/08 10:51:32 west Exp $");
00044 
00045 //    Definition of all member functions (static or otherwise)
00046 //    *******************************************************
00047 //
00048 //    -  ordered: ctors, dtor, operators then in alphabetical order.
00049 
00050 //.....................................................................
00051 
00052 DbiOutRowStream::DbiOutRowStream(const DbiTableMetaData* metaData) :
00053 DbiRowStream(metaData),
00054 fBadData(kFALSE)
00055 {
00056 //
00057 //
00058 //  Purpose:  Default constructor
00059 //
00060 //  Arguments:
00061 //     metaData in  Meta data for table to be written to..
00062 
00063   LEA_CTOR    //Leak Checker
00064 
00065   MSG("Dbi", Msg::kVerbose) << "Creating DbiOutRowStream" << endl;
00066 
00067 }
00068 
00069 
00070 //.....................................................................
00071 
00072 DbiOutRowStream::~DbiOutRowStream() {
00073 //
00074 //
00075 //  Purpose: Destructor
00076 
00077   LEA_DTOR    //Leak Checker
00078 
00079   MSG("Dbi", Msg::kVerbose) << "Destroying DbiOutRowStream" << endl;
00080 
00081 }
00082 
00083 //.....................................................................
00084 
00085 DbiOutRowStream& DbiOutRowStream::operator<<(Bool_t src) {
00086                                      OUT(Dbi::kBool,src);  return *this;}
00087 
00088 DbiOutRowStream& DbiOutRowStream::operator<<(Char_t src) {
00089                                      OUT(Dbi::kChar,src);  return *this;}
00090 
00091 DbiOutRowStream& DbiOutRowStream::operator<<(const Char_t* src) {
00092                                      OUT(Dbi::kString,src);  return *this;}
00093 
00094 DbiOutRowStream& DbiOutRowStream::operator<<(Short_t src) {
00095                                      OUT(Dbi::kShort,src);  return *this;}
00096 
00097 DbiOutRowStream& DbiOutRowStream::operator<<(UShort_t src) {
00098                                      OUT2(Dbi::kUShort,src); return *this;}
00099 
00100 DbiOutRowStream& DbiOutRowStream::operator<<(Int_t src) {
00101                                      OUT(Dbi::kInt,src);  return *this;}
00102 
00103 DbiOutRowStream& DbiOutRowStream::operator<<(UInt_t src) {
00104                                      OUT2(Dbi::kUInt,src);  return *this;}
00105 
00106 DbiOutRowStream& DbiOutRowStream::operator<<(Float_t src) {
00107                                      OUT(Dbi::kFloat,src); return *this;}
00108 
00109 DbiOutRowStream& DbiOutRowStream::operator<<(Double_t src) {
00110                                      OUT(Dbi::kDouble,src);  return *this;}
00111 
00112 DbiOutRowStream& DbiOutRowStream::operator<<(const string& src) {
00113                                     OUT(Dbi::kString,src); return *this;}
00114 
00115 DbiOutRowStream& DbiOutRowStream::operator<<(const VldTimeStamp& src) {
00116   if ( ! StoreDefaultIfInvalid(Dbi::kDate) )
00117                           Store(Dbi::MakeDateTimeString(src).c_str());
00118   return *this;
00119 }
00120 
00121 
00122 
00123 
00124 //.....................................................................
00125 
00126 Bool_t DbiOutRowStream::StoreDefaultIfInvalid(Dbi::DataTypes type) {
00127 //
00128 //
00129 //  Purpose:  Store default value if illegal type supplied.
00130 //
00131 //  Arguments: 
00132 //    type         in    Type of supplied value.
00133 //
00134 //  Return:    kTRUE if illegal type supplied.
00135 //
00136 //  Contact:   N. West
00137 //
00138 //  Specification:-
00139 //  =============
00140 //
00141 //  o If type of supplied value is incompatible with current column
00142 //    type store default value, report error and return kTRUE, otherwise
00143 //    return kFALSE.
00144 
00145 //  Program Notes:-
00146 //  =============
00147 
00148 //  None.
00149 
00150   DbiFieldType typeSupplied(type);
00151   DbiFieldType typeRequired(CurColFieldType());
00152   if ( typeSupplied.IsCompatible(typeRequired) ) return kFALSE;
00153 
00154   string udef = typeRequired.UndefinedValue();
00155   MAXMSG("Dbi",Msg::kError,20) 
00156       << "In table " << TableNameTc() 
00157       << " column "<< CurColNum() 
00158       << " (" << CurColName() << ")"
00159       << " of type " << typeRequired.AsString()
00160       << " is incompatible with user type " << typeSupplied.AsString()
00161       << ", value \"" << udef
00162       << "\" will be substituted." <<  endl;
00163   Store(udef.c_str());
00164   fBadData = kTRUE;
00165   return kTRUE; 
00166 
00167 }
00168 //.....................................................................
00169 
00170 void DbiOutRowStream::Store(const string& str)  {
00171 //
00172 //
00173 //  Purpose: Store string value as comma separated values but exclude SeqNo.  
00174 //
00175 //  Arguments: 
00176 //    str          in    Value to be stored.
00177 //
00178 //  Return:    
00179 //
00180 //  Contact:   N. West
00181 //
00182 //  Specification:-
00183 //  =============
00184 //
00185 //  o Store string value in CSV with separator from 
00186 //    previous value and increment fNumValues.  If required
00187 //    enclose value in quotes. If storing the SeqNo column then
00188 //    store a single '?' instead which will be replace during
00189 //    SQL generation - see 
00190 
00191 //  Program Notes:-
00192 //  =============
00193 
00194 //  None.
00195 
00196   UInt_t concept = CurColFieldType().GetConcept();
00197   string delim = "";
00198   if (    concept == Dbi::kString 
00199        || concept == Dbi::kDate 
00200        || concept == Dbi::kChar ) delim = "\'";  
00201 
00202   if ( CurColNum()> 1 ) fCSV += ',';
00203   fCSV += delim;
00204   if ( concept != Dbi::kString ) fCSV += str;
00205 //  When exporting strings, take care of special characters.
00206   else {
00207     UtilString::MakePrintable(str.c_str(),fCSV);
00208   }
00209   fCSV += delim;
00210   IncrementCurCol();
00211 }
00212 

Generated on Mon Feb 15 11:06:34 2010 for loon by  doxygen 1.3.9.1