00001
00002
00003
00004
00005
00006
00007
00008 #include <cassert>
00009 #include <fstream>
00010
00011 #include "DatabaseInterface/DbiCascader.h"
00012 #include "DatabaseInterface/DbiOutRowStream.h"
00013 #include "DatabaseInterface/DbiResultPtr.h"
00014 #include "DatabaseInterface/DbiSqlValPacket.h"
00015 #include "DatabaseInterface/DbiTableProxy.h"
00016 #include "DatabaseInterface/DbiWriter.h"
00017 #include "LeakChecker/Lea.h"
00018 #include "MessageService/MsgService.h"
00019
00020
00021 ClassImpT(DbiWriter,T)
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 template<class T>
00036 DbiWriter<T>::DbiWriter() :
00037 fAggregateNo(-2),
00038 fDbNo(0),
00039 fPacket(new DbiSqlValPacket),
00040 fRequireGlobalSeqno(0),
00041 fTableProxy(&DbiWriter<T>::GetTableProxy()),
00042 fTableName(fTableProxy->GetTableName()),
00043 fUseOverlayCreationDate(kFALSE),
00044 fValidRec(0),
00045 fLogEntry(fTableName)
00046 {
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 LEA_CTOR
00064
00065 MSG("Dbi", Msg::kVerbose)
00066 << "Creating default DbiWriter for " << fTableName << endl;
00067
00068 }
00069
00070
00071 template<class T>
00072 DbiWriter<T>::DbiWriter(const VldRange& vr,
00073 Int_t aggNo,
00074 Dbi::Task task,
00075 VldTimeStamp creationDate,
00076 UInt_t dbNo,
00077 const std::string& logComment,
00078 const std::string& tableName) :
00079 fAggregateNo(aggNo),
00080 fDbNo(dbNo),
00081 fPacket(new DbiSqlValPacket),
00082 fRequireGlobalSeqno(0),
00083 fTableProxy(&DbiWriter<T>::GetTableProxy(tableName)),
00084 fTableName(fTableProxy->GetTableName()),
00085 fUseOverlayCreationDate(creationDate == VldTimeStamp(0,0)),
00086 fValidRec(0),
00087 fLogEntry(fTableName,logComment,vr.GetDetectorMask(),vr.GetSimMask(),task)
00088 {
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 LEA_CTOR
00117
00118 MSG("Dbi", Msg::kVerbose)
00119 << "Creating DbiWriter for " << fTableName << endl;
00120
00121 Open(vr,aggNo,task,creationDate,dbNo,logComment);
00122
00123 }
00124
00125
00126
00127 template<class T>
00128 DbiWriter<T>::DbiWriter(const VldRange& vr,
00129 Int_t aggNo,
00130 Dbi::Task task,
00131 VldTimeStamp creationDate,
00132 const std::string& dbName,
00133 const std::string& logComment,
00134 const std::string& tableName) :
00135 fAggregateNo(aggNo),
00136 fPacket(new DbiSqlValPacket),
00137 fRequireGlobalSeqno(0),
00138 fTableProxy(&DbiWriter<T>::GetTableProxy(tableName)),
00139 fTableName(fTableProxy->GetTableName()),
00140 fUseOverlayCreationDate(creationDate == VldTimeStamp(0,0)),
00141 fValidRec(0),
00142 fLogEntry(fTableName,logComment,vr.GetDetectorMask(),vr.GetSimMask(),task)
00143 {
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 LEA_CTOR
00172
00173 MSG("Dbi", Msg::kVerbose)
00174 << "Creating DbiWriter for " << fTableName << endl;
00175
00176 Open(vr,aggNo,task,creationDate,dbName,logComment);
00177
00178 }
00179
00180
00181
00182 template<class T>
00183 DbiWriter<T>::DbiWriter(const DbiValidityRec& vrec,
00184 UInt_t dbNo,
00185 const std::string& logComment) :
00186 fAggregateNo(0),
00187 fDbNo(dbNo),
00188 fPacket(new DbiSqlValPacket),
00189 fRequireGlobalSeqno(0),
00190 fTableProxy(0),
00191 fUseOverlayCreationDate(kFALSE),
00192 fValidRec(new DbiValidityRec(vrec)),
00193 fLogEntry(fTableName,logComment,vrec.GetVldRange().GetDetectorMask(),
00194 vrec.GetVldRange().GetSimMask(),vrec.GetTask())
00195 {
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 LEA_CTOR
00222
00223 T pet;
00224 MSG("Dbi", Msg::kVerbose)
00225 << "Creating DbiWriter for " << pet.GetName() << endl;
00226
00227 this->Open(vrec,dbNo,logComment);
00228
00229 }
00230
00231
00232
00233 template<class T>
00234 DbiWriter<T>::DbiWriter(const DbiValidityRec& vrec,
00235 const std::string& dbName,
00236 const std::string& logComment) :
00237 fAggregateNo(0),
00238
00239 fPacket(new DbiSqlValPacket),
00240 fRequireGlobalSeqno(0),
00241 fTableProxy(0),
00242 fUseOverlayCreationDate(kFALSE),
00243 fValidRec(new DbiValidityRec(vrec)),
00244 fLogEntry(fTableName,logComment,vrec.GetVldRange().GetDetectorMask(),
00245 vrec.GetVldRange().GetSimMask(),vrec.GetTask())
00246 {
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 LEA_CTOR
00273
00274 T pet;
00275 MSG("Dbi", Msg::kVerbose)
00276 << "Creating DbiWriter for " << pet.GetName() << endl;
00277
00278 this->Open(vrec,dbName,logComment);
00279
00280 }
00281
00282
00283
00284 template<class T>
00285 DbiWriter<T>::~DbiWriter() {
00286
00287
00288
00289
00290 LEA_DTOR
00291
00292 MSG("Dbi", Msg::kVerbose) << "Destroying DbiWriter" << endl;
00293
00294 Reset();
00295 delete fPacket;
00296 fPacket = 0;
00297 delete fValidRec;
00298 fValidRec = 0;
00299
00300 }
00301
00302
00303
00304
00305 template<class T>
00306 DbiWriter<T>& DbiWriter<T>::operator<<(const T& row) {
00307
00308
00309
00310
00311 if ( ! this->IsOpen() ) return *this;
00312
00313 Int_t AggNoRow = row.GetAggregateNo();
00314 if ( fAggregateNo != AggNoRow ) {
00315 MSG("Dbi",Msg::kError)
00316 << "Cannot store row data for table "
00317 << fTableName
00318 << ", current set has aggregate no.: " << fAggregateNo
00319 << ", but it has: " << AggNoRow
00320 << " \n closing DbiWriter" << endl;
00321 this->Abort();
00322 return *this;
00323 }
00324 if ( ! fPacket->AddDataRow(*fTableProxy,fValidRec,row) ) {
00325 MSG("Dbi",Msg::kError) << "Closing DbiWriter due to above error." << endl;
00326 this->Abort();
00327 }
00328
00329 return *this;
00330
00331 }
00332
00333
00334
00335 template<class T>
00336 Bool_t DbiWriter<T>::CanOutput(Bool_t reportErrors) const {
00337
00338
00339
00340
00341 if ( ! this->IsOpen(reportErrors) ) return kFALSE;
00342
00343 if ( this->NeedsLogEntry() && ! fLogEntry.HasReason() ) {
00344 if ( reportErrors ) MSG("Dbi",Msg::kError)
00345 << "Cannot output validity set for table "
00346 << fTableName
00347 << ", writing to Master DB but no log comment has been supplied." << endl;
00348 return kFALSE;
00349 }
00350
00351 UInt_t nstmts = fPacket->GetNumSqlStmts();
00352
00353 if ( nstmts == 0 ) {
00354
00355 if ( reportErrors ) MSG("Dbi",Msg::kError)
00356 << "Cannot output validity set for table "
00357 << fTableName
00358 << ", no data has been written." << endl;
00359 return kFALSE;
00360 }
00361
00362 if ( nstmts == 1 ) {
00363
00364
00365 string stmt = fPacket->GetStmt(0);
00366 bool isvldentry =
00367 (stmt.find("INSERT INTO") != string::npos ) &&
00368 (stmt.find("VLD VALUES") != string::npos);
00369 if ( isvldentry ) {
00370 if ( reportErrors ) MSG("Dbi",Msg::kWarning)
00371 << "VLD entry but no data for "
00372 << fTableName << endl;
00373 }
00374 else {
00375
00376 if ( reportErrors ) {
00377 MSG("Dbi",Msg::kError)
00378 << "Cannot output validity set for table "
00379 << fTableName
00380 << ", (no VLD info) no data has been written." << endl;
00381 fPacket->Print();
00382 }
00383 return kFALSE;
00384 }
00385 }
00386
00387 return kTRUE;
00388 }
00389
00390
00391
00392
00393 template<class T>
00394 Bool_t DbiWriter<T>::Close(const char* fileSpec) {
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417 Bool_t ok = kFALSE;
00418
00419
00420
00421 if ( CanOutput() ) {
00422
00423
00424
00425 int seqNoType = fileSpec ? 1 : fRequireGlobalSeqno;
00426 Int_t seqNo = fTableProxy->GetCascader().AllocateSeqNo(fTableName,seqNoType,fDbNo);
00427 if ( seqNo <= 0 ) {
00428 MSG("Dbi",Msg::kError)
00429 << "Cannot get sequence number for table "
00430 << fTableName << endl;
00431 }
00432
00433 else {
00434
00435
00436 if ( fUseOverlayCreationDate && fValidRec
00437 ) fPacket->SetCreationDate(fTableProxy->QueryOverlayCreationDate(*fValidRec,fDbNo));
00438
00439
00440 fPacket->SetSeqNo(seqNo);
00441 if ( fileSpec ) {
00442 ofstream fs(fileSpec,ios_base::app);
00443 if ( ! fs ) MSG("Dbm", Msg::kError) << "Unable to open " << fileSpec << endl;
00444 else ok = fPacket->Write(fs,true);
00445 }
00446 else {
00447 ok = fPacket->Store(fDbNo);
00448 }
00449
00450
00451 if ( ok && this->WritingToMaster() ) {
00452 if ( this->NeedsLogEntry() || fLogEntry.HasReason() )
00453 ok = fLogEntry.Write(fDbNo,seqNo);
00454 }
00455 }
00456 }
00457
00458
00459
00460 fPacket->Clear();
00461
00462 return ok;
00463 }
00464
00465
00466 template<class T>
00467 void DbiWriter<T>::CompleteOpen(UInt_t dbNo,
00468 const std::string& logComment) {
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 fDbNo = dbNo;
00491 if ( (int) fDbNo < 0 ) {
00492 MSG("Dbi",Msg::kError) << "Bad database name/number selected. " << endl;
00493 this->Reset();
00494 return;
00495 }
00496
00497 fAggregateNo = fValidRec->GetAggregateNo();
00498 fTableName = fTableProxy->GetTableName(),
00499
00500
00501 fPacket->Recreate(fTableName,
00502 fValidRec->GetVldRange(),
00503 fAggregateNo,
00504 fValidRec->GetTask(),
00505 fValidRec->GetCreationDate());
00506
00507
00508 const VldRange& vr = fValidRec->GetVldRange();
00509 fLogEntry.Recreate(fTableName,
00510 logComment,
00511 vr.GetDetectorMask(),
00512 vr.GetSimMask(),
00513 fValidRec->GetTask());
00514
00515 }
00516
00517
00518
00519 template<class T>
00520 DbiTableProxy& DbiWriter<T>::GetTableProxy() {
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533 return DbiResultPtr<T>::GetTableProxy();
00534 }
00535
00536
00537
00538 template<class T>
00539 DbiTableProxy& DbiWriter<T>::GetTableProxy(
00540 const std::string& tableName){
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562 return DbiResultPtr<T>::GetTableProxy(tableName);
00563
00564 }
00565
00566
00567 template<class T>
00568 Bool_t DbiWriter<T>::IsOpen(Bool_t reportErrors) const {
00569
00570
00571
00572
00573 if ( ! DbiTableProxyRegistry::IsActive() ) {
00574 if ( reportErrors ) MSG("Dbi",Msg::kError)
00575 << "Cannot use DbiWriter, the DBI has been shutdown." << endl;
00576 }
00577 else if ( !fValidRec
00578 || fPacket->GetNumSqlStmts() < 1 ) {
00579 if ( reportErrors ) MSG("Dbi",Msg::kError)
00580 << "Cannot do I/O on DbiWriter for "
00581 << fTableName
00582 <<", it is currently closed." << endl;
00583 }
00584 else return kTRUE;
00585
00586 return kFALSE;
00587
00588 }
00589
00590
00591 template<class T>
00592 Bool_t DbiWriter<T>::NeedsLogEntry() const {
00593
00594
00595
00596
00597
00598 string tableName(fTableName);
00599 if ( tableName.substr(0,7) == "BEAMMON"
00600 || tableName.substr(0,16) == "BFLDDBICOILSTATE"
00601 || tableName.substr(0,3) == "CAL"
00602 || tableName.substr(0,20) == "CANDDIGITBADCHANNELS"
00603 || tableName.substr(0,3) == "DBU"
00604 || tableName.substr(0,3) == "DCS"
00605 || tableName.substr(0,6) == "PULSER"
00606 || tableName.substr(0,9) == "SPILLTIME" ) return kFALSE;
00607
00608
00609
00610 return this->WritingToMaster();
00611
00612 }
00613
00614
00615
00616 template<class T>
00617 Bool_t DbiWriter<T>::Open (const VldRange& vr,
00618 Int_t aggNo,
00619 Dbi::Task task,
00620 VldTimeStamp creationDate,
00621 UInt_t dbNo,
00622 const std::string& logComment) {
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649 bool ok = true;
00650
00651 if ( ! DbiTableProxyRegistry::IsActive() ) return kFALSE;
00652 if ( this->CanOutput(kFALSE) ) ok = Close();
00653
00654
00655 fUseOverlayCreationDate = creationDate == VldTimeStamp(0,0);
00656
00657
00658 delete fValidRec;
00659 fValidRec = new DbiValidityRec(vr,task,aggNo,0,0,kFALSE,creationDate);
00660
00661
00662 fTableProxy = &DbiWriter<T>::GetTableProxy(fTableName);
00663
00664
00665 this->CompleteOpen(dbNo,logComment);
00666 return ok;
00667
00668 }
00669
00670
00671 template<class T>
00672 Bool_t DbiWriter<T>::Open (const VldRange& vr,
00673 Int_t aggNo,
00674 Dbi::Task task,
00675 VldTimeStamp creationDate,
00676 const string& dbName,
00677 const std::string& logComment) {
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692 this->SetDbName(dbName);
00693 return this->Open(vr,
00694 aggNo,
00695 task,
00696 creationDate,
00697 fDbNo,
00698 logComment);
00699
00700 }
00701
00702
00703
00704 template<class T>
00705 Bool_t DbiWriter<T>::Open(const DbiValidityRec& vrec,
00706 UInt_t dbNo,
00707 const std::string& logComment) {
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734 bool ok = true;
00735
00736 if ( ! DbiTableProxyRegistry::IsActive() ) return kFALSE;
00737 if ( this->CanOutput(kFALSE) ) ok = Close();
00738
00739
00740
00741
00742 const DbiTableProxy& proxyDefault = DbiWriter<T>::GetTableProxy();
00743 const DbiTableProxy& proxyVrec = *vrec.GetTableProxy();
00744 if ( proxyDefault.GetTableName() != "DBICONFIGSET"
00745 && proxyVrec.GetTableName() != proxyDefault.GetTableName() ) {
00746 MSG("Dbi",Msg::kError)
00747 << "Unable to create DbiWriter from query:" << endl
00748 << vrec
00749 << " was filled by " << proxyVrec.GetTableName()
00750 << " not by " << proxyDefault.GetTableName() << endl;
00751 this->Reset();
00752 return false;
00753 }
00754 else {
00755
00756 fTableProxy = const_cast<DbiTableProxy*>(&proxyVrec);
00757 }
00758
00759 fUseOverlayCreationDate = vrec.GetCreationDate() == VldTimeStamp(0,0);
00760
00761 delete fValidRec;
00762 fValidRec = new DbiValidityRec(vrec);
00763
00764
00765 this->CompleteOpen(dbNo,logComment);
00766 return ok;
00767
00768 }
00769
00770
00771
00772 template<class T>
00773 Bool_t DbiWriter<T>::Open(const DbiValidityRec& vrec,
00774 const string& dbName,
00775 const std::string& logComment) {
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789 this->SetDbName(dbName);
00790 return this->Open(vrec,
00791 fDbNo,
00792 logComment);
00793
00794 }
00795
00796
00797
00798 template<class T>
00799 void DbiWriter<T>::Reset() {
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820 fAggregateNo = 0;
00821 fDbNo = 0;
00822 fPacket->Clear();
00823 fTableProxy = &DbiWriter<T>::GetTableProxy();
00824 fTableName = fTableProxy->GetTableName();
00825
00826 delete fValidRec;
00827 fValidRec = 0;
00828
00829 }
00830
00831
00832 template<class T>
00833 void DbiWriter<T>::SetDbName(const string& dbName) {
00834
00835
00836
00837
00838 if ( dbName == "" ) {
00839 fDbNo = 0;
00840 }
00841 else {
00842 fDbNo = DbiTableProxyRegistry::Instance().GetCascader().GetDbNo(dbName);
00843 }
00844 }
00845
00846
00847
00848 template<class T>
00849 void DbiWriter<T>::SetLogComment(const std::string& reason) {
00850
00851
00852
00853
00854 fLogEntry.SetReason(reason);
00855
00856 }
00857
00858
00859
00860 template<class T>
00861 DbiTableProxy& DbiWriter<T>::TableProxy() const {
00862
00863
00864
00865
00866
00867 assert( DbiTableProxyRegistry::IsActive() );
00868 return *fTableProxy;
00869 }
00870
00871
00872
00873 template<class T>
00874 Bool_t DbiWriter<T>::WritingToMaster() const {
00875
00876
00877
00878
00879
00880 DbiCascader& cascader = DbiTableProxyRegistry::Instance().GetCascader();
00881 return ( fDbNo == (UInt_t) cascader.GetAuthorisingDbNo()
00882 && ! cascader.IsTemporaryTable(fTableName,fDbNo));
00883
00884 }
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917