TDbiResultSetNonAgg.cxx

Go to the documentation of this file.
00001 // $Id: TDbiResultSetNonAgg.cxx,v 1.2 2011/06/08 09:49:18 finch Exp $
00002 
00003 #include "TDbiBinaryFile.hxx"
00004 #include "TDbiResultKey.hxx"
00005 #include "TDbiResultSetNonAgg.hxx"
00006 #include "TDbiInRowStream.hxx"
00007 #include "TDbiTableRow.hxx"
00008 #include "TDbiTimerManager.hxx"
00009 #include <TSK_DBI_Log.hxx>
00010 #include <MsgFormat.h>
00011 using std::endl;
00012 
00013 ClassImp(TDbiResultSetNonAgg)
00014 
00015 //   Definition of static data members
00016 //   *********************************
00017 
00018 
00019 
00020 //    Definition of all member functions (static or otherwise)
00021 //    *******************************************************
00022 //
00023 //    -  ordered: ctors, dtor, operators then in alphabetical order.
00024 
00025 //
00026 //.....................................................................
00027 ///\verbatim
00028 ///  Purpose:  Default constructor
00029 ///
00030 ///  Arguments:
00031 ///      resultSet    in/out Pointer TDbiInRowStream from query. May be null.
00032 ///      tableRow     in     Pointer to a sample tableRow object.
00033 ///                          May be null.
00034 ///      vrec         in     Pointer to validity record from query.
00035 ///                          May be null
00036 ///      dropSeqNo    in     If = kTRUE, drop SeqNo if it is the first col.
00037 ///      sqlQualifier in     Extended Context sql qualifiers
00038 ///
00039 ///  Return:    n/a
00040 ///
00041 ///  Contact:   N. West
00042 ///
00043 ///  Specification:-
00044 ///  =============
00045 ///
00046 ///  o Create Result from TDbiInRowStream generated by query.  If first
00047 ///    column is named SeqNo then strip it off before filling each
00048 ///    TDbiTableRow and quit as soon as the SeqNo changes.
00049 ///
00050 ///
00051 ///  Program Notes:-
00052 ///  =============
00053 ///
00054 ///  o tableRow is just used to create new subclass TDbiTableRow objects.
00055 ///
00056 ///  o  The special treatment for tables that start with SeqNo allow
00057 ///     a single TDbiInRowStream to fill multiple TDbiResultSet objects but does
00058 ///     require that the result set is ordered by SeqNo.
00059 ///
00060 ///  The look-up table is not built by default, its construction is
00061 ///  triggered by use (GetTableRowByIndex).  For TDbiResultSetNonAgg
00062 ///  that are part of a TDbiResultSetAgg there is no need to build the
00063 ///  table.
00064 ///\endverbatim
00065 
00066 TDbiResultSetNonAgg::TDbiResultSetNonAgg(TDbiInRowStream* resultSet,
00067                                  const TDbiTableRow* tableRow,
00068                                  const TDbiValidityRec* vrec,
00069                                  Bool_t dropSeqNo,
00070                                  const string& sqlQualifiers) :
00071 TDbiResultSet(resultSet,vrec,sqlQualifiers),
00072 fBuffer(0)
00073 {
00074 
00075   this->DebugCtor();
00076 
00077   if ( ! resultSet || resultSet->IsExhausted() || ! tableRow ) return;
00078 
00079   if ( vrec ) TDbiTimerManager::gTimerManager.RecFillAgg(vrec->GetAggregateNo());
00080 
00081 //Move to first row if result set not yet started.
00082   TDbiInRowStream& rs = *resultSet;
00083   if ( rs.IsBeforeFirst() ) rs.FetchRow();
00084   if ( rs.IsExhausted() ) return;
00085 
00086 //Check and load sequence number if necessary.
00087   Int_t seqNo = 0;
00088   if ( dropSeqNo && rs.CurColName() == "SEQNO" ) {
00089     rs >> seqNo;
00090     rs.DecrementCurCol();
00091   }
00092 
00093 // Main (non-VLD) tables have a ROW_COUNTER (which has to be ignored when reading).
00094   bool hasRowCounter = ! rs.IsVLDTable();
00095 
00096 // Create and fill table row object and move result set onto next row.
00097 
00098   while ( ! rs.IsExhausted() ) {
00099 
00100 //  If stripping off sequence numbers check the next and quit,
00101 //  having restored the last, if it changes.
00102     if ( seqNo != 0 ) {
00103       Int_t nextSeqNo;
00104       rs >> nextSeqNo;
00105       if ( nextSeqNo != seqNo ) {
00106         rs.DecrementCurCol();
00107         break;
00108       }
00109     }
00110 
00111 //  Strip off ROW_COUNTER if present.
00112     if ( hasRowCounter ) rs.IncrementCurCol();
00113     TDbiTableRow* row = tableRow->CreateTableRow();
00114     if ( vrec) TDbiTimerManager::gTimerManager.StartSubWatch(3);
00115     row->SetOwner(this);
00116     row->Fill(rs,vrec);
00117     if ( vrec) TDbiTimerManager::gTimerManager.StartSubWatch(2);
00118     fRows.push_back(row);
00119     rs.FetchRow();
00120     if ( vrec) TDbiTimerManager::gTimerManager.StartSubWatch(1);
00121   }
00122 
00123   //Flag that data was read from Database.
00124   this->SetResultsFromDb();
00125   if ( seqNo  == 0 )
00126         SK_DBI_Info( "Created unaggregated VLD result set no. of rows: "
00127                                                << this->GetNumRows() << "  ");
00128   else  SK_DBI_Info( "Created unaggregated result set for SeqNo: " << seqNo
00129                                   << " no. of rows: " << this->GetNumRows() << "  ");
00130 
00131 }
00132 
00133 
00134 //.....................................................................
00135 //
00136 ///\verbatim
00137 ///  Purpose: Destructor
00138 ///
00139 ///  Arguments:
00140 ///    None.
00141 ///
00142 ///  Return:    n/a
00143 ///
00144 ///  Contact:   N. West
00145 ///
00146 ///  Specification:-
00147 ///  =============
00148 ///
00149 ///  o  Destroy ResultNonAgg and all its owned TDbiTableRow subclass
00150 ///     objects.
00151 ///
00152 ///
00153 ///  Program Notes:-
00154 ///  =============
00155 ///
00156 ///  If fRows restored from BinaryFile then it doesn't own its
00157 ///  TDbiTableRows.
00158 ///\endverbatim
00159 TDbiResultSetNonAgg::~TDbiResultSetNonAgg() {
00160 
00161 
00162   SK_DBI_Trace( "Destroying TDbiResultSetNonAgg."  << "  ");
00163 
00164   if ( ! fBuffer ) for ( vector<TDbiTableRow*>::iterator itr = fRows.begin();
00165         itr != fRows.end();
00166         ++itr) delete *itr;
00167   else {
00168     delete [] fBuffer;
00169     fBuffer = 0;
00170   }
00171 }
00172 //.....................................................................
00173 ///  Purpose:  Create a key that corresponds to this result.
00174 TDbiResultKey* TDbiResultSetNonAgg::CreateKey() const {
00175 //
00176 //
00177 
00178 
00179 
00180   string rowName("empty_table");
00181   const TDbiTableRow* row = this->GetTableRow(0);
00182   if ( row ) rowName = row->GetName();
00183   const TDbiValidityRec& vrec = this->GetValidityRec();
00184   return new TDbiResultKey(this->TableName(),
00185                           rowName,
00186                           vrec.GetSeqNo(),
00187                           vrec.GetCreationDate() );
00188 
00189 }
00190 
00191 //.....................................................................
00192 
00193 void TDbiResultSetNonAgg::DebugCtor() const {
00194 
00195   SK_DBI_Trace( "Creating TDbiResultSetNonAgg" << (void*) this << "  ");
00196   static const TDbiResultSetNonAgg* that = 0;
00197   if ( this == that ) {
00198     cout << "debug " << (void*) this << endl;
00199   }
00200 }
00201 //.....................................................................
00202 ///\verbatim
00203 ///
00204 ///  Purpose:  Return TableRow from last query.
00205 ///
00206 ///  Arguments:
00207 ///    rowNum      in    Required row number (0..NumRows-1)
00208 ///
00209 ///  Return:    TableRow ptr, or =0 if no row.
00210 ///
00211 ///  Contact:   N. West
00212 ///
00213 ///  Specification:-
00214 ///  =============
00215 ///
00216 ///  o Return TableRow from last query, or =0 if no row.
00217 ///\endverbatim
00218 const TDbiTableRow* TDbiResultSetNonAgg::GetTableRow(UInt_t rowNum) const {
00219 
00220 //  Program Notes:-
00221 //  =============
00222 
00223 //  None.
00224 
00225   if ( rowNum >= fRows.size() ) return 0;
00226   return fRows[rowNum];
00227 }
00228 
00229 //.....................................................................
00230 ///\verbatim
00231 ///
00232 ///  Purpose: Return TableRow with supplied index, or =0 if no row.
00233 ///
00234 ///  Arguments:
00235 ///    index        in    Required index.
00236 ///
00237 ///  Return:    TableRow with required index, or =0 if no row.
00238 ///
00239 ///  Contact:   N. West
00240 ///
00241 ///  Specification:-
00242 ///  =============
00243 ///
00244 ///  o If look-up table not yet built, build it.
00245 ///
00246 ///  o Return TableRow with supplied index, or =0 if no row.
00247 ///\endverbatim
00248 const TDbiTableRow* TDbiResultSetNonAgg::GetTableRowByIndex(UInt_t index) const {
00249 
00250 
00251   if ( ! this->LookUpBuilt() ) this->BuildLookUpTable();
00252 
00253 // The real look-up still takes place in the base class.
00254   return this->TDbiResultSet::GetTableRowByIndex(index);
00255 
00256 }
00257 //.....................................................................
00258 //
00259 ///\verbatim
00260 ///  Purpose:  Return true if owns row.
00261 ///
00262 ///
00263 ///  Program Notes:-
00264 ///  =============
00265 ///
00266 ///  Only TDbiResultSetNonAggs own rows; the base class TDbiResultSet supplies
00267 ///  the default method that returns false.
00268 ///\endverbatim
00269 Bool_t TDbiResultSetNonAgg::Owns(const TDbiTableRow* row ) const {
00270 
00271  vector<TDbiTableRow*>::const_iterator itr    = fRows.begin();
00272  vector<TDbiTableRow*>::const_iterator itrEnd = fRows.end();
00273 
00274  for (; itr != itrEnd; ++itr) if ( *itr == row ) return kTRUE;
00275 
00276  return kFALSE;
00277 
00278 
00279 }
00280 
00281 //.....................................................................
00282 ///  Purpose: Check to see if this Result matches the supplied  TDbiValidityRec.
00283 Bool_t TDbiResultSetNonAgg::Satisfies(const TDbiValidityRec& vrec,
00284                                   const string& sqlQualifiers) {
00285 //
00286 //
00287 
00288 
00289   SK_DBI_Debug(  "Trying to satisfy: Vrec " << vrec << " SQL: " << sqlQualifiers
00290     << "\n with CanReuse: " << this->CanReuse()
00291     << " vrec: " << this->GetValidityRec()
00292     << " sqlQualifiers: " << this->GetSqlQualifiers()
00293     << "  ");
00294 
00295   if ( this->CanReuse() ) {
00296     const TDbiValidityRec& this_vrec = this->GetValidityRec();
00297     if (    sqlQualifiers           == this->GetSqlQualifiers()
00298          && vrec.GetSeqNo()         == this_vrec.GetSeqNo()
00299          && vrec.GetCreationDate()  == this_vrec.GetCreationDate()
00300        )  return kTRUE;
00301   }
00302 
00303   return kFALSE;
00304 
00305 }
00306 
00307 //.....................................................................
00308 ///\verbatim
00309 ///
00310 ///  Purpose:  I/O to binary file
00311 ///
00312 ///  Program Notes:-
00313 ///  =============
00314 ///  Do I/O for base class TDbiResultSet first.  Rebuild fIndexKeys on input.
00315 ///\endverbatim
00316 void TDbiResultSetNonAgg::Streamer(TDbiBinaryFile& file) {
00317 
00318   if ( file.IsReading() ) {
00319     this->TDbiResultSet::Streamer(file);
00320     SK_DBI_Debug( "    Restoring TDbiResultSetNonAgg ..." << "  ");
00321     file >> fRows;
00322 //  Take ownership of the memory holding the array.
00323     fBuffer = file.ReleaseArrayBuffer();
00324     this->BuildLookUpTable();
00325     SK_DBI_Debug( "    Restored TDbiResultSetNonAgg. Size:"
00326                             << fRows.size() << " rows" << "  ");
00327   }
00328   else if ( file.IsWriting() ) {
00329     this->TDbiResultSet::Streamer(file);
00330     SK_DBI_Debug( "    Saving TDbiResultSetNonAgg. Size:"
00331                             << fRows.size() << " rows" << "  ");
00332     file << fRows;
00333   }
00334 }
00335 
00336 /*    Template for New Member Function
00337 
00338 //.....................................................................
00339 
00340 TDbiResultSetNonAgg:: {
00341 //
00342 //
00343 //  Purpose:
00344 //
00345 //  Arguments:
00346 //    xxxxxxxxx    in    yyyyyy
00347 //
00348 //  Return:
00349 //
00350 //  Contact:   N. West
00351 //
00352 //  Specification:-
00353 //  =============
00354 //
00355 //  o
00356 
00357 //  Program Notes:-
00358 //  =============
00359 
00360 //  None.
00361 
00362 
00363 }
00364 
00365 */
00366 
00367 

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1