1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
|
/*
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#ifdef _WIN32
#define HAVE_ROUND
#endif
#ifdef __APPLE__
// All the functions in sql.h are deprecated, but we have no replacement atm.
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include "python_copy_data_source.h" // python stuff need to be 1st #include
#include "copytable.h"
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include "base/log.h"
#include "base/sqlstring.h"
#include "boost/scoped_ptr.hpp"
#undef tolower
#undef toupper
#include "base/string_utilities.h"
#include "base/file_utilities.h"
#include "workbench/wb_version.h"
class input_error : public std::runtime_error
{
public:
input_error(const std::string &what) : std::runtime_error(what) {}
};
DEFAULT_LOG_DOMAIN("copytable");
static void count_rows(boost::scoped_ptr<CopyDataSource>& source, const std::string &source_schema,
const std::string &source_table, const std::vector<std::string> &pk_columns,
const CopySpec &spec, const std::vector<std::string> &last_pkeys)
{
unsigned long long total = source->count_rows(source_schema, source_table, pk_columns, spec, last_pkeys);
printf("ROW_COUNT:%s:%s: %llu\n", source_schema.c_str(), source_table.c_str(), total);
fflush(stdout);
}
//-----------------------
static bool set_log_level(const std::string& value)
{
std::string level = base::tolower(value);
bool ret = base::Logger::active_level(level);
if (ret) // TODO: if the logger is set to error or warning the following log call won't do anything.
log_info("Logger set to level '%s'. '%s'\n", level.c_str(), base::Logger::get_state().c_str());
return ret;
}
static bool check_arg_with_value(char **argv, int &argi, const char *arg, char *&value, bool arg_required)
{
char *a = argv[argi];
if (strcmp(a, arg) == 0)
{
// value must be in next arg
if (argv[argi+1] != NULL)
{
++argi;
value = argv[argi];
}
else
{
value = NULL;
if (arg_required)
{
fprintf(stderr, "Missing argument for option %s\n", argv[argi]);
return false;
}
}
return true;
}
else if (strncmp(a, arg, strlen(arg)) == 0 && a[strlen(arg)] == '=')
{
// value must be after =
value = a + strlen(arg)+1;
return true;
}
return false;
}
static bool parse_mysql_connstring(const std::string &connstring,
std::string &user, std::string &password,
std::string &host, int &port, std::string &sock)
{
// format is user[:pass]@host:port or user[:pass]@::socket, like what cmdline utilities use
std::string::size_type p = connstring.rfind('@');
if (p == std::string::npos)
return false;
std::string user_part = connstring.substr(0, p);
std::string server_part = connstring.substr(p+1);
if ((p = user_part.find(':')) != std::string::npos)
{
user = user_part.substr(0, p);
password = user_part.substr(p+1);
}
else
user = user_part;
p = server_part.find(':');
if (p != std::string::npos)
{
host = server_part.substr(0, p);
server_part = server_part.substr(p+1);
p = server_part.find(':');
if (p != std::string::npos)
sock = server_part.substr(p+1);
else
if (!sscanf(server_part.substr(0, p).c_str(), "%i", &port))
return false;
}
else
host = server_part;
return true;
}
static void show_help()
{
printf("copytable --*-source=<source db> --target=<target db> <options> <table spec> [<table spec> ...]\n");
printf("--odbc-source=<odbc connstring>\n");
printf("--pythondbapi-source=<python connstring>\n");
printf("--mysql-source=<mysql connstring>\n");
printf("--source-password=<password>\n");
printf("--target=<mysql connstring>\n");
printf("--target-password=<password>\n");
printf("--force-utf8-for-source\n");
printf("--truncate-target\n");
printf("--progress\n");
printf("--count-only\n");
printf("--jobs-from-stdin\n");
printf("--abort-on-oversized-blobs\n");
printf("--max-count=<max rows count>\n");
printf("--resume\n");
printf("Table Specification from file:\n");
printf("--table-file=<filename>\n");
printf("<source schema><TAB><source table><TAB><target schema><TAB><target table><TAB><source pk columns><TAB><target pk columns><TAB>*|<select expression>\n");
printf("Table Specification from command line:\n");
printf("--table <source schema> <source table> <target schema> <target table> <source pk columns> <target pk columns> *|<select expression>\n");
printf("--table-where <source schema> <source table> <target schema> <target table> <source pk columns> <target pk columns> *|<select expression> <where expression>\n");
printf("\n");
printf("--log-file=<file_path>\n");
printf("--log-level=<level>\n");
printf("--thread-count=<count>\n");
printf("--bulk-insert-batch-size=<size>\n");
printf("--disable-triggers-on=<schema>\n");
printf("--reenable-triggers-on=<schema>\n");
printf("--dont-disable-triggers");
printf("--version\n");
printf("--help\n");
}
/*
* read_tasks_from_file : reads the table information from a text file.
* Parameters:
* - file_name : the file containing the table definitions
* - count_only : indicates if the file contains information to count the records
* from the source DB or to actually trasmit the data
* - tasks : output parameter that will contain a task for each table definition loaded
* from the file
- resume : indicates if the file contains information to resume copying data from
last PK
* - max_count : limit copied rows count to max_count
*
* Remarks : Each table is defined in a single line with the next format for count_only = true and resume = false
* <src_schema>\t<src_table>\n
*
* and in the next format for a count_only = false or resume = true
* <src_schema>\t<src_table>\t<tgt_schema>\t<tgt_table>\t<source_pk_columns>\t<target_pk_columns>\t<select_expression>
*/
bool read_tasks_from_file(const std::string file_name, bool count_only, TaskQueue& tasks, std::set<std::string> &trigger_schemas,
bool resume, long long int max_count)
{
std::ifstream ifs ( file_name.data() , std::ifstream::in );
unsigned int field_count = count_only && !resume ? 2 : 7;
bool error = false;
printf("Loading table information from file %s\n", file_name.data());
while (!error && ifs.good())
{
TableParam param;
std::string line;
getline(ifs, line);
if (line.length())
{
log_info("--table %s\n", line.data());
std::vector<std::string> fields = base::split(line, "\t", field_count);
if (fields.size() == field_count)
{
param.source_schema = fields[0];
param.source_table = fields[1];
if (!(count_only && !resume))
{
param.target_schema = fields[2];
param.target_table = fields[3];
if(std::strcmp(fields[4].c_str(), "-") != 0)
param.source_pk_columns = base::split(fields[4], ",", -1);
if(std::strcmp(fields[5].c_str(), "-") != 0)
param.target_pk_columns = base::split(fields[5], ",", -1);
param.select_expression = fields[6];
trigger_schemas.insert(param.target_schema);
}
param.copy_spec.resume = resume;
param.copy_spec.max_count = max_count;
param.copy_spec.type = CopyAll;
tasks.add_task(param);
}
else
error = true;
}
}
ifs.close();
return !error;
}
int main(int argc, char **argv)
{
std::string app_name = base::basename(argv[0]);
base::threading_init();
TaskQueue tables;
std::string source_password;
std::string source_connstring;
bool source_use_cleartext_plugin = false;
bool source_is_utf8 = false;
std::string source_charset;
SourceType source_type = ST_MYSQL;
std::string target_connstring;
std::string target_password;
bool target_use_cleartext_plugin = false;
std::string log_level;
std::string log_file;
bool passwords_from_stdin = false;
bool count_only = false;
bool check_types_only = false;
bool truncate_target = false;
bool show_progress = false;
bool abort_on_oversized_blobs = false;
bool disable_triggers = false;
bool reenable_triggers = false;
bool disable_triggers_on_copy = true;
bool resume = false;
int thread_count = 1;
long long bulk_insert_batch = 100;
long long max_count = 0;
std::string table_file;
std::set<std::string> trigger_schemas;
std::string source_rdbms_type = "unknown";
bool log_level_set = false;
int i = 1;
while (i < argc)
{
char *argval = NULL;
if (check_arg_with_value(argv, i, "--log-level", argval, true))
log_level = argval;
else if (check_arg_with_value(argv, i, "--log-file", argval, true))
log_file = argval;
else if (check_arg_with_value(argv, i, "--odbc-source", argval, true))
{
source_type = ST_ODBC;
source_connstring = base::trim(argval, "\"");
}
else if (check_arg_with_value(argv, i, "--mysql-source", argval, true))
{
source_type = ST_MYSQL;
source_connstring = base::trim(argval, "\"");
}
else if (check_arg_with_value(argv, i, "--pythondbapi-source", argval, true))
{
source_type = ST_PYTHON;
source_connstring = base::trim(argval, "\"");
}
else if (check_arg_with_value(argv, i, "--source-password", argval, true))
source_password = argval;
else if (check_arg_with_value(argv, i, "--target-password", argval, true))
target_password = argval;
else if (strcmp(argv[i], "--force-utf8-for-source") == 0)
source_is_utf8 = true;
else if (check_arg_with_value(argv, i, "--source-charset", argval, true))
source_charset = argval;
else if (strcmp(argv[i], "--progress") == 0)
show_progress = true;
else if (strcmp(argv[i], "--truncate-target") == 0)
truncate_target = true;
else if (strcmp(argv[i], "--count-only") == 0)
{
// Count only will be allowed only if one of the trigger
// operations has not been indicated first
if ( !disable_triggers && !reenable_triggers)
count_only = true;
}
else if (strcmp(argv[i], "--check-types-only") == 0)
check_types_only = true;
else if (strcmp(argv[i], "--passwords-from-stdin") == 0)
passwords_from_stdin = true;
else if (strcmp(argv[i], "--abort-on-oversized-blobs") == 0)
abort_on_oversized_blobs = true;
else if (strcmp(argv[i], "--dont-disable-triggers") == 0)
disable_triggers_on_copy = false;
else if (strcmp(argv[i], "--resume") == 0)
resume = true;
else if (check_arg_with_value(argv, i, "--disable-triggers-on", argval, true))
{
// disabling/enabling triggers are standalone operations and mutually exclusive
// so here it ensures a request for trigger enabling was not found first
if (!reenable_triggers && !count_only)
{
disable_triggers = true;
trigger_schemas.insert(argval);
}
}
else if (check_arg_with_value(argv, i, "--reenable-triggers-on", argval, true))
{
// disabling/enabling triggers are standalone operations and mutually exclusive
// so here it ensures a request for trigger enabling was not found first
if (!disable_triggers && !count_only)
{
reenable_triggers = true;
trigger_schemas.insert(argval);
}
}
else if (check_arg_with_value(argv, i, "--thread-count", argval, true))
{
thread_count = base::atoi<int>(argval, 0);
if (thread_count < 1)
thread_count = 1;
}
else if (check_arg_with_value(argv, i, "--bulk-insert-batch-size", argval, true))
{
bulk_insert_batch = base::atoi<int>(argval, 0);
if (bulk_insert_batch < 1)
bulk_insert_batch = 100;
}
else if (strcmp(argv[i], "--version") == 0)
{
const char *type = APP_EDITION_NAME;
if (strcmp(APP_EDITION_NAME, "Community") == 0)
type = "CE";
printf("%s %s (%s) %i.%i.%i %s build %i\n"
, base::basename(argv[0]).c_str()
, type, APP_LICENSE_TYPE
, APP_MAJOR_NUMBER
, APP_MINOR_NUMBER
, APP_RELEASE_NUMBER
, APP_RELEASE_TYPE
, APP_BUILD_NUMBER
);
exit(0);
}
else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
{
show_help();
exit(0);
}
else if (check_arg_with_value(argv, i, "--target", argval, true))
{
target_connstring = base::trim(argval, "\"");
}
else if (check_arg_with_value(argv, i, "--table-file", argval, true))
table_file = argval;
else if (strcmp(argv[i], "--table") == 0)
{
TableParam param;
if ((!count_only && i + 7 >= argc) || (count_only && i + 2 >= argc))
{
fprintf(stderr, "%s: Missing value for table copy specification\n", argv[0]);
exit(1);
}
param.source_schema = argv[++i];
param.source_table = argv[++i];
if (!(count_only && !resume))
{
param.target_schema = argv[++i];
param.target_table = argv[++i];
if(std::strcmp(argv[++i], "-") != 0)
param.source_pk_columns = base::split(argv[i], ",", -1);
if(std::strcmp(argv[++i], "-") != 0)
param.target_pk_columns = base::split(argv[i], ",", -1);
param.select_expression = argv[++i];
trigger_schemas.insert(param.target_schema);
}
param.copy_spec.resume = resume;
param.copy_spec.max_count = max_count;
param.copy_spec.type = CopyAll;
tables.add_task(param);
}
else if (strcmp(argv[i], "--table-range") == 0)
{
TableParam param;
if ((!count_only && i + 10 >= argc) || (count_only && i + 5 >= argc))
{
fprintf(stderr, "%s: Missing value for table copy specification\n", argv[0]);
exit(1);
}
param.source_schema = argv[++i];
param.source_table = argv[++i];
if (!(count_only && !resume))
{
param.target_schema = argv[++i];
param.target_table = argv[++i];
if(std::strcmp(argv[++i], "-") != 0)
param.source_pk_columns = base::split(argv[i], ",", -1);
if(std::strcmp(argv[++i], "-") != 0)
param.target_pk_columns = base::split(argv[i], ",", -1);
param.select_expression = argv[++i];
trigger_schemas.insert(param.target_schema);
}
param.copy_spec.range_key = argv[++i];
param.copy_spec.range_start = base::atoi<long long>(argv[++i], 0ll);
param.copy_spec.range_end = base::atoi<long long>(argv[++i], 0ll);
param.copy_spec.type = CopyRange;
tables.add_task(param);
}
else if (strcmp(argv[i], "--table-row-count") == 0)
{
TableParam param;
if ((!count_only && i + 8 >= argc) || (count_only && i + 3 >= argc))
{
fprintf(stderr, "%s: Missing value for table copy specification\n", argv[0]);
exit(1);
}
param.source_schema = argv[++i];
param.source_table = argv[++i];
if (!(count_only && !resume))
{
param.target_schema = argv[++i];
param.target_table = argv[++i];
if(std::strcmp(argv[++i], "-") != 0)
param.source_pk_columns = base::split(argv[i], ",", -1);
if(std::strcmp(argv[++i], "-") != 0)
param.target_pk_columns = base::split(argv[i], ",", -1);
param.select_expression = argv[++i];
}
param.copy_spec.row_count = base::atoi<long long>(argv[++i], 0ll);
param.copy_spec.resume = resume;
param.copy_spec.type = CopyCount;
tables.add_task(param);
}
else if (check_arg_with_value(argv, i, "--source-rdbms-type", argval, false))
source_rdbms_type = argval;
else if (strcmp(argv[i], "--table-where") == 0)
{
TableParam param;
if ((!count_only && i + 8 >= argc) || (count_only && i + 4 >= argc))
{
fprintf(stderr, "%s: Missing value for table copy specification\n", argv[0]);
exit(1);
}
param.source_schema = argv[++i];
param.source_table = argv[++i];
if (!(count_only && !resume))
{
param.target_schema = argv[++i];
param.target_table = argv[++i];
if(std::strcmp(argv[++i], "-") != 0)
param.source_pk_columns = base::split(argv[i], ",", -1);
if(std::strcmp(argv[++i], "-") != 0)
param.target_pk_columns = base::split(argv[i], ",", -1);
param.select_expression = argv[++i];
param.copy_spec.where_expression = argv[++i];
trigger_schemas.insert(param.target_schema);
}
else
{
param.select_expression = argv[++i];
param.copy_spec.where_expression = argv[++i];
}
param.copy_spec.type = CopyWhere;
tables.add_task(param);
}
else if (check_arg_with_value(argv, i, "--max-count", argval, true))
{
max_count = base::atoi<int>(argval, 0);
}
else if (strcmp(argv[i], "--source-use-cleartext") == 0)
source_use_cleartext_plugin = true;
else if (strcmp(argv[i], "--target-use-cleartext") == 0)
target_use_cleartext_plugin = true;
else
{
fprintf(stderr, "%s: Invalid option %s\n", argv[0], argv[i]);
exit(1);
}
i++;
}
// Creates the log to the target file if any, if not
// uses std_error
base::Logger logger(true, log_file);
if (!log_level.empty())
{
if (!set_log_level(log_level))
{
fprintf(stderr, "%s: invalid argument '%s' for option %s\n", argv[0], log_level.data(), "--log-level");
exit(1);
}
else
log_level_set = true;
}
// Set the log level from environment var WB_LOG_LEVEL if specified or set a default log level.
if (!log_level_set)
{
const char* log_setting = getenv("WB_LOG_LEVEL");
if (log_setting == NULL)
log_setting = "info";
else
log_level_set = true;
std::string level = base::tolower(log_setting);
base::Logger::active_level(level);
}
// If needed, reads the tasks from the table definition file
if (!table_file.empty())
{
if (!read_tasks_from_file(table_file, count_only, tables, trigger_schemas, resume, max_count))
{
fprintf(stderr, "Invalid table definitions format in file: %s\n", table_file.data());
exit(1);
}
}
// Not having the source connection data is an error unless
// the standalone operations to disable or reenable triggers
// are called
if (source_connstring.empty() && !reenable_triggers && ! disable_triggers)
{
fprintf(stderr, "Missing source DB server\n");
exit(1);
}
if (target_connstring.empty() && !(count_only && !resume))
{
fprintf(stderr, "Missing target DB server\n");
exit(1);
}
// Table definitions will be required only if the standalone operations to
// Reenable or disable triggers are not called
if (tables.empty() && !reenable_triggers && ! disable_triggers)
{
log_warning("Missing table list specification\n");
exit(0);
}
std::string source_host;
std::string source_user;
int source_port = -1;
std::string source_socket;
// Source connection is parsed only when NOT executing the
// Standalone operatios on triggers
if (source_type == ST_MYSQL && !reenable_triggers && ! disable_triggers)
{
if (!parse_mysql_connstring(source_connstring, source_user, source_password,
source_host, source_port, source_socket))
{
fprintf(stderr, "Invalid MySQL connection string %s for source database. Must be in format user[:pass]@host:port or user[:pass]@::socket\n", target_connstring.c_str());
exit(1);
}
}
std::string target_host;
std::string target_user;
int target_port = -1;
std::string target_socket;
if (!(count_only && !resume) && !parse_mysql_connstring(target_connstring, target_user, target_password,
target_host, target_port, target_socket))
{
fprintf(stderr, "Invalid MySQL connection string %s for target database. Must be in format user[:pass]@host:port or user[:pass]@::socket\n", target_connstring.c_str());
exit(1);
}
if (passwords_from_stdin)
{
char password[200];
if (!fgets(password, sizeof(password), stdin))
{
log_error("Error reading passwords from stdin\n");
exit(1);
}
if ((count_only && !resume)|| reenable_triggers || disable_triggers)
{
char *ptr = strtok(password, "\t\r\n");
if (ptr)
{
if (count_only)
source_password = ptr;
else
target_password = ptr;
}
}
else
{
char *ptr = strtok(password, "\r\n");
if (ptr)
{
ptr = strchr(password, '\t');
if (ptr)
{
source_password = std::string(password, ptr-password);
target_password = ptr+1;
}
else
source_password = password;
}
}
}
static SQLHENV odbc_env;
PyThreadState *state = NULL;
if (source_type == ST_PYTHON)
{
Py_Initialize();
PyEval_InitThreads();
state = PyEval_SaveThread();
}
try
{
if (count_only)
{
boost::scoped_ptr<CopyDataSource> psource;
if (source_type == ST_ODBC)
{
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &odbc_env);
SQLSetEnvAttr(odbc_env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
psource.reset(new ODBCCopyDataSource(odbc_env, source_connstring, source_password, source_is_utf8, source_rdbms_type));
}
else if (source_type == ST_MYSQL)
psource.reset(new MySQLCopyDataSource(source_host, source_port, source_user, source_password, source_socket, source_use_cleartext_plugin));
else
psource.reset(new PythonCopyDataSource(source_connstring, source_password));
boost::scoped_ptr<MySQLCopyDataTarget> ptarget;
TableParam task;
while(tables.get_task(task))
{
std::vector<std::string> last_pkeys;
if (task.copy_spec.resume)
{
if(!ptarget.get())
ptarget.reset(new MySQLCopyDataTarget(target_host, target_port, target_user, target_password, target_socket, target_use_cleartext_plugin, app_name, source_charset, source_rdbms_type));
last_pkeys = ptarget->get_last_pkeys(task.target_pk_columns, task.target_schema, task.target_table);
}
count_rows(psource, task.source_schema, task.source_table, task.source_pk_columns, task.copy_spec, last_pkeys);
}
}
else if (reenable_triggers || disable_triggers)
{
boost::scoped_ptr<MySQLCopyDataTarget> ptarget;
ptarget.reset(new MySQLCopyDataTarget(target_host, target_port, target_user, target_password, target_socket, target_use_cleartext_plugin, app_name, source_charset, source_rdbms_type));
if (disable_triggers)
ptarget->backup_triggers(trigger_schemas);
else
ptarget->restore_triggers(trigger_schemas);
}
else
{
std::vector<CopyDataTask*> threads;
boost::scoped_ptr<MySQLCopyDataTarget> ptarget_conn;
MySQLCopyDataTarget *ptarget = NULL;
CopyDataSource *psource = NULL;
if (disable_triggers_on_copy)
{
ptarget_conn.reset(new MySQLCopyDataTarget(target_host, target_port, target_user, target_password, target_socket, target_use_cleartext_plugin, app_name, source_charset, source_rdbms_type));
ptarget_conn->backup_triggers(trigger_schemas);
}
for (int index = 0; index < thread_count; index++)
{
if (source_type == ST_ODBC)
{
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &odbc_env);
SQLSetEnvAttr(odbc_env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
psource = new ODBCCopyDataSource(odbc_env, source_connstring, source_password, source_is_utf8, source_rdbms_type);
}
else if (source_type == ST_MYSQL)
psource = new MySQLCopyDataSource(source_host, source_port, source_user, source_password, source_socket, source_use_cleartext_plugin);
else
psource = new PythonCopyDataSource(source_connstring, source_password);
ptarget = new MySQLCopyDataTarget(target_host, target_port, target_user, target_password, target_socket, target_use_cleartext_plugin, app_name, source_charset, source_rdbms_type);
psource->set_max_blob_chunk_size(ptarget->get_max_allowed_packet());
psource->set_max_parameter_size((unsigned long)ptarget->get_max_long_data_size());
psource->set_abort_on_oversized_blobs(abort_on_oversized_blobs);
ptarget->set_truncate(truncate_target);
if (max_count > 0)
bulk_insert_batch = max_count;
ptarget->set_bulk_insert_batch_size((int)bulk_insert_batch);
if (check_types_only)
{
//XXXX
delete psource;
}
else
{
threads.push_back(new CopyDataTask(base::strfmt("Task %d", index + 1), psource, ptarget, &tables, show_progress));
}
}
// Waits for all the threads to complete
for (size_t index = 0; index < threads.size(); index++)
threads[index]->wait();
// Finally destroys the threads and connections
for (size_t index = 0; index < threads.size(); index++)
delete threads[index];
// Finally restores the triggers
if (disable_triggers_on_copy)
ptarget_conn->restore_triggers(trigger_schemas);
}
}
catch (std::exception &e)
{
log_error("Exception: %s\n", e.what());
if (source_type == ST_PYTHON)
{
PyEval_RestoreThread(state);
Py_Finalize();
}
exit(1);
}
if (source_type == ST_PYTHON)
{
PyEval_RestoreThread(state);
Py_Finalize();
}
printf("FINISHED\n");
fflush(stdout);
return 0;
}
|