Changeset 19147 for trunk/FACT++
- Timestamp:
- 08/10/18 22:56:13 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/fits2sql.cc
r19140 r19147 66 66 ("duplicate", vars<string>(), "Specifies an assignment_list for an 'ON DUPLICATE KEY UPDATE' expression") 67 67 ("ignore-errors", po_switch(), "Adds the IGNORE keyword to the INSERT query (turns errors into warnings, ignores rows with errors)") 68 ("const.*", var<string>(), "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/..../N/`") 68 69 ; 69 70 … … 176 177 "are marked as (-ignored-).\n" 177 178 "\n" 179 "A constant value for the given file can be inserted by using the --const directive. " 180 "For example --const.mycolumn=42 would insert 42 into a column called mycolumn. " 181 "The column is created as INT UNSIGNED as default which can be altered by " 182 "--sql-type. A special case is a value of the form `/regex/N/`. Here, the given " 183 "regular expression is applied to the filename and N specifies the N-th " 184 "sub-sequence which matches. To debug what matches, verbosity can be set to 3.\n" 185 "\n" 178 186 "If a query failed, the query is printed to stderr together with the error message. " 179 187 "For the main INSERT query, this is only true if the verbosity level is at least 2 " … … 191 199 { 192 200 kNone = 0, 201 kConst, 193 202 kVarchar, 194 203 kBool, … … 238 247 { 239 248 } 249 Container(const string &c, const string &value) : branch(value), column(c), type(kConst), num(1), ptr(0) 250 { 251 } 240 252 ~Container() 241 253 { … … 262 274 case kInt64: str << reinterpret_cast<int64_t*>(ptr)[index]; break; 263 275 case kUInt64: str << reinterpret_cast<uint64_t*>(ptr)[index]; break; 276 case kConst: str << branch; break; 264 277 case kNone: 265 278 break; … … 379 392 vector<Container> vec; 380 393 381 for (const auto &ic : cols) 382 { 383 const auto &col = ic.second; 394 const auto fixed = conf.GetWildcardOptions("const.*"); 395 396 for (auto it=fixed.cbegin(); it!=fixed.cend(); it++) 397 { 398 const string name = it->substr(6); 399 string val = conf.Get<string>(*it); 400 401 boost::smatch match; 402 if (boost::regex_match(val, match, boost::regex("\\/(.+)\\/([0-9]*)\\/"))) 403 { 404 string s = match[1]; 405 size_t r = atoi(match[2].str().c_str()); 406 407 if (boost::regex_search(file, match, boost::regex(s))) 408 { 409 if (verbose>2) 410 for (size_t i=0; i<match.size(); i++) 411 cout << "Regex match " << setw(2) << i << ": `" << match[i] << "`" << endl; 412 413 val = match[r]; 414 } 415 } 416 417 if (verbose==2) 418 cout << "\n" << val << " [-const-]"; 419 if (verbose>1) 420 cout << " (" << name << ")"; 421 422 string sqltype = "INT UNSIGNED"; 423 424 for (auto m=sqltypes.cbegin(); m!=sqltypes.cend(); m++) 425 if (m->first==name) 426 sqltype = m->second; 427 428 if (!vec.empty()) 429 query += ",\n"; 430 query += " `"+name+"` "+sqltype+" NOT NULL COMMENT '--user--'"; 431 432 vec.emplace_back(name, val); 433 } 434 435 const size_t nvec = vec.size(); 436 437 for (auto ic=cols.cbegin(); ic!=cols.cend(); ic++) 438 { 439 const auto &col = ic->second; 384 440 385 441 if (verbose>2) 386 cout << '\n' << col.type << " " << ic .first << "[" << col.num << "]";387 388 string name = ic .first;442 cout << '\n' << col.type << " " << ic->first << "[" << col.num << "]"; 443 444 string name = ic->first; 389 445 390 446 bool found = false; … … 402 458 continue; 403 459 404 const char tn = find(notsigned.cbegin(), notsigned.cend(), ic .first)!=notsigned.cend() ?460 const char tn = find(notsigned.cbegin(), notsigned.cend(), ic->first)!=notsigned.cend() ? 405 461 tolower(col.type) : toupper(col.type); 406 462 … … 436 492 query += " `"+name; 437 493 if (N>1) 438 query += "["+to_string( i)+"]";494 query += "["+to_string((long long int)i)+"]"; 439 495 query += "` "+sqltype; 440 496 if (col.type=='A') 441 query += '('+to_string( col.num)+')';442 query += " NOT NULL COMMENT '"+ic .first;497 query += '('+to_string((long long int)col.num)+')'; 498 query += " NOT NULL COMMENT '"+ic->first; 443 499 if (!col.unit.empty()) 444 500 query += "["+col.unit+"]"; … … 454 510 kMJD :*/ it->second.first; 455 511 456 vec.emplace_back(ic .first, name, bt, col.num/*, mjdref*/);457 vec.back().ptr = f.SetPtrAddress(ic .first);512 vec.emplace_back(ic->first, name, bt, col.num/*, mjdref*/); 513 vec.back().ptr = f.SetPtrAddress(ic->first); 458 514 } 459 515 … … 461 517 cout << "\n\n"; 462 518 if (verbose>0) 463 cout << vec.size() << " columns setup for reading." << endl; 519 { 520 if (nvec>0) 521 cout << nvec << " constant value column(s) configured." << endl; 522 cout << vec.size()-nvec << " FITS columns setup for reading." << endl; 523 } 464 524 465 525 // ------------------------------------------------------------------------- … … 509 569 510 570 if (!primary.empty()) 511 query += ",\n PRIMARY KEY USING BTREE (`"+boost::algorithm::join(primary, "`, `")+"`) \n";571 query += ",\n PRIMARY KEY USING BTREE (`"+boost::algorithm::join(primary, "`, `")+"`)"; 512 572 513 573 query += 514 " )\n"574 "\n)\n" 515 575 "DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci\n"; 516 576 if (!engine.empty()) … … 573 633 for (auto c=vec.cbegin(); c!=vec.cend(); c++) 574 634 { 635 if (c!=vec.cbegin()) 636 query += ",\n"; 637 575 638 const size_t N = c->type==kVarchar ? 1 : c->num; 576 639 for (size_t i=0; i<N; i++) 577 640 { 578 if (c!=vec.cbegin())579 query += ",\n";580 581 641 if (N==1) 582 642 query += " `"+c->column+"`"; 583 643 else 584 query += " `"+c->column+"["+to_string( i)+"]`";644 query += " `"+c->column+"["+to_string((long long int)i)+"]`"; 585 645 586 646 if (N>1 && i!=N-1) … … 607 667 for (auto c=vec.cbegin(); c!=vec.cend(); c++) 608 668 { 669 if (c!=vec.cbegin()) 670 query += ",\n"; 671 609 672 const size_t N = c->type==kVarchar ? 1 : c->num; 610 673 for (size_t i=0; i<N; i++) 611 674 { 612 if (c!=vec.cbegin())613 query += ",\n";614 615 675 if (c->type==kVarchar) 616 676 query += " '"+c->fmt(i)+"'";
Note:
See TracChangeset
for help on using the changeset viewer.