Commit 76a09831 authored by unknown's avatar unknown

mysqldump.c Dumping via SHOW CREATE added.

               Autofallback to the old behaviour for pre-3.23.26 servers.


client/mysqldump.c:
  Dumping via SHOW CREATE added.
  Autofallback to the old behaviour for pre-3.23.26 servers.
parent 293d7c83
...@@ -594,176 +594,258 @@ static uint getTableStructure(char *table, char* db) ...@@ -594,176 +594,258 @@ static uint getTableStructure(char *table, char* db)
if (verbose) if (verbose)
fprintf(stderr, "# Retrieving table structure for table %s...\n", table); fprintf(stderr, "# Retrieving table structure for table %s...\n", table);
sprintf(insert_pat,"SET OPTION SQL_QUOTE_SHOW_CREATE=%d", opt_quoted);
table_name=quote_name(table,table_buff); table_name=quote_name(table,table_buff);
sprintf(insert_pat,"show fields from %s",table_name);
if (mysql_query(sock,insert_pat) || !(tableRes=mysql_store_result(sock)))
{
fprintf(stderr, "%s: Can't get info about table: '%s'\nerror: %s\n",
my_progname, table, mysql_error(sock));
safe_exit(EX_MYSQLERR);
DBUG_RETURN(0);
}
/* Make an sql-file, if path was given iow. option -T was given */ if (mysql_query(sock,insert_pat))
if (!tFlag)
{ {
if (path) /* using SHOW CREATE statement */
if (!tFlag)
{ {
char filename[FN_REFLEN], tmp_path[FN_REFLEN]; /* Make an sql-file, if path was given iow. option -T was given */
strmov(tmp_path,path); char buff[20+FN_REFLEN];
convert_dirname(tmp_path);
sql_file= my_fopen(fn_format(filename, table, tmp_path, ".sql", 4), sprintf(buff,"show create table %s",table_name);
O_WRONLY, MYF(MY_WME)); if (mysql_query(sock, buff))
if (!sql_file) /* If file couldn't be opened */
{ {
safe_exit(EX_MYSQLERR); fprintf(stderr, "%s: Can't get CREATE TABLE for table '%s' (%s)\n",
DBUG_RETURN(0); my_progname, table, mysql_error(sock));
safe_exit(EX_MYSQLERR);
DBUG_RETURN(0);
} }
write_heder(sql_file, db);
}
fprintf(sql_file, "\n#\n# Table structure for table '%s'\n#\n\n", table);
if (opt_drop)
fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n",table_name);
fprintf(sql_file, "CREATE TABLE %s (\n", table_name);
}
if (cFlag)
sprintf(insert_pat, "INSERT %sINTO %s (", delayed, table_name);
else
{
sprintf(insert_pat, "INSERT %sINTO %s VALUES ", delayed, table_name);
if (!extended_insert)
strcat(insert_pat,"(");
}
strpos=strend(insert_pat); if (path)
while ((row=mysql_fetch_row(tableRes))) {
{ char filename[FN_REFLEN], tmp_path[FN_REFLEN];
ulong *lengths=mysql_fetch_lengths(tableRes); strmov(tmp_path,path);
if (init) convert_dirname(tmp_path);
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(0);
}
write_heder(sql_file, db);
}
fprintf(sql_file, "\n#\n# Table structure for table '%s'\n#\n\n", table);
if (opt_drop)
fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n",table_name);
tableRes=mysql_store_result(sock);
row=mysql_fetch_row(tableRes);
fprintf(sql_file, "%s;\n", row[1]);
mysql_free_result(tableRes);
}
sprintf(insert_pat,"show fields from %s",table_name);
if (mysql_query(sock,insert_pat) || !(tableRes=mysql_store_result(sock)))
{ {
if (!tFlag) fprintf(stderr, "%s: Can't get info about table: '%s'\nerror: %s\n",
fputs(",\n",sql_file); my_progname, table, mysql_error(sock));
if (cFlag) safe_exit(EX_MYSQLERR);
strpos=strmov(strpos,", "); DBUG_RETURN(0);
} }
init=1;
if (cFlag) if (cFlag)
strpos=strmov(strpos,quote_name(row[SHOW_FIELDNAME],name_buff)); sprintf(insert_pat, "INSERT %sINTO %s (", delayed, table_name);
if (!tFlag) else
{ {
if (opt_keywords) sprintf(insert_pat, "INSERT %sINTO %s VALUES ", delayed, table_name);
fprintf(sql_file, " %s.%s %s", table_name, if (!extended_insert)
quote_name(row[SHOW_FIELDNAME],name_buff), row[SHOW_TYPE]); strcat(insert_pat,"(");
else }
fprintf(sql_file, " %s %s", quote_name(row[SHOW_FIELDNAME],name_buff),
row[SHOW_TYPE]); strpos=strend(insert_pat);
if (row[SHOW_DEFAULT]) while ((row=mysql_fetch_row(tableRes)))
{
ulong *lengths=mysql_fetch_lengths(tableRes);
if (init)
{ {
fputs(" DEFAULT ", sql_file); if (cFlag)
unescape(sql_file,row[SHOW_DEFAULT],lengths[SHOW_DEFAULT]); strpos=strmov(strpos,", ");
} }
if (!row[SHOW_NULL][0]) init=1;
fputs(" NOT NULL", sql_file); if (cFlag)
if (row[SHOW_EXTRA][0]) strpos=strmov(strpos,quote_name(row[SHOW_FIELDNAME],name_buff));
fprintf(sql_file, " %s",row[SHOW_EXTRA]);
} }
numFields = (uint) mysql_num_rows(tableRes);
mysql_free_result(tableRes);
} }
numFields = (uint) mysql_num_rows(tableRes); else
mysql_free_result(tableRes);
if (!tFlag)
{ {
/* Make an sql-file, if path was given iow. option -T was given */ /* fprintf(stderr, "%s: Can't set SQL_QUOTE_SHOW_CREATE option (%s)\n",
char buff[20+FN_REFLEN]; my_progname, mysql_error(sock)); */
uint keynr,primary_key;
sprintf(buff,"show keys from %s",table_name); sprintf(insert_pat,"show fields from %s",table_name);
if (mysql_query(sock, buff)) if (mysql_query(sock,insert_pat) || !(tableRes=mysql_store_result(sock)))
{ {
fprintf(stderr, "%s: Can't get keys for table '%s' (%s)\n", fprintf(stderr, "%s: Can't get info about table: '%s'\nerror: %s\n",
my_progname, table, mysql_error(sock)); my_progname, table, mysql_error(sock));
if (sql_file != stdout)
my_fclose(sql_file, MYF(MY_WME));
safe_exit(EX_MYSQLERR); safe_exit(EX_MYSQLERR);
DBUG_RETURN(0); DBUG_RETURN(0);
} }
tableRes=mysql_store_result(sock); /* Make an sql-file, if path was given iow. option -T was given */
/* Find first which key is primary key */ if (!tFlag)
keynr=0;
primary_key=INT_MAX;
while ((row=mysql_fetch_row(tableRes)))
{ {
if (atoi(row[3]) == 1) if (path)
{ {
keynr++; char filename[FN_REFLEN], tmp_path[FN_REFLEN];
#ifdef FORCE_PRIMARY_KEY strmov(tmp_path,path);
if (atoi(row[1]) == 0 && primary_key == INT_MAX) convert_dirname(tmp_path);
primary_key=keynr; sql_file= my_fopen(fn_format(filename, table, tmp_path, ".sql", 4),
#endif O_WRONLY, MYF(MY_WME));
if (!strcmp(row[2],"PRIMARY")) if (!sql_file) /* If file couldn't be opened */
{ {
primary_key=keynr; safe_exit(EX_MYSQLERR);
break; DBUG_RETURN(0);
} }
write_heder(sql_file, db);
} }
fprintf(sql_file, "\n#\n# Table structure for table '%s'\n#\n\n", table);
if (opt_drop)
fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n",table_name);
fprintf(sql_file, "CREATE TABLE %s (\n", table_name);
}
if (cFlag)
sprintf(insert_pat, "INSERT %sINTO %s (", delayed, table_name);
else
{
sprintf(insert_pat, "INSERT %sINTO %s VALUES ", delayed, table_name);
if (!extended_insert)
strcat(insert_pat,"(");
} }
mysql_data_seek(tableRes,0);
keynr=0; strpos=strend(insert_pat);
while ((row=mysql_fetch_row(tableRes))) while ((row=mysql_fetch_row(tableRes)))
{ {
if (atoi(row[3]) == 1) ulong *lengths=mysql_fetch_lengths(tableRes);
if (init)
{ {
if (keynr++) if (!tFlag)
putc(')', sql_file); fputs(",\n",sql_file);
if (atoi(row[1])) /* Test if duplicate key */ if (cFlag)
/* Duplicate allowed */ strpos=strmov(strpos,", ");
fprintf(sql_file, ",\n KEY %s (",quote_name(row[2],name_buff)); }
else if (keynr == primary_key) init=1;
fputs(",\n PRIMARY KEY (",sql_file); /* First UNIQUE is primary */ if (cFlag)
else strpos=strmov(strpos,quote_name(row[SHOW_FIELDNAME],name_buff));
fprintf(sql_file, ",\n UNIQUE %s (",quote_name(row[2],name_buff)); if (!tFlag)
{
if (opt_keywords)
fprintf(sql_file, " %s.%s %s", table_name,
quote_name(row[SHOW_FIELDNAME],name_buff), row[SHOW_TYPE]);
else
fprintf(sql_file, " %s %s", quote_name(row[SHOW_FIELDNAME],name_buff),
row[SHOW_TYPE]);
if (row[SHOW_DEFAULT])
{
fputs(" DEFAULT ", sql_file);
unescape(sql_file,row[SHOW_DEFAULT],lengths[SHOW_DEFAULT]);
}
if (!row[SHOW_NULL][0])
fputs(" NOT NULL", sql_file);
if (row[SHOW_EXTRA][0])
fprintf(sql_file, " %s",row[SHOW_EXTRA]);
} }
else
putc(',', sql_file);
fputs(quote_name(row[4],name_buff), sql_file);
if (row[7])
fprintf(sql_file, "(%s)",row[7]); /* Sub key */
} }
if (keynr) numFields = (uint) mysql_num_rows(tableRes);
putc(')', sql_file); mysql_free_result(tableRes);
fputs("\n)",sql_file); if (!tFlag)
/* Get MySQL specific create options */
if (create_options)
{ {
sprintf(buff,"show table status like '%s'",table); /* Make an sql-file, if path was given iow. option -T was given */
char buff[20+FN_REFLEN];
uint keynr,primary_key;
sprintf(buff,"show keys from %s",table_name);
if (mysql_query(sock, buff)) if (mysql_query(sock, buff))
{ {
if (mysql_errno(sock) != ER_PARSE_ERROR) fprintf(stderr, "%s: Can't get keys for table '%s' (%s)\n",
{ /* If old MySQL version */ my_progname, table, mysql_error(sock));
if (verbose) if (sql_file != stdout)
fprintf(stderr, my_fclose(sql_file, MYF(MY_WME));
"# Warning: Couldn't get status information for table '%s' (%s)\n", safe_exit(EX_MYSQLERR);
table,mysql_error(sock)); DBUG_RETURN(0);
}
} }
else if (!(tableRes=mysql_store_result(sock)) ||
!(row=mysql_fetch_row(tableRes))) tableRes=mysql_store_result(sock);
/* Find first which key is primary key */
keynr=0;
primary_key=INT_MAX;
while ((row=mysql_fetch_row(tableRes)))
{ {
fprintf(stderr, if (atoi(row[3]) == 1)
"Error: Couldn't read status information for table '%s' (%s)\n", {
table,mysql_error(sock)); keynr++;
#ifdef FORCE_PRIMARY_KEY
if (atoi(row[1]) == 0 && primary_key == INT_MAX)
primary_key=keynr;
#endif
if (!strcmp(row[2],"PRIMARY"))
{
primary_key=keynr;
break;
}
}
} }
else mysql_data_seek(tableRes,0);
keynr=0;
while ((row=mysql_fetch_row(tableRes)))
{
if (atoi(row[3]) == 1)
{
if (keynr++)
putc(')', sql_file);
if (atoi(row[1])) /* Test if duplicate key */
/* Duplicate allowed */
fprintf(sql_file, ",\n KEY %s (",quote_name(row[2],name_buff));
else if (keynr == primary_key)
fputs(",\n PRIMARY KEY (",sql_file); /* First UNIQUE is primary */
else
fprintf(sql_file, ",\n UNIQUE %s (",quote_name(row[2],name_buff));
}
else
putc(',', sql_file);
fputs(quote_name(row[4],name_buff), sql_file);
if (row[7])
fprintf(sql_file, "(%s)",row[7]); /* Sub key */
}
if (keynr)
putc(')', sql_file);
fputs("\n)",sql_file);
/* Get MySQL specific create options */
if (create_options)
{ {
fputs("/*!",sql_file); sprintf(buff,"show table status like '%s'",table);
print_value(sql_file,tableRes,row,"type=","Type",0); if (mysql_query(sock, buff))
print_value(sql_file,tableRes,row,"","Create_options",0); {
print_value(sql_file,tableRes,row,"comment=","Comment",1); if (mysql_errno(sock) != ER_PARSE_ERROR)
fputs(" */",sql_file); { /* If old MySQL version */
if (verbose)
fprintf(stderr,
"# Warning: Couldn't get status information for table '%s' (%s)\n",
table,mysql_error(sock));
}
}
else if (!(tableRes=mysql_store_result(sock)) ||
!(row=mysql_fetch_row(tableRes)))
{
fprintf(stderr,
"Error: Couldn't read status information for table '%s' (%s)\n",
table,mysql_error(sock));
}
else
{
fputs("/*!",sql_file);
print_value(sql_file,tableRes,row,"type=","Type",0);
print_value(sql_file,tableRes,row,"","Create_options",0);
print_value(sql_file,tableRes,row,"comment=","Comment",1);
fputs(" */",sql_file);
}
mysql_free_result(tableRes); /* Is always safe to free */
} }
mysql_free_result(tableRes); /* Is always safe to free */ fputs(";\n", sql_file);
} }
fputs(";\n", sql_file);
} }
if (cFlag) if (cFlag)
{ {
......
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