Commit db7357a9 authored by unknown's avatar unknown

fix xml-output of mysqldump


client/mysqldump.c:
  fix xml-output
parent 20243b65
...@@ -294,7 +294,10 @@ static void short_usage(void) ...@@ -294,7 +294,10 @@ static void short_usage(void)
static void write_header(FILE *sql_file, char *db_name) static void write_header(FILE *sql_file, char *db_name)
{ {
if (opt_xml) if (opt_xml)
{
fprintf(sql_file,"<?xml version=\"1.0\"?>\n"); fprintf(sql_file,"<?xml version=\"1.0\"?>\n");
fprintf(sql_file,"<mysqldump>\n");
}
else else
{ {
fprintf(sql_file, "-- MySQL dump %s\n--\n", DUMP_VERSION); fprintf(sql_file, "-- MySQL dump %s\n--\n", DUMP_VERSION);
...@@ -308,6 +311,12 @@ static void write_header(FILE *sql_file, char *db_name) ...@@ -308,6 +311,12 @@ static void write_header(FILE *sql_file, char *db_name)
return; return;
} /* write_header */ } /* write_header */
static void write_footer(FILE *sql_file)
{
if (opt_xml)
fprintf(sql_file,"</mysqldump>");
fputs("\n", sql_file);
} /* write_footer */
static my_bool static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)), get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
...@@ -978,7 +987,7 @@ static void dumpTable(uint numFields, char *table) ...@@ -978,7 +987,7 @@ static void dumpTable(uint numFields, char *table)
rownr=0; rownr=0;
init_length=(uint) strlen(insert_pat)+4; init_length=(uint) strlen(insert_pat)+4;
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "\t<%s>\n", table); fprintf(md_result_file, "\t<table name=\"%s\">\n", table);
if (opt_autocommit) if (opt_autocommit)
fprintf(md_result_file, "set autocommit=0;\n"); fprintf(md_result_file, "set autocommit=0;\n");
...@@ -1067,8 +1076,8 @@ static void dumpTable(uint numFields, char *table) ...@@ -1067,8 +1076,8 @@ static void dumpTable(uint numFields, char *table)
/* change any strings ("inf","nan",..) into NULL */ /* change any strings ("inf","nan",..) into NULL */
char *ptr = row[i]; char *ptr = row[i];
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "\t\t<%s>%s</%s>\n", fprintf(md_result_file, "\t\t<field name=\"%s\">%s</field>\n",
field->name,!isalpha(*ptr) ?ptr: "NULL",field->name); field->name,!isalpha(*ptr) ?ptr: "NULL");
else else
fputs((!isalpha(*ptr)) ? ptr : "NULL", md_result_file); fputs((!isalpha(*ptr)) ? ptr : "NULL", md_result_file);
} }
...@@ -1076,8 +1085,8 @@ static void dumpTable(uint numFields, char *table) ...@@ -1076,8 +1085,8 @@ static void dumpTable(uint numFields, char *table)
else else
{ {
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "\t\t<%s>%s</%s>\n", fprintf(md_result_file, "\t\t<field name=\"%s\">%s</field>\n",
field->name, "NULL", field->name); field->name, "NULL");
else else
fputs("NULL", md_result_file); fputs("NULL", md_result_file);
} }
...@@ -1118,7 +1127,7 @@ static void dumpTable(uint numFields, char *table) ...@@ -1118,7 +1127,7 @@ static void dumpTable(uint numFields, char *table)
/* XML - close table tag and supress regular output */ /* XML - close table tag and supress regular output */
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "\t</%s>\n", table); fprintf(md_result_file, "\t</table>\n");
else if (extended_insert && row_break) else if (extended_insert && row_break)
fputs(";\n", md_result_file); /* If not empty table */ fputs(";\n", md_result_file); /* If not empty table */
fflush(md_result_file); fflush(md_result_file);
...@@ -1150,7 +1159,7 @@ static void print_quoted_xml(FILE *output, char *fname, char *str, uint len) ...@@ -1150,7 +1159,7 @@ static void print_quoted_xml(FILE *output, char *fname, char *str, uint len)
{ {
const char *end; const char *end;
fprintf(output, "\t\t<%s>", fname); fprintf(output, "\t\t<field name=\"%s\">", fname);
for (end = str + len; str != end; str++) for (end = str + len; str != end; str++)
{ {
if (*str == '<') if (*str == '<')
...@@ -1164,7 +1173,7 @@ static void print_quoted_xml(FILE *output, char *fname, char *str, uint len) ...@@ -1164,7 +1173,7 @@ static void print_quoted_xml(FILE *output, char *fname, char *str, uint len)
else else
fputc(*str, output); fputc(*str, output);
} }
fprintf(output, "</%s>\n", fname); fprintf(output, "</field>\n");
} }
static char *getTableName(int reset) static char *getTableName(int reset)
...@@ -1219,13 +1228,8 @@ static int dump_databases(char **db_names) ...@@ -1219,13 +1228,8 @@ static int dump_databases(char **db_names)
int result=0; int result=0;
for ( ; *db_names ; db_names++) for ( ; *db_names ; db_names++)
{ {
/* XML edit - add database element */
if (opt_xml)
fprintf(md_result_file, "<%s>\n", *db_names);
if (dump_all_tables_in_db(*db_names)) if (dump_all_tables_in_db(*db_names))
result=1; result=1;
if (opt_xml)
fprintf(md_result_file, "</%s>\n", *db_names);
} }
return result; return result;
} /* dump_databases */ } /* dump_databases */
...@@ -1238,7 +1242,7 @@ static int init_dumping(char *database) ...@@ -1238,7 +1242,7 @@ static int init_dumping(char *database)
DBerror(sock, "when selecting the database"); DBerror(sock, "when selecting the database");
return 1; /* If --force */ return 1; /* If --force */
} }
if (!path) if (!path && !opt_xml)
{ {
if (opt_databases || opt_alldbs) if (opt_databases || opt_alldbs)
{ {
...@@ -1264,6 +1268,8 @@ static int dump_all_tables_in_db(char *database) ...@@ -1264,6 +1268,8 @@ static int dump_all_tables_in_db(char *database)
if (init_dumping(database)) if (init_dumping(database))
return 1; return 1;
if (opt_xml)
fprintf(md_result_file, "<database name=\"%s\">\n", database);
if (lock_tables) if (lock_tables)
{ {
DYNAMIC_STRING query; DYNAMIC_STRING query;
...@@ -1290,6 +1296,8 @@ static int dump_all_tables_in_db(char *database) ...@@ -1290,6 +1296,8 @@ static int dump_all_tables_in_db(char *database)
if (!dFlag && numrows > 0) if (!dFlag && numrows > 0)
dumpTable(numrows,table); dumpTable(numrows,table);
} }
if (opt_xml)
fprintf(md_result_file, "</database>\n");
if (lock_tables) if (lock_tables)
mysql_query(sock,"UNLOCK_TABLES"); mysql_query(sock,"UNLOCK_TABLES");
return 0; return 0;
...@@ -1326,12 +1334,16 @@ static int dump_selected_tables(char *db, char **table_names, int tables) ...@@ -1326,12 +1334,16 @@ static int dump_selected_tables(char *db, char **table_names, int tables)
DBerror(sock, "when doing refresh"); DBerror(sock, "when doing refresh");
/* We shall countinue here, if --force was given */ /* We shall countinue here, if --force was given */
} }
if (opt_xml)
fprintf(md_result_file, "<database name=\"%s\">\n", db);
for (; tables > 0 ; tables-- , table_names++) for (; tables > 0 ; tables-- , table_names++)
{ {
numrows = getTableStructure(*table_names, db); numrows = getTableStructure(*table_names, db);
if (!dFlag && numrows > 0) if (!dFlag && numrows > 0)
dumpTable(numrows, *table_names); dumpTable(numrows, *table_names);
} }
if (opt_xml)
fprintf(md_result_file, "</database>\n");
if (lock_tables) if (lock_tables)
mysql_query(sock,"UNLOCK_TABLES"); mysql_query(sock,"UNLOCK_TABLES");
return 0; return 0;
...@@ -1462,7 +1474,7 @@ int main(int argc, char **argv) ...@@ -1462,7 +1474,7 @@ int main(int argc, char **argv)
} }
} }
dbDisconnect(current_host); dbDisconnect(current_host);
fputs("\n", md_result_file); write_footer(md_result_file);
if (md_result_file != stdout) if (md_result_file != stdout)
my_fclose(md_result_file, MYF(0)); my_fclose(md_result_file, MYF(0));
my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR)); my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment