diff options
author | Sergei Golubchik <serg@mariadb.org> | 2015-12-21 21:24:22 +0100 |
---|---|---|
committer | Sergei Golubchik <serg@mariadb.org> | 2015-12-21 21:24:22 +0100 |
commit | a2bcee626d4ef2836e38e4932305871390164644 (patch) | |
tree | b41e357427318bad8985078b91bbd2b0360defc8 /client/mysqldump.c | |
parent | 1788bfe93a745582d938a608d5959b7d2e6b2f23 (diff) | |
parent | 4fdf25afa8188905653a83e08fc387243e584600 (diff) | |
download | mariadb-git-a2bcee626d4ef2836e38e4932305871390164644.tar.gz |
Merge branch '10.0' into 10.1
Diffstat (limited to 'client/mysqldump.c')
-rw-r--r-- | client/mysqldump.c | 206 |
1 files changed, 131 insertions, 75 deletions
diff --git a/client/mysqldump.c b/client/mysqldump.c index cffa5bf21f9..1c939b87c65 100644 --- a/client/mysqldump.c +++ b/client/mysqldump.c @@ -1,6 +1,6 @@ /* Copyright (c) 2000, 2013, Oracle and/or its affiliates. - Copyright (c) 2010, 2013, Monty Program Ab. + Copyright (c) 2010, 2015, Monty Program Ab. 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 @@ -148,6 +148,12 @@ static int first_error=0; */ static uint multi_source= 0; static DYNAMIC_STRING extended_row; +static DYNAMIC_STRING dynamic_where; +static MYSQL_RES *get_table_name_result= NULL; +static MEM_ROOT glob_root; +static MYSQL_RES *routine_res, *routine_list_res; + + #include <sslopt-vars.h> FILE *md_result_file= 0; FILE *stderror_file=0; @@ -1144,16 +1150,14 @@ static int fetch_db_collation(const char *db_name, int db_cl_size) { my_bool err_status= FALSE; - char query[QUERY_LENGTH]; MYSQL_RES *db_cl_res; MYSQL_ROW db_cl_row; - char quoted_database_buf[NAME_LEN*2+3]; - char *qdatabase= quote_name(db_name, quoted_database_buf, 1); - - my_snprintf(query, sizeof (query), "use %s", qdatabase); - if (mysql_query_with_error_report(mysql, NULL, query)) - return 1; + if (mysql_select_db(mysql, db_name)) + { + DB_error(mysql, "when selecting the database"); + return 1; /* If --force */ + } if (mysql_query_with_error_report(mysql, &db_cl_res, "select @@collation_database")) @@ -1591,14 +1595,26 @@ static void free_resources() { if (md_result_file && md_result_file != stdout) my_fclose(md_result_file, MYF(0)); + if (get_table_name_result) + mysql_free_result(get_table_name_result); + if (routine_res) + mysql_free_result(routine_res); + if (routine_list_res) + mysql_free_result(routine_list_res); + if (mysql) + { + mysql_close(mysql); + mysql= 0; + } + my_free(order_by); my_free(opt_password); my_free(current_host); + free_root(&glob_root, MYF(0)); if (my_hash_inited(&ignore_table)) my_hash_free(&ignore_table); - if (extended_insert) - dynstr_free(&extended_row); - if (insert_pat_inited) - dynstr_free(&insert_pat); + dynstr_free(&extended_row); + dynstr_free(&dynamic_where); + dynstr_free(&insert_pat); if (defaults_argv) free_defaults(defaults_argv); mysql_library_end(); @@ -1615,8 +1631,6 @@ static void maybe_exit(int error) ignore_errors= 1; /* don't want to recurse, if something fails below */ if (opt_slave_data) do_start_slave_sql(mysql); - if (mysql) - mysql_close(mysql); free_resources(); exit(error); } @@ -1709,6 +1723,7 @@ static void dbDisconnect(char *host) { verbose_msg("-- Disconnecting from %s...\n", host ? host : "localhost"); mysql_close(mysql); + mysql= 0; } /* dbDisconnect */ @@ -2350,6 +2365,8 @@ static uint dump_events_for_db(char *db) (const char *) (query_str != NULL ? query_str : row[3]), (const char *) delimiter); + my_free(query_str); + restore_time_zone(sql_file, delimiter); restore_sql_mode(sql_file, delimiter); @@ -2434,7 +2451,6 @@ static uint dump_routines_for_db(char *db) char *routine_name; int i; FILE *sql_file= md_result_file; - MYSQL_RES *routine_res, *routine_list_res; MYSQL_ROW row, routine_list_row; char db_cl_name[MY_CS_NAME_SIZE]; @@ -2458,7 +2474,7 @@ static uint dump_routines_for_db(char *db) /* Get database collation. */ - if (fetch_db_collation(db_name_buff, db_cl_name, sizeof (db_cl_name))) + if (fetch_db_collation(db, db_cl_name, sizeof (db_cl_name))) DBUG_RETURN(1); if (switch_character_set_results(mysql, "binary")) @@ -2489,7 +2505,11 @@ static uint dump_routines_for_db(char *db) routine_type[i], routine_name); if (mysql_query_with_error_report(mysql, &routine_res, query_buff)) + { + mysql_free_result(routine_list_res); + routine_list_res= 0; DBUG_RETURN(1); + } while ((row= mysql_fetch_row(routine_res))) { @@ -2507,7 +2527,8 @@ static uint dump_routines_for_db(char *db) print_comment(sql_file, 1, "-- does %s have permissions on mysql.proc?\n\n", current_user); - maybe_die(EX_MYSQLERR,"%s has insufficent privileges to %s!", current_user, query_buff); + maybe_die(EX_MYSQLERR,"%s has insufficent privileges to %s!", + current_user, query_buff); } else if (strlen(row[2])) { @@ -2527,9 +2548,12 @@ static uint dump_routines_for_db(char *db) if (mysql_num_fields(routine_res) >= 6) { - if (switch_db_collation(sql_file, db_name_buff, ";", + if (switch_db_collation(sql_file, db, ";", db_cl_name, row[5], &db_cl_altered)) { + mysql_free_result(routine_res); + mysql_free_result(routine_list_res); + routine_res= routine_list_res= 0; DBUG_RETURN(1); } @@ -2574,18 +2598,25 @@ static uint dump_routines_for_db(char *db) if (db_cl_altered) { - if (restore_db_collation(sql_file, db_name_buff, ";", db_cl_name)) + if (restore_db_collation(sql_file, db, ";", db_cl_name)) + { + mysql_free_result(routine_res); + mysql_free_result(routine_list_res); + routine_res= routine_list_res= 0; DBUG_RETURN(1); + } } } } } /* end of routine printing */ mysql_free_result(routine_res); + routine_res= 0; } /* end of list of routines */ } mysql_free_result(routine_list_res); + routine_list_res= 0; } /* end of for i (0 .. 1) */ if (opt_xml) @@ -2708,13 +2739,20 @@ static uint get_table_structure(char *table, char *db, char *table_type, if (switch_character_set_results(mysql, "binary") || mysql_query_with_error_report(mysql, &result, buff) || switch_character_set_results(mysql, default_charset)) + { + my_free(order_by); + order_by= 0; DBUG_RETURN(0); + } if (path) { if (!(sql_file= open_sql_file_for_table(table, O_WRONLY))) + { + my_free(order_by); + order_by= 0; DBUG_RETURN(0); - + } write_header(sql_file, db); } @@ -3286,10 +3324,6 @@ static int dump_trigger(FILE *sql_file, MYSQL_RES *show_create_trigger_rs, continue; } - query_str= cover_definer_clause(row[2], strlen(row[2]), - C_STRING_WITH_LEN("50017"), - C_STRING_WITH_LEN("50003"), - C_STRING_WITH_LEN(" TRIGGER")); if (switch_db_collation(sql_file, db_name, ";", db_cl_name, row[5], &db_cl_altered)) DBUG_RETURN(TRUE); @@ -3301,12 +3335,18 @@ static int dump_trigger(FILE *sql_file, MYSQL_RES *show_create_trigger_rs, switch_sql_mode(sql_file, ";", row[1]); + query_str= cover_definer_clause(row[2], strlen(row[2]), + C_STRING_WITH_LEN("50017"), + C_STRING_WITH_LEN("50003"), + C_STRING_WITH_LEN(" TRIGGER")); fprintf(sql_file, "DELIMITER ;;\n" "/*!50003 %s */;;\n" "DELIMITER ;\n", (const char *) (query_str != NULL ? query_str : row[2])); + my_free(query_str); + restore_sql_mode(sql_file, ";"); restore_cs_variables(sql_file, ";"); @@ -3315,8 +3355,6 @@ static int dump_trigger(FILE *sql_file, MYSQL_RES *show_create_trigger_rs, if (restore_db_collation(sql_file, db_name, ";", db_cl_name)) DBUG_RETURN(TRUE); } - - my_free(query_str); } DBUG_RETURN(FALSE); @@ -3409,13 +3447,14 @@ static int dump_triggers_for_table(char *table_name, char *db_name) { MYSQL_RES *show_create_trigger_rs= mysql_store_result(mysql); - if (!show_create_trigger_rs || - dump_trigger(sql_file, show_create_trigger_rs, db_name, db_cl_name)) - goto done; - + int error= (!show_create_trigger_rs || + dump_trigger(sql_file, show_create_trigger_rs, db_name, + db_cl_name)); mysql_free_result(show_create_trigger_rs); + if (error) + goto done; } - + } if (opt_xml) @@ -3662,12 +3701,14 @@ static void dump_table(char *table, char *db) { dynstr_append_checked(&query_string, " ORDER BY "); dynstr_append_checked(&query_string, order_by); + my_free(order_by); + order_by= 0; } if (mysql_real_query(mysql, query_string.str, query_string.length)) { - DB_error(mysql, "when executing 'SELECT INTO OUTFILE'"); dynstr_free(&query_string); + DB_error(mysql, "when executing 'SELECT INTO OUTFILE'"); DBUG_VOID_RETURN; } } @@ -3693,6 +3734,8 @@ static void dump_table(char *table, char *db) dynstr_append_checked(&query_string, " ORDER BY "); dynstr_append_checked(&query_string, order_by); + my_free(order_by); + order_by= 0; } if (!opt_xml && !opt_compact) @@ -3702,6 +3745,7 @@ static void dump_table(char *table, char *db) } if (mysql_query_with_error_report(mysql, 0, query_string.str)) { + dynstr_free(&query_string); DB_error(mysql, "when retrieving data from server"); goto err; } @@ -3711,6 +3755,7 @@ static void dump_table(char *table, char *db) res=mysql_store_result(mysql); if (!res) { + dynstr_free(&query_string); DB_error(mysql, "when retrieving data from server"); goto err; } @@ -4026,23 +4071,22 @@ err: static char *getTableName(int reset) { - static MYSQL_RES *res= NULL; - MYSQL_ROW row; + MYSQL_ROW row; - if (!res) + if (!get_table_name_result) { - if (!(res= mysql_list_tables(mysql,NullS))) + if (!(get_table_name_result= mysql_list_tables(mysql,NullS))) return(NULL); } - if ((row= mysql_fetch_row(res))) + if ((row= mysql_fetch_row(get_table_name_result))) return((char*) row[0]); if (reset) - mysql_data_seek(res,0); /* We want to read again */ + mysql_data_seek(get_table_name_result,0); /* We want to read again */ else { - mysql_free_result(res); - res= NULL; + mysql_free_result(get_table_name_result); + get_table_name_result= NULL; } return(NULL); } /* getTableName */ @@ -4059,46 +4103,44 @@ static int dump_all_tablespaces() static int dump_tablespaces_for_tables(char *db, char **table_names, int tables) { - DYNAMIC_STRING where; int r; int i; char name_buff[NAME_LEN*2+3]; mysql_real_escape_string(mysql, name_buff, db, strlen(db)); - init_dynamic_string_checked(&where, " AND TABLESPACE_NAME IN (" + init_dynamic_string_checked(&dynamic_where, " AND TABLESPACE_NAME IN (" "SELECT DISTINCT TABLESPACE_NAME FROM" " INFORMATION_SCHEMA.PARTITIONS" " WHERE" " TABLE_SCHEMA='", 256, 1024); - dynstr_append_checked(&where, name_buff); - dynstr_append_checked(&where, "' AND TABLE_NAME IN ("); + dynstr_append_checked(&dynamic_where, name_buff); + dynstr_append_checked(&dynamic_where, "' AND TABLE_NAME IN ("); for (i=0 ; i<tables ; i++) { mysql_real_escape_string(mysql, name_buff, table_names[i], strlen(table_names[i])); - dynstr_append_checked(&where, "'"); - dynstr_append_checked(&where, name_buff); - dynstr_append_checked(&where, "',"); + dynstr_append_checked(&dynamic_where, "'"); + dynstr_append_checked(&dynamic_where, name_buff); + dynstr_append_checked(&dynamic_where, "',"); } - dynstr_trunc(&where, 1); - dynstr_append_checked(&where,"))"); + dynstr_trunc(&dynamic_where, 1); + dynstr_append_checked(&dynamic_where,"))"); - DBUG_PRINT("info",("Dump TS for Tables where: %s",where.str)); - r= dump_tablespaces(where.str); - dynstr_free(&where); + DBUG_PRINT("info",("Dump TS for Tables where: %s",dynamic_where.str)); + r= dump_tablespaces(dynamic_where.str); + dynstr_free(&dynamic_where); return r; } static int dump_tablespaces_for_databases(char** databases) { - DYNAMIC_STRING where; int r; int i; - init_dynamic_string_checked(&where, " AND TABLESPACE_NAME IN (" + init_dynamic_string_checked(&dynamic_where, " AND TABLESPACE_NAME IN (" "SELECT DISTINCT TABLESPACE_NAME FROM" " INFORMATION_SCHEMA.PARTITIONS" " WHERE" @@ -4109,16 +4151,16 @@ static int dump_tablespaces_for_databases(char** databases) char db_name_buff[NAME_LEN*2+3]; mysql_real_escape_string(mysql, db_name_buff, databases[i], strlen(databases[i])); - dynstr_append_checked(&where, "'"); - dynstr_append_checked(&where, db_name_buff); - dynstr_append_checked(&where, "',"); + dynstr_append_checked(&dynamic_where, "'"); + dynstr_append_checked(&dynamic_where, db_name_buff); + dynstr_append_checked(&dynamic_where, "',"); } - dynstr_trunc(&where, 1); - dynstr_append_checked(&where,"))"); + dynstr_trunc(&dynamic_where, 1); + dynstr_append_checked(&dynamic_where,"))"); - DBUG_PRINT("info",("Dump TS for DBs where: %s",where.str)); - r= dump_tablespaces(where.str); - dynstr_free(&where); + DBUG_PRINT("info",("Dump TS for DBs where: %s",dynamic_where.str)); + r= dump_tablespaces(dynamic_where.str); + dynstr_free(&dynamic_where); return r; } @@ -4526,9 +4568,12 @@ static int dump_all_tables_in_db(char *database) } } if (numrows && mysql_real_query(mysql, query.str, query.length-1)) + { + dynstr_free(&query); DB_error(mysql, "when using LOCK TABLES"); - /* We shall continue here, if --force was given */ - dynstr_free(&query); + /* We shall continue here, if --force was given */ + } + dynstr_free(&query); /* Safe to call twice */ } if (flush_logs) { @@ -4542,7 +4587,9 @@ static int dump_all_tables_in_db(char *database) { verbose_msg("-- Setting savepoint...\n"); if (mysql_query_with_error_report(mysql, 0, "SAVEPOINT sp")) + { DBUG_RETURN(1); + } } while ((table= getTableName(0))) { @@ -4839,7 +4886,6 @@ static int dump_selected_tables(char *db, char **table_names, int tables) { char table_buff[NAME_LEN*2+3]; DYNAMIC_STRING lock_tables_query; - MEM_ROOT root; char **dump_tables, **pos, **end; int lower_case_table_names; DBUG_ENTER("dump_selected_tables"); @@ -4847,8 +4893,9 @@ static int dump_selected_tables(char *db, char **table_names, int tables) if (init_dumping(db, init_dumping_tables)) DBUG_RETURN(1); - init_alloc_root(&root, 8192, 0, MYF(0)); - if (!(dump_tables= pos= (char**) alloc_root(&root, tables * sizeof(char *)))) + init_alloc_root(&glob_root, 8192, 0, MYF(0)); + if (!(dump_tables= pos= (char**) alloc_root(&glob_root, + tables * sizeof(char *)))) die(EX_EOM, "alloc_root failure."); /* Figure out how to compare table names. */ @@ -4859,7 +4906,7 @@ static int dump_selected_tables(char *db, char **table_names, int tables) { /* the table name passed on commandline may be wrong case */ if ((*pos= get_actual_table_name(*table_names, lower_case_table_names, - &root))) + &glob_root))) { /* Add found table name to lock_tables_query */ if (lock_tables) @@ -4874,7 +4921,7 @@ static int dump_selected_tables(char *db, char **table_names, int tables) if (!ignore_errors) { dynstr_free(&lock_tables_query); - free_root(&root, MYF(0)); + free_root(&glob_root, MYF(0)); } maybe_die(EX_ILLEGAL_TABLE, "Couldn't find table: \"%s\"", *table_names); /* We shall countinue here, if --force was given */ @@ -4895,7 +4942,7 @@ static int dump_selected_tables(char *db, char **table_names, int tables) if (!ignore_errors) { dynstr_free(&lock_tables_query); - free_root(&root, MYF(0)); + free_root(&glob_root, MYF(0)); } DB_error(mysql, "when doing LOCK TABLES"); /* We shall countinue here, if --force was given */ @@ -4907,7 +4954,7 @@ static int dump_selected_tables(char *db, char **table_names, int tables) if (mysql_refresh(mysql, REFRESH_LOG)) { if (!ignore_errors) - free_root(&root, MYF(0)); + free_root(&glob_root, MYF(0)); DB_error(mysql, "when doing refresh"); } /* We shall countinue here, if --force was given */ @@ -4921,7 +4968,10 @@ static int dump_selected_tables(char *db, char **table_names, int tables) { verbose_msg("-- Setting savepoint...\n"); if (mysql_query_with_error_report(mysql, 0, "SAVEPOINT sp")) + { + free_root(&glob_root, MYF(0)); DBUG_RETURN(1); + } } /* Dump each selected table */ @@ -4936,6 +4986,8 @@ static int dump_selected_tables(char *db, char **table_names, int tables) { if (path) my_fclose(md_result_file, MYF(MY_WME)); + if (!ignore_errors) + free_root(&glob_root, MYF(0)); maybe_exit(EX_MYSQLERR); } } @@ -4954,7 +5006,11 @@ static int dump_selected_tables(char *db, char **table_names, int tables) { verbose_msg("-- Rolling back to savepoint sp...\n"); if (mysql_query_with_error_report(mysql, 0, "ROLLBACK TO SAVEPOINT sp")) + { + if (!ignore_errors) + free_root(&glob_root, MYF(0)); maybe_exit(EX_MYSQLERR); + } } } @@ -4962,8 +5018,10 @@ static int dump_selected_tables(char *db, char **table_names, int tables) { verbose_msg("-- Releasing savepoint...\n"); if (mysql_query_with_error_report(mysql, 0, "RELEASE SAVEPOINT sp")) + { + free_root(&glob_root, MYF(0)); DBUG_RETURN(1); - + } } /* Dump each selected view */ @@ -4983,9 +5041,7 @@ static int dump_selected_tables(char *db, char **table_names, int tables) DBUG_PRINT("info", ("Dumping routines for database %s", db)); dump_routines_for_db(db); } - free_root(&root, MYF(0)); - my_free(order_by); - order_by= 0; + free_root(&glob_root, MYF(0)); if (opt_xml) { fputs("</database>\n", md_result_file); |