00001
00002
00003 #include <cassert>
00004
00005 #include "DatabaseInterface/DbiResultKey.h"
00006 #include "DatabaseInterface/DbiResultPtr.h"
00007 #include "DatabaseInterface/DbiSqlContext.h"
00008 #include "DatabaseInterface/DbiTableProxy.h"
00009 #include "DatabaseInterface/DbiTimerManager.h"
00010 #include "LeakChecker/Lea.h"
00011 #include "MessageService/MsgService.h"
00012 #include "Validity/VldTimeStamp.h"
00013
00014 #include <cstdlib>
00015
00016 ClassImpT(DbiResultPtr,T)
00017
00018
00019
00020
00021
00022
00023 template<class T>
00024 map<string,DbiTableProxy*> DbiResultPtr<T>::fgNameToProxy;
00025
00026 template<class T>
00027 DbiTableProxy* DbiResultPtr<T>::fgTableProxy = 0;
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 template<class T>
00038 DbiResultPtr<T>::DbiResultPtr() :
00039 fAbortTest(Dbi::kDisabled),
00040 fTableProxy(DbiResultPtr<T>::GetTableProxy()),
00041 fResult(0),
00042 fDetType(Detector::kUnknown),
00043 fSimType(SimFlag::kUnknown )
00044 {
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 LEA_CTOR
00067
00068 T pet;
00069 MSG("Dbi", Msg::kVerbose)
00070 << "Creating DbiResultPtr for " << pet.GetName()
00071 << " Table Proxy at " << &fTableProxy << endl;
00072 }
00073
00074
00075 template<class T>
00076 DbiResultPtr<T>::DbiResultPtr(const DbiResultPtr& that) :
00077 fAbortTest(that.fAbortTest),
00078 fTableProxy(that.fTableProxy),
00079 fResult(that.fResult),
00080 fDetType(that.fDetType),
00081 fSimType(that.fSimType)
00082 {
00083
00084
00085
00086
00087 LEA_CTOR
00088
00089 T pet;
00090 MSG("Dbi", Msg::kVerbose)
00091 << "Creating copy DbiResultPtr for " << pet.GetName()
00092 << " Table Proxy at " << &fTableProxy << endl;
00093 if ( fResult ) fResult->Connect();
00094
00095 }
00096
00097
00098
00099 template<class T>
00100 DbiResultPtr<T>::DbiResultPtr(const VldContext& vc,
00101 Dbi::Task task,
00102 Dbi::AbortTest abortTest,
00103 Bool_t findFullTimeWindow) :
00104 fAbortTest(abortTest),
00105 fTableProxy(DbiResultPtr<T>::GetTableProxy()),
00106 fResult(0),
00107 fDetType(vc.GetDetector()),
00108 fSimType(vc.GetSimFlag())
00109 {
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 LEA_CTOR
00138
00139 T pet;
00140 MSG("Dbi", Msg::kVerbose) << "Creating DbiResultPtr for "
00141 << pet.GetName() << " Table Proxy at "
00142 << &fTableProxy << endl;
00143 NewQuery(vc, task, findFullTimeWindow);
00144
00145 }
00146
00147
00148
00149 template<class T>
00150 DbiResultPtr<T>::DbiResultPtr(const string& tableName,
00151 const VldContext& vc,
00152 Dbi::Task task,
00153 Dbi::AbortTest abortTest,
00154 Bool_t findFullTimeWindow) :
00155 fAbortTest(abortTest),
00156 fTableProxy(DbiResultPtr<T>::GetTableProxy(tableName)),
00157 fResult(0),
00158 fDetType(vc.GetDetector()),
00159 fSimType(vc.GetSimFlag())
00160 {
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 LEA_CTOR
00189
00190 MSG("Dbi", Msg::kVerbose) << "Creating DbiResultPtr for "
00191 << tableName << " Table Proxy at "
00192 << &fTableProxy << endl;
00193 NewQuery(vc, task, findFullTimeWindow);
00194
00195 }
00196
00197
00198 template<class T>
00199 DbiResultPtr<T>::DbiResultPtr(const string& tableName,
00200 const DbiSqlContext& context,
00201 const Dbi::Task& task,
00202 const string& data,
00203 const string& fillOpts,
00204 Dbi::AbortTest abortTest) :
00205 fAbortTest(abortTest),
00206 fTableProxy(DbiResultPtr<T>::GetTableProxy(tableName)),
00207 fResult(0),
00208 fDetType(context.GetDetector()),
00209 fSimType(context.GetSimFlag())
00210 {
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 LEA_CTOR
00240
00241 T pet;
00242 MSG("Dbi", Msg::kVerbose) << "Creating DbiResultPtr for "
00243 << tableName << " Table Proxy at "
00244 << &fTableProxy << endl
00245 << "Extended context " << context.GetString() << endl;
00246
00247 NewQuery(context,task,data,fillOpts);
00248
00249 }
00250
00251
00252
00253 template<class T>
00254 DbiResultPtr<T>::DbiResultPtr(const string& tableName,
00255 const DbiValidityRec& vrec,
00256 Dbi::AbortTest abortTest) :
00257 fAbortTest(abortTest),
00258 fTableProxy(DbiResultPtr<T>::GetTableProxy(tableName)),
00259 fResult(0)
00260 {
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 LEA_CTOR
00286
00287 this->SetContext(vrec);
00288 T pet;
00289 MSG("Dbi", Msg::kVerbose) << "Creating DbiResultPtr for "
00290 << tableName << " Table Proxy at "
00291 << &fTableProxy << endl;
00292 NewQuery(vrec);
00293
00294 }
00295
00296
00297 template<class T>
00298 DbiResultPtr<T>::DbiResultPtr(const string& tableName,
00299 UInt_t seqNo,
00300 UInt_t dbNo,
00301 Dbi::AbortTest abortTest) :
00302 fAbortTest(abortTest),
00303 fTableProxy(DbiResultPtr<T>::GetTableProxy(tableName)),
00304 fResult(0)
00305 {
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 LEA_CTOR
00332
00333
00334 T pet;
00335 MSG("Dbi", Msg::kVerbose) << "Creating DbiResultPtr for "
00336 << tableName << " Table Proxy at "
00337 << &fTableProxy << endl;
00338 NewQuery(seqNo,dbNo);
00339
00340 }
00341
00342
00343
00344 template<class T>
00345 DbiResultPtr<T>::~DbiResultPtr() {
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 LEA_DTOR
00369
00370 MSG("Dbi", Msg::kVerbose) << "Destroying DbiResultPtr" << endl;
00371 Disconnect();
00372
00373 }
00374
00375
00376 template<class T>
00377 Bool_t DbiResultPtr<T>::ApplyAbortTest() {
00378
00379
00380
00381
00382 if ( fAbortTest == Dbi::kDisabled
00383 || this->GetNumRows() > 0 ) return kFALSE;
00384
00385 bool tableExists = fTableProxy.TableExists();
00386
00387 if ( ! tableExists ) {
00388 MSG("Dbi",Msg::kFatal) << "Fatal error: table "
00389 << fTableProxy.GetTableName() << " does not exist"
00390 << endl;
00391 return kTRUE;
00392 }
00393 if ( fAbortTest == Dbi::kDataMissing) {
00394 MSG("Dbi",Msg::kFatal) << "Fatal error: no data found in existing table "
00395 << fTableProxy.GetTableName() << endl;
00396 return kTRUE;
00397 }
00398
00399 return kFALSE;
00400 }
00401
00402
00403
00404 template<class T>
00405 void DbiResultPtr<T>::Disconnect() {
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 if ( fResult && DbiTableProxyRegistry::IsActive() ) {
00428 fResult->Disconnect();
00429 }
00430 fResult = 0;
00431
00432 }
00433
00434
00435 template<class T>
00436 const DbiResultKey* DbiResultPtr<T>::GetKey() const {
00437
00438
00439
00440
00441
00442 return fResult ? fResult->GetKey() : DbiResultKey::GetEmptyKey();
00443
00444 }
00445
00446
00447 template<class T>
00448 Int_t DbiResultPtr<T>::GetResultID() const {
00449
00450
00451
00452
00453 return fResult ? fResult->GetID() : 0;
00454
00455 }
00456
00457
00458 template<class T>
00459 DbiTableProxy& DbiResultPtr<T>::GetTableProxy() {
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484 if ( ! fgTableProxy ) {
00485 T pet;
00486 fgTableProxy = &DbiTableProxyRegistry::Instance()
00487 .GetTableProxy(pet.GetName(),&pet);
00488 }
00489 return *fgTableProxy;
00490 }
00491
00492
00493
00494 template<class T>
00495 DbiTableProxy& DbiResultPtr<T>::GetTableProxy(const string& tableName){
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522 if ( tableName == "" ) return DbiResultPtr::GetTableProxy();
00523
00524
00525 map<string,DbiTableProxy*>::const_iterator itr
00526 = fgNameToProxy.find(tableName);
00527 if ( itr != fgNameToProxy.end() ) return *( (*itr).second );
00528
00529
00530 T pet;
00531 DbiTableProxy* proxy = &DbiTableProxyRegistry::Instance()
00532 .GetTableProxy(tableName,&pet);
00533 fgNameToProxy[tableName] = proxy;
00534 return *proxy;
00535 }
00536
00537
00538
00539 template<class T>
00540 UInt_t DbiResultPtr<T>::GetNumRows() const {
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563 return ( fResult && DbiTableProxyRegistry::IsActive() )
00564 ? fResult->GetNumRows() : 0;
00565
00566 }
00567
00568
00569 template<class T>
00570 const T* DbiResultPtr<T>::GetRow(UInt_t rowNum) const {
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594 return ( fResult && DbiTableProxyRegistry::IsActive() ) ?
00595 dynamic_cast<const T*>(fResult->GetTableRow(rowNum))
00596 : 0;
00597 }
00598
00599
00600 template<class T>
00601 const T* DbiResultPtr<T>::GetRowByIndex(UInt_t index) const {
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625 return ( fResult && DbiTableProxyRegistry::IsActive() ) ?
00626 dynamic_cast<const T*>(fResult->GetTableRowByIndex(index))
00627 : 0;
00628 }
00629
00630
00631
00632 template<class T>
00633 const DbiValidityRec* DbiResultPtr<T>::GetValidityRec(
00634 const DbiTableRow* row) const {
00635
00636
00637
00638
00639
00640
00641 return ( fResult && DbiTableProxyRegistry::IsActive() )
00642 ? &(fResult->GetValidityRec(row)) : 0 ;
00643
00644 }
00645
00646
00647
00648 template<class T>
00649 UInt_t DbiResultPtr<T>::NextQuery(Bool_t forwards) {
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667 static VldTimeStamp startOfTime(0,0);
00668 static VldTimeStamp endOfTime(0x7FFFFFFF,0);
00669
00670 if ( ! fResult ) return 0;
00671
00672 MSG("Dbi",Msg::kSynopsis) << "\n\nStarting next query: direction "
00673 << ( forwards ? "forwards" : "backwards" ) << "\n" << endl;
00674
00675 const DbiValidityRec& vrec = fResult->GetValidityRec();
00676 const VldRange& vrnge = vrec.GetVldRange();
00677
00678
00679 if ( forwards && vrnge.GetTimeEnd() == endOfTime ) return fResult->GetNumRows();
00680 if ( ! forwards && vrnge.GetTimeStart() == startOfTime ) return fResult->GetNumRows();
00681
00682
00683
00684
00685 time_t ts = forwards ? vrnge.GetTimeEnd().GetSec()
00686 : vrnge.GetTimeStart().GetSec() - 1;
00687 VldContext vc(fDetType,fSimType,VldTimeStamp(ts,0));
00688
00689 return this->NewQuery(vc,vrec.GetTask(), true);
00690
00691 }
00692
00693
00694
00695
00696 template<class T>
00697 UInt_t DbiResultPtr<T>::NewQuery(VldContext vc,
00698 Dbi::Task task,
00699 Bool_t findFullTimeWindow) {
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727 if ( ! DbiTableProxyRegistry::IsActive() ) {
00728 fResult = 0;
00729 return 0;
00730 }
00731 fDetType = vc.GetDetector();
00732 fSimType = vc.GetSimFlag();
00733
00734 MSG("Dbi",Msg::kSynopsis) << "\n\nStarting context query: "
00735 << vc << " task " << task << "\n" << endl;
00736
00737 DbiTimerManager::gTimerManager.RecBegin(fTableProxy.GetTableName(), sizeof(T));
00738 Disconnect();
00739 fResult = fTableProxy.Query(vc,task,findFullTimeWindow);
00740 fResult->Connect();
00741 DbiTimerManager::gTimerManager.RecEnd(fResult->GetNumRows());
00742
00743 if ( this->ApplyAbortTest() ) {
00744 MSG("Dbi",Msg::kFatal) << "while applying validity context query for "
00745 << vc.AsString() << " with task " << task << endl;
00746 abort();
00747 }
00748 MSG("Dbi",Msg::kSynopsis) << "\nCompleted context query: "
00749 << vc << " task " << task
00750 << " Found: " << fResult->GetNumRows() << " rows\n" << endl;
00751 return fResult->GetNumRows();
00752
00753 }
00754
00755
00756 template<class T>
00757 UInt_t DbiResultPtr<T>::NewQuery(const DbiSqlContext& context,
00758 const Dbi::Task& task,
00759 const string& data,
00760 const string& fillOpts) {
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787 if ( ! DbiTableProxyRegistry::IsActive() ) {
00788 fResult = 0;
00789 return 0;
00790 }
00791 fDetType = context.GetDetector();
00792 fSimType = context.GetSimFlag();
00793
00794 MSG("Dbi",Msg::kSynopsis) << "\n\nStarting extended context query: "
00795 << context.GetString() << " task " << task
00796 << " data " << data << " fillOpts " << fillOpts << "\n" <<endl;
00797
00798 DbiTimerManager::gTimerManager.RecBegin(fTableProxy.GetTableName(), sizeof(T));
00799 Disconnect();
00800 fResult = fTableProxy.Query(context.GetString(),task,data,fillOpts);
00801 fResult->Connect();
00802 DbiTimerManager::gTimerManager.RecEnd(fResult->GetNumRows());
00803 if ( this->ApplyAbortTest() ) {
00804 MSG("Dbi",Msg::kFatal) << "while applying extended context query for "
00805 << context.c_str()<< " with task " << task
00806 << " secondary query SQL: " << data
00807 << " and fill options: " << fillOpts << endl;
00808 abort();
00809 }
00810
00811 MSG("Dbi",Msg::kSynopsis) << "\n\nCompleted extended context query: "
00812 << context.GetString() << " task " << task
00813 << " data " << data << " fillOpts" << fillOpts
00814 << " Found: " << fResult->GetNumRows() << " rows\n" << endl;
00815
00816 return fResult->GetNumRows();
00817
00818 }
00819
00820
00821 template<class T>
00822 UInt_t DbiResultPtr<T>::NewQuery(const DbiValidityRec& vrec) {
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846 if ( ! DbiTableProxyRegistry::IsActive() ) {
00847 fResult = 0;
00848 return 0;
00849 }
00850 MSG("Dbi",Msg::kSynopsis) << "\n\nStarting DbiValidityRec query: "
00851 << vrec << "\n" << endl;
00852
00853 this->SetContext(vrec);
00854 DbiTimerManager::gTimerManager.RecBegin(fTableProxy.GetTableName(), sizeof(T));
00855 Disconnect();
00856
00857
00858
00859 fResult = fTableProxy.Query(vrec,kFALSE);
00860 fResult->Connect();
00861 DbiTimerManager::gTimerManager.RecEnd(fResult->GetNumRows());
00862 if ( this->ApplyAbortTest() ) {
00863 MSG("Dbi",Msg::kFatal) << "while applying validity rec query for "
00864 << vrec << endl;
00865 abort();
00866 }
00867 MSG("Dbi",Msg::kSynopsis) << "\n\nCompletedDbiValidityRec query: "
00868 << vrec
00869 << " Found: " << fResult->GetNumRows() << " rows\n" << endl;
00870 return fResult->GetNumRows();
00871
00872 }
00873
00874
00875 template<class T>
00876 UInt_t DbiResultPtr<T>::NewQuery(UInt_t seqNo,UInt_t dbNo) {
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901 if ( ! DbiTableProxyRegistry::IsActive() ) {
00902 fResult = 0;
00903 return 0;
00904 }
00905 MSG("Dbi",Msg::kSynopsis) << "\n\nStarting SeqNo query: "
00906 << seqNo << "\n" << endl;
00907 DbiTimerManager::gTimerManager.RecBegin(fTableProxy.GetTableName(), sizeof(T));
00908 Disconnect();
00909 fResult = fTableProxy.Query(seqNo,dbNo);
00910 fResult->Connect();
00911 DbiTimerManager::gTimerManager.RecEnd(fResult->GetNumRows());
00912 if ( this->ApplyAbortTest() ) {
00913 MSG("Dbi",Msg::kFatal) << "while applying SEQNO query for "
00914 << seqNo << " on database " << dbNo << endl;
00915 abort();
00916 }
00917 this->SetContext(fResult->GetValidityRec());
00918 MSG("Dbi",Msg::kSynopsis) << "\n\nCompleted SeqNo query: "
00919 << seqNo
00920 << " Found: " << fResult->GetNumRows() << " rows\n" << endl;
00921 return fResult->GetNumRows();
00922
00923 }
00924
00925
00926
00927 template<class T>
00928 Bool_t DbiResultPtr<T>::ResultsFromDb() const {
00929
00930
00931
00932
00933
00934 return fResult ? fResult->ResultsFromDb() : kFALSE;
00935
00936 }
00937
00938
00939 template<class T>
00940 void DbiResultPtr<T>::SetContext(const DbiValidityRec& vrec) {
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961 const VldRange& vrng = vrec.GetVldRange();
00962 Int_t detMask = vrng.GetDetectorMask();
00963 Int_t simMask = vrng.GetSimMask();
00964
00965 fDetType = Detector::kUnknown;
00966 if ( detMask & Detector::kFar ) fDetType = Detector::kFar;
00967 else if ( detMask & Detector::kNear) fDetType = Detector::kNear;
00968 else if ( detMask & Detector::kCalDet) fDetType = Detector::kCalDet;
00969 else if ( detMask & Detector::kCalib) fDetType = Detector::kCalib;
00970 else if ( detMask & Detector::kTestStand) fDetType = Detector::kTestStand;
00971 else if ( detMask & Detector::kMapper) fDetType = Detector::kMapper;
00972
00973 fSimType = SimFlag::kUnknown;
00974 if ( simMask & SimFlag::kData) fSimType = SimFlag::kData;
00975 else if ( simMask & SimFlag::kMC) fSimType = SimFlag::kMC;
00976 else if ( simMask & SimFlag::kReroot) fSimType = SimFlag::kReroot;
00977 else if ( simMask & SimFlag::kDaqFakeData) fSimType = SimFlag::kDaqFakeData;
00978
00979 }
00980
00981
00982
00983 template<class T>
00984 DbiTableProxy& DbiResultPtr<T>::TableProxy() const {
00985
00986
00987
00988
00989
00990
00991 assert( DbiTableProxyRegistry::IsActive() );
00992 return fTableProxy;
00993 }
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026