Commit b6b4c5b1 authored by unknown's avatar unknown

WL #1279: Add table description to xml dump (SCRUM)

Bug #1707: mysqldump -X does't quote field and table names
code cleanup

parent 87eb9ea2
...@@ -68,6 +68,13 @@ ...@@ -68,6 +68,13 @@
/* Size of buffer for dump's select query */ /* Size of buffer for dump's select query */
#define QUERY_LENGTH 1536 #define QUERY_LENGTH 1536
#define print_xml_tag(out_file, sbeg, sval, send) \
{ \
fputs(sbeg, out_file); \
print_quoted_xml(out_file, sval, 0); \
fputs(send, out_file); \
}
static char *add_load_option(char *ptr, const char *object, static char *add_load_option(char *ptr, const char *object,
const char *statement); const char *statement);
static ulong find_set(TYPELIB *lib, const char *x, uint length, static ulong find_set(TYPELIB *lib, const char *x, uint length,
...@@ -298,7 +305,7 @@ static int init_dumping(char *); ...@@ -298,7 +305,7 @@ static int init_dumping(char *);
static int dump_databases(char **); static int dump_databases(char **);
static int dump_all_databases(); static int dump_all_databases();
static char *quote_name(const char *name, char *buff, my_bool force); static char *quote_name(const char *name, char *buff, my_bool force);
static void print_quoted_xml(FILE *output, char *fname, char *str, uint len); static void print_quoted_xml(FILE *output, char *str, ulong len);
static void print_version(void) static void print_version(void)
{ {
...@@ -339,8 +346,8 @@ static void write_header(FILE *sql_file, char *db_name) ...@@ -339,8 +346,8 @@ static void write_header(FILE *sql_file, char *db_name)
{ {
if (opt_xml) if (opt_xml)
{ {
fprintf(sql_file,"<?xml version=\"1.0\"?>\n"); fputs("<?xml version=\"1.0\"?>\n", sql_file);
fprintf(sql_file,"<mysqldump>\n"); fputs("<mysqldump>\n", sql_file);
} }
else else
{ {
...@@ -366,7 +373,7 @@ static void write_header(FILE *sql_file, char *db_name) ...@@ -366,7 +373,7 @@ static void write_header(FILE *sql_file, char *db_name)
static void write_footer(FILE *sql_file) static void write_footer(FILE *sql_file)
{ {
if (opt_xml) if (opt_xml)
fprintf(sql_file,"</mysqldump>"); fputs("</mysqldump>", sql_file);
else else
{ {
fprintf(md_result_file,"\n\ fprintf(md_result_file,"\n\
...@@ -651,6 +658,29 @@ static char *quote_name(const char *name, char *buff, my_bool force) ...@@ -651,6 +658,29 @@ static char *quote_name(const char *name, char *buff, my_bool force)
} /* quote_name */ } /* quote_name */
void print_xml_row(FILE *xml_file, const char *row_name, MYSQL_RES *tableRes,
MYSQL_ROW *row)
{
uint i;
MYSQL_FIELD *field;
ulong *lengths= mysql_fetch_lengths(tableRes);
fprintf(xml_file, "\t\t<%s", row_name);
mysql_field_seek(tableRes, 0);
for (i= 0; (field= mysql_fetch_field(tableRes)); i++)
{
if ((*row)[i] && (*row)[i][0])
{
fputs(" ", xml_file);
print_quoted_xml(xml_file, field->name, 0);
fputs("=\"", xml_file);
print_quoted_xml(xml_file, (*row)[i], lengths[i]);
fputs("\"", xml_file);
}
}
fputs(" />\n", xml_file);
}
/* /*
getStructure -- retrievs database structure, prints out corresponding getStructure -- retrievs database structure, prints out corresponding
CREATE statement and fills out insert_pat. CREATE statement and fills out insert_pat.
...@@ -680,7 +710,7 @@ static uint getTableStructure(char *table, char* db) ...@@ -680,7 +710,7 @@ static uint getTableStructure(char *table, char* db)
sprintf(insert_pat,"SET OPTION SQL_QUOTE_SHOW_CREATE=%d", (opt_quoted || opt_keywords)); sprintf(insert_pat,"SET OPTION SQL_QUOTE_SHOW_CREATE=%d", (opt_quoted || opt_keywords));
result_table= quote_name(table, table_buff, 1); result_table= quote_name(table, table_buff, 1);
opt_quoted_table= quote_name(table, table_buff2, 0); opt_quoted_table= quote_name(table, table_buff2, 0);
if (!mysql_query(sock,insert_pat)) if (!opt_xml && !mysql_query(sock,insert_pat))
{ {
/* using SHOW CREATE statement */ /* using SHOW CREATE statement */
if (!tFlag) if (!tFlag)
...@@ -735,7 +765,6 @@ static uint getTableStructure(char *table, char* db) ...@@ -735,7 +765,6 @@ static uint getTableStructure(char *table, char* db)
} }
write_header(sql_file, db); write_header(sql_file, db);
} }
if (!opt_xml)
fprintf(sql_file, "\n--\n-- Table structure for table %s\n--\n\n", fprintf(sql_file, "\n--\n-- Table structure for table %s\n--\n\n",
result_table); result_table);
if (opt_drop) if (opt_drop)
...@@ -743,7 +772,6 @@ static uint getTableStructure(char *table, char* db) ...@@ -743,7 +772,6 @@ static uint getTableStructure(char *table, char* db)
tableRes=mysql_store_result(sock); tableRes=mysql_store_result(sock);
row=mysql_fetch_row(tableRes); row=mysql_fetch_row(tableRes);
if (!opt_xml)
fprintf(sql_file, "%s;\n", row[1]); fprintf(sql_file, "%s;\n", row[1]);
mysql_free_result(tableRes); mysql_free_result(tableRes);
} }
...@@ -818,7 +846,10 @@ static uint getTableStructure(char *table, char* db) ...@@ -818,7 +846,10 @@ static uint getTableStructure(char *table, char* db)
result_table); result_table);
if (opt_drop) if (opt_drop)
fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n",result_table); fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n",result_table);
if (!opt_xml)
fprintf(sql_file, "CREATE TABLE %s (\n", result_table); fprintf(sql_file, "CREATE TABLE %s (\n", result_table);
else
print_xml_tag(sql_file, "\t<table_structure name=\"", table, "\">\n");
} }
if (cFlag) if (cFlag)
sprintf(insert_pat, "INSERT %sINTO %s (", delayed, result_table); sprintf(insert_pat, "INSERT %sINTO %s (", delayed, result_table);
...@@ -835,7 +866,7 @@ static uint getTableStructure(char *table, char* db) ...@@ -835,7 +866,7 @@ static uint getTableStructure(char *table, char* db)
ulong *lengths=mysql_fetch_lengths(tableRes); ulong *lengths=mysql_fetch_lengths(tableRes);
if (init) if (init)
{ {
if (!tFlag) if (!opt_xml && !tFlag)
fputs(",\n",sql_file); fputs(",\n",sql_file);
if (cFlag) if (cFlag)
strpos=strmov(strpos,", "); strpos=strmov(strpos,", ");
...@@ -845,6 +876,12 @@ static uint getTableStructure(char *table, char* db) ...@@ -845,6 +876,12 @@ static uint getTableStructure(char *table, char* db)
strpos=strmov(strpos,quote_name(row[SHOW_FIELDNAME], name_buff, 0)); strpos=strmov(strpos,quote_name(row[SHOW_FIELDNAME], name_buff, 0));
if (!tFlag) if (!tFlag)
{ {
if (opt_xml)
{
print_xml_row(sql_file, "field", tableRes, &row);
continue;
}
if (opt_keywords) if (opt_keywords)
fprintf(sql_file, " %s.%s %s", result_table, fprintf(sql_file, " %s.%s %s", result_table,
quote_name(row[SHOW_FIELDNAME],name_buff, 0), quote_name(row[SHOW_FIELDNAME],name_buff, 0),
...@@ -906,6 +943,12 @@ static uint getTableStructure(char *table, char* db) ...@@ -906,6 +943,12 @@ static uint getTableStructure(char *table, char* db)
keynr=0; keynr=0;
while ((row=mysql_fetch_row(tableRes))) while ((row=mysql_fetch_row(tableRes)))
{ {
if (opt_xml)
{
print_xml_row(sql_file, "key", tableRes, &row);
continue;
}
if (atoi(row[3]) == 1) if (atoi(row[3]) == 1)
{ {
if (keynr++) if (keynr++)
...@@ -924,14 +967,26 @@ static uint getTableStructure(char *table, char* db) ...@@ -924,14 +967,26 @@ static uint getTableStructure(char *table, char* db)
if (row[7]) if (row[7])
fprintf(sql_file, " (%s)",row[7]); /* Sub key */ fprintf(sql_file, " (%s)",row[7]); /* Sub key */
} }
if (!opt_xml)
{
if (keynr) if (keynr)
putc(')', sql_file); putc(')', sql_file);
fputs("\n)",sql_file); fputs("\n)",sql_file);
}
/* Get MySQL specific create options */ /* Get MySQL specific create options */
if (create_options) if (create_options)
{ {
sprintf(buff,"show table status like %s",result_table); ulong len= strlen(table);
char *tmp= (char*) my_malloc(len * 2 + 1, MYF(MY_WME));
if (!tmp)
{
ignore_errors= 0;
safe_exit(EX_MYSQLERR);
}
mysql_real_escape_string(&mysql_connection, tmp, table, len);
sprintf(buff,"show table status like \"%s\"", tmp);
my_free(tmp, MYF(MY_WME));
if (mysql_query(sock, buff)) if (mysql_query(sock, buff))
{ {
if (mysql_errno(sock) != ER_PARSE_ERROR) if (mysql_errno(sock) != ER_PARSE_ERROR)
...@@ -950,6 +1005,12 @@ static uint getTableStructure(char *table, char* db) ...@@ -950,6 +1005,12 @@ static uint getTableStructure(char *table, char* db)
result_table,mysql_error(sock)); result_table,mysql_error(sock));
} }
else else
{
if (opt_xml)
{
print_xml_row(sql_file, "options", tableRes, &row);
}
else
{ {
fputs("/*!",sql_file); fputs("/*!",sql_file);
print_value(sql_file,tableRes,row,"type=","Type",0); print_value(sql_file,tableRes,row,"type=","Type",0);
...@@ -957,9 +1018,13 @@ static uint getTableStructure(char *table, char* db) ...@@ -957,9 +1018,13 @@ static uint getTableStructure(char *table, char* db)
print_value(sql_file,tableRes,row,"comment=","Comment",1); print_value(sql_file,tableRes,row,"comment=","Comment",1);
fputs(" */",sql_file); fputs(" */",sql_file);
} }
}
mysql_free_result(tableRes); /* Is always safe to free */ mysql_free_result(tableRes); /* Is always safe to free */
} }
if (!opt_xml)
fputs(";\n", sql_file); fputs(";\n", sql_file);
else
fputs("\t</table_structure>\n", sql_file);
} }
} }
if (cFlag) if (cFlag)
...@@ -1127,7 +1192,9 @@ static void dumpTable(uint numFields, char *table) ...@@ -1127,7 +1192,9 @@ 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<table name=\"%s\">\n", table); {
print_xml_tag(md_result_file, "\t<table_data name=\"", table, "\">\n");
}
if (opt_autocommit) if (opt_autocommit)
fprintf(md_result_file, "set autocommit=0;\n"); fprintf(md_result_file, "set autocommit=0;\n");
...@@ -1142,7 +1209,7 @@ static void dumpTable(uint numFields, char *table) ...@@ -1142,7 +1209,7 @@ static void dumpTable(uint numFields, char *table)
mysql_field_seek(res,0); mysql_field_seek(res,0);
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "\t<row>\n"); fputs("\t<row>\n", md_result_file);
for (i = 0; i < mysql_num_fields(res); i++) for (i = 0; i < mysql_num_fields(res); i++)
{ {
...@@ -1207,8 +1274,12 @@ static void dumpTable(uint numFields, char *table) ...@@ -1207,8 +1274,12 @@ static void dumpTable(uint numFields, char *table)
if (!IS_NUM_FIELD(field)) if (!IS_NUM_FIELD(field))
{ {
if (opt_xml) if (opt_xml)
print_quoted_xml(md_result_file, field->name, row[i], {
lengths[i]); print_xml_tag(md_result_file, "\t\t<field name=\"", field->name,
"\">");
print_quoted_xml(md_result_file, row[i], lengths[i]);
fputs("</field>\n", md_result_file);
}
else else
unescape(md_result_file, row[i], lengths[i]); unescape(md_result_file, row[i], lengths[i]);
} }
...@@ -1217,9 +1288,13 @@ static void dumpTable(uint numFields, char *table) ...@@ -1217,9 +1288,13 @@ 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<field name=\"%s\">%s</field>\n", {
field->name, print_xml_tag(md_result_file, "\t\t<field name=\"", field->name,
!my_isalpha(charset_info, *ptr) ? ptr: "NULL"); "\">");
fputs(!my_isalpha(charset_info, *ptr) ? ptr: "NULL",
md_result_file);
fputs("</field>\n", md_result_file);
}
else else
fputs((!my_isalpha(charset_info,*ptr)) ? fputs((!my_isalpha(charset_info,*ptr)) ?
ptr : "NULL", md_result_file); ptr : "NULL", md_result_file);
...@@ -1228,8 +1303,10 @@ static void dumpTable(uint numFields, char *table) ...@@ -1228,8 +1303,10 @@ static void dumpTable(uint numFields, char *table)
else else
{ {
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "\t\t<field name=\"%s\">%s</field>\n", {
field->name, "NULL"); print_xml_tag(md_result_file, "\t\t<field name=\"", field->name,
"\">NULL</field>\n");
}
else else
fputs("NULL", md_result_file); fputs("NULL", md_result_file);
} }
...@@ -1237,7 +1314,7 @@ static void dumpTable(uint numFields, char *table) ...@@ -1237,7 +1314,7 @@ static void dumpTable(uint numFields, char *table)
} }
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "\t</row>\n"); fputs("\t</row>\n", md_result_file);
if (extended_insert) if (extended_insert)
{ {
...@@ -1267,7 +1344,7 @@ static void dumpTable(uint numFields, char *table) ...@@ -1267,7 +1344,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</table>\n"); fputs("\t</table_data>\n", md_result_file);
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);
...@@ -1295,25 +1372,30 @@ static void dumpTable(uint numFields, char *table) ...@@ -1295,25 +1372,30 @@ static void dumpTable(uint numFields, char *table)
} /* dumpTable */ } /* dumpTable */
static void print_quoted_xml(FILE *output, char *fname, char *str, uint len) static void print_quoted_xml(FILE *output, char *str, ulong len)
{ {
const char *end; const char *end= str + (len ? len : strlen(str));
fprintf(output, "\t\t<field name=\"%s\">", fname); for (; str != end; str++)
for (end = str + len; str != end; str++)
{ {
if (*str == '<') switch (*str) {
case '<':
fputs("&lt;", output); fputs("&lt;", output);
else if (*str == '>') break;
case '>':
fputs("&gt;", output); fputs("&gt;", output);
else if (*str == '&') break;
case '&':
fputs("&amp;", output); fputs("&amp;", output);
else if (*str == '\"') break;
case '\"':
fputs("&quot;", output); fputs("&quot;", output);
else break;
default:
fputc(*str, output); fputc(*str, output);
break;
}
} }
fprintf(output, "</field>\n");
} }
static char *getTableName(int reset) static char *getTableName(int reset)
...@@ -1434,7 +1516,9 @@ static int dump_all_tables_in_db(char *database) ...@@ -1434,7 +1516,9 @@ static int dump_all_tables_in_db(char *database)
if (init_dumping(database)) if (init_dumping(database))
return 1; return 1;
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "<database name=\"%s\">\n", database); {
print_xml_tag(md_result_file, "<database name=\"", database, "\">\n");
}
if (lock_tables) if (lock_tables)
{ {
DYNAMIC_STRING query; DYNAMIC_STRING query;
...@@ -1462,7 +1546,7 @@ static int dump_all_tables_in_db(char *database) ...@@ -1462,7 +1546,7 @@ static int dump_all_tables_in_db(char *database)
dumpTable(numrows,table); dumpTable(numrows,table);
} }
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "</database>\n"); fputs("</database>\n", md_result_file);
if (lock_tables) if (lock_tables)
mysql_query(sock,"UNLOCK_TABLES"); mysql_query(sock,"UNLOCK_TABLES");
return 0; return 0;
...@@ -1500,7 +1584,9 @@ static int dump_selected_tables(char *db, char **table_names, int tables) ...@@ -1500,7 +1584,9 @@ static int dump_selected_tables(char *db, char **table_names, int tables)
/* We shall countinue here, if --force was given */ /* We shall countinue here, if --force was given */
} }
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "<database name=\"%s\">\n", db); {
print_xml_tag(md_result_file, "<database name=\"", db, "\">\n");
}
for (; tables > 0 ; tables-- , table_names++) for (; tables > 0 ; tables-- , table_names++)
{ {
numrows = getTableStructure(*table_names, db); numrows = getTableStructure(*table_names, db);
...@@ -1508,7 +1594,7 @@ static int dump_selected_tables(char *db, char **table_names, int tables) ...@@ -1508,7 +1594,7 @@ static int dump_selected_tables(char *db, char **table_names, int tables)
dumpTable(numrows, *table_names); dumpTable(numrows, *table_names);
} }
if (opt_xml) if (opt_xml)
fprintf(md_result_file, "</database>\n"); fputs("</database>\n", md_result_file);
if (lock_tables) if (lock_tables)
mysql_query(sock,"UNLOCK_TABLES"); mysql_query(sock,"UNLOCK_TABLES");
return 0; return 0;
......
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