diff options
Diffstat (limited to 'client/mysqldump.c')
-rw-r--r-- | client/mysqldump.c | 243 |
1 files changed, 225 insertions, 18 deletions
diff --git a/client/mysqldump.c b/client/mysqldump.c index c36f9d3e23e..042ce7e28a5 100644 --- a/client/mysqldump.c +++ b/client/mysqldump.c @@ -108,6 +108,8 @@ static uint opt_protocol= 0; static char *default_charset= (char*) MYSQL_UNIVERSAL_CLIENT_CHARSET; static CHARSET_INFO *charset_info= &my_charset_latin1; const char *default_dbug_option="d:t:o,/tmp/mysqldump.trace"; +/* do we met VIEWs during tables scaning */ +my_bool was_views= 0; const char *compatible_mode_names[]= { @@ -354,7 +356,7 @@ static struct my_option my_long_options[] = (gptr*) &where, (gptr*) &where, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"xml", 'X', "Dump a database as well formed XML.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, - { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0} + {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0} }; static const char *load_default_groups[]= { "mysqldump","client",0 }; @@ -372,6 +374,8 @@ static int dump_all_databases(); static char *quote_name(const char *name, char *buff, my_bool force); static const char *check_if_ignore_table(const char *table_name); static char *primary_key_fields(const char *table_name); +static my_bool getViewStructure(char *table, char* db); +static my_bool dump_all_views_in_db(char *database); #include <help_start.h> @@ -1042,6 +1046,7 @@ static uint getTableStructure(char *table, char* db) { /* Make an sql-file, if path was given iow. option -T was given */ char buff[20+FN_REFLEN]; + MYSQL_FIELD *field; sprintf(buff,"show create table %s", result_table); if (mysql_query_with_error_report(sock, 0, buff)) @@ -1075,8 +1080,16 @@ static uint getTableStructure(char *table, char* db) check_io(sql_file); } - tableRes=mysql_store_result(sock); - row=mysql_fetch_row(tableRes); + tableRes= mysql_store_result(sock); + field= mysql_fetch_field_direct(tableRes, 0); + if (strcmp(field->name, "View") == 0) + { + if (verbose) + fprintf(stderr, "-- It's a view, skipped\n"); + was_views= 1; + DBUG_RETURN(0); + } + row= mysql_fetch_row(tableRes); fprintf(sql_file, "%s;\n", row[1]); check_io(sql_file); mysql_free_result(tableRes); @@ -1220,6 +1233,14 @@ static uint getTableStructure(char *table, char* db) sprintf(buff,"show keys from %s", result_table); if (mysql_query_with_error_report(sock, &tableRes, buff)) { + if (mysql_errno(sock) == ER_WRONG_OBJECT) + { + /* it is VIEW */ + fputs("\t\t<options Comment=\"view\" />\n", sql_file); + goto continue_xml; + } + fprintf(stderr, "%s: Can't get keys for table %s (%s)\n", + my_progname, result_table, mysql_error(sock)); if (path) my_fclose(sql_file, MYF(MY_WME)); safe_exit(EX_MYSQLERR); @@ -1323,6 +1344,7 @@ static uint getTableStructure(char *table, char* db) } mysql_free_result(tableRes); /* Is always safe to free */ } +continue_xml: if (!opt_xml) fputs(";\n", sql_file); else @@ -1601,12 +1623,13 @@ static void dumpTable(uint numFields, char *table) we'll dump in hex only BLOB columns. */ is_blob= (opt_hex_blob && field->charsetnr == 63 && - (field->type == FIELD_TYPE_STRING || - field->type == FIELD_TYPE_VAR_STRING || - field->type == FIELD_TYPE_BLOB || - field->type == FIELD_TYPE_LONG_BLOB || - field->type == FIELD_TYPE_MEDIUM_BLOB || - field->type == FIELD_TYPE_TINY_BLOB)) ? 1 : 0; + (field->type == MYSQL_TYPE_STRING || + field->type == MYSQL_TYPE_VAR_STRING || + field->type == MYSQL_TYPE_VARCHAR || + field->type == MYSQL_TYPE_BLOB || + field->type == MYSQL_TYPE_LONG_BLOB || + field->type == MYSQL_TYPE_MEDIUM_BLOB || + field->type == MYSQL_TYPE_TINY_BLOB)) ? 1 : 0; if (extended_insert) { ulong length = lengths[i]; @@ -1867,6 +1890,21 @@ static int dump_all_databases() if (dump_all_tables_in_db(row[0])) result=1; } + if (was_views) + { + if (mysql_query(sock, "SHOW DATABASES") || + !(tableres = mysql_store_result(sock))) + { + my_printf_error(0, "Error: Couldn't execute 'SHOW DATABASES': %s", + MYF(0), mysql_error(sock)); + return 1; + } + while ((row = mysql_fetch_row(tableres))) + { + if (dump_all_views_in_db(row[0])) + result=1; + } + } return result; } /* dump_all_databases */ @@ -1875,17 +1913,30 @@ static int dump_all_databases() static int dump_databases(char **db_names) { int result=0; - for ( ; *db_names ; db_names++) + char **db; + for (db= db_names ; *db ; db++) { - if (dump_all_tables_in_db(*db_names)) + if (dump_all_tables_in_db(*db)) result=1; } + if (!result && was_views) + { + for (db= db_names ; *db ; db++) + { + if (dump_all_views_in_db(*db)) + result=1; + } + } return result; } /* dump_databases */ static int init_dumping(char *database) { + if (mysql_get_server_version(sock) >= 50003 && + !my_strcasecmp(&my_charset_latin1, database, "information_schema")) + return 1; + if (mysql_select_db(sock, database)) { DBerror(sock, "when selecting the database"); @@ -1989,11 +2040,64 @@ static int dump_all_tables_in_db(char *database) return 0; } /* dump_all_tables_in_db */ +/* + dump structure of views of database + + SYNOPSIS + dump_all_views_in_db() + database database name + RETURN + 0 OK + 1 ERROR +*/ + +static my_bool dump_all_views_in_db(char *database) +{ + char *table; + uint numrows; + char table_buff[NAME_LEN*2+3]; + + if (init_dumping(database)) + return 1; + if (opt_xml) + print_xml_tag1(md_result_file, "", "database name=", database, "\n"); + if (lock_tables) + { + DYNAMIC_STRING query; + init_dynamic_string(&query, "LOCK TABLES ", 256, 1024); + for (numrows= 0 ; (table= getTableName(1)); numrows++) + { + dynstr_append(&query, quote_name(table, table_buff, 1)); + dynstr_append(&query, " READ /*!32311 LOCAL */,"); + } + if (numrows && mysql_real_query(sock, query.str, query.length-1)) + DBerror(sock, "when using LOCK TABLES"); + /* We shall continue here, if --force was given */ + dynstr_free(&query); + } + if (flush_logs) + { + if (mysql_refresh(sock, REFRESH_LOG)) + DBerror(sock, "when doing refresh"); + /* We shall continue here, if --force was given */ + } + while ((table= getTableName(0))) + getViewStructure(table, database); + if (opt_xml) + { + fputs("</database>\n", md_result_file); + check_io(md_result_file); + } + if (lock_tables) + mysql_query(sock,"UNLOCK TABLES"); + return 0; +} /* dump_all_tables_in_db */ static int dump_selected_tables(char *db, char **table_names, int tables) { uint numrows; + int i; char table_buff[NAME_LEN*+3]; if (init_dumping(db)) @@ -2001,7 +2105,6 @@ static int dump_selected_tables(char *db, char **table_names, int tables) if (lock_tables) { DYNAMIC_STRING query; - int i; init_dynamic_string(&query, "LOCK TABLES ", 256, 1024); for (i=0 ; i < tables ; i++) @@ -2022,14 +2125,19 @@ static int dump_selected_tables(char *db, char **table_names, int tables) } if (opt_xml) print_xml_tag1(md_result_file, "", "database name=", db, "\n"); - for (; tables > 0 ; tables-- , table_names++) + for (i=0 ; i < tables ; i++) { - numrows = getTableStructure(*table_names, db); + numrows = getTableStructure(table_names[i], db); if (!dFlag && numrows > 0) - dumpTable(numrows, *table_names); + dumpTable(numrows, table_names[i]); my_free(order_by, MYF(MY_ALLOW_ZERO_PTR)); order_by= 0; } + if (was_views) + { + for (i=0 ; i < tables ; i++) + getViewStructure(table_names[i], db); + } if (opt_xml) { fputs("</database>\n", md_result_file); @@ -2233,9 +2341,14 @@ static const char *check_if_ignore_table(const char *table_name) mysql_free_result(res); return 0; /* assume table is ok */ } - if (strcmp(row[1], (result= "MRG_MyISAM")) && - strcmp(row[1], (result= "MRG_ISAM"))) - result= 0; + if (!(row[1])) + result= "VIEW"; + else + { + if (strcmp(row[1], (result= "MRG_MyISAM")) && + strcmp(row[1], (result= "MRG_ISAM"))) + result= 0; + } mysql_free_result(res); return result; } @@ -2315,6 +2428,100 @@ cleanup: } +/* + Getting VIEW structure + + SYNOPSIS + getViewStructure() + table view name + db db name + + RETURN + 0 OK + 1 ERROR +*/ + +static my_bool getViewStructure(char *table, char* db) +{ + MYSQL_RES *tableRes; + MYSQL_ROW row; + MYSQL_FIELD *field; + char *result_table, *opt_quoted_table; + char table_buff[NAME_LEN*2+3]; + char table_buff2[NAME_LEN*2+3]; + char buff[20+FN_REFLEN]; + FILE *sql_file = md_result_file; + DBUG_ENTER("getViewStructure"); + + if (tFlag) + DBUG_RETURN(0); + + if (verbose) + fprintf(stderr, "-- Retrieving view structure for table %s...\n", table); + + sprintf(insert_pat,"SET OPTION SQL_QUOTE_SHOW_CREATE=%d", + (opt_quoted || opt_keywords)); + result_table= quote_name(table, table_buff, 1); + opt_quoted_table= quote_name(table, table_buff2, 0); + + sprintf(buff,"show create table %s", result_table); + if (mysql_query(sock, buff)) + { + fprintf(stderr, "%s: Can't get CREATE TABLE for view %s (%s)\n", + my_progname, result_table, mysql_error(sock)); + safe_exit(EX_MYSQLERR); + DBUG_RETURN(0); + } + + if (path) + { + char filename[FN_REFLEN], tmp_path[FN_REFLEN]; + convert_dirname(tmp_path,path,NullS); + sql_file= my_fopen(fn_format(filename, table, tmp_path, ".sql", 4), + O_WRONLY, MYF(MY_WME)); + if (!sql_file) /* If file couldn't be opened */ + { + safe_exit(EX_MYSQLERR); + DBUG_RETURN(1); + } + write_header(sql_file, db); + } + tableRes= mysql_store_result(sock); + field= mysql_fetch_field_direct(tableRes, 0); + if (strcmp(field->name, "View") != 0) + { + if (verbose) + fprintf(stderr, "-- It's base table, skipped\n"); + DBUG_RETURN(0); + } + + if (!opt_xml && opt_comments) + { + fprintf(sql_file, "\n--\n-- View structure for view %s\n--\n\n", + result_table); + check_io(sql_file); + } + if (opt_drop) + { + fprintf(sql_file, "DROP VIEW IF EXISTS %s;\n", opt_quoted_table); + check_io(sql_file); + } + + row= mysql_fetch_row(tableRes); + fprintf(sql_file, "%s;\n", row[1]); + check_io(sql_file); + mysql_free_result(tableRes); + + if (sql_file != md_result_file) + { + fputs("\n", sql_file); + write_footer(sql_file); + my_fclose(sql_file, MYF(MY_WME)); + } + DBUG_RETURN(0); +} + + int main(int argc, char **argv) { compatible_mode_normal_str[0]= 0; |