===== client/Makefile.am 1.97 vs edited ===== --- 1.97/client/Makefile.am 2008-02-19 14:57:51 +01:00 +++ edited/client/Makefile.am 2008-04-30 14:17:46 +02:00 @@ -68,7 +68,9 @@ mysqlbinlog_LDADD = $(LDADD) $(CXXLDFLA mysqldump_SOURCES= mysqldump.c \ my_user.c \ - $(top_srcdir)/mysys/mf_getdate.c + $(top_srcdir)/mysys/mf_getdate.c \ + $(top_srcdir)/mysys/my_mkdir.c \ + $(top_srcdir)/mysys/my_copy.c mysqlimport_SOURCES= mysqlimport.c ===== client/client_priv.h 1.93 vs edited ===== --- 1.93/client/client_priv.h 2008-02-01 15:08:31 +01:00 +++ edited/client/client_priv.h 2008-04-29 19:01:41 +02:00 @@ -92,5 +92,6 @@ enum options_client OPT_AUTO_VERTICAL_OUTPUT, OPT_DEBUG_INFO, OPT_DEBUG_CHECK, OPT_COLUMN_TYPES, OPT_ERROR_LOG_FILE, OPT_WRITE_BINLOG, OPT_DUMP_DATE, - OPT_MAX_CLIENT_OPTION + OPT_MAX_CLIENT_OPTION, + OPT_CSV }; ===== client/mysqldump.c 1.326 vs edited ===== --- 1.326/client/mysqldump.c 2008-04-07 17:26:35 +02:00 +++ edited/client/mysqldump.c 2008-04-30 16:56:12 +02:00 @@ -91,7 +91,7 @@ static my_bool verbose= 0, opt_no_creat opt_delayed=0,create_options=1,opt_quoted=0,opt_databases=0, opt_alldbs=0,opt_create_db=0,opt_lock_all_tables=0, opt_set_charset=0, opt_dump_date=1, - opt_autocommit=0,opt_disable_keys=1,opt_xml=0, + opt_autocommit=0,opt_disable_keys=1,opt_tab=0,opt_xml=0,opt_csv=0, opt_delete_master_logs=0, tty_password=0, opt_single_transaction=0, opt_comments= 0, opt_compact= 0, opt_hex_blob=0, opt_order_by_primary=0, opt_ignore=0, @@ -243,6 +243,8 @@ static struct my_option my_long_options[ "Include all MySQL specific create options.", (uchar**) &create_options, (uchar**) &create_options, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0}, + {"csv", OPT_CSV, "Creates in the given location a directory per database, and dumps each table in seperate files: DDL and CSV data are seperated. Can dump all or selected databases.", + (uchar**) &path, (uchar**) &path, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"databases", 'B', "To dump several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames. 'USE db_name;' will be included in the output.", (uchar**) &opt_databases, (uchar**) &opt_databases, 0, GET_BOOL, NO_ARG, 0, 0, @@ -616,7 +618,7 @@ static void write_header(FILE *sql_file, fputs(">\n", sql_file); check_io(sql_file); } - else if (!opt_compact) + else if (!opt_csv && !opt_compact) { if (opt_comments) { @@ -644,7 +646,7 @@ static void write_header(FILE *sql_file, fprintf(sql_file, "/*!40103 SET TIME_ZONE='+00:00' */;\n"); } - if (!path) + if (!opt_tab) { fprintf(md_result_file,"\ /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n\ @@ -654,7 +656,7 @@ static void write_header(FILE *sql_file, fprintf(sql_file, "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='%s%s%s' */;\n" "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n", - path?"":"NO_AUTO_VALUE_ON_ZERO",compatible_mode_normal_str[0]==0?"":",", + opt_tab?"":"NO_AUTO_VALUE_ON_ZERO",compatible_mode_normal_str[0]==0?"":",", compatible_mode_normal_str); check_io(sql_file); } @@ -668,13 +670,13 @@ static void write_footer(FILE *sql_file) fputs("\n", sql_file); check_io(sql_file); } - else if (!opt_compact) + else if (!opt_csv && !opt_compact) { if (opt_tz_utc) fprintf(sql_file,"/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n"); fprintf(sql_file,"\n/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n"); - if (!path) + if (!opt_tab) { fprintf(md_result_file,"\ /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n\ @@ -757,6 +759,7 @@ get_one_option(int optid, const struct m opt_set_charset= 0; break; case 'T': + opt_tab= 1; opt_disable_keys=0; if (strlen(argument) >= FN_REFLEN) @@ -782,6 +785,15 @@ get_one_option(int optid, const struct m extended_insert= opt_drop= opt_lock= opt_disable_keys= opt_autocommit= opt_create_db= 0; break; + case (int) OPT_CSV: + opt_csv= 1; + if (strlen(argument) >= FN_REFLEN) + { + die(EX_USAGE, "Input filename to long: %s", argument); + } + extended_insert= opt_drop= opt_lock= opt_comments= + opt_disable_keys= opt_autocommit= opt_create_db= 0; + break; case 'I': case '?': usage(); @@ -922,11 +934,11 @@ static int get_options(int *argc, char * if (opt_delayed) opt_lock=0; /* Can't have lock with delayed */ - if (!path && (enclosed || opt_enclosed || escaped || lines_terminated || + if (!(opt_tab || opt_csv) && (enclosed || opt_enclosed || escaped || lines_terminated || fields_terminated)) { fprintf(stderr, - "%s: You must use option --tab with --fields-...\n", my_progname); + "%s: You must use option %s with --fields-...\n", my_progname, opt_tab ? "--tab" : "--csv"); return(EX_USAGE); } @@ -959,7 +971,7 @@ static int get_options(int *argc, char * fprintf(stderr, "%s: You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n", my_progname); return(EX_USAGE); } - if ((opt_databases || opt_alldbs) && path) + if ((opt_databases || opt_alldbs) && opt_tab) { fprintf(stderr, "%s: --databases or --all-databases can't be used with --tab.\n", @@ -1441,17 +1453,32 @@ static char *cover_definer_clause_in_sp( SYNOPSIS open_sql_file_for_table - name name of the table or view + table name of the table or view + database database in which table or view is located RETURN VALUES 0 Failed to open file > 0 Handle of the open file */ -static FILE* open_sql_file_for_table(const char* table) +static FILE* open_sql_file_for_table(const char* table, const char* db) { + DBUG_ENTER("open_sql_file_for_table"); FILE* res; - char filename[FN_REFLEN], tmp_path[FN_REFLEN]; - convert_dirname(tmp_path,path,NullS); + + char buff[FN_REFLEN], filename[FN_REFLEN], tmp_path[FN_REFLEN]; + if( opt_csv ) + { + my_snprintf(buff, sizeof(buff), "%s/%s", path, db); + if( !my_mkdir(buff, 0700, MYF(0))) + DBUG_PRINT("enter", ("dir: '%s'", buff)); + // Do nothing when it's there already.. + convert_dirname(tmp_path,buff,NullS); + } + else + { + convert_dirname(tmp_path,path,NullS); + } + res= my_fopen(fn_format(filename, table, tmp_path, ".sql", 4), O_WRONLY, MYF(MY_WME)); return res; @@ -2335,9 +2362,9 @@ static uint get_table_structure(char *ta switch_character_set_results(mysql, default_charset)) DBUG_RETURN(0); - if (path) + if (opt_tab || opt_csv) { - if (!(sql_file= open_sql_file_for_table(table))) + if (!(sql_file= open_sql_file_for_table(table,db))) DBUG_RETURN(0); write_header(sql_file, db); @@ -2455,7 +2482,7 @@ static uint get_table_structure(char *ta mysql_free_result(result); - if (path) + if (opt_tab || opt_csv) my_fclose(sql_file, MYF(MY_WME)); seen_views= 1; @@ -2464,13 +2491,22 @@ static uint get_table_structure(char *ta row= mysql_fetch_row(result); - fprintf(sql_file, + if( opt_csv ) + { + fprintf(sql_file, + "-- Data saved as CSV in file %s/%s.csv\n" + "%s;\n", + db,table,row[1]); + } + else + { + fprintf(sql_file, "SET @saved_cs_client = @@character_set_client;\n" "SET character_set_client = utf8;\n" "%s;\n" "SET character_set_client = @saved_cs_client;\n", row[1]); - + } check_io(sql_file); mysql_free_result(result); } @@ -2478,7 +2514,7 @@ static uint get_table_structure(char *ta result_table); if (mysql_query_with_error_report(mysql, &result, query_buff)) { - if (path) + if (opt_tab || opt_csv) my_fclose(sql_file, MYF(MY_WME)); DBUG_RETURN(0); } @@ -2536,12 +2572,12 @@ static uint get_table_structure(char *ta if (mysql_query_with_error_report(mysql, &result, query_buff)) DBUG_RETURN(0); - /* Make an sql-file, if path was given iow. option -T was given */ + /* Make an sql-file, if path was given iow. option -T or --csv was given */ if (!opt_no_create_info) { - if (path) + if (opt_tab || opt_csv) { - if (!(sql_file= open_sql_file_for_table(table))) + if (!(sql_file= open_sql_file_for_table(table,db))) DBUG_RETURN(0); write_header(sql_file, db); } @@ -2640,7 +2676,7 @@ static uint get_table_structure(char *ta } fprintf(stderr, "%s: Can't get keys for table %s (%s)\n", my_progname, result_table, mysql_error(mysql)); - if (path) + if (opt_tab || opt_csv) my_fclose(sql_file, MYF(MY_WME)); DBUG_RETURN(0); } @@ -3083,6 +3119,7 @@ static void dump_table(char *table, char MYSQL_RES *res; MYSQL_FIELD *field; MYSQL_ROW row; + FILE *orig_md_result_file= 0; DBUG_ENTER("dump_table"); /* @@ -3145,8 +3182,30 @@ static void dump_table(char *table, char init_dynamic_string_checked(&query_string, "", 1024, 1024); - if (path) + if (opt_csv) + { + char filename[FN_REFLEN], tmp_path[FN_REFLEN], fnbuff[FN_REFLEN]; + verbose_msg("-- Dumping table as CSV"); + + my_snprintf(fnbuff, sizeof(fnbuff), "%s/%s", path, db); + if( !my_mkdir(fnbuff, 0700, MYF(0))) + DBUG_PRINT("enter", ("dir: '%s'", buff)); + // Do nothing when it's there already.. + + convert_dirname(tmp_path,fnbuff,NullS); + my_load_path(tmp_path, tmp_path, NULL); + fn_format(filename, table, tmp_path, ".csv", MYF(MY_UNPACK_FILENAME)); + my_delete(filename, MYF(0)); + + orig_md_result_file= md_result_file; + if (!(md_result_file= my_fopen(filename, O_WRONLY | FILE_BINARY, + MYF(MY_WME)))) + exit(1); + } + + if (opt_tab) { + verbose_msg("-- Creating SELECT INTO OUTFILE\n"); char filename[FN_REFLEN], tmp_path[FN_REFLEN]; /* @@ -3202,7 +3261,7 @@ static void dump_table(char *table, char } else { - if (!opt_xml && opt_comments) + if (!(opt_xml || opt_csv) && opt_comments) { fprintf(md_result_file,"\n--\n-- Dumping data for table %s\n--\n", result_table); @@ -3214,7 +3273,7 @@ static void dump_table(char *table, char if (where) { - if (!opt_xml && opt_comments) + if (!(opt_xml || opt_csv) && opt_comments) { fprintf(md_result_file, "-- WHERE: %s\n", where); check_io(md_result_file); @@ -3225,7 +3284,7 @@ static void dump_table(char *table, char } if (order_by) { - if (!opt_xml && opt_comments) + if (!(opt_xml || opt_csv) && opt_comments) { fprintf(md_result_file, "-- ORDER BY: %s\n", order_by); check_io(md_result_file); @@ -3234,7 +3293,7 @@ static void dump_table(char *table, char dynstr_append_checked(&query_string, order_by); } - if (!opt_xml && !opt_compact) + if (!(opt_xml || opt_csv) && !opt_compact) { fputs("\n", md_result_file); check_io(md_result_file); @@ -3263,13 +3322,13 @@ static void dump_table(char *table, char goto err; } - if (opt_lock) + if (!opt_csv && opt_lock) { fprintf(md_result_file,"LOCK TABLES %s WRITE;\n", opt_quoted_table); check_io(md_result_file); } /* Moved disable keys to after lock per bug 15977 */ - if (opt_disable_keys) + if (!opt_csv && opt_disable_keys) { fprintf(md_result_file, "/*!40000 ALTER TABLE %s DISABLE KEYS */;\n", opt_quoted_table); @@ -3283,7 +3342,7 @@ static void dump_table(char *table, char if (opt_xml) print_xml_tag(md_result_file, "\t", "\n", "table_data", "name=", table, NullS); - if (opt_autocommit) + if (!opt_csv && opt_autocommit) { fprintf(md_result_file, "set autocommit=0;\n"); check_io(md_result_file); @@ -3294,7 +3353,7 @@ static void dump_table(char *table, char uint i; ulong *lengths= mysql_fetch_lengths(res); rownr++; - if (!extended_insert && !opt_xml) + if (!extended_insert && !(opt_xml || opt_csv)) { fputs(insert_pat.str,md_result_file); check_io(md_result_file); @@ -3403,7 +3462,12 @@ static void dump_table(char *table, char } else { - if (i && !opt_xml) + if (i && !(opt_xml || opt_csv)) + { + fputc(',', md_result_file); + check_io(md_result_file); + } + else if (i && opt_csv) { fputc(',', md_result_file); check_io(md_result_file); @@ -3466,9 +3530,9 @@ static void dump_table(char *table, char else { /* The field value is NULL */ - if (!opt_xml) + if (!(opt_xml || opt_csv)) fputs("NULL", md_result_file); - else + else if (opt_xml) print_xml_null_tag(md_result_file, "\t\t", "field name=", field->name, "\n"); } @@ -3481,7 +3545,7 @@ static void dump_table(char *table, char fputs("\t\n", md_result_file); check_io(md_result_file); } - + if (extended_insert) { ulong row_length; @@ -3505,17 +3569,23 @@ static void dump_table(char *table, char } check_io(md_result_file); } - else if (!opt_xml) + else if (!(opt_xml || opt_csv)) { fputs(");\n", md_result_file); check_io(md_result_file); } + else if (opt_csv) + { + fputc('\n', md_result_file); + check_io(md_result_file); + } + } /* XML - close table tag and supress regular output */ if (opt_xml) fputs("\t\n", md_result_file); - else if (extended_insert && row_break) + else if (!opt_csv && extended_insert && row_break) fputs(";\n", md_result_file); /* If not empty table */ fflush(md_result_file); check_io(md_result_file); @@ -3534,28 +3604,40 @@ static void dump_table(char *table, char } /* Moved enable keys to before unlock per bug 15977 */ - if (opt_disable_keys) + if (!opt_csv && opt_disable_keys) { fprintf(md_result_file,"/*!40000 ALTER TABLE %s ENABLE KEYS */;\n", opt_quoted_table); check_io(md_result_file); } - if (opt_lock) + if (!opt_csv && opt_lock) { fputs("UNLOCK TABLES;\n", md_result_file); check_io(md_result_file); } - if (opt_autocommit) + if (!opt_csv && opt_autocommit) { fprintf(md_result_file, "commit;\n"); check_io(md_result_file); } mysql_free_result(res); } + if( orig_md_result_file ) + { + fflush(md_result_file); + my_fclose(md_result_file, MYF(MY_WME)); + md_result_file = orig_md_result_file; + } dynstr_free(&query_string); DBUG_VOID_RETURN; err: + if( orig_md_result_file ) + { + fflush(md_result_file); + my_fclose(md_result_file, MYF(MY_WME)); + md_result_file = orig_md_result_file; + } dynstr_free(&query_string); maybe_exit(error); DBUG_VOID_RETURN; @@ -3982,7 +4064,7 @@ static int init_dumping(char *database, DB_error(mysql, "when selecting the database"); return 1; /* If --force */ } - if (!path && !opt_xml) + if (!opt_tab && !opt_xml && !opt_csv) { if (opt_databases || opt_alldbs) { @@ -4073,7 +4155,7 @@ static int dump_all_tables_in_db(char *d { if (dump_triggers_for_table(table, database)) { - if (path) + if (opt_tab) my_fclose(md_result_file, MYF(MY_WME)); maybe_exit(EX_MYSQLERR); } @@ -4306,7 +4388,7 @@ static int dump_selected_tables(char *db { if (dump_triggers_for_table(*pos, db)) { - if (path) + if (opt_tab) my_fclose(md_result_file, MYF(MY_WME)); maybe_exit(EX_MYSQLERR); } @@ -4939,9 +5021,9 @@ static my_bool get_view_structure(char * } /* If requested, open separate .sql file for this view */ - if (path) + if (opt_tab) { - if (!(sql_file= open_sql_file_for_table(table))) + if (!(sql_file= open_sql_file_for_table(table,db))) DBUG_RETURN(1); write_header(sql_file, db); @@ -5162,7 +5244,7 @@ int main(int argc, char **argv) free_resources(0); exit(EX_MYSQLERR); } - if (!path) + if (!opt_tab) write_header(md_result_file, *argv); if (opt_slave_data && do_stop_slave_sql(mysql)) @@ -5250,7 +5332,7 @@ int main(int argc, char **argv) */ err: dbDisconnect(current_host); - if (!path) + if (!opt_tab) write_footer(md_result_file); free_resources();