~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/mysqldump.c

MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
80
80
                      char **err_pos, uint *err_len);
81
81
 
82
82
static void field_escape(DYNAMIC_STRING* in, const char *from);
83
 
static my_bool  verbose= 0, opt_no_create_info= 0, opt_no_data= 0,
 
83
static bool  verbose= 0, opt_no_create_info= 0, opt_no_data= 0,
84
84
                quick= 1, extended_insert= 1,
85
85
                lock_tables=1,ignore_errors=0,flush_logs=0,flush_privileges=0,
86
86
                opt_drop=1,opt_keywords=0,opt_lock=1,opt_compress=0,
98
98
                opt_include_master_host_port= 0,
99
99
                opt_events= 0,
100
100
                opt_alltspcs=0, opt_notspcs= 0;
101
 
static my_bool insert_pat_inited= 0, debug_info_flag= 0, debug_check_flag= 0;
 
101
static bool insert_pat_inited= 0, debug_info_flag= 0, debug_check_flag= 0;
102
102
static ulong opt_max_allowed_packet, opt_net_buffer_length;
103
103
static MYSQL mysql_connection,*mysql=0;
104
104
static DYNAMIC_STRING insert_pat;
112
112
static char **defaults_argv= 0;
113
113
static char compatible_mode_normal_str[255];
114
114
/* Server supports character_set_results session variable? */
115
 
static my_bool server_supports_switching_charsets= TRUE;
 
115
static bool server_supports_switching_charsets= TRUE;
116
116
static ulong opt_compatible_mode= 0;
117
117
#define MYSQL_OPT_MASTER_DATA_EFFECTIVE_SQL 1
118
118
#define MYSQL_OPT_MASTER_DATA_COMMENTED_SQL 2
154
154
static CHARSET_INFO *charset_info= &my_charset_latin1;
155
155
const char *default_dbug_option="d:t:o,/tmp/mysqldump.trace";
156
156
/* have we seen any VIEWs during table scanning? */
157
 
my_bool seen_views= 0;
 
157
bool seen_views= 0;
158
158
const char *compatible_mode_names[]=
159
159
{
160
160
  "MYSQL323", "MYSQL40", "POSTGRESQL", "ORACLE", "MSSQL", "DB2",
237
237
   "To dump several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames. 'USE db_name;' will be included in the output.",
238
238
   (char**) &opt_databases, (char**) &opt_databases, 0, GET_BOOL, NO_ARG, 0, 0,
239
239
   0, 0, 0, 0},
240
 
#ifdef DBUG_OFF
241
 
  {"debug", '#', "This is a non-debug version. Catch this and exit",
242
 
   0,0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
243
 
#else
244
 
  {"debug", '#', "Output debug log", (char**) &default_dbug_option,
245
 
   (char**) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
246
 
#endif
247
240
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
248
241
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
249
242
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
487
480
static int init_dumping(char *, int init_func(char*));
488
481
static int dump_databases(char **);
489
482
static int dump_all_databases(void);
490
 
static char *quote_name(const char *name, char *buff, my_bool force);
 
483
static char *quote_name(const char *name, char *buff, bool force);
491
484
char check_if_ignore_table(const char *table_name, char *table_type);
492
485
static char *primary_key_fields(const char *table_name);
493
486
 
505
498
static void verbose_msg(const char *fmt, ...)
506
499
{
507
500
  va_list args;
508
 
  DBUG_ENTER("verbose_msg");
 
501
 
509
502
 
510
503
  if (!verbose)
511
 
    DBUG_VOID_RETURN;
 
504
    return;
512
505
 
513
506
  va_start(args, fmt);
514
507
  vfprintf(stderr, fmt, args);
515
508
  va_end(args);
516
509
 
517
 
  DBUG_VOID_RETURN;
 
510
  return;
518
511
}
519
512
 
520
513
/*
674
667
 
675
668
 
676
669
static uchar* get_table_key(const char *entry, size_t *length,
677
 
                            my_bool not_used __attribute__((unused)))
 
670
                            bool not_used __attribute__((unused)))
678
671
{
679
672
  *length= strlen(entry);
680
673
  return (uchar*) entry;
681
674
}
682
675
 
683
676
 
684
 
static my_bool
 
677
static bool
685
678
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
686
679
               char *argument)
687
680
{
723
716
    }
724
717
 
725
718
    break;
726
 
  case '#':
727
 
    DBUG_PUSH(argument ? argument : default_dbug_option);
728
 
    debug_check_flag= 1;
729
 
    break;
730
719
  case 'V': print_version(); exit(0);
731
720
  case 'X':
732
721
    opt_xml= 1;
793
782
        fprintf(stderr, "Invalid mode to --compatible: %s\n", buff);
794
783
        exit(1);
795
784
      }
796
 
#if !defined(DBUG_OFF)
797
 
      {
798
 
        uint size_for_sql_mode= 0;
799
 
        const char **ptr;
800
 
        for (ptr= compatible_mode_names; *ptr; ptr++)
801
 
          size_for_sql_mode+= strlen(*ptr);
802
 
        size_for_sql_mode+= sizeof(compatible_mode_names)-1;
803
 
        DBUG_ASSERT(sizeof(compatible_mode_normal_str)>=size_for_sql_mode);
804
 
      }
805
 
#endif
806
785
      mode= opt_compatible_mode;
807
786
      for (i= 0, mode= opt_compatible_mode; mode; mode>>= 1, i++)
808
787
      {
933
912
*/
934
913
static void DB_error(MYSQL *mysql_arg, const char *when)
935
914
{
936
 
  DBUG_ENTER("DB_error");
 
915
 
937
916
  maybe_die(EX_MYSQLERR, "Got error: %d: %s %s",
938
917
          mysql_errno(mysql_arg), mysql_error(mysql_arg), when);
939
 
  DBUG_VOID_RETURN;
 
918
  return;
940
919
}
941
920
 
942
921
 
1122
1101
static int connect_to_db(char *host, char *user,char *passwd)
1123
1102
{
1124
1103
  char buff[20+FN_REFLEN];
1125
 
  DBUG_ENTER("connect_to_db");
 
1104
 
1126
1105
 
1127
1106
  verbose_msg("-- Connecting to %s...\n", host ? host : "localhost");
1128
1107
  mysql_init(&mysql_connection);
1140
1119
                                  0)))
1141
1120
  {
1142
1121
    DB_error(&mysql_connection, "when trying to connect");
1143
 
    DBUG_RETURN(1);
 
1122
    return(1);
1144
1123
  }
1145
1124
  if (mysql_get_server_version(&mysql_connection) < 40100)
1146
1125
  {
1158
1137
  {
1159
1138
    snprintf(buff, sizeof(buff), "/*!40103 SET TIME_ZONE='+00:00' */");
1160
1139
    if (mysql_query_with_error_report(mysql, 0, buff))
1161
 
      DBUG_RETURN(1);
 
1140
      return(1);
1162
1141
  }
1163
 
  DBUG_RETURN(0);
 
1142
  return(0);
1164
1143
} /* connect_to_db */
1165
1144
 
1166
1145
 
1177
1156
static void unescape(FILE *file,char *pos,uint length)
1178
1157
{
1179
1158
  char *tmp;
1180
 
  DBUG_ENTER("unescape");
 
1159
 
1181
1160
  if (!(tmp=(char*) my_malloc(length*2+1, MYF(MY_WME))))
1182
1161
    die(EX_MYSQLERR, "Couldn't allocate memory");
1183
1162
 
1187
1166
  fputc('\'', file);
1188
1167
  check_io(file);
1189
1168
  my_free(tmp, MYF(MY_WME));
1190
 
  DBUG_VOID_RETURN;
 
1169
  return;
1191
1170
} /* unescape */
1192
1171
 
1193
1172
 
1194
 
static my_bool test_if_special_chars(const char *str)
 
1173
static bool test_if_special_chars(const char *str)
1195
1174
{
1196
1175
#if MYSQL_VERSION_ID >= 32300
1197
1176
  for ( ; *str ; str++)
1219
1198
  buff                 quoted string
1220
1199
 
1221
1200
*/
1222
 
static char *quote_name(const char *name, char *buff, my_bool force)
 
1201
static char *quote_name(const char *name, char *buff, bool force)
1223
1202
{
1224
1203
  char *to= buff;
1225
1204
  char qtype= (opt_compatible_mode & MASK_ANSI_QUOTES) ? '\"' : '`';
1369
1348
  while (attribute_name != NullS)
1370
1349
  {
1371
1350
    attribute_value= va_arg(arg_list, char *);
1372
 
    DBUG_ASSERT(attribute_value != NullS);
 
1351
    assert(attribute_value != NullS);
1373
1352
 
1374
1353
    fputc(' ', xml_file);
1375
1354
    fputs(attribute_name, xml_file);    
1506
1485
static uint get_table_structure(char *table, char *db, char *table_type,
1507
1486
                                char *ignore_flag)
1508
1487
{
1509
 
  my_bool    init=0, delayed, write_data, complete_insert;
 
1488
  bool    init=0, delayed, write_data, complete_insert;
1510
1489
  my_ulonglong num_fields;
1511
1490
  char       *result_table, *opt_quoted_table;
1512
1491
  const char *insert_option;
1516
1495
  int        len;
1517
1496
  MYSQL_RES  *result;
1518
1497
  MYSQL_ROW  row;
1519
 
  DBUG_ENTER("get_table_structure");
1520
 
  DBUG_PRINT("enter", ("db: %s  table: %s", db, table));
1521
1498
 
1522
1499
  *ignore_flag= check_if_ignore_table(table, table_type);
1523
1500
 
1574
1551
      if (switch_character_set_results(mysql, "binary") ||
1575
1552
          mysql_query_with_error_report(mysql, &result, buff) ||
1576
1553
          switch_character_set_results(mysql, default_charset))
1577
 
        DBUG_RETURN(0);
 
1554
        return(0);
1578
1555
 
1579
1556
      if (path)
1580
1557
      {
1581
1558
        if (!(sql_file= open_sql_file_for_table(table)))
1582
 
          DBUG_RETURN(0);
 
1559
          return(0);
1583
1560
 
1584
1561
        write_header(sql_file, db);
1585
1562
      }
1645
1622
 
1646
1623
          my_free(scv_buff, MYF(MY_ALLOW_ZERO_PTR));
1647
1624
 
1648
 
          DBUG_RETURN(0);
 
1625
          return(0);
1649
1626
        }
1650
1627
        else
1651
1628
          my_free(scv_buff, MYF(MY_ALLOW_ZERO_PTR));
1700
1677
          my_fclose(sql_file, MYF(MY_WME));
1701
1678
 
1702
1679
        seen_views= 1;
1703
 
        DBUG_RETURN(0);
 
1680
        return(0);
1704
1681
      }
1705
1682
 
1706
1683
      row= mysql_fetch_row(result);
1721
1698
    {
1722
1699
      if (path)
1723
1700
        my_fclose(sql_file, MYF(MY_WME));
1724
 
      DBUG_RETURN(0);
 
1701
      return(0);
1725
1702
    }
1726
1703
 
1727
1704
    /*
1775
1752
    snprintf(query_buff, sizeof(query_buff), "show fields from %s",
1776
1753
             result_table);
1777
1754
    if (mysql_query_with_error_report(mysql, &result, query_buff))
1778
 
      DBUG_RETURN(0);
 
1755
      return(0);
1779
1756
 
1780
1757
    /* Make an sql-file, if path was given iow. option -T was given */
1781
1758
    if (!opt_no_create_info)
1783
1760
      if (path)
1784
1761
      {
1785
1762
        if (!(sql_file= open_sql_file_for_table(table)))
1786
 
          DBUG_RETURN(0);
 
1763
          return(0);
1787
1764
        write_header(sql_file, db);
1788
1765
      }
1789
1766
      if (!opt_xml && opt_comments)
1883
1860
                my_progname, result_table, mysql_error(mysql));
1884
1861
        if (path)
1885
1862
          my_fclose(sql_file, MYF(MY_WME));
1886
 
        DBUG_RETURN(0);
 
1863
        return(0);
1887
1864
      }
