- Timestamp:
- 08/11/18 20:10:40 (6 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/fits2sql.cc
r19150 r19151 67 67 ("ignore-errors", po_switch(), "Adds the IGNORE keyword to the INSERT query (turns errors into warnings, ignores rows with errors)") 68 68 ("const.*", var<string>(), "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/..../N/`") 69 ("conditional", po_switch(), "Conditional insert. Only insert if no entry exists yet with the constants defined by --const") 69 70 ("delete", po_switch(), "Delete all entries first which fit all constant columns defined by --const") 70 71 ; … … 79 80 ("print-insert", po_switch(), "Print the INSERT query (note that it contains all data)") 80 81 ("print-create", po_switch(), "Print the CREATE query") 82 ("print-select", po_switch(), "Print the SELECT query for the conditional execution") 81 83 ("print-delete", po_switch(), "Print the DELETE query") 82 84 ("verbose,v", var<uint16_t>(1), "Verbosity (0: quiet, 1: default, 2: more, 3, ...)") … … 192 194 "data from the table before inserting new data which fulfills the condition " 193 195 "defined by the `--const` directives.\n" 196 "\n" 197 "The constant values can also be used for a conditional execution (--conditional). " 198 "If any row with the given constant values are found, the execution is stopped " 199 "(note that this happend after the table drop/create but before the delete/insert.\n" 194 200 "\n" 195 201 "If a query failed, the query is printed to stderr together with the error message. " … … 318 324 const bool noinsert = conf.Get<bool>("no-insert"); 319 325 const bool dry_run = conf.Get<bool>("dry-run"); 326 const bool conditional = conf.Get<bool>("conditional"); 320 327 const bool run_delete = conf.Get<bool>("delete"); 321 328 … … 330 337 const bool print_create = conf.Get<bool>("print-create"); 331 338 const bool print_insert = conf.Get<bool>("print-insert"); 339 const bool print_select = conf.Get<bool>("print-select"); 332 340 const bool print_delete = conf.Get<bool>("print-delete"); 333 341 … … 405 413 const auto fixed = conf.GetWildcardOptions("const.*"); 406 414 407 string rmquery = "DELETE FROM `"+table+"` WHERE 1";415 string where; 408 416 for (auto it=fixed.cbegin(); it!=fixed.cend(); it++) 409 417 { … … 443 451 444 452 vec.emplace_back(name, val); 445 rmquery+= " AND `"+name+"`="+val;453 where += " AND `"+name+"`="+val; 446 454 } 447 455 … … 635 643 try 636 644 { 645 if (conditional && !fixed.empty() && !drop) 646 { 647 const mysqlpp::StoreQueryResult res = 648 connection.query("SELECT 1 FROM `"+table+"` WHERE 1"+where+" LIMIT 1").store(); 649 650 if (res.num_rows()>0) 651 { 652 if (verbose>0) 653 { 654 cout << "Conditional execution... detected existing rows!\n"; 655 cout << "Exit." << endl; 656 } 657 return 0; 658 } 659 } 660 } 661 catch (const exception &e) 662 { 663 cerr << "SELECT 1 FROM `"+table+"` WHERE 1" << where << " LIMIT 1\n\n"; 664 cerr << "SQL query failed: " << e.what() << endl; 665 return 7; 666 } 667 668 if (print_select) 669 cout << "SELECT 1 FROM `"+table+"` WHERE 1" << where << " LIMIT 1" << endl; 670 671 672 try 673 { 637 674 if (run_delete && !fixed.empty() && !drop && !dry_run) 638 675 { 639 676 const mysqlpp::SimpleResult res = 640 connection.query( rmquery).execute();677 connection.query("DELETE FROM `"+table+"` WHERE 1"+where).execute(); 641 678 642 679 if (verbose>0) … … 646 683 catch (const exception &e) 647 684 { 648 cerr << rmquery<< "\n\n";685 cerr << "DELETE FROM `"+table+"` WHERE 1" << where << "\n\n"; 649 686 cerr << "SQL query failed: " << e.what() << endl; 650 687 return 7; … … 652 689 653 690 if (print_delete) 654 cout << rmquery<< endl;691 cout << "DELETE FROM `"+table+"` WHERE 1" << where << endl; 655 692 656 693 // ------------------------------------------------------------------------- -
trunk/FACT++/src/root2sql.cc
r19150 r19151 70 70 ("ignore-errors", po_switch(), "Adds the IGNORE keyword to the INSERT query (turns errors into warnings, ignores rows with errors)") 71 71 ("const.*", var<string>(), "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/..../N/`") 72 ("conditional", po_switch(), "Conditional insert. Only insert if no entry exists yet with the constants defined by --const") 72 73 ("delete", po_switch(), "Delete all entries first which fit all constant columns defined by --const") 73 74 ; … … 82 83 ("print-insert", po_switch(), "Print the INSERT query (note that it contains all data)") 83 84 ("print-create", po_switch(), "Print the CREATE query") 85 ("print-select", po_switch(), "Print the SELECT query for the conditional execution") 84 86 ("print-delete", po_switch(), "Print the DELETE query") 85 87 ("verbose,v", var<uint16_t>(1), "Verbosity (0: quiet, 1: default, 2: more, 3, ...)") … … 199 201 "data from the table before inserting new data which fulfills the condition " 200 202 "defined by the `--const` directives.\n" 203 "\n" 204 "The constant values can also be used for a conditional execution (--conditional). " 205 "If any row with the given constant values are found, the execution is stopped " 206 "(note that this happend after the table drop/create but before the delete/insert.\n" 201 207 "\n" 202 208 "If a query failed, the query is printed to stderr together with the error message. " … … 347 353 const bool noinsert = conf.Get<bool>("no-insert"); 348 354 const bool dry_run = conf.Get<bool>("dry-run"); 355 const bool conditional = conf.Get<bool>("conditional"); 349 356 const bool run_delete = conf.Get<bool>("delete"); 350 357 … … 361 368 const bool print_create = conf.Get<bool>("print-create"); 362 369 const bool print_insert = conf.Get<bool>("print-insert"); 370 const bool print_select = conf.Get<bool>("print-select"); 363 371 const bool print_delete = conf.Get<bool>("print-delete"); 364 372 … … 428 436 const auto fixed = conf.GetWildcardOptions("const.*"); 429 437 430 string rmquery = "DELETE FROM `"+table+"` WHERE 1";438 string where; 431 439 for (auto it=fixed.cbegin(); it!=fixed.cend(); it++) 432 440 { … … 466 474 467 475 vec.emplace_back(name, val); 468 rmquery+= " AND `"+name+"`="+val;476 where += " AND `"+name+"`="+val; 469 477 } 470 478 … … 671 679 try 672 680 { 681 if (conditional && !fixed.empty() && !drop) 682 { 683 const mysqlpp::StoreQueryResult res = 684 connection.query("SELECT 1 FROM `"+table+"` WHERE 1"+where+" LIMIT 1").store(); 685 686 if (res.num_rows()>0) 687 { 688 if (verbose>0) 689 { 690 cout << "Conditional execution... detected existing rows!\n"; 691 cout << "Exit." << endl; 692 } 693 return 0; 694 } 695 } 696 } 697 catch (const exception &e) 698 { 699 cerr << "SELECT 1 FROM `"+table+"` WHERE 1" << where << " LIMIT 1\n\n"; 700 cerr << "SQL query failed: " << e.what() << endl; 701 return 7; 702 } 703 704 if (print_select) 705 cout << "SELECT 1 FROM `"+table+"` WHERE 1" << where << " LIMIT 1" << endl; 706 707 try 708 { 673 709 if (run_delete && !fixed.empty() && !drop && !dry_run) 674 710 { 675 711 const mysqlpp::SimpleResult res = 676 connection.query( rmquery).execute();712 connection.query("DELETE FROM `"+table+"` WHERE 1"+where).execute(); 677 713 678 714 if (verbose>0) … … 682 718 catch (const exception &e) 683 719 { 684 cerr << rmquery<< "\n\n";720 cerr << "DELETE FROM `"+table+"` WHERE 1" << where << "\n\n"; 685 721 cerr << "SQL query failed: " << e.what() << endl; 686 722 return 7; … … 688 724 689 725 if (print_delete) 690 cout << rmquery<< endl;726 cout << "DELETE FROM `"+table+"` WHERE 1" << where << endl; 691 727 692 728
Note:
See TracChangeset
for help on using the changeset viewer.