Changeset 19146
- Timestamp:
- 08/10/18 22:54:57 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/root2sql.cc
r19139 r19146 69 69 ("duplicate", vars<string>(), "Specifies an assignment_list for an 'ON DUPLICATE KEY UPDATE' expression") 70 70 ("ignore-errors", po_switch(), "Adds the IGNORE keyword to the INSERT query (turns errors into warnings, ignores rows with errors)") 71 ("const.*", var<string>(), "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/..../N/`") 71 72 ; 72 73 … … 183 184 "are marked as (-ignored-).\n" 184 185 "\n" 186 "A constant value for the given file can be inserted by using the --const directive. " 187 "For example --const.mycolumn=42 would insert 42 into a column called mycolumn. " 188 "The column is created as INT UNSIGNED as default which can be altered by " 189 "--sql-type. A special case is a value of the form `/regex/N/`. Here, the given " 190 "regular expression is applied to the filename and N specifies the N-th " 191 "sub-sequence which matches. To debug what matches, verbosity can be set to 3.\n" 192 "\n" 185 193 "If a query failed, the query is printed to stderr together with the error message. " 186 194 "For the main INSERT query, this is only true if the verbosity level is at least 2 " … … 198 206 { 199 207 kNone = 0, 208 kConst, 200 209 kFloat, 201 210 kDouble, … … 222 231 struct Container 223 232 { 233 static map<void*, size_t> counter; 234 224 235 string branch; // branch name 225 236 string column; // column name 226 237 BasicType_t type; 238 size_t num; 227 239 void *ptr; 228 240 229 Container(const string &b, const string &c, const BasicType_t &t ) : branch(b), column(c), type(t), ptr(0)241 Container(const string &b, const string &c, const BasicType_t &t, const size_t &n) : branch(b), column(c), type(t), num(n), ptr(0) 230 242 { 231 243 switch (t) 232 244 { 233 case kFloat: ptr = new Float_t; break; 234 case kDouble: ptr = new Double_t; break; 235 case kInt16: ptr = new Short_t; break; 236 case kUInt16: ptr = new UShort_t; break; 237 case kInt32: ptr = new Int_t; break; 238 case kUInt32: ptr = new UInt_t; break; 239 case kInt64: ptr = new Long64_t; break; 240 case kUInt64: ptr = new ULong64_t; break; 245 case kFloat: ptr = new Float_t[n]; break; 246 case kDouble: ptr = new Double_t[n]; break; 247 case kInt16: ptr = new Short_t[n]; break; 248 case kUInt16: ptr = new UShort_t[n]; break; 249 case kInt32: ptr = new Int_t[n]; break; 250 case kUInt32: ptr = new UInt_t[n]; break; 251 case kInt64: ptr = new Long64_t[n]; break; 252 case kUInt64: ptr = new ULong64_t[n]; break; 253 case kConst: 241 254 case kNone: 242 255 break; 243 256 } 244 } 257 counter[ptr]++; 258 } 259 Container(const string &c, const string &value) : branch(value), column(c), type(kConst), num(1), ptr(0) 260 { 261 } 262 263 Container(const Container &c) : branch(c.branch), column(c.column), type(c.type), num(c.num), ptr(c.ptr) 264 { 265 counter[ptr]++; 266 } 267 245 268 ~Container() 246 269 { 247 //::operator delete(ptr); // It seems root is deleting it already 248 } 249 250 string fmt() const 270 counter[ptr]--; 271 if (counter[ptr]==0) 272 ::operator delete[](ptr); // It seems root is deleting it already 273 } 274 275 string fmt(const size_t &index) const 251 276 { 252 277 ostringstream str; … … 254 279 switch (type) 255 280 { 256 case kFloat: str << setprecision(8) << *reinterpret_cast<Float_t*>(ptr); break; 257 case kDouble: str << setprecision(16) << *reinterpret_cast<Double_t*>(ptr); break; 258 case kInt16: str << *reinterpret_cast<Short_t*>(ptr); break; 259 case kUInt16: str << *reinterpret_cast<UShort_t*>(ptr); break; 260 case kInt32: str << *reinterpret_cast<Int_t*>(ptr); break; 261 case kUInt32: str << *reinterpret_cast<UInt_t*>(ptr); break; 262 case kInt64: str << *reinterpret_cast<Long64_t*>(ptr); break; 263 case kUInt64: str << *reinterpret_cast<ULong64_t*>(ptr); break; 281 case kFloat: str << setprecision(8) << reinterpret_cast<Float_t*>(ptr)[index]; break; 282 case kDouble: str << setprecision(16) << reinterpret_cast<Double_t*>(ptr)[index]; break; 283 case kInt16: str << reinterpret_cast<Short_t*>(ptr)[index]; break; 284 case kUInt16: str << reinterpret_cast<UShort_t*>(ptr)[index]; break; 285 case kInt32: str << reinterpret_cast<Int_t*>(ptr)[index]; break; 286 case kUInt32: str << reinterpret_cast<UInt_t*>(ptr)[index]; break; 287 case kInt64: str << reinterpret_cast<Long64_t*>(ptr)[index]; break; 288 case kUInt64: str << reinterpret_cast<ULong64_t*>(ptr)[index]; break; 289 case kConst: str << branch; break; 264 290 case kNone: 265 291 break; … … 272 298 } 273 299 }; 300 301 map<void*, size_t> Container::counter; 274 302 275 303 void ErrorHandlerAll(Int_t level, Bool_t abort, const char *location, const char *msg) … … 387 415 vector<Container> vec; 388 416 417 const auto fixed = conf.GetWildcardOptions("const.*"); 418 419 for (auto it=fixed.cbegin(); it!=fixed.cend(); it++) 420 { 421 const string name = it->substr(6); 422 string val = conf.Get<string>(*it); 423 424 boost::smatch match; 425 if (boost::regex_match(val, match, boost::regex("\\/(.+)\\/([0-9]*)\\/"))) 426 { 427 string s = match[1]; 428 size_t r = atoi(match[2].str().c_str()); 429 430 if (boost::regex_search(file, match, boost::regex(s))) 431 { 432 if (verbose>2) 433 for (size_t i=0; i<match.size(); i++) 434 cout << "Regex match " << setw(2) << i << ": `" << match[i] << "`" << endl; 435 436 val = match[r]; 437 } 438 } 439 440 if (verbose>2) 441 cout << "\n" << val << " [-const-]"; 442 if (verbose>1) 443 cout << " (" << name << ")"; 444 445 string sqltype = "INT UNSIGNED"; 446 447 for (auto m=sqltypes.cbegin(); m!=sqltypes.cend(); m++) 448 if (m->first==name) 449 sqltype = m->second; 450 451 if (!vec.empty()) 452 query += ",\n"; 453 query += " `"+name+"` "+sqltype+" NOT NULL COMMENT '--user--'"; 454 455 vec.emplace_back(name, val); 456 } 457 458 const size_t nvec = vec.size(); 389 459 390 460 TIter Next(leaves); … … 395 465 396 466 if (verbose>2) 397 cout << '\n' << o->GetName() << " [" << L->GetTypeName() << "]"; 467 cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}"; 468 469 if (L->GetLenStatic()=!L->GetLen()) 470 { 471 if (verbose>2) 472 cout << " (-skipped-)"; 473 continue; 474 } 398 475 399 476 string name = o->GetName(); 400 401 477 402 478 bool found = false; … … 425 501 426 502 if (verbose==2) 427 cout << '\n' << o->GetName() << " [" << L->GetTypeName() << "]";503 cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}"; 428 504 429 505 for (auto m=mymap.cbegin(); m!=mymap.cend(); m++) … … 441 517 if (!vec.empty()) 442 518 query += ",\n"; 443 query += " `"+name+"` "+sqltype+" NOT NULL COMMENT '"+o->GetName()+"'"; 444 445 vec.emplace_back(o->GetName(), name, it->second.first); 446 T->SetBranchAddress(o->GetName(), vec.back().ptr); 519 520 const size_t N = L->GetLenStatic(); 521 for (size_t i=0; i<N; i++) 522 { 523 query += " `"+name; 524 if (N>1) 525 query += "["+to_string(i)+"]"; 526 query += "` "+sqltype+" NOT NULL COMMENT '"+o->GetTitle()+"'"; 527 if (N>1 && i!=N-1) 528 query += ",\n"; 529 } 530 531 vec.emplace_back(o->GetTitle(), name, it->second.first, L->GetLenStatic()); 532 T->SetBranchAddress(o->GetTitle(), vec.back().ptr); 447 533 } 448 534 … … 450 536 cout << "\n\n"; 451 537 if (verbose>0) 452 cout << vec.size() << " leaves setup for reading." << endl; 538 { 539 if (nvec>0) 540 cout << nvec << " constant value column(s) configured." << endl; 541 cout << vec.size()-nvec << " leaf/leaves setup for reading." << endl; 542 } 453 543 454 544 UInt_t datatype = 0; … … 458 548 T->SetBranchStatus("*", 0); 459 549 for (auto c=vec.cbegin(); c!=vec.cend(); c++) 460 T->SetBranchStatus(c->branch.c_str(), 1); 550 if (c->type!=kConst) 551 T->SetBranchStatus(c->branch.c_str(), 1); 461 552 462 553 if (has_datatype) … … 467 558 } 468 559 560 469 561 // ------------------------------------------------------------------------- 470 562 // Checking for database connection … … 513 605 514 606 if (!primary.empty()) 515 query += ",\n PRIMARY KEY USING BTREE (`"+boost::algorithm::join(primary, "`, `")+"`) \n";607 query += ",\n PRIMARY KEY USING BTREE (`"+boost::algorithm::join(primary, "`, `")+"`)"; 516 608 517 609 query += 518 " )\n"610 "\n)\n" 519 611 "DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci\n"; 520 612 if (!engine.empty()) … … 578 670 if (c!=vec.cbegin()) 579 671 query += ",\n"; 580 query += " `"+c->column+"`"; 672 673 const size_t N = c->num; 674 for (size_t i=0; i<N; i++) 675 { 676 if (N==1) 677 query += " `"+c->column+"`"; 678 else 679 query += " `"+c->column+"["+to_string(i)+"]`"; 680 681 if (N>1 && i!=N-1) 682 query += ",\n"; 683 } 581 684 } 582 685 … … 604 707 query += ",\n"; 605 708 606 query += " "+c->fmt(); 607 608 if (print_insert) 609 query += " /* "+c->column+" -> "+c->branch+" */"; 709 const size_t N = c->num; 710 for (size_t i=0; i<N; i++) 711 { 712 query += " "+c->fmt(i); 713 714 if (print_insert && i==0) 715 query += " /* "+c->column+" -> "+c->branch+" */"; 716 717 if (N>1 && i!=N-1) 718 query += ",\n"; 719 } 610 720 } 611 721 query += "\n)";
Note:
See TracChangeset
for help on using the changeset viewer.