1888
1865
 
1889
1866
      /* Find first which key is primary key */
2003
1980
    write_footer(sql_file);
2004
1981
    my_fclose(sql_file, MYF(MY_WME));
2005
1982
  }
2006
 
  DBUG_RETURN((uint) num_fields);
 
1983
  return((uint) num_fields);
2007
1984
} /* get_table_structure */
2008
1985
 
2009
1986
static void add_load_option(DYNAMIC_STRING *str, const char *option,
2098
2075
  MYSQL_RES     *res;
2099
2076
  MYSQL_FIELD   *field;
2100
2077
  MYSQL_ROW     row;
2101
 
  DBUG_ENTER("dump_table");
 
2078
 
2102
2079
 
2103
2080
  /*
2104
2081
    Make sure you get the create table info before the following check for
2110
2087
    The "table" could be a view.  If so, we don't do anything here.
2111
2088
  */
2112
2089
  if (strcmp(table_type, "VIEW") == 0)
2113
 
    DBUG_VOID_RETURN;
 
2090
    return;
2114
2091
 
2115
2092
  /* Check --no-data flag */
2116
2093
  if (opt_no_data)
2117
2094
  {
2118
2095
    verbose_msg("-- Skipping dump data for table '%s', --no-data was used\n",
2119
2096
                table);
2120
 
    DBUG_VOID_RETURN;
 
2097
    return;
2121
2098
  }
2122
2099
 
2123
 
  DBUG_PRINT("info",
2124
 
             ("ignore_flag: %x  num_fields: %d", (int) ignore_flag,
2125
 
              num_fields));
2126
2100
  /*
2127
2101
    If the table type is a merge table or any type that has to be
2128
2102
     _completely_ ignored and no data dumped
2131
2105
  {
2132
2106
    verbose_msg("-- Warning: Skipping data for table '%s' because " \
2133
2107
                "it's of type %s\n", table, table_type);
2134
 
    DBUG_VOID_RETURN;
 
2108
    return;
2135
2109
  }
2136
2110
  /* Check that there are any fields in the table */
2137
2111
  if (num_fields == 0)
2138
2112
  {
2139
2113
    verbose_msg("-- Skipping dump data for table '%s', it has no fields\n",
2140
2114
                table);
2141
 
    DBUG_VOID_RETURN;
 
2115
    return;
2142
2116
  }
2143
2117
 
2144
2118
  /*
2150
2124
      !my_strcasecmp(&my_charset_latin1, table, "event"))
2151
2125
  {
2152
2126
    verbose_msg("-- Skipping data table mysql.event, --skip-events was used\n");
2153
 
    DBUG_VOID_RETURN;
 
2127
    return;
2154
2128
  }
2155
2129
 
2156
2130
  result_table= quote_name(table,table_buff, 1);
2212
2186
    {
2213
2187
      DB_error(mysql, "when executing 'SELECT INTO OUTFILE'");
2214
2188
      dynstr_free(&query_string);
2215
 
      DBUG_VOID_RETURN;
 
2189
      return;
2216
2190
    }
2217
2191
  }
2218
2192
  else
2369
2343
                  extended_row.length+= mysql_hex_string(extended_row.str +
2370
2344
                                                         extended_row.length,
2371
2345
                                                         row[i], length);
2372
 
                  DBUG_ASSERT(extended_row.length+1 <= extended_row.max_length);
 
2346
                  assert(extended_row.length+1 <= extended_row.max_length);
2373
2347
                  /* mysql_hex_string() already terminated string by '\0' */
2374
 
                  DBUG_ASSERT(extended_row.str[extended_row.length] == '\0');
 
2348
                  assert(extended_row.str[extended_row.length] == '\0');
2375
2349
                }
