Commit d8f39341 authored by unknown's avatar unknown

Change 'mysql' client to output XML like the 'mysqldump'

tool does, with the column names as attributes on <field>
elements, instead of trying to use the column name as the
element name. Also fix some encoding issues. (Bug #7811)


client/mysql.cc:
  Quote > and " in XML output, and use <field name="XXX"></field>
  instead of <XXX></XXX>, to make the output more like
  mysqldump --xml and avoid having to turn XXX into a sensible
  element name.
parent 4bb1c716
......@@ -154,6 +154,8 @@ static char mysql_charsets_dir[FN_REFLEN+1];
static const char *xmlmeta[] = {
"&", "&amp;",
"<", "&lt;",
">", "&gt;",
"\"", "&quot;",
0, 0
};
static const char *day_names[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
......@@ -2116,13 +2118,11 @@ print_table_data_xml(MYSQL_RES *result)
(void) tee_fputs("\n <row>\n", PAGER);
for (uint i=0; i < mysql_num_fields(result); i++)
{
tee_fprintf(PAGER, "\t<%s>", (fields[i].name ?
(fields[i].name[0] ? fields[i].name :
" &nbsp; ") : "NULL"));
tee_fprintf(PAGER, "\t<field name=\"");
xmlencode_print(fields[i].name, strlen(fields[i].name));
tee_fprintf(PAGER, "\">");
xmlencode_print(cur[i], lengths[i]);
tee_fprintf(PAGER, "</%s>\n", (fields[i].name ?
(fields[i].name[0] ? fields[i].name :
" &nbsp; ") : "NULL"));
tee_fprintf(PAGER, "</field>\n");
}
(void) tee_fputs(" </row>\n", PAGER);
}
......
create table t1 (
`a&b` int,
`a<b` int,
`a>b` text
);
insert into t1 values (1, 2, 'a&b a<b a>b');
<?xml version="1.0"?>
<resultset statement="select * from t1
">
<row>
<field name="a&amp;b">1</field>
<field name="a&lt;b">2</field>
<field name="a&gt;b">a&amp;b a&lt;b a&gt;b</field>
</row>
</resultset>
<?xml version="1.0"?>
<mysqldump>
<database name="test">
<table_structure name="t1">
<field Field="a&amp;b" Type="int(11)" Null="YES" Key="" Extra="" />
<field Field="a&lt;b" Type="int(11)" Null="YES" Key="" Extra="" />
<field Field="a&gt;b" Type="text" Null="YES" Key="" Extra="" />
<options Name="t1" Engine="MyISAM" Version="9" Row_format="Dynamic" Rows="1" Avg_row_length="28" Data_length="28" Max_data_length="4294967295" Index_length="1024" Data_free="0" Create_time="2005-01-26 01:21:39" Update_time="2005-01-26 01:21:39" Collation="latin1_swedish_ci" Create_options="" Comment="" />
</table_structure>
<table_data name="t1">
<row>
<field name="a&amp;b">1</field>
<field name="a&lt;b">2</field>
<field name="a&gt;b">a&amp;b a&lt;b a&gt;b</field>
</row>
</table_data>
</database>
</mysqldump>
<?xml version="1.0"?>
<resultset statement="select count(*) from t1
">
<row>
<field name="count(*)">1</field>
</row>
</resultset>
<?xml version="1.0"?>
<resultset statement="select 1 &lt; 2 from dual
">
<row>
<field name="1 &lt; 2">1</field>
</row>
</resultset>
<?xml version="1.0"?>
<resultset statement="select 1 &gt; 2 from dual
">
<row>
<field name="1 &gt; 2">0</field>
</row>
</resultset>
<?xml version="1.0"?>
<resultset statement="select 1 &amp; 3 from dual
">
<row>
<field name="1 &amp; 3">1</field>
</row>
</resultset>
<?xml version="1.0"?>
<resultset statement="select null from dual
">
<row>
<field name="NULL">NULL</field>
</row>
</resultset>
drop table t1;
# Test of the xml output of the 'mysql' and 'mysqldump' clients -- makes
# sure that basic encoding issues are handled properly
create table t1 (
`a&b` int,
`a<b` int,
`a>b` text
);
insert into t1 values (1, 2, 'a&b a<b a>b');
--exec $MYSQL --xml test -e 'select * from t1'
--exec $MYSQL_DUMP --xml test
--exec $MYSQL --xml test -e 'select count(*) from t1'
--exec $MYSQL --xml test -e 'select 1 < 2 from dual'
--exec $MYSQL --xml test -e 'select 1 > 2 from dual'
--exec $MYSQL --xml test -e 'select 1 & 3 from dual'
--exec $MYSQL --xml test -e 'select null from dual'
drop table t1;
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