summaryrefslogtreecommitdiff
path: root/client/mysqldump.c
diff options
context:
space:
mode:
authorunknown <monty@donna.mysql.com>2000-09-02 08:20:54 +0300
committerunknown <monty@donna.mysql.com>2000-09-02 08:20:54 +0300
commitaaca0e3c6bd71a7b34fcb3a137ab1113a20dca1f (patch)
tree56a90e48b67792ba66ccfc2c856b09814bec0e56 /client/mysqldump.c
parent8d0dafe3e85713cb4b0a399a699bdfc9f6434464 (diff)
parent40f6a9a9da1b6aeda9a850e468cb3f584caaa254 (diff)
downloadmariadb-git-aaca0e3c6bd71a7b34fcb3a137ab1113a20dca1f.tar.gz
Abort patch
Build-tools/Do-compile: Auto merged client/mysqldump.c: Aborted Serges patch as this would have caused incompatibility problems with old MySQL servers.
Diffstat (limited to 'client/mysqldump.c')
-rw-r--r--client/mysqldump.c167
1 files changed, 134 insertions, 33 deletions
diff --git a/client/mysqldump.c b/client/mysqldump.c
index 60191d2916b..78d64c76a75 100644
--- a/client/mysqldump.c
+++ b/client/mysqldump.c
@@ -604,6 +604,28 @@ static uint getTableStructure(char *table, char* db)
DBUG_RETURN(0);
}
+ /* Make an sql-file, if path was given iow. option -T was given */
+ if (!tFlag)
+ {
+ if (path)
+ {
+ char filename[FN_REFLEN], tmp_path[FN_REFLEN];
+ strmov(tmp_path,path);
+ 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);
+ fprintf(sql_file, "CREATE TABLE %s (\n", table_name);
+ }
if (cFlag)
sprintf(insert_pat, "INSERT %sINTO %s (", delayed, table_name);
else
@@ -619,18 +641,32 @@ static uint getTableStructure(char *table, char* db)
ulong *lengths=mysql_fetch_lengths(tableRes);
if (init)
{
+ if (!tFlag)
+ fputs(",\n",sql_file);
if (cFlag)
strpos=strmov(strpos,", ");
}
init=1;
if (cFlag)
strpos=strmov(strpos,quote_name(row[SHOW_FIELDNAME],name_buff));
- }
- if (cFlag)
- {
- strpos=strmov(strpos,") VALUES ");
- if (!extended_insert)
- strpos=strmov(strpos,"(");
+ 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]);
+ }
}
numFields = (uint) mysql_num_rows(tableRes);
mysql_free_result(tableRes);
@@ -638,37 +674,102 @@ static uint getTableStructure(char *table, char* db)
{
/* Make an sql-file, if path was given iow. option -T was given */
char buff[20+FN_REFLEN];
-
- sprintf(buff,"show create table %s",table_name);
+ uint keynr,primary_key;
+ sprintf(buff,"show keys from %s",table_name);
if (mysql_query(sock, buff))
{
- fprintf(stderr, "%s: Can't get CREATE TABLE for table '%s' (%s)\n",
+ fprintf(stderr, "%s: Can't get keys for table '%s' (%s)\n",
my_progname, table, mysql_error(sock));
+ if (sql_file != stdout)
+ my_fclose(sql_file, MYF(MY_WME));
safe_exit(EX_MYSQLERR);
DBUG_RETURN(0);
}
- if (path)
+ tableRes=mysql_store_result(sock);
+ /* Find first which key is primary key */
+ keynr=0;
+ primary_key=INT_MAX;
+ while ((row=mysql_fetch_row(tableRes)))
{
- char filename[FN_REFLEN], tmp_path[FN_REFLEN];
- strmov(tmp_path,path);
- 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 */
+ if (atoi(row[3]) == 1)
{
- safe_exit(EX_MYSQLERR);
- DBUG_RETURN(0);
+ 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;
+ }
}
- 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);
+ 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);
- tableRes=mysql_store_result(sock);
- row=mysql_fetch_row(tableRes);
- fprintf(sql_file, "%s;\n", row[1]);
+ /* Get MySQL specific create options */
+ if (create_options)
+ {
+ sprintf(buff,"show table status like '%s'",table);
+ if (mysql_query(sock, buff))
+ {
+ if (mysql_errno(sock) != ER_PARSE_ERROR)
+ { /* 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 */
+ }
+ fputs(";\n", sql_file);
+ }
+ if (cFlag)
+ {
+ strpos=strmov(strpos,") VALUES ");
+ if (!extended_insert)
+ strpos=strmov(strpos,"(");
}
DBUG_RETURN(numFields);
} /* getTableStructure */
@@ -841,7 +942,7 @@ static void dumpTable(uint numFields, char *table)
{
if (length)
{
- if (!IS_NUM(field->type))
+ if (!IS_NUM_FIELD(field))
{
if (dynstr_realloc(&extended_row,length * 2+2))
{
@@ -873,7 +974,7 @@ static void dumpTable(uint numFields, char *table)
putchar(',');
if (row[i])
{
- if (!IS_NUM(field->type))
+ if (!IS_NUM_FIELD(field))
unescape(stdout, row[i], lengths[i]);
else
fputs(row[i],stdout);
@@ -945,7 +1046,7 @@ static char *getTableName(int reset)
}
if ((row = mysql_fetch_row(res)))
return((char*) row[0]);
-
+
if (reset)
mysql_data_seek(res,0); /* We want to read again */
else
@@ -1033,14 +1134,14 @@ static int dump_all_tables_in_db(char *database)
dynstr_append(&query, " READ /*!32311 LOCAL */,");
}
if (numrows && mysql_real_query(sock, query.str, query.length-1))
- DBerror(sock, "when using LOCK TABLES");
+ DBerror(sock, "when using LOCK TABLES");
/* We shall continue here, if --force was given */
dynstr_free(&query);
}
if (flush_logs)
{
if (mysql_refresh(sock, REFRESH_LOG))
- DBerror(sock, "when doing refresh");
+ DBerror(sock, "when doing refresh");
/* We shall continue here, if --force was given */
}
while ((table = getTableName(0)))
@@ -1074,14 +1175,14 @@ static int dump_selected_tables(char *db, char **table_names, int tables)
dynstr_append(&query, " READ /*!32311 LOCAL */,");
}
if (mysql_real_query(sock, query.str, query.length-1))
- DBerror(sock, "when doing LOCK TABLES");
+ DBerror(sock, "when doing LOCK TABLES");
/* We shall countinue here, if --force was given */
dynstr_free(&query);
}
if (flush_logs)
{
if (mysql_refresh(sock, REFRESH_LOG))
- DBerror(sock, "when doing refresh");
+ DBerror(sock, "when doing refresh");
/* We shall countinue here, if --force was given */
}
for (; tables > 0 ; tables-- , table_names++)
@@ -1157,7 +1258,7 @@ int main(int argc, char **argv)
else if (argc > 1 && !opt_databases)
dump_selected_tables(*argv, (argv + 1), (argc - 1));
/* One or more databases, all tables */
- else
+ else
dump_databases(argv);
if (opt_first_slave)