2376
2350
                else
2377
2351
                {
2548
2522
    mysql_free_result(res);
2549
2523
  }
2550
2524
  dynstr_free(&query_string);
2551
 
  DBUG_VOID_RETURN;
 
2525
  return;
2552
2526
 
2553
2527
err:
2554
2528
  dynstr_free(&query_string);
2555
2529
  maybe_exit(error);
2556
 
  DBUG_VOID_RETURN;
 
2530
  return;
2557
2531
} /* dump_table */
2558
2532
 
2559
2533
 
2603
2577
{
2604
2578
  int result=0;
2605
2579
  char **db;
2606
 
  DBUG_ENTER("dump_databases");
 
2580
 
2607
2581
 
2608
2582
  for (db= db_names ; *db ; db++)
2609
2583
  {
2610
2584
    if (dump_all_tables_in_db(*db))
2611
2585
      result=1;
2612
2586
  }
2613
 
  DBUG_RETURN(result);
 
2587
  return(result);
2614
2588
} /* dump_databases */
2615
2589
 
2616
2590
 
2628
2602
 
2629
2603
int init_dumping_tables(char *qdatabase)
2630
2604
{
2631
 
  DBUG_ENTER("init_dumping_tables");
 
2605
 
2632
2606
 
2633
2607
  if (!opt_create_db)
2634
2608
  {
2665
2639
      mysql_free_result(dbinfo);
2666
2640
    }
2667
2641
  }
2668
 
  DBUG_RETURN(0);
 
2642
  return(0);
2669
2643
} /* init_dumping_tables */
2670
2644
 
2671
2645
 
2710
2684
 
2711
2685
/* Return 1 if we should copy the table */
2712
2686
 
2713
 
static my_bool include_table(const uchar *hash_key, size_t len)
 
2687
static bool include_table(const uchar *hash_key, size_t len)
2714
2688
{
2715
2689
  return !hash_search(&ignore_table, hash_key, len);
2716
2690
}
2724
2698
  char hash_key[2*NAME_LEN+2];  /* "db.tablename" */
2725
2699
  char *afterdot;
2726
2700
  int using_mysql_db= my_strcasecmp(&my_charset_latin1, database, "mysql");
2727
 
  DBUG_ENTER("dump_all_tables_in_db");
 
2701
 
2728
2702
 
2729
2703
  afterdot= strmov(hash_key, database);
2730
2704
  *afterdot++= '.';
2731
2705
 
2732
2706
  if (init_dumping(database, init_dumping_tables))
2733
 
    DBUG_RETURN(1);
 
2707
    return(1);
2734
2708
  if (opt_xml)
2735
2709
    print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NullS);
2736
2710
  if (lock_tables)
2780
2754
    fprintf(md_result_file,"\n--\n-- Flush Grant Tables \n--\n");
2781
2755
    fprintf(md_result_file,"\n/*! FLUSH PRIVILEGES */;\n");
2782
2756
  }
2783
 
  DBUG_RETURN(0);
 
2757
  return(0);
2784
2758
} /* dump_all_tables_in_db */
2785
2759
 
2786
2760
 
2802
2776
  MYSQL_ROW  row;
2803
2777
  char query[50 + 2*NAME_LEN];
2804
2778
  char show_name_buff[FN_REFLEN];
2805
 
  DBUG_ENTER("get_actual_table_name");
 
2779
 
2806
2780
 
2807
2781
  /* Check memory for quote_for_like() */
2808
 
  DBUG_ASSERT(2*sizeof(old_table_name) < sizeof(show_name_buff));
 
