Changeset 15439 for trunk/Mars/msql
- Timestamp:
- 04/25/13 14:13:54 (12 years ago)
- Location:
- trunk/Mars/msql
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mars/msql/MBasketSQL.cc
r15417 r15439 1 // @(#)root/tree:$Id: MBasketSQL.cxx 43518 2012-03-28 01:04:07Z pcanal $2 // Author: Philippe Canal and al. 08/20043 4 /*************************************************************************5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *6 * All rights reserved. *7 * *8 * For the licensing terms see $ROOTSYS/LICENSE. *9 * For the list of contributors see $ROOTSYS/README/CREDITS. *10 *************************************************************************/11 12 1 ////////////////////////////////////////////////////////////////////////// 13 2 // // … … 18 7 ////////////////////////////////////////////////////////////////////////// 19 8 20 #ifndef TBASKETSQL_CXX 21 #define TBASKETSQL_CXX 9 #ifndef MARS_MBasketSQL 10 #define MARS_MBasketSQL 11 12 #include "MBasketSQL.h" 22 13 23 14 #include "TBasket.h" 24 15 #include "TTree.h" 25 16 #include "TBranch.h" 26 #include "TFile.h"27 #include "TMath.h"28 #include "MBasketSQL.h"29 #include <Riostream.h>30 #include <vector>31 #include "MTreeSQL.h"32 17 #include "MBufferSQL.h" 33 18 … … 37 22 38 23 //_________________________________________________________________________ 39 MBasketSQL::MBasketSQL() : TBasket(), fR esultPtr(0), fRowPtr(0), fInsertQuery(0)24 MBasketSQL::MBasketSQL() : TBasket(), fRowPtr(0) 40 25 { 41 26 // Default constructor. 42 43 27 } 44 28 45 29 //_________________________________________________________________________ 46 MBasketSQL::MBasketSQL(const char *name, const char *title, TBranch *branch, 47 TSQLResult ** rs, TString *insert_query, 48 vector<Int_t> *vc, TSQLRow **r) : 49 fResultPtr(rs),fRowPtr(r) 50 { 51 // Regular constructor. 30 MBasketSQL::MBasketSQL(TBranch *branch, TSQLRow **sqlrow, Int_t index, TString type) 31 { 32 // Create a TSQLBuffer for this basket. 33 SetName(branch->GetName()); 34 SetTitle(branch->GetName()); 52 35 53 SetName(name); 54 SetTitle(title); 55 fClassName = "MBasketSQL"; 56 fBufferSize = branch->GetBasketSize(); 57 fNevBufSize = branch->GetEntryOffsetLen(); 58 fNevBuf = 0; 59 fEntryOffset = 0; //Must be set to 0 before calling Sizeof 60 fDisplacement= 0; //Must be set to 0 before calling Sizeof 61 fBuffer = 0; //Must be set to 0 before calling Sizeof 62 fInsertQuery = insert_query; 36 fClassName = "MBasketSQL"; 63 37 64 if (vc==0) { 65 fBufferRef = 0; 66 } else { 67 fBufferRef = new MBufferSQL(TBuffer::kWrite, fBufferSize, vc, fInsertQuery, fRowPtr); 68 } 69 fHeaderOnly = kTRUE; 70 fLast = 0; // Must initialize before calling Streamer() 71 //Streamer(*fBufferRef); 72 fBuffer = 0; 73 fBranch = branch; 74 fHeaderOnly = kFALSE; 75 branch->GetTree()->IncrementTotalBuffers(fBufferSize); 38 fBufferSize = branch->GetBasketSize(); 39 fNevBufSize = branch->GetEntryOffsetLen(); 40 fNevBuf = 0; 41 fEntryOffset = 0; //Must be set to 0 before calling Sizeof 42 fDisplacement= 0; //Must be set to 0 before calling Sizeof 43 fBuffer = 0; //Must be set to 0 before calling Sizeof 44 fBufferRef = 0; 76 45 77 } 46 fBufferRef = new MBufferSQL(sqlrow, index, type); 78 47 79 //_________________________________________________________________________ 80 MBasketSQL::~MBasketSQL() 81 { 82 // Destructor 83 } 84 85 //_________________________________________________________________________ 86 void MBasketSQL::CreateBuffer(const char *name, TString title, 87 vector<Int_t> *vc, 88 TBranch *branch, TSQLResult ** rs) 89 { 90 // Create a TSQLBuffer for this basket. 91 92 fResultPtr = rs; 93 SetName(name); 94 SetTitle(title); 95 fClassName = "MBasketSQL"; 96 fBufferSize = branch->GetBasketSize(); 97 fNevBufSize = branch->GetEntryOffsetLen(); 98 fNevBuf = 0; 99 fEntryOffset = 0; //Must be set to 0 before calling Sizeof 100 fDisplacement= 0; //Must be set to 0 before calling Sizeof 101 fBuffer = 0; //Must be set to 0 before calling Sizeof 102 103 if(vc==0) { 104 fBufferRef = 0; 105 Error("CreateBuffer","Need a vector of columns\n"); 106 } else { 107 fBufferRef = new MBufferSQL(TBuffer::kWrite, fBufferSize, vc, fInsertQuery, fRowPtr); 108 } 109 fHeaderOnly = kTRUE; 110 fLast = 0; 111 //Streamer(*fBufferRef); 112 fBuffer = 0; 113 fBranch = branch; 114 fHeaderOnly = kFALSE; 115 branch->GetTree()->IncrementTotalBuffers(fBufferSize); 48 fHeaderOnly = kTRUE; 49 fLast = 0; 50 //Streamer(*fBufferRef); 51 fBuffer = 0; 52 fBranch = branch; 53 fHeaderOnly = kFALSE; 54 branch->GetTree()->IncrementTotalBuffers(fBufferSize); 116 55 } 117 56 … … 119 58 void MBasketSQL::PrepareBasket(Long64_t entry) 120 59 { 121 // Prepare the basket for the next entry. 122 123 ((MBufferSQL*)fBufferRef)->ResetOffset(); 124 ((MTreeSQL*)fBranch->GetTree())->PrepEntry(entry); 125 fBufferRef->Reset(); 60 // Prepare the basket for the next entry. 61 fBranch->GetTree()->LoadTree(entry); 126 62 } 127 63 … … 129 65 Int_t MBasketSQL::ReadBasketBytes(Long64_t , TFile *) 130 66 { 131 // See TBasket::ReadBasketBytes. This is not implemented in MBasketSQL. 132 133 Error("ReadBasketBytes","This member function should not be called!"); 134 return 0; 67 Error("ReadBasketBytes","This member function should not be called!"); 68 return 0; 135 69 } 136 70 137 71 //_________________________________________________________________________ 138 72 Int_t MBasketSQL::ReadBasketBuffers(Long64_t , Int_t, TFile *) 139 { 140 // See TBasket::ReadBasketBuffers. This is not implemented in MBasketSQL. 141 142 Error("ReadBasketBuffers","This member function should not be called!"); 143 return 0; 73 { 74 Error("ReadBasketBuffers","This member function should not be called!"); 75 return 0; 144 76 } 145 146 //_________________________________________________________________________147 void MBasketSQL::Reset()148 {149 // See TBasket::Reset150 151 TBasket::Reset();152 }153 154 77 155 78 //_________________________________________________________________________ 156 79 void MBasketSQL::Update(Int_t, Int_t) 157 80 { 158 // See TBasket::Update. 159 160 ((MBufferSQL*)fBufferRef)->ResetOffset(); 161 fNevBuf++; 81 // See TBasket::Update. 82 fNevBuf++; 162 83 } 163 84 164 165 85 #endif -
trunk/Mars/msql/MBasketSQL.h
r15418 r15439 1 // @(#)root/tree:$Id: MBasketSQL.h 35231 2010-09-10 17:59:10Z pcanal $2 // Author: Philippe Canal 20053 4 /*************************************************************************5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *6 * All rights reserved. *7 * *8 * For the licensing terms see $ROOTSYS/LICENSE. *9 * For the list of contributors see $ROOTSYS/README/CREDITS. *10 *************************************************************************/11 12 1 #ifndef ROOT_MBasketSQL 13 2 #define ROOT_MBasketSQL … … 21 10 ////////////////////////////////////////////////////////////////////////// 22 11 23 #include <vector>24 12 #include "TBasket.h" 25 13 26 class TSQLResult;27 14 class TSQLRow; 28 class MBufferSQL;29 15 30 16 class MBasketSQL : public TBasket 31 17 { 32 33 18 private: 34 19 MBasketSQL(const MBasketSQL&); // MBasketSQL objects are not copiable. … … 36 21 37 22 protected: 38 TSQLResult **fResultPtr; //! 39 TSQLRow **fRowPtr; //! 40 TString *fInsertQuery; //! 23 TSQLRow **fRowPtr; //! 41 24 42 25 public: 43 26 MBasketSQL(); 44 MBasketSQL(const char *name, const char *title, 45 TBranch *branch, TSQLResult **rs, 46 TString *insert_query, std::vector<Int_t> *vc, TSQLRow **row); 47 ~MBasketSQL(); 48 void PrepareBasket(Long64_t entry); 49 virtual Int_t ReadBasketBuffers(Long64_t pos, Int_t len, TFile *file); 50 virtual Int_t ReadBasketBytes(Long64_t pos, TFile *file); 51 virtual void Reset(); 52 TSQLResult * GetResultSet() { return *fResultPtr;} 53 void CreateBuffer(const char *name, TString title, std::vector<Int_t> * vc, TBranch *branch, TSQLResult ** rs); 27 MBasketSQL(TBranch *b, TSQLRow **r, Int_t index, TString type); 28 29 void PrepareBasket(Long64_t entry); 30 31 virtual Int_t ReadBasketBuffers(Long64_t pos, Int_t len, TFile *file); 32 virtual Int_t ReadBasketBytes(Long64_t pos, TFile *file); 54 33 55 34 void Update(Int_t offset, Int_t skipped); -
trunk/Mars/msql/MBufferSQL.cc
r15417 r15439 17 17 // // 18 18 ////////////////////////////////////////////////////////////////////////// 19 #include "MBufferSQL.h" 19 20 20 #include <vector>21 21 #include <stdio.h> 22 #include <stdlib.h> 23 22 24 #include "TError.h" 23 25 24 #include "MBufferSQL.h"25 26 #include "TSQLResult.h" 26 27 #include "TSQLRow.h" 27 #include <stdlib.h> 28 29 #include "MTime.h" 28 30 29 31 using namespace std; … … 32 34 33 35 //________________________________________________________________________ 34 MBufferSQL::MBufferSQL(TBuffer::EMode mode, vector<Int_t> *vc, 35 TString *insert_query, TSQLRow ** r) : 36 TBufferFile(mode), 37 fColumnVec(vc), fInsertQuery(insert_query), fRowPtr(r) 36 MBufferSQL::MBufferSQL() : TBufferFile(), fRowPtr(0) 38 37 { 39 38 // Constructor. 40 41 fIter = fColumnVec->begin();42 39 } 43 40 44 41 //________________________________________________________________________ 45 MBufferSQL::MBufferSQL(TBuffer::EMode mode, Int_t bufsiz, vector<Int_t> *vc, 46 TString *insert_query, TSQLRow ** r) : 47 TBufferFile(mode,bufsiz), 48 fColumnVec(vc), fInsertQuery(insert_query), fRowPtr(r) 42 MBufferSQL::MBufferSQL(TSQLRow **r, Int_t index, TString type) : 43 TBufferFile(TBufferFile::kRead), fRowPtr(r), fIndex(index), fIsDate(kFALSE) 49 44 { 50 // Constructor. 51 52 fIter = fColumnVec->begin(); 53 } 54 55 //________________________________________________________________________ 56 MBufferSQL::MBufferSQL(TBuffer::EMode mode, Int_t bufsiz, vector<Int_t> *vc, 57 TString *insert_query, TSQLRow ** r, 58 void *buf, Bool_t adopt) : 59 TBufferFile(mode,bufsiz,buf,adopt), 60 fColumnVec(vc), fInsertQuery(insert_query), fRowPtr(r) 61 { 62 // Constructor. 63 64 fIter = fColumnVec->begin(); 65 } 66 67 //________________________________________________________________________ 68 MBufferSQL::MBufferSQL() : TBufferFile(), fColumnVec(0),fInsertQuery(0),fRowPtr(0) 69 { 70 // Constructor. 71 72 } 73 74 //________________________________________________________________________ 75 MBufferSQL::~MBufferSQL() 76 { 77 // Destructo. 78 79 delete fColumnVec; 80 } 81 82 //________________________________________________________________________ 83 void MBufferSQL::ReadBool(Bool_t &b) 84 { 85 // Operator>> 86 const char *ptr = (*fRowPtr)->GetField(*fIter); 87 88 b = (Bool_t)atoi(ptr?ptr:""); 89 90 if (fIter != fColumnVec->end()) ++fIter; 91 92 if(ptr==0) Error("operator>>(Bool_t&)","Error reading Bool_t"); 93 } 94 95 //________________________________________________________________________ 96 void MBufferSQL::ReadChar(Char_t &c) 97 { 98 // Operator>> 99 const char *ptr = (*fRowPtr)->GetField(*fIter); 100 101 c = (Char_t)atoi(ptr?ptr:""); 102 103 if (fIter != fColumnVec->end()) ++fIter; 104 105 if(ptr==0) Error("operator>>(Char_t&)","Error reading Char_t"); 106 } 107 108 //________________________________________________________________________ 109 void MBufferSQL::ReadShort(Short_t &h) 110 { 111 // Operator>> 112 const char *ptr = (*fRowPtr)->GetField(*fIter); 113 114 h = (Short_t)atoi(ptr?ptr:""); 115 116 if (fIter != fColumnVec->end()) ++fIter; 117 118 if(ptr==0) Error("operator>>(Short_t&)","Error reading Short_t"); 119 } 120 121 //________________________________________________________________________ 122 void MBufferSQL::ReadInt(Int_t &i) 123 { 124 // Operator>> 125 const char *ptr = (*fRowPtr)->GetField(*fIter); 126 127 i = atoi(ptr?ptr:""); 128 129 if (fIter != fColumnVec->end()) ++fIter; 130 131 if(ptr==0) Error("operator>>(Int_t&)","Error reading Int_t"); 132 } 133 134 //________________________________________________________________________ 135 void MBufferSQL::ReadFloat(Float_t &f) 136 { 137 // Operator>> 138 const char *ptr = (*fRowPtr)->GetField(*fIter); 139 140 f = atof(ptr?ptr:""); 141 142 if (fIter != fColumnVec->end()) ++fIter; 143 144 if(ptr==0) Error("operator>>(Float_t&)","Error reading Float_t"); 145 } 146 147 //________________________________________________________________________ 148 void MBufferSQL::ReadLong(Long_t &l) 149 { 150 // Operator>> 151 const char *ptr = (*fRowPtr)->GetField(*fIter); 152 153 l = atol(ptr?ptr:""); 154 155 if (fIter != fColumnVec->end()) ++fIter; 156 157 if(ptr==0) Error("operator>>(Long_t&)","Error reading Long_t"); 45 // Constructor. 46 if (type.BeginsWith("date", TString::kIgnoreCase) || 47 type.BeginsWith("time", TString::kIgnoreCase)) 48 fIsDate = kTRUE; 158 49 } 159 50 … … 162 53 { 163 54 // Operator>> 164 const char *ptr = (*fRowPtr)->GetField( *fIter);55 const char *ptr = (*fRowPtr)->GetField(fIndex); 165 56 166 d = atof(ptr?ptr:""); 57 if (ptr==0) 58 { 59 d = 0; 60 Error("operator>>(Double_t&)","NULL value found in cell"); 61 return; 62 } 167 63 168 if (fIter != fColumnVec->end()) ++fIter; 64 if (!fIsDate) 65 { 66 d = atof(ptr); 67 return; 68 } 169 69 170 if(ptr==0) Error("operator>>(Double_t&)","Error reading Double_t");70 d = MTime(ptr).GetAxisTime(); 171 71 } 172 173 174 //________________________________________________________________________175 void MBufferSQL::WriteBool(Bool_t b)176 {177 // Operator<<178 179 (*fInsertQuery) += b;180 (*fInsertQuery) += ",";181 if (fIter != fColumnVec->end()) ++fIter;182 }183 184 //________________________________________________________________________185 void MBufferSQL::WriteChar(Char_t c)186 {187 // Operator<<188 189 (*fInsertQuery) += c;190 (*fInsertQuery) += ",";191 if (fIter != fColumnVec->end()) ++fIter;192 }193 194 //________________________________________________________________________195 void MBufferSQL::WriteShort(Short_t h)196 {197 // Operator<<198 199 (*fInsertQuery) += h;200 (*fInsertQuery) += ",";201 if (fIter != fColumnVec->end()) ++fIter;202 }203 204 //________________________________________________________________________205 void MBufferSQL::WriteInt(Int_t i)206 {207 // Operator<<208 209 (*fInsertQuery) += i;210 (*fInsertQuery) += ",";211 if (fIter != fColumnVec->end()) ++fIter;212 }213 214 //________________________________________________________________________215 void MBufferSQL::WriteLong(Long_t l)216 {217 // Operator<<218 219 (*fInsertQuery) += l;220 (*fInsertQuery) += ",";221 if (fIter != fColumnVec->end()) ++fIter;222 }223 224 //________________________________________________________________________225 void MBufferSQL::WriteFloat(Float_t f)226 {227 // Operator<<228 229 (*fInsertQuery) += f;230 (*fInsertQuery) += ",";231 if (fIter != fColumnVec->end()) ++fIter;232 }233 234 //________________________________________________________________________235 void MBufferSQL::WriteDouble(Double_t d)236 {237 // Operator<<238 239 (*fInsertQuery) += d;240 (*fInsertQuery) += ",";241 if (fIter != fColumnVec->end()) ++fIter;242 }243 244 //________________________________________________________________________245 void MBufferSQL::ReadUChar(UChar_t& uc)246 {247 // Operator>>248 const char *ptr = (*fRowPtr)->GetField(*fIter);249 250 uc = (UChar_t)atoi(ptr?ptr:"");251 252 if (fIter != fColumnVec->end()) ++fIter;253 254 if(ptr==0) Error("operator>>(UChar_t&)","Error reading UChar_t");255 }256 257 //________________________________________________________________________258 void MBufferSQL::ReadUShort(UShort_t& us)259 {260 // Operator>>261 const char *ptr = (*fRowPtr)->GetField(*fIter);262 263 us = (UShort_t)atoi(ptr?ptr:"");264 265 if (fIter != fColumnVec->end()) ++fIter;266 267 if(ptr==0) Error("operator>>(UShort_t&)","Error reading UShort_t");268 }269 270 //________________________________________________________________________271 void MBufferSQL::ReadUInt(UInt_t& ui)272 {273 // Operator>>274 275 TString val = (*fRowPtr)->GetField(*fIter);276 Int_t code = sscanf(val.Data(), "%u",&ui);277 if(code == 0) Error("operator>>(UInt_t&)","Error reading UInt_t");278 279 if (fIter != fColumnVec->end()) ++fIter;280 }281 282 //________________________________________________________________________283 void MBufferSQL::ReadULong(ULong_t& ul)284 {285 // Operator>>286 287 TString val = (*fRowPtr)->GetField(*fIter);288 Int_t code = sscanf(val.Data(), "%lu",&ul);289 if(code == 0) Error("operator>>(ULong_t&)","Error reading ULong_t");290 291 if (fIter != fColumnVec->end()) ++fIter;292 }293 294 //________________________________________________________________________295 void MBufferSQL::ReadLong64(Long64_t &ll)296 {297 // Operator>>298 299 TString val = (*fRowPtr)->GetField(*fIter);300 Int_t code = sscanf(val.Data(), "%lld",&ll);301 if(code == 0) Error("operator>>(ULong_t&)","Error reading Long64_t");302 303 if (fIter != fColumnVec->end()) ++fIter;304 }305 306 //________________________________________________________________________307 void MBufferSQL::ReadULong64(ULong64_t &ull)308 {309 // Operator>>310 311 TString val = (*fRowPtr)->GetField(*fIter);312 Int_t code = sscanf(val.Data(), "%llu",&ull);313 if(code == 0) Error("operator>>(ULong_t&)","Error reading ULong64_t");314 315 if (fIter != fColumnVec->end()) ++fIter;316 }317 318 //________________________________________________________________________319 void MBufferSQL::ReadCharP(Char_t *str)320 {321 // Operator>>322 323 strcpy(str,(*fRowPtr)->GetField(*fIter)); // Legacy interface, we have no way to know the user's buffer size ....324 if (fIter != fColumnVec->end()) ++fIter;325 }326 327 //________________________________________________________________________328 void MBufferSQL::ReadTString(TString &)329 {330 // Operator>>331 332 //strcpy(str,(*fRowPtr)->GetField(*fIter));333 //if (fIter != fColumnVec->end()) ++fIter;334 printf("ERROR NOT IMPLEMENTED\n");335 }336 337 //________________________________________________________________________338 void MBufferSQL::WriteTString(const TString &)339 {340 // Operator>>341 342 //strcpy(str,(*fRowPtr)->GetField(*fIter));343 //if (fIter != fColumnVec->end()) ++fIter;344 printf("ERROR NOT IMPLEMENTED\n");345 }346 347 // Method to send to database.348 349 //________________________________________________________________________350 void MBufferSQL::WriteUChar(UChar_t uc)351 {352 // Operator<<353 354 (*fInsertQuery) += uc;355 (*fInsertQuery) += ",";356 ++fIter;357 }358 359 //________________________________________________________________________360 void MBufferSQL::WriteUShort(UShort_t us)361 {362 // Operator<<363 364 (*fInsertQuery) += us;365 (*fInsertQuery) += ",";366 ++fIter;367 }368 369 //________________________________________________________________________370 void MBufferSQL::WriteUInt(UInt_t ui)371 {372 // Operator<<373 374 (*fInsertQuery) += ui;375 (*fInsertQuery) += ",";376 ++fIter;377 }378 379 //________________________________________________________________________380 void MBufferSQL::WriteULong(ULong_t ul)381 {382 // Operator<<383 384 (*fInsertQuery) += ul;385 (*fInsertQuery) += ",";386 ++fIter;387 }388 389 //________________________________________________________________________390 void MBufferSQL::WriteLong64(Long64_t ll)391 {392 // Operator<<393 394 (*fInsertQuery) += ll;395 (*fInsertQuery) += ",";396 ++fIter;397 }398 399 //________________________________________________________________________400 void MBufferSQL::WriteULong64(ULong64_t ull)401 {402 // Operator<<403 404 (*fInsertQuery) += ull;405 (*fInsertQuery) += ",";406 ++fIter;407 }408 409 //________________________________________________________________________410 void MBufferSQL::WriteCharP(const Char_t *str)411 {412 // Operator<<413 414 (*fInsertQuery) += "\"";415 (*fInsertQuery) += str;416 (*fInsertQuery) += "\",";417 ++fIter;418 }419 420 //________________________________________________________________________421 void MBufferSQL::WriteFastArray(const Bool_t *b, Int_t n)422 {423 // WriteFastArray SQL implementation.424 for(int i=0; i<n; ++i) {425 (*fInsertQuery) += b[i];426 (*fInsertQuery) += ",";427 ++fIter;428 }429 }430 431 //________________________________________________________________________432 void MBufferSQL::WriteFastArray(const Char_t *c, Int_t n)433 {434 // WriteFastArray SQL implementation.435 436 for(int i=0; i<n; ++i) {437 (*fInsertQuery) += (Short_t)c[i];438 (*fInsertQuery) += ",";439 ++fIter;440 }441 }442 443 //________________________________________________________________________444 void MBufferSQL::WriteFastArrayString(const Char_t *c, Int_t /* n */)445 {446 // WriteFastArray SQL implementation.447 448 (*fInsertQuery) += "\"";449 (*fInsertQuery) += c;450 (*fInsertQuery) += "\",";451 ++fIter;452 }453 454 //________________________________________________________________________455 void MBufferSQL::WriteFastArray(const UChar_t *uc, Int_t n)456 {457 // WriteFastArray SQL implementation.458 459 for(int i=0; i<n; ++i) {460 (*fInsertQuery) += uc[i];461 (*fInsertQuery) += ",";462 ++fIter;463 }464 }465 466 //________________________________________________________________________467 void MBufferSQL::WriteFastArray(const Short_t *h, Int_t n)468 {469 // WriteFastArray SQL implementation.470 471 for(int i=0; i<n; ++i) {472 (*fInsertQuery) += h[i];473 (*fInsertQuery) += ",";474 ++fIter;475 }476 }477 478 //________________________________________________________________________479 void MBufferSQL::WriteFastArray(const UShort_t *us, Int_t n)480 {481 // WriteFastArray SQL implementation.482 483 for(int i=0; i<n; ++i) {484 (*fInsertQuery) += us[i];485 (*fInsertQuery) += ",";486 ++fIter;487 }488 }489 490 //________________________________________________________________________491 void MBufferSQL::WriteFastArray(const Int_t *ii, Int_t n)492 {493 // WriteFastArray SQL implementation.494 495 // cerr << "Column: " <<*fIter << " i:" << *ii << endl;496 for(int i=0; i<n; ++i) {497 (*fInsertQuery) += ii[i];498 (*fInsertQuery) += ",";499 ++fIter;500 }501 }502 503 //________________________________________________________________________504 void MBufferSQL::WriteFastArray(const UInt_t *ui, Int_t n)505 {506 // WriteFastArray SQL implementation.507 508 for(int i=0; i<n; ++i) {509 (*fInsertQuery) += ui[i];510 (*fInsertQuery) += ",";511 ++fIter;512 }513 }514 515 //________________________________________________________________________516 void MBufferSQL::WriteFastArray(const Long_t *l, Int_t n)517 {518 // WriteFastArray SQL implementation.519 520 for(int i=0; i<n; ++i) {521 (*fInsertQuery)+= l[i];522 (*fInsertQuery)+= ",";523 ++fIter;524 }525 }526 527 //________________________________________________________________________528 void MBufferSQL::WriteFastArray(const ULong_t *ul, Int_t n)529 {530 // WriteFastArray SQL implementation.531 532 for(int i=0; i<n; ++i) {533 (*fInsertQuery) += ul[i];534 (*fInsertQuery) += ",";535 ++fIter;536 }537 }538 539 //________________________________________________________________________540 void MBufferSQL::WriteFastArray(const Long64_t *l, Int_t n)541 {542 // WriteFastArray SQL implementation.543 544 for(int i=0; i<n; ++i) {545 (*fInsertQuery) += l[i];546 (*fInsertQuery) += ",";547 ++fIter;548 }549 }550 551 //________________________________________________________________________552 void MBufferSQL::WriteFastArray(const ULong64_t *ul, Int_t n)553 {554 // WriteFastArray SQL implementation.555 556 for(int i=0; i<n; ++i) {557 (*fInsertQuery) += ul[i];558 (*fInsertQuery) += ",";559 ++fIter;560 }561 }562 563 //________________________________________________________________________564 void MBufferSQL::WriteFastArray(const Float_t *f, Int_t n)565 {566 // WriteFastArray SQL implementation.567 568 for(int i=0; i<n; ++i) {569 (*fInsertQuery) += f[i];570 (*fInsertQuery) += ",";571 ++fIter;572 }573 }574 575 //________________________________________________________________________576 void MBufferSQL::WriteFastArray(const Double_t *d, Int_t n)577 {578 // WriteFastArray SQL implementation.579 580 for(int i=0; i<n; ++i) {581 (*fInsertQuery) += d[i];582 (*fInsertQuery )+= ",";583 ++fIter;584 }585 }586 587 //________________________________________________________________________588 void MBufferSQL::WriteFastArray(void*, const TClass*, Int_t, TMemberStreamer *)589 {590 // WriteFastArray SQL implementation.591 592 Fatal("riteFastArray(void*, const TClass*, Int_t, TMemberStreamer *)","Not implemented yet");593 }594 595 //________________________________________________________________________596 Int_t MBufferSQL::WriteFastArray(void **, const TClass*, Int_t, Bool_t, TMemberStreamer*)597 {598 // WriteFastArray SQL implementation.599 600 Fatal("WriteFastArray(void **, const TClass*, Int_t, Bool_t, TMemberStreamer*)","Not implemented yet");601 return 0;602 }603 604 //________________________________________________________________________605 void MBufferSQL::ReadFastArray(Bool_t *b, Int_t n)606 {607 // ReadFastArray SQL implementation.608 609 for(int i=0; i<n; ++i) {610 b[i] = (Bool_t)atoi((*fRowPtr)->GetField(*fIter));611 ++fIter;612 }613 }614 615 //________________________________________________________________________616 void MBufferSQL::ReadFastArray(Char_t *c, Int_t n)617 {618 // ReadFastArray SQL implementation.619 for(int i=0; i<n; ++i) {620 c[i] = (Char_t)atoi((*fRowPtr)->GetField(*fIter));621 ++fIter;622 }623 }624 625 //________________________________________________________________________626 void MBufferSQL::ReadFastArrayString(Char_t *c, Int_t /* n */)627 {628 // ReadFastArray SQL implementation.629 strcpy(c,((*fRowPtr)->GetField(*fIter)));630 ++fIter;631 }632 633 //________________________________________________________________________634 void MBufferSQL::ReadFastArray(UChar_t *uc, Int_t n)635 {636 // ReadFastArray SQL implementation.637 for(int i=0; i<n; ++i) {638 uc[i] = (UChar_t)atoi((*fRowPtr)->GetField(*fIter));639 ++fIter;640 }641 }642 643 //________________________________________________________________________644 void MBufferSQL::ReadFastArray(Short_t *s, Int_t n)645 {646 // ReadFastArray SQL implementation.647 for(int i=0; i<n; ++i) {648 s[i] = (Short_t)atoi((*fRowPtr)->GetField(*fIter));649 ++fIter;650 }651 }652 653 //________________________________________________________________________654 void MBufferSQL::ReadFastArray(UShort_t *us, Int_t n)655 {656 // ReadFastArray SQL implementation.657 for(int i=0; i<n; ++i) {658 us[i] = (UShort_t)atoi((*fRowPtr)->GetField(*fIter));659 ++fIter;660 }661 }662 663 //________________________________________________________________________664 void MBufferSQL::ReadFastArray(Int_t *in, Int_t n)665 {666 // ReadFastArray SQL implementation.667 for(int i=0; i<n; ++i) {668 in[i] = atoi((*fRowPtr)->GetField(*fIter));669 ++fIter;670 }671 }672 673 //________________________________________________________________________674 void MBufferSQL::ReadFastArray(UInt_t *ui, Int_t n)675 {676 // ReadFastArray SQL implementation.677 for(int i=0; i<n; ++i) {678 ui[i] = atoi((*fRowPtr)->GetField(*fIter));679 ++fIter;680 }681 }682 683 //________________________________________________________________________684 void MBufferSQL::ReadFastArray(Long_t *l, Int_t n)685 {686 // ReadFastArray SQL implementation.687 for(int i=0; i<n; ++i) {688 l[i] = atol((*fRowPtr)->GetField(*fIter));689 ++fIter;690 }691 }692 693 //________________________________________________________________________694 void MBufferSQL::ReadFastArray(ULong_t *ul, Int_t n)695 {696 // ReadFastArray SQL implementation.697 for(int i=0; i<n; ++i) {698 (*this) >> ul[i];699 }700 }701 702 //________________________________________________________________________703 void MBufferSQL::ReadFastArray(Long64_t *ll, Int_t n)704 {705 // ReadFastArray SQL implementation.706 for(int i=0; i<n; ++i) {707 (*this) >> ll[i];708 }709 }710 711 //________________________________________________________________________712 void MBufferSQL::ReadFastArray(ULong64_t *ull, Int_t n)713 {714 // ReadFastArray SQL implementation.715 for(int i=0; i<n; ++i) {716 (*this) >> ull[i];717 }718 }719 720 //________________________________________________________________________721 void MBufferSQL::ReadFastArray(Float_t *f, Int_t n)722 {723 // ReadFastArray SQL implementation.724 for(int i=0; i<n; ++i) {725 f[i] = atof((*fRowPtr)->GetField(*fIter));726 ++fIter;727 }728 }729 730 //________________________________________________________________________731 void MBufferSQL::ReadFastArray(Double_t *d, Int_t n)732 {733 // ReadFastArray SQL implementation.734 for(int i=0; i<n; ++i) {735 d[i] = atof((*fRowPtr)->GetField(*fIter));736 ++fIter;737 }738 }739 740 //________________________________________________________________________741 void MBufferSQL::ReadFastArrayFloat16(Float_t *, Int_t , TStreamerElement *)742 {743 // ReadFastArray SQL implementation.744 Fatal("ReadFastArrayFloat16(Float_t *, Int_t , TStreamerElement *)","Not implemented yet");745 }746 747 //________________________________________________________________________748 void MBufferSQL::ReadFastArrayDouble32(Double_t *, Int_t , TStreamerElement *)749 {750 // ReadFastArray SQL implementation.751 Fatal("ReadFastArrayDouble32(Double_t *, Int_t , TStreamerElement *)","Not implemented yet");752 }753 754 //________________________________________________________________________755 void MBufferSQL::ReadFastArray(void *, const TClass *, Int_t, TMemberStreamer *, const TClass *)756 {757 // ReadFastArray SQL implementation.758 Fatal("ReadFastArray(void *, const TClass *, Int_t, TMemberStreamer *, const TClass *)","Not implemented yet");759 }760 761 //________________________________________________________________________762 void MBufferSQL::ReadFastArray(void **, const TClass *, Int_t, Bool_t, TMemberStreamer *, const TClass *)763 {764 // ReadFastArray SQL implementation.765 Fatal("ReadFastArray(void **, const TClass *, Int_t, Bool_t, TMemberStreamer *, const TClass *)","Not implemented yet");766 }767 768 //________________________________________________________________________769 void MBufferSQL::ResetOffset()770 {771 // Reset Offset.772 fIter = fColumnVec->begin();773 }774 775 #if 0776 //________________________________________________________________________777 void MBufferSQL::insert_test(const char* dsn, const char* usr,778 const char* pwd, const TString& tblname)779 {780 TString str;781 TString select = "select * from ";782 TString sql;783 TSQLStatement* stmt;784 sql = select + "ins";785 786 con = gSQLDriverManager->GetConnection(dsn,usr,pwd);787 788 if(!con)789 printf("\n\n\nConnection NOT Successful\n\n\n");790 else791 printf("\n\n\nConnection Sucessful\n\n\n");792 793 794 795 stmt = con->CreateStatement(0, odbc::ResultSet::CONCUR_READ_ONLY);796 797 ptr = stmt->ExecuteQuery(sql.Data());798 if(!ptr) printf("No recorSet found!");799 800 ptr->Next();801 ptr->MoveToInsertRow();802 cerr << "IsAfterLast(): " << ptr->IsAfterLast() << endl;803 ptr->UpdateInt(1, 5555);804 ptr->InsertRow();805 con->Commit();806 807 ptr1 = stmt->ExecuteQuery(sql.Data());808 809 }810 #endif -
trunk/Mars/msql/MBufferSQL.h
r15417 r15439 32 32 class TSQLRow; 33 33 34 class MBufferSQL : public TBufferFile {35 34 class MBufferSQL : public TBufferFile 35 { 36 36 private: 37 std::vector<Int_t>::const_iterator fIter; 38 39 std::vector<Int_t> *fColumnVec; //! 40 TString *fInsertQuery; //! 41 TSQLRow **fRowPtr; //! 37 TSQLRow **fRowPtr; //! 38 Int_t fIndex; 39 Bool_t fIsDate; 42 40 43 41 // TBuffer objects cannot be copied or assigned 44 MBufferSQL(const MBufferSQL &); // not implemented45 void operator=(const MBufferSQL &); // not implemented42 MBufferSQL(const MBufferSQL &); // not implemented 43 void operator=(const MBufferSQL &); // not implemented 46 44 47 45 public: 48 MBufferSQL(); 49 MBufferSQL(TBuffer::EMode mode, std::vector<Int_t> *vc, TString *insert_query, TSQLRow **rowPtr); 50 MBufferSQL(TBuffer::EMode mode, Int_t bufsiz, std::vector<Int_t> *vc, TString *insert_query, TSQLRow **rowPtr); 51 MBufferSQL(TBuffer::EMode mode, Int_t bufsiz, std::vector<Int_t> *vc, TString *insert_query, TSQLRow **rowPtr,void *buf, Bool_t adopt = kTRUE); 52 ~MBufferSQL(); 46 MBufferSQL(); 47 MBufferSQL(TSQLRow **row, Int_t index, TString type); 53 48 54 void ResetOffset();49 void ReadDouble(Double_t &d); 55 50 56 virtual void ReadBool(Bool_t &b); 57 virtual void ReadChar(Char_t &c); 58 virtual void ReadUChar(UChar_t &c); 59 virtual void ReadShort(Short_t &s); 60 virtual void ReadUShort(UShort_t &s); 61 virtual void ReadInt(Int_t &i); 62 virtual void ReadUInt(UInt_t &i); 63 virtual void ReadLong(Long_t &l); 64 virtual void ReadULong(ULong_t &l); 65 virtual void ReadLong64(Long64_t &l); 66 virtual void ReadULong64(ULong64_t &l); 67 virtual void ReadFloat(Float_t &f); 68 virtual void ReadDouble(Double_t &d); 69 virtual void ReadCharP(Char_t *c); 70 virtual void ReadTString(TString &s); 71 72 virtual void WriteBool(Bool_t b); 73 virtual void WriteChar(Char_t c); 74 virtual void WriteUChar(UChar_t c); 75 virtual void WriteShort(Short_t s); 76 virtual void WriteUShort(UShort_t s); 77 virtual void WriteInt(Int_t i); 78 virtual void WriteUInt(UInt_t i); 79 virtual void WriteLong(Long_t l); 80 virtual void WriteULong(ULong_t l); 81 virtual void WriteLong64(Long64_t l); 82 virtual void WriteULong64(ULong64_t l); 83 virtual void WriteFloat(Float_t f); 84 virtual void WriteDouble(Double_t d); 85 virtual void WriteCharP(const Char_t *c); 86 virtual void WriteTString(const TString &s); 87 88 89 virtual void WriteFastArray(const Bool_t *b, Int_t n); 90 virtual void WriteFastArray(const Char_t *c, Int_t n); 91 virtual void WriteFastArrayString(const Char_t *c, Int_t n); 92 virtual void WriteFastArray(const UChar_t *c, Int_t n); 93 virtual void WriteFastArray(const Short_t *h, Int_t n); 94 virtual void WriteFastArray(const UShort_t *h, Int_t n); 95 virtual void WriteFastArray(const Int_t *i, Int_t n); 96 virtual void WriteFastArray(const UInt_t *i, Int_t n); 97 virtual void WriteFastArray(const Long_t *l, Int_t n); 98 virtual void WriteFastArray(const ULong_t *l, Int_t n); 99 virtual void WriteFastArray(const Long64_t *l, Int_t n); 100 virtual void WriteFastArray(const ULong64_t *l, Int_t n); 101 virtual void WriteFastArray(const Float_t *f, Int_t n); 102 virtual void WriteFastArray(const Double_t *d, Int_t n); 103 virtual void WriteFastArray(void *start, const TClass *cl, Int_t n=1, TMemberStreamer *s=0); 104 virtual Int_t WriteFastArray(void **startp, const TClass *cl, Int_t n=1, Bool_t isPreAlloc=kFALSE, TMemberStreamer *s=0); 105 106 virtual void ReadFastArray(Bool_t *, Int_t ); 107 virtual void ReadFastArray(Char_t *, Int_t ); 108 virtual void ReadFastArrayString(Char_t *, Int_t ); 109 virtual void ReadFastArray(UChar_t *, Int_t ); 110 virtual void ReadFastArray(Short_t *, Int_t ); 111 virtual void ReadFastArray(UShort_t *, Int_t ); 112 virtual void ReadFastArray(Int_t *, Int_t ); 113 virtual void ReadFastArray(UInt_t *, Int_t ); 114 virtual void ReadFastArray(Long_t *, Int_t ); 115 virtual void ReadFastArray(ULong_t *, Int_t ); 116 virtual void ReadFastArray(Long64_t *, Int_t ); 117 virtual void ReadFastArray(ULong64_t *, Int_t ); 118 virtual void ReadFastArray(Float_t *, Int_t ); 119 virtual void ReadFastArray(Double_t *, Int_t ); 120 virtual void ReadFastArrayFloat16(Float_t *f, Int_t n, TStreamerElement *ele=0); 121 virtual void ReadFastArrayDouble32(Double_t *d, Int_t n, TStreamerElement *ele=0); 122 virtual void ReadFastArray(void *, const TClass *, Int_t n=1, TMemberStreamer *s=0, const TClass *onFileClass=0); 123 virtual void ReadFastArray(void **, const TClass *, Int_t n=1, Bool_t isPreAlloc=kFALSE, TMemberStreamer *s=0, const TClass *onFileClass=0); 124 125 ClassDef(MBufferSQL, 1); // Implementation of TBuffer to load and write to a SQL database 126 51 ClassDef(MBufferSQL, 1); // Implementation of TBuffer to load and write to a SQL database 127 52 }; 128 53 -
trunk/Mars/msql/MSQLServer.cc
r15193 r15439 59 59 #include <TObjArray.h> 60 60 61 #include "MTreeSQL.h" 62 61 63 ClassImp(MSQLServer); 62 64 … … 445 447 if (TestBit(kIsOwner)) 446 448 { 449 if (fTree) 450 delete fTree; 451 447 452 delete fServ; 448 453 fServ=0; … … 621 626 fType = kIsZombie; 622 627 628 fTree = 0; 623 629 fList.SetOwner(); 624 630 } … … 714 720 { 715 721 fServ = serv.fServ; 722 fTree = 0; 716 723 717 724 fDataBase = serv.fDataBase; … … 726 733 if (gDebug>0) 727 734 cout << "Delete: " << GetName() << endl; 735 728 736 Close(); 729 737 } … … 873 881 } 874 882 883 MTreeSQL *MSQLServer::GetTree(const char *table, const char *addon) 884 { 885 if (fType!=kIsServer || !fServ || !TestBit(kIsOwner)) 886 return 0; 887 888 if (!fTree) 889 fTree = new MTreeSQL(this, table, addon); 890 891 return fTree; 892 } 893 875 894 void MSQLServer::RecursiveRemove(TObject *obj) 876 895 { -
trunk/Mars/msql/MSQLServer.h
r9195 r15439 12 12 class TSQLResult; 13 13 14 class MTreeSQL; 15 14 16 class MSQLServer : public TObject 15 17 { 16 18 private: 17 19 TSQLServer *fServ; 20 MTreeSQL *fTree; 18 21 19 22 TString fDataBase; … … 66 69 public: 67 70 MSQLServer(TSQLServer *serv, const char *dbname=0, const char *tname=0, const char *col=0) 68 : fServ(serv), f DataBase(dbname), fTable(tname), fColumn(col), fType(kIsZombie)71 : fServ(serv), fTree(0), fDataBase(dbname), fTable(tname), fColumn(col), fType(kIsZombie) 69 72 { 70 73 fList.SetOwner(); … … 128 131 void RecursiveRemove(TObject *obj); 129 132 133 MTreeSQL *GetTree(const char *table, const char *addon=0); 134 130 135 ClassDef(MSQLServer, 0) // An enhancement of TSQLServer 131 136 }; -
trunk/Mars/msql/MTreeSQL.cc
r15419 r15439 34 34 #include "TSQLRow.h" 35 35 #include "TSQLResult.h" 36 #include "TSQLServer.h" 36 37 #include "MSQLServer.h" 37 38 38 39 #include "MTreeSQL.h" … … 44 45 45 46 //______________________________________________________________________________ 46 MTreeSQL::MTreeSQL( TSQLServer *server, TString DB, const TString& table, const TString &addon) :47 MTreeSQL::MTreeSQL(MSQLServer *server, const TString& table, const TString &addon) : 47 48 TTree(table.Data(), "Database read from table: " + table, 0), 48 fCurrentEntry(-1), fDB(DB), fQuery(table+" "+addon),fResult(0), fRow(0), 49 fServer(server) 49 fServer(server), fQuery(table+" "+addon), fResult(0), fRow(0) 50 50 { 51 51 fEntries = 0; … … 60 60 61 61 // Constructor with an explicit TSQLServer 62 TPRegexp reg(" LEFT+JOIN +([a-zA-Z0-9_.]+)");62 TPRegexp reg(" +JOIN +([a-zA-Z0-9_.]+)"); 63 63 64 64 Int_t p = -1; … … 79 79 80 80 for (auto it=fTables.begin(); it!=fTables.end(); it++) 81 {82 cout << *it << endl;83 81 if (!CheckTable(*it)) 84 82 return; 83 84 Init(); 85 } 86 87 MTreeSQL::~MTreeSQL() 88 { 89 for (auto it=fRows.begin(); it!=fRows.end(); it++) 90 delete *it; 91 92 delete fResult; 93 } 94 95 //______________________________________________________________________________ 96 Bool_t MTreeSQL::CheckTable(const TString &table) const 97 { 98 // Check the table exist in the database 99 if (fServer==0) 100 return kFALSE; 101 102 TSQLResult *tables = fServer->GetTables(table); 103 if (!tables) 104 return kFALSE; 105 106 TSQLRow * row = 0; 107 while ((row = tables->Next())) 108 { 109 if (table.CompareTo(row->GetField(0),TString::kIgnoreCase)==0) 110 return kTRUE; 111 112 delete row; 85 113 } 114 delete tables; 115 116 // The table is a not a permanent table, let's see if it is a 'temporary' table 117 const Int_t before = gErrorIgnoreLevel; 118 gErrorIgnoreLevel = kFatal; 119 120 TSQLResult *res = fServer->GetColumns(table); 121 if (res) 122 delete res; 123 124 gErrorIgnoreLevel = before; 125 126 return res ? kTRUE : kFALSE; 127 } 128 129 //______________________________________________________________________________ 130 void MTreeSQL::Init() 131 { 132 // Initializeation routine 133 // Number of entries needed to create the branches 134 //GetEntries(); 135 136 vector<pair<TString,TString>> cols; 137 138 for (auto it=fTables.begin(); it!=fTables.end(); it++) 139 { 140 TSQLResult *res = fServer->GetColumns(*it); 141 142 TSQLRow *row = 0; 143 while ((row=res->Next())) 144 { 145 TString f1 = row->GetField(1); 146 147 Int_t p0 = f1.First('('); 148 if (p0<0) 149 p0 = f1.First(' '); 150 if (p0<0) 151 p0 = f1.Length(); 152 153 const TString type = f1(0, p0); 154 155 if (type.CompareTo("int", TString::kIgnoreCase) && 156 type.CompareTo("date", TString::kIgnoreCase) && 157 type.CompareTo("time", TString::kIgnoreCase) && 158 type.CompareTo("datetime", TString::kIgnoreCase) && 159 type.CompareTo("year", TString::kIgnoreCase) && 160 type.CompareTo("timestamp", TString::kIgnoreCase) && 161 type.CompareTo("bit", TString::kIgnoreCase) && 162 type.CompareTo("tinyint", TString::kIgnoreCase) && 163 type.CompareTo("smallint", TString::kIgnoreCase) && 164 type.CompareTo("mediumint", TString::kIgnoreCase) && 165 type.CompareTo("integer", TString::kIgnoreCase) && 166 type.CompareTo("bigint", TString::kIgnoreCase) && 167 type.CompareTo("decimal", TString::kIgnoreCase) && 168 type.CompareTo("int", TString::kIgnoreCase) && 169 type.CompareTo("real", TString::kIgnoreCase) && 170 type.CompareTo("float", TString::kIgnoreCase) && 171 type.CompareTo("double", TString::kIgnoreCase)) 172 { 173 //cout << "Skip: " << type << endl; 174 continue; 175 } 176 177 const TString name = *it+"."+row->GetField(0); 178 cols.push_back(make_pair(name, type)); 179 180 delete row; 181 } 182 delete res; 183 } 184 185 TString list; 186 for (auto it=cols.begin(); it!=cols.end(); it++) 187 list += ","+it->first; 188 189 list.Remove(0, 1); 190 191 fResult = fServer->Query(("SELECT "+list+" FROM "+fQuery).Data()); 192 if (!fResult) 193 return; 194 195 TSQLRow *row = 0; 196 while ((row=(TSQLRow*)fResult->Next())) 197 fRows.push_back(row); 198 199 fEntries = fRows.size(); 200 201 // ============================================================== 202 203 // determine leaf description string 204 Int_t idx = 0; 205 for (auto it=cols.begin(); it!=cols.end(); it++, idx++) 206 { 207 const TString name = it->first; 208 209 TBranch *br = TTree::Branch(name, 0, name+"/D"); 210 211 br->ResetAddress(); 212 br->SetEntries(fEntries); 213 214 auto entry = br->GetBasketEntry(); 215 entry[0] = 0; 216 entry[1] = fEntries; 217 218 TBasket *bsk = new MBasketSQL(br, &fRow, idx, it->second); 219 220 br->GetListOfBaskets()->AddAtAndExpand(bsk, 0); 221 } 222 } 223 224 //______________________________________________________________________________ 225 Long64_t MTreeSQL::GetEntries() const 226 { 227 // Get the number of rows in the database 228 return GetEntriesFast(); 229 } 230 231 //______________________________________________________________________________ 232 Long64_t MTreeSQL::GetEntriesFast() const 233 { 234 // Return the number of entries as of the last check. 235 // Use GetEntries for a more accurate count. 236 237 return fEntries; 238 } 239 240 //______________________________________________________________________________ 241 Long64_t MTreeSQL::LoadTree(Long64_t entry) 242 { 243 // Setup the tree to the load the specified entry. 244 // Make sure the server and result set are setup for the requested entry 245 if (entry<0 || entry>=fEntries) 246 return -1; 247 248 fReadEntry = entry; 249 fRow = fRows[entry]; 250 251 return entry; 252 } 253 254 //______________________________________________________________________________ 255 void MTreeSQL::Refresh() 256 { 257 if (!fServer) 258 return; 259 260 // Refresh contents of this Tree and his branches from the current 261 // Tree status in the database 262 // One can call this function in case the Tree on its file is being 263 // updated by another process 264 for (auto it=fRows.begin(); it!=fRows.end(); it++) 265 delete *it; 266 267 fRows.clear(); 268 269 delete fResult; 86 270 87 271 Init(); … … 169 353 170 354 //______________________________________________________________________________ 171 TBranch * MTreeSQL::Branch(const char *name, void *address,172 const char *leaflist, Int_t bufsize)173 {174 // Create a branch175 176 Int_t nb = fBranches.GetEntriesFast();177 TBranch *branch;178 TString brName;179 180 for (int i=0;i<nb;i++) {181 branch = (TBranch*)fBranches.UncheckedAt(i);182 brName = branch->GetName();183 if (brName.Index(name) == 0) {184 // Now if the branch exists in db, root gives a warning and exit185 // Dealing with duplicate branch has been done, but not tested yet.186 // So if you want to allow duplicate branch, just comment Fatal() line and uncomment commented187 // below Fatal() line188 189 Fatal("Branch()", "Duplicate branch!!!");190 }191 }192 return TTree::Branch(name, address, leaflist, bufsize);193 }194 195 //______________________________________________________________________________196 Bool_t MTreeSQL::CheckTable(const TString &table) const197 {198 // Check the table exist in the database199 200 if (fServer==0) return kFALSE;201 TSQLResult * tables = fServer->GetTables(fDB.Data(),table);202 if (!tables) return kFALSE;203 TSQLRow * row = 0;204 while( (row = tables->Next()) ) {205 if(table.CompareTo(row->GetField(0),TString::kIgnoreCase)==0){206 return kTRUE;207 }208 }209 // The table is a not a permanent table, let's see if it is a 'temporary' table210 Int_t before = gErrorIgnoreLevel;211 gErrorIgnoreLevel = kFatal;212 TSQLResult *res = fServer->GetColumns(fDB.Data(),table);213 if (res) {214 delete res;215 return kTRUE;216 }217 gErrorIgnoreLevel = before;218 219 return kFALSE;220 }221 222 //______________________________________________________________________________223 TBasket * MTreeSQL::CreateBasket(TBranch * tb)224 {225 // Create a TBasketSQL226 227 if (fServer==0) {228 Error("CreateBasket","No TSQLServer specified");229 return 0;230 }231 232 static TString dummy;233 234 vector<Int_t> *columnVec = GetColumnIndice(tb);235 if (columnVec) {236 return new MBasketSQL(tb->GetName(), tb->GetName(), tb,237 &fResult, &dummy, columnVec, &fRow);238 } else {239 return 0;240 }241 }242 243 //_________________________________________________________________________244 TString MTreeSQL::CreateBranches(TSQLResult * rs, const TString &table)245 {246 // determine leaf description string247 248 if(!rs) return "";249 250 Int_t rows;251 TString type;252 TString res;253 TString branchName;254 TString leafName;255 Int_t prec=0;256 TBranch * br = 0;257 rows = rs->GetRowCount();258 TString decl;259 TString prevBranch;260 261 for( int i=0; i < rows; ++i ) {262 TSQLRow * row = rs->Next();263 if (!row) continue;264 type = row->GetField(1);265 Int_t index = type.First('(');266 if(index>0){267 prec = atoi(type(index+1,type.First(')')-1).Data());268 type = type(0,index);269 }270 branchName = table+"."+row->GetField(0);271 //cout << table << "." << branchName << endl;272 Int_t pos;273 if ((pos=branchName.Index("__"))!=kNPOS) {274 leafName = branchName(pos+2,branchName.Length());275 branchName.Remove(pos);276 } else {277 leafName = branchName;278 }279 if (prevBranch.Length()) {280 if (prevBranch != branchName) {281 // new branch let's flush.282 if (decl.Length()) decl.Remove(decl.Length()-1);283 br = TTree::Branch(prevBranch,0,decl);284 br->ResetAddress();285 286 (br->GetBasketEntry())[0] = 0;287 (br->GetBasketEntry())[1] = fEntries;288 289 br->SetEntries(fEntries);290 291 //++(br->fNBaskets);292 br->GetListOfBaskets()->AddAtAndExpand(CreateBasket(br),0);293 294 prevBranch = branchName;295 decl = "";296 }297 } else {298 prevBranch = branchName;299 }300 301 if(type.CompareTo("varchar",TString::kIgnoreCase)==0 || type.CompareTo("varchar2",TString::kIgnoreCase)==0 || type.CompareTo("char",TString::kIgnoreCase)==0 ) {302 char siz[6];303 snprintf(siz,6,"[%d]",prec);304 decl.Append( leafName+siz+"/C:" );305 }306 else if(type.CompareTo("int",TString::kIgnoreCase)==0){307 decl.Append( leafName+"/I:" );308 }309 else if( type.CompareTo("date",TString::kIgnoreCase)==0 ||310 type.CompareTo("time",TString::kIgnoreCase)==0 ||311 type.CompareTo("timestamp",TString::kIgnoreCase)==0 ) {312 decl.Append( leafName+"/I:" );313 }314 else if(type.CompareTo("bit",TString::kIgnoreCase)==0 ||315 type.CompareTo("tinyint",TString::kIgnoreCase)==0 ||316 type.CompareTo("smallint",TString::kIgnoreCase)==0 ) {317 decl.Append( leafName+"/i:" );318 }319 else if(type.CompareTo("real",TString::kIgnoreCase)==0 || type.CompareTo("longvarchar",TString::kIgnoreCase)==0 || type.CompareTo("longvarbinary",TString::kIgnoreCase)==0 || type.CompareTo("varbinary",TString::kIgnoreCase)==0 ){320 decl.Append( leafName+"/S:" );321 }322 323 // case kLONGVARCHAR: // not resolved yet how to handle324 // case kLONGVARBINARY:325 //case kVARBINARY:326 // break;327 else328 //if(type.CompareTo("bigint",TString::kIgnoreCase)==0 || type.CompareTo("decimal",TString::kIgnoreCase)==0 || type.CompareTo("numeric",TString::kIgnoreCase)==0 || type.CompareTo("double",TString::kIgnoreCase)==0 ||329 //type.CompareTo("float",TString::kIgnoreCase)==0 )330 {331 332 decl.Append( leafName+"/F:" );333 }334 335 }336 337 // new branch let's flush.338 if (decl.Length()) decl.Remove(decl.Length()-1);339 if (prevBranch.Length()) {340 br = TTree::Branch(prevBranch,0,decl);341 br->ResetAddress();342 343 (br->GetBasketEntry())[0] = 0;344 (br->GetBasketEntry())[1] = fEntries;345 br->SetEntries(fEntries);346 br->GetListOfBaskets()->AddAtAndExpand(CreateBasket(br),0);347 }348 349 if(!res.IsNull()) res.Resize(res.Length()-1); // cut off last ":"350 return res;351 }352 353 //______________________________________________________________________________354 void MTreeSQL::Init()355 {356 // Initializeation routine357 358 fCurrentEntry = -1;359 360 GetEntries();361 362 delete fResult;363 fResult = fServer->Query(("SELECT * FROM "+fQuery).Data());364 if(!fResult) return;365 366 for (auto it=fTables.begin(); it!=fTables.end(); it++)367 CreateBranches(fServer->GetColumns(fDB, *it), *it);368 }369 370 //______________________________________________________________________________371 355 Int_t MTreeSQL::Fill() 372 356 { … … 374 358 return -1; 375 359 } 376 377 //______________________________________________________________________________378 vector<Int_t> *MTreeSQL::GetColumnIndice(TBranch *branch)379 {380 // Return a vector of columns index corresponding to the381 // current SQL table and the branch given as argument382 // Returns 0 if no columns indices is found383 // Otherwise returns a pointer to a vector to be deleted by the caller384 385 //if (!CheckTable(fTable)) return 0;386 387 vector<Int_t> *columns = new vector<Int_t>;388 389 Int_t nl = branch->GetNleaves();390 391 vector<TString> names;392 pair<TString,Int_t> value;393 394 for (auto it=fTables.begin(); it!=fTables.end(); it++)395 {396 TSQLResult *rs = fServer->GetColumns(fDB, *it);397 if (rs==0) { delete columns; return 0; }398 Int_t rows = rs->GetRowCount();399 400 for (Int_t i=0;i<rows;++i) {401 TSQLRow *row = rs->Next();402 names.push_back( *it+"."+row->GetField(0) );403 delete row;404 }405 delete rs;406 }407 408 for(int j=0;j<nl;j++) {409 410 Int_t col = -1;411 TLeaf *leaf = (TLeaf*)branch->GetListOfLeaves()->UncheckedAt(j);412 TString leafName = leaf->GetName();413 TString str;414 415 str = "";416 str = branch->GetName();417 str += "__";418 str += leafName;419 for (UInt_t i=0;i<names.size();++i) {420 if (str.CompareTo(names[i],TString::kIgnoreCase)==0) {421 col = i;422 break;423 }424 }425 if (col<0) {426 str = leafName;427 for (UInt_t i=0;i<names.size();++i) {428 if (str.CompareTo(names[i],TString::kIgnoreCase)==0) {429 col = i;430 break;431 }432 }433 }434 if(col>=0){435 columns->push_back(col);436 } else Error("GetColumnIndice","Error finding column %d %s",j,str.Data());437 }438 if (columns->empty()) {439 delete columns; return 0;440 } else441 return columns;442 }443 444 //______________________________________________________________________________445 Long64_t MTreeSQL::GetEntries() const446 {447 // Get the number of rows in the database448 449 if (fServer==0) return GetEntriesFast();450 //if (!CheckTable(fTable.Data())) return 0;451 452 MTreeSQL* thisvar = const_cast<MTreeSQL*>(this);453 454 // What if the user already started to call GetEntry455 // What about the initial value of fEntries is it really 0?456 457 const TString counting = "SELECT COUNT(*) FROM " + fQuery;458 TSQLResult *count = fServer->Query(counting);459 460 if (count==0) {461 thisvar->fEntries = 0;462 } else {463 TSQLRow * row = count->Next();464 if (row) {465 TString val = row->GetField(0);466 Long_t ret;467 sscanf(val.Data(), "%ld",&(ret) );468 thisvar->fEntries = ret;469 } else {470 thisvar->fEntries = 0;471 }472 }473 return fEntries;474 }475 476 //______________________________________________________________________________477 Long64_t MTreeSQL::GetEntriesFast() const478 {479 // Return the number of entries as of the last check.480 // Use GetEntries for a more accurate count.481 482 return fEntries;483 }484 485 //______________________________________________________________________________486 Int_t MTreeSQL::GetEntry(Long64_t entry, Int_t getall)487 {488 // Load the data for the entry from the database.489 490 if (PrepEntry(entry)>=0) return TTree::GetEntry(entry,getall);491 else return -1;492 }493 494 //______________________________________________________________________________495 Long64_t MTreeSQL::LoadTree(Long64_t entry)496 {497 // Setup the tree to the load the specified entry.498 499 fReadEntry = entry;500 return PrepEntry(entry);501 }502 503 //______________________________________________________________________________504 Long64_t MTreeSQL::PrepEntry(Long64_t entry)505 {506 // Make sure the server and result set are setup for the requested entry507 508 if (entry < 0 || entry >= fEntries || fServer==0) return 0;509 fReadEntry = entry;510 511 if(entry == fCurrentEntry) return entry;512 513 if(entry < fCurrentEntry || fResult==0){514 delete fResult;515 fResult = fServer->Query(("SELECT * FROM "+fQuery).Data());516 fCurrentEntry = -1;517 }518 519 Bool_t reset = false;520 while ( fResult && fCurrentEntry < entry ) {521 ++fCurrentEntry;522 delete fRow;523 fRow = fResult->Next();524 if (fRow==0 && !reset) {525 delete fResult;526 fResult = fServer->Query(("SELECT * FROM "+fQuery).Data());527 fCurrentEntry = -1;528 reset = true;529 }530 }531 if (fRow==0) return -1;532 return entry;533 }534 /*535 //______________________________________________________________________________536 // void MTreeSQL::LoadNumberEntries()537 // {538 // R__ASSERT(0);539 540 // fResult = fServer->Query(fQuery.Data());541 // fEntries=0;542 543 // while(fResult->Next()){544 // fEntries++;545 // }546 // fResult = fServer->Query(fQuery.Data());547 // }548 */549 //______________________________________________________________________________550 void MTreeSQL::Refresh()551 {552 // Refresh contents of this Tree and his branches from the current553 // Tree status in the database554 // One can call this function in case the Tree on its file is being555 // updated by another process556 557 // Note : something to be done?558 GetEntries(); // Re-load the number of entries559 fCurrentEntry = -1;560 delete fResult; fResult = 0;561 delete fRow; fRow = 0;562 } -
trunk/Mars/msql/MTreeSQL.h
r15419 r15439 37 37 #include <vector> 38 38 39 class TSQLServer;39 class MSQLServer; 40 40 class TSQLRow; 41 41 42 class MTreeSQL : public TTree { 42 class MTreeSQL : public TTree 43 { 44 protected: 45 MSQLServer *fServer; 43 46 44 protected: 45 Int_t fCurrentEntry; 46 TString fDB; 47 TString fQuery; 48 TSQLResult *fResult; 49 TSQLRow *fRow; 50 TSQLServer *fServer; 51 std::vector<TString> fTables; 47 TString fQuery; 52 48 53 Bool_t CheckTable(const TString &table) const; 54 TString CreateBranches(TSQLResult * rs, const TString &table); 55 std::vector<Int_t> *GetColumnIndice(TBranch *branch); 56 void Init(); 57 virtual TBasket *CreateBasket(TBranch * br); 49 TSQLResult *fResult; 50 TSQLRow *fRow; 51 52 std::vector<TString> fTables; 53 std::vector<TSQLRow*> fRows; 54 55 Bool_t CheckTable(const TString &table) const; 56 void Init(); 58 57 59 58 virtual TBranch *BranchImp(const char *branchname, const char *classname, TClass *ptrClass, void *addobj, Int_t bufsize, Int_t splitlevel); … … 61 60 62 61 public: 63 MTreeSQL(TSQLServer * server, TString DB, const TString& table, const TString &addon=""); 62 MTreeSQL(MSQLServer *server, const TString& table, const TString &addon=""); 63 ~MTreeSQL(); 64 64 65 virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name=""); 66 virtual Int_t Branch(TList *list, Int_t bufsize=32000, Int_t splitlevel=99); 67 virtual Int_t Branch(const char *folder, Int_t bufsize=32000, Int_t splitlevel=99); 68 virtual TBranch *Bronch(const char *name, const char *classname, void *addobj, Int_t bufsize=32000, Int_t splitlevel=99); 69 virtual TBranch *BranchOld(const char *name, const char *classname, void *addobj, Int_t bufsize=32000, Int_t splitlevel=1); 65 Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name=""); 66 Int_t Branch(TList *list, Int_t bufsize=32000, Int_t splitlevel=99); 67 Int_t Branch(const char *folder, Int_t bufsize=32000, Int_t splitlevel=99); 68 TBranch *Bronch(const char *name, const char *classname, void *addobj, Int_t bufsize=32000, Int_t splitlevel=99); 69 TBranch *BranchOld(const char *name, const char *classname, void *addobj, Int_t bufsize=32000, Int_t splitlevel=1); 70 TBranch *Branch(const char *name, void *address, const char *leaflist, Int_t bufsize) { return TTree::Branch(name, address, leaflist, bufsize); } 70 71 #if !defined(__CINT__) 71 virtual TBranch*Branch(const char *name, const char *classname, void *addobj, Int_t bufsize=32000, Int_t splitlevel=99);72 TBranch *Branch(const char *name, const char *classname, void *addobj, Int_t bufsize=32000, Int_t splitlevel=99); 72 73 #endif 73 74 74 virtual TBranch *Branch(const char *name, void *address, const char *leaflist, Int_t bufsize); 75 Int_t Fill(); 76 Long64_t GetEntries() const; 77 Long64_t GetEntries(const char *sel) { return TTree::GetEntries(sel); } 78 Long64_t GetEntriesFast()const; 79 Long64_t LoadTree(Long64_t entry); 75 80 76 virtual Int_t Fill(); 77 virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0); 78 virtual Long64_t GetEntries() const; 79 virtual Long64_t GetEntries(const char *sel) { return TTree::GetEntries(sel); } 80 virtual Long64_t GetEntriesFast()const; 81 virtual Long64_t LoadTree(Long64_t entry); 82 virtual Long64_t PrepEntry(Long64_t entry); 83 void Refresh(); 81 void Refresh(); 84 82 85 83 ClassDef(MTreeSQL,1); // TTree Implementation read and write to a SQL database.
Note:
See TracChangeset
for help on using the changeset viewer.