TDbiOutRowStream.cxx

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

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1