2782
  assert(2*sizeof(old_table_name) < sizeof(show_name_buff));
2809
2783
  snprintf(query, sizeof(query), "SHOW TABLES LIKE %s",
2810
2784
           quote_for_like(old_table_name, show_name_buff));
2811
2785
 
2828
2802
    }
2829
2803
    mysql_free_result(table_res);
2830
2804
  }
2831
 
  DBUG_PRINT("exit", ("new_table_name: %s", name));
2832
 
  DBUG_RETURN(name);
 
2805
  return(name);
2833
2806
}
2834
2807
 
2835
2808
 
2839
2812
  DYNAMIC_STRING lock_tables_query;
2840
2813
  MEM_ROOT root;
2841
2814
  char **dump_tables, **pos, **end;
2842
 
  DBUG_ENTER("dump_selected_tables");
 
2815
 
2843
2816
 
2844
2817
  if (init_dumping(db, init_dumping_tables))
2845
 
    DBUG_RETURN(1);
 
2818
    return(1);
2846
2819
 
2847
2820
  init_alloc_root(&root, 8192, 0);
2848
2821
  if (!(dump_tables= pos= (char**) alloc_root(&root, tables * sizeof(char *))))
2905
2878
 
2906
2879
  /* Dump each selected table */
2907
2880
  for (pos= dump_tables; pos < end; pos++)
2908
 
  {
2909
 
    DBUG_PRINT("info",("Dumping table %s", *pos));
2910
2881
    dump_table(*pos, db);
2911
 
  }
2912
2882
 
2913
2883
  free_root(&root, MYF(0));
2914
2884
  my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
2920
2890
  }
2921
2891
  if (lock_tables)
2922
2892
    VOID(mysql_query_with_error_report(mysql, 0, "UNLOCK TABLES"));
2923
 
  DBUG_RETURN(0);
 
2893
  return(0);
2924
2894
} /* dump_selected_tables */
2925
2895
 
2926
2896
 
3279
3249
  char buff[FN_REFLEN+80], show_name_buff[FN_REFLEN];
3280
3250
  MYSQL_RES *res= NULL;
3281
3251
  MYSQL_ROW row;
3282
 
  DBUG_ENTER("check_if_ignore_table");
 
3252
 
3283
3253
 
3284
3254
  /* Check memory for quote_for_like() */
3285
 
  DBUG_ASSERT(2*sizeof(table_name) < sizeof(show_name_buff));
 
3255
  assert(2*sizeof(table_name) < sizeof(show_name_buff));
3286
3256
  snprintf(buff, sizeof(buff), "show table status like %s",
3287
3257
           quote_for_like(table_name, show_name_buff));
3288
3258
  if (mysql_query_with_error_report(mysql, &res, buff))
3291
3261
    {                                   /* If old MySQL version */
3292
3262
      verbose_msg("-- Warning: Couldn't get status information for "
3293
3263
                  "table %s (%s)\n", table_name, mysql_error(mysql));
3294
 
      DBUG_RETURN(result);                       /* assume table is ok */
 
3264
      return(result);                       /* assume table is ok */
3295
3265
    }
3296
3266
  }
3297
3267
  if (!(row= mysql_fetch_row(res)))
3300
3270
            "Error: Couldn't read status information for table %s (%s)\n",
3301
3271
            table_name, mysql_error(mysql));
3302
3272
    mysql_free_result(res);
3303
 
    DBUG_RETURN(result);                         /* assume table is ok */
 
3273
    return(result);                         /* assume table is ok */
3304
3274
  }
3305
3275
  if (!(row[1]))
3306
3276
    strmake(table_type, "VIEW", NAME_LEN-1);
3332
3302
      result= IGNORE_DATA;
3333
3303
  }
3334
3304
  mysql_free_result(res);
3335
 
  DBUG_RETURN(result);
 
3305
  return(result);
3336
3306
}
3337
3307
 
3338
3308