- Timestamp:
- 07/31/18 20:57:08 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/calcsource.cc
r19112 r19113 36 36 ("drop", po_switch(), "Drop the table (implies create)") 37 37 ("create", po_switch(), "Create the table if it does not yet exist") 38 ("update", po_switch(), "U pdate the table.position instead of inserting it (disables drop, create and delete)")38 ("update", po_switch(), "Uses the ON DUPLICATE KEY to update already existing extries") 39 39 ("ra", var<double>(), "Right ascension of the source (use together with --dec)") 40 40 ("dec", var<double>(), "Declination of the source (use together with --ra)") … … 322 322 ins << setprecision(16); 323 323 324 vector<string> upd;325 326 324 size_t count = 0; 327 325 while (auto row=res1.fetch_row()) … … 475 473 */ 476 474 477 if (!update) 478 ins << "( " << file << ", " << event << ", " << v.X() << ", " << v.Y() << " ),\n"; 479 else 480 { 481 ostringstream out; 482 out << setprecision(16); 483 out << "UPDATE `" << tab_position << "` SET X=" << v.X() << ", Y=" << v.Y() << " WHERE FileId=" << file << " AND EvtNumber=" << event; 484 upd.emplace_back(out.str()); 485 } 475 ins << "( " << file << ", " << event << ", " << v.X() << ", " << v.Y() << " ),\n"; 486 476 } 487 477 … … 505 495 // drop table if requested 506 496 507 if (drop && !update)497 if (drop) 508 498 { 509 499 try … … 529 519 // crate table if requested 530 520 531 if (create && !update)521 if (create) 532 522 { 533 523 if (verbose>0) … … 572 562 // delete old entries from table 573 563 574 if (!drop && !update)564 if (!drop) 575 565 { 576 566 if (verbose>0) … … 605 595 // insert data into table 606 596 607 if (!update) 608 { 609 if (verbose>0) 610 cout << "Inserting data into table " << tab_position << "." << endl; 611 612 string query2 = "INSERT "; 613 if (ignore_errors) 614 query2 += "IGNORE "; 615 query2 += "`"+tab_position+"` (FileId, EvtNumber, X, Y) VALUES\n"+ 616 ins.str().substr(0, ins.str().size()-2)+ 617 "\n"; 618 619 try 620 { 621 if (!noinsert) 622 { 623 const mysqlpp::SimpleResult res = 624 connection.query(query2).execute(); 625 626 if (verbose>0) 627 cout << res.info() << '\n' << endl; 628 } 629 } 630 catch (const exception &e) 631 { 632 cerr << query2 << "\n\n"; 633 cerr << "SQL query (" << query2.length() << " bytes) failed:\n" << e.what() << endl; 634 return 8; 635 } 636 637 if (print_insert) 638 cout << query2 << endl; 639 } 640 641 642 643 // ------------------------------------------------------------------------- 644 // update data in table 645 597 if (verbose>0) 598 cout << "Inserting data into table " << tab_position << "." << endl; 599 600 string query2 = "INSERT "; 601 if (ignore_errors) 602 query2 += "IGNORE "; 603 query2 += "`"+tab_position+"` (FileId, EvtNumber, X, Y) VALUES\n"+ 604 ins.str().substr(0, ins.str().size()-2)+ 605 "\n"; 646 606 if (update) 647 { 648 if (verbose>0) 649 cout << "Updating data in table " << tab_position << "." << endl; 650 651 size_t cnt = 0; 652 for (const auto &str : upd) 653 { 654 try 655 { 656 if (!noinsert && !dry_run) 657 { 658 const mysqlpp::SimpleResult res = 659 connection.query(str).execute(); 660 661 cnt += res.rows(); 662 } 663 } 664 catch (const exception &e) 665 { 666 cerr << str << "\n\n"; 667 cerr << "SQL query failed:\n" << e.what() << endl; 668 return 9; 669 } 670 } 671 672 if (verbose>0) 673 cout << cnt << " row(s) out of " << upd.size() << " affected.\n" << endl; 674 675 if (print_insert) 676 { 677 for (const auto &str : upd) 678 cout << str << '\n'; 679 cout << endl; 680 } 681 } 607 query2 += "ON DUPLICATE KEY X=VALUES(X), Y=VALUES(Y)\n"; 608 609 try 610 { 611 if (!noinsert) 612 { 613 const mysqlpp::SimpleResult res = 614 connection.query(query2).execute(); 615 616 if (verbose>0) 617 cout << res.info() << '\n' << endl; 618 } 619 } 620 catch (const exception &e) 621 { 622 cerr << query2 << "\n\n"; 623 cerr << "SQL query (" << query2.length() << " bytes) failed:\n" << e.what() << endl; 624 return 8; 625 } 626 627 if (print_insert) 628 cout << query2 << endl; 682 629 683 630 if (verbose>0)
Note:
See TracChangeset
for help on using the changeset viewer.