13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* drizzledump.cc - Dump a tables contents and format to an ASCII file
16
/* drizzledump.c - Dump a tables contents and format to an ASCII file
18
18
** The author's original notes follow :-
22
22
** WARRANTY: None, expressed, impressed, implied
24
24
** STATUS: Public domain
25
** Adapted and optimized for DRIZZLE by
26
** Michael Widenius, Sinisa Milivojevic, Jani Tolonen
27
** -w --where added 9/10/98 by Jim Faucette
28
** slave code by David Saez Padros <david@ols.es>
29
** master/autocommit code by Brian Aker <brian@tangent.org>
31
** Andrei Errapart <andreie@no.spam.ee>
32
** Tõnu Samuel <tonu@please.do.not.remove.this.spam.ee>
33
** XML by Gary Huntress <ghuntress@mediaone.net> 10/10/01, cleaned up
34
** and adapted to drizzledump 05/11/01 by Jani Tolonen
35
** Added --single-transaction option 06/06/2002 by Peter Zaitsev
36
** 10 Jun 2003: SET NAMES and --no-set-names by Alexander Barkov
27
39
#define DUMP_VERSION "10.13"
31
41
#include "client_priv.h"
33
43
#include <mysys/my_sys.h>
64
73
#define IGNORE_DATA 0x01 /* don't dump data for this table */
65
74
#define IGNORE_INSERT_DELAYED 0x02 /* table doesn't support INSERT DELAYED */
67
static void add_load_option(string &str, const char *option,
68
const char *option_value);
69
static uint32_t find_set(TYPELIB *lib, const char *x, uint length,
76
static void add_load_option(DYNAMIC_STRING *str, const char *option,
77
const char *option_value);
78
static ulong find_set(TYPELIB *lib, const char *x, uint length,
70
79
char **err_pos, uint *err_len);
72
static void field_escape(string &in, const char *from);
81
static void field_escape(DYNAMIC_STRING* in, const char *from);
73
82
static bool verbose= 0, opt_no_create_info= 0, opt_no_data= 0,
74
83
quick= 1, extended_insert= 1,
75
84
lock_tables=1,ignore_errors=0,flush_logs=0,flush_privileges=0,
88
97
opt_include_master_host_port= 0,
90
99
opt_alltspcs=0, opt_notspcs= 0;
91
static bool debug_info_flag= 0, debug_check_flag= 0;
92
static uint32_t opt_max_allowed_packet, opt_net_buffer_length;
100
static bool insert_pat_inited= 0, debug_info_flag= 0, debug_check_flag= 0;
101
static ulong opt_max_allowed_packet, opt_net_buffer_length;
93
102
static DRIZZLE drizzle_connection,*drizzle=0;
94
static string insert_pat;
103
static DYNAMIC_STRING insert_pat;
95
104
static char *opt_password=0,*current_user=0,
96
105
*current_host=0,*path=0,*fields_terminated=0,
97
106
*lines_terminated=0, *enclosed=0, *opt_enclosed=0, *escaped=0,
103
112
static char compatible_mode_normal_str[255];
104
113
/* Server supports character_set_results session variable? */
105
114
static bool server_supports_switching_charsets= true;
106
static uint32_t opt_compatible_mode= 0;
115
static ulong opt_compatible_mode= 0;
107
116
#define DRIZZLE_OPT_MASTER_DATA_EFFECTIVE_SQL 1
108
117
#define DRIZZLE_OPT_MASTER_DATA_COMMENTED_SQL 2
109
118
#define DRIZZLE_OPT_SLAVE_DATA_EFFECTIVE_SQL 1
112
121
static uint opt_slave_data;
113
122
static uint my_end_arg;
114
123
static int first_error=0;
115
static string extended_row;
124
static DYNAMIC_STRING extended_row;
116
125
FILE *md_result_file= 0;
117
126
FILE *stderror_file=0;
128
static uint opt_protocol= DRIZZLE_PROTOCOL_TCP;
131
Dynamic_string wrapper functions. In this file use these
132
wrappers, they will terminate the process if there is
133
an allocation failure.
135
static void init_dynamic_string_checked(DYNAMIC_STRING *str, const char *init_str,
136
uint init_alloc, uint alloc_increment);
137
static void dynstr_append_checked(DYNAMIC_STRING* dest, const char* src);
138
static void dynstr_set_checked(DYNAMIC_STRING *str, const char *init_str);
139
static void dynstr_append_mem_checked(DYNAMIC_STRING *str, const char *append,
141
static void dynstr_realloc_checked(DYNAMIC_STRING *str, ulong additional_size);
120
143
Constant for detection of default value of default_charset.
121
144
If default_charset is equal to drizzle_universal_client_charset, then
122
145
it is the default value which assigned at the very beginning of main().
124
147
static const char *drizzle_universal_client_charset=
125
DRIZZLE_UNIVERSAL_CLIENT_CHARSET;
148
MYSQL_UNIVERSAL_CLIENT_CHARSET;
126
149
static char *default_charset;
127
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
150
static const CHARSET_INFO *charset_info= &my_charset_latin1;
128
151
const char *default_dbug_option="d:t:o,/tmp/drizzledump.trace";
129
152
/* have we seen any VIEWs during table scanning? */
130
153
bool seen_views= 0;
133
156
"MYSQL323", "MYSQL40", "POSTGRESQL", "ORACLE", "MSSQL", "DB2",
134
157
"MAXDB", "NO_KEY_OPTIONS", "NO_TABLE_OPTIONS", "NO_FIELD_OPTIONS",
138
161
#define MASK_ANSI_QUOTES \
727
754
char *end= compatible_mode_normal_str;
733
760
opt_set_charset= 0;
734
761
opt_compatible_mode_str= argument;
735
762
opt_compatible_mode= find_set(&compatible_mode_typelib,
736
763
argument, strlen(argument),
737
&err_ptr, &error_len);
740
strmake(buff, err_ptr, min((uint32_t)sizeof(buff), error_len));
767
strmake(buff, err_ptr, min(sizeof(buff), err_len));
741
768
fprintf(stderr, "Invalid mode to --compatible: %s\n", buff);
783
810
/* Don't copy internal log tables */
784
811
if (my_hash_insert(&ignore_table,
785
(unsigned char*) my_strdup("mysql.apply_status", MYF(MY_WME))) ||
786
my_hash_insert(&ignore_table,
787
(unsigned char*) my_strdup("mysql.schema", MYF(MY_WME))) ||
788
my_hash_insert(&ignore_table,
789
(unsigned char*) my_strdup("mysql.general_log", MYF(MY_WME))) ||
790
my_hash_insert(&ignore_table,
791
(unsigned char*) my_strdup("mysql.slow_log", MYF(MY_WME))) ||
792
my_hash_insert(&ignore_table,
793
(unsigned char*) my_strdup("mysql.online_backup", MYF(MY_WME))) ||
794
my_hash_insert(&ignore_table,
795
(unsigned char*) my_strdup("mysql.online_backup_progress", MYF(MY_WME))))
812
(uchar*) my_strdup("mysql.apply_status", MYF(MY_WME))) ||
813
my_hash_insert(&ignore_table,
814
(uchar*) my_strdup("mysql.schema", MYF(MY_WME))) ||
815
my_hash_insert(&ignore_table,
816
(uchar*) my_strdup("mysql.general_log", MYF(MY_WME))) ||
817
my_hash_insert(&ignore_table,
818
(uchar*) my_strdup("mysql.slow_log", MYF(MY_WME))) ||
819
my_hash_insert(&ignore_table,
820
(uchar*) my_strdup("mysql.online_backup", MYF(MY_WME))) ||
821
my_hash_insert(&ignore_table,
822
(uchar*) my_strdup("mysql.online_backup_progress", MYF(MY_WME))))
798
825
if ((ho_error= handle_options(argc, argv, my_long_options, get_one_option)))
1018
1045
char filename[FN_REFLEN], tmp_path[FN_REFLEN];
1019
convert_dirname(tmp_path,path,NULL);
1046
convert_dirname(tmp_path,path,NullS);
1020
1047
res= my_fopen(fn_format(filename, table, tmp_path, ".sql", 4),
1021
1048
O_WRONLY, MYF(MY_WME));
1028
1055
if (md_result_file && md_result_file != stdout)
1029
1056
my_fclose(md_result_file, MYF(0));
1057
my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
1031
1058
if (hash_inited(&ignore_table))
1032
1059
hash_free(&ignore_table);
1060
if (extended_insert)
1061
dynstr_free(&extended_row);
1062
if (insert_pat_inited)
1063
dynstr_free(&insert_pat);
1033
1064
if (defaults_argv)
1034
1065
free_defaults(defaults_argv);
1035
1066
my_end(my_end_arg);
1061
1092
verbose_msg("-- Connecting to %s...\n", host ? host : "localhost");
1062
1093
drizzle_create(&drizzle_connection);
1063
1094
if (opt_compress)
1064
drizzle_options(&drizzle_connection,DRIZZLE_OPT_COMPRESS,NULL);
1095
drizzle_options(&drizzle_connection,DRIZZLE_OPT_COMPRESS,NullS);
1097
drizzle_options(&drizzle_connection,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
1098
drizzle_options(&drizzle_connection, DRIZZLE_SET_CHARSET_NAME, default_charset);
1065
1099
if (!(drizzle= drizzle_connect(&drizzle_connection,host,user,passwd,
1066
1100
NULL,opt_drizzle_port, NULL,
1108
1142
if (!(tmp=(char*) my_malloc(length*2+1, MYF(MY_WME))))
1109
1143
die(EX_DRIZZLEERR, "Couldn't allocate memory");
1111
drizzle_escape_string(tmp, pos, length);
1145
drizzle_real_escape_string(&drizzle_connection, tmp, pos, length);
1112
1146
fputc('\'', file);
1113
1147
fputs(tmp, file);
1114
1148
fputc('\'', file);
1115
1149
check_io(file);
1150
my_free(tmp, MYF(MY_WME));
1118
1152
} /* unescape */
1256
1290
print_xml_tag(xml_file, sbeg, send, tag_name, first_attribute_name,
1257
..., attribute_name_n, attribute_value_n, NULL)
1291
..., attribute_name_n, attribute_value_n, NullS)
1258
1292
xml_file - output file
1259
1293
sbeg - line beginning
1260
1294
line_end - line ending
1292
1326
va_start(arg_list, first_attribute_name);
1293
1327
attribute_name= first_attribute_name;
1294
while (attribute_name != NULL)
1328
while (attribute_name != NullS)
1296
1330
attribute_value= va_arg(arg_list, char *);
1297
assert(attribute_value != NULL);
1331
assert(attribute_value != NullS);
1299
1333
fputc(' ', xml_file);
1300
1334
fputs(attribute_name, xml_file);
1404
1438
Print hex value for blob data.
1407
static void print_blob_as_hex(FILE *output_file, const char *str, uint32_t len)
1441
static void print_blob_as_hex(FILE *output_file, const char *str, ulong len)
1409
1443
/* sakaik got the idea to to provide blob's in hex notation. */
1410
1444
const char *ptr= str, *end= ptr + len;
1411
1445
for (; ptr < end ; ptr++)
1412
fprintf(output_file, "%02X", *((unsigned char *)ptr));
1446
fprintf(output_file, "%02X", *((uchar *)ptr));
1413
1447
check_io(output_file);
1560
1600
if (drizzle_errno(drizzle) == ER_VIEW_INVALID)
1561
1601
fprintf(sql_file, "\n-- failed on view %s: %s\n\n", result_table, scv_buff ? scv_buff : "");
1603
my_free(scv_buff, MYF(MY_ALLOW_ZERO_PTR));
1608
my_free(scv_buff, MYF(MY_ALLOW_ZERO_PTR));
1570
1610
if (drizzle_num_rows(result))
1618
1663
row= drizzle_fetch_row(result);
1620
fprintf(sql_file, "%s;\n", row[1]);
1666
"SET @saved_cs_client = @@character_set_client;\n"
1667
"SET character_set_client = utf8;\n"
1669
"SET character_set_client = @saved_cs_client;\n",
1622
1672
check_io(sql_file);
1623
1673
drizzle_free_result(result);
1640
1690
if (write_data)
1642
1692
if (opt_replace_into)
1643
insert_pat.append("REPLACE ");
1693
dynstr_append_checked(&insert_pat, "REPLACE ");
1645
insert_pat.append("INSERT ");
1646
insert_pat.append(insert_option);
1647
insert_pat.append("INTO ");
1648
insert_pat.append(opt_quoted_table);
1695
dynstr_append_checked(&insert_pat, "INSERT ");
1696
dynstr_append_checked(&insert_pat, insert_option);
1697
dynstr_append_checked(&insert_pat, "INTO ");
1698
dynstr_append_checked(&insert_pat, opt_quoted_table);
1649
1699
if (complete_insert)
1651
insert_pat.append(" (");
1701
dynstr_append_checked(&insert_pat, " (");
1655
insert_pat.append(" VALUES ");
1705
dynstr_append_checked(&insert_pat, " VALUES ");
1656
1706
if (!extended_insert)
1657
insert_pat.append("(");
1707
dynstr_append_checked(&insert_pat, "(");
1667
insert_pat.append(", ");
1717
dynstr_append_checked(&insert_pat, ", ");
1670
insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1720
dynstr_append_checked(&insert_pat,
1721
quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1673
1724
num_fields= drizzle_num_rows(result);
1701
1752
fprintf(sql_file, "CREATE TABLE %s (\n", result_table);
1703
1754
print_xml_tag(sql_file, "\t", "\n", "table_structure", "name=", table,
1705
1756
check_io(sql_file);
1708
1759
if (write_data)
1710
1761
if (opt_replace_into)
1711
insert_pat.append("REPLACE ");
1762
dynstr_append_checked(&insert_pat, "REPLACE ");
1713
insert_pat.append("INSERT ");
1714
insert_pat.append(insert_option);
1715
insert_pat.append("INTO ");
1716
insert_pat.append(result_table);
1764
dynstr_append_checked(&insert_pat, "INSERT ");
1765
dynstr_append_checked(&insert_pat, insert_option);
1766
dynstr_append_checked(&insert_pat, "INTO ");
1767
dynstr_append_checked(&insert_pat, result_table);
1717
1768
if (complete_insert)
1718
insert_pat.append(" (");
1769
dynstr_append_checked(&insert_pat, " (");
1721
insert_pat.append(" VALUES ");
1772
dynstr_append_checked(&insert_pat, " VALUES ");
1722
1773
if (!extended_insert)
1723
insert_pat.append("(");
1774
dynstr_append_checked(&insert_pat, "(");
1911
1963
return((uint) num_fields);
1912
1964
} /* get_table_structure */
1914
static void add_load_option(string &str, const char *option,
1915
const char *option_value)
1966
static void add_load_option(DYNAMIC_STRING *str, const char *option,
1967
const char *option_value)
1917
1969
if (!option_value)
1942
1994
syntax errors from the SQL parser.
1945
static void field_escape(string &in, const char *from)
1997
static void field_escape(DYNAMIC_STRING* in, const char *from)
1947
1999
uint end_backslashes= 0;
2001
dynstr_append_checked(in, "'");
2005
dynstr_append_mem_checked(in, from, 1);
1955
2007
if (*from == '\\')
1956
2008
end_backslashes^=1; /* find odd number of backslashes */
1995
2047
char ignore_flag;
1996
2048
char buf[200], table_buff[NAME_LEN+3];
1997
string query_string;
2049
DYNAMIC_STRING query_string;
1998
2050
char table_type[NAME_LEN];
1999
2051
char *result_table, table_buff2[NAME_LEN*2+3], *opt_quoted_table;
2001
uint32_t rownr, row_break, total_length, init_length;
2053
ulong rownr, row_break, total_length, init_length;
2002
2054
uint num_fields;
2003
2055
DRIZZLE_RES *res;
2004
2056
DRIZZLE_FIELD *field;
2048
2100
discarding SHOW CREATE EVENT statements generation. The myslq.event
2049
2101
table data should be skipped too.
2051
if (!opt_events && !my_strcasecmp(&my_charset_utf8_general_ci, db, "mysql") &&
2052
!my_strcasecmp(&my_charset_utf8_general_ci, table, "event"))
2103
if (!opt_events && !my_strcasecmp(&my_charset_latin1, db, "mysql") &&
2104
!my_strcasecmp(&my_charset_latin1, table, "event"))
2054
2106
verbose_msg("-- Skipping data table mysql.event, --skip-events was used\n");
2071
2122
Convert the path to native os format
2072
2123
and resolve to the full filepath.
2074
convert_dirname(tmp_path,path,NULL);
2125
convert_dirname(tmp_path,path,NullS);
2075
2126
my_load_path(tmp_path, tmp_path, NULL);
2076
2127
fn_format(filename, table, tmp_path, ".txt", MYF(MY_UNPACK_FILENAME));
2084
2135
/* now build the query string */
2086
query_string.append( "SELECT * INTO OUTFILE '");
2087
query_string.append( filename);
2088
query_string.append( "'");
2137
dynstr_append_checked(&query_string, "SELECT * INTO OUTFILE '");
2138
dynstr_append_checked(&query_string, filename);
2139
dynstr_append_checked(&query_string, "'");
2090
2141
if (fields_terminated || enclosed || opt_enclosed || escaped)
2091
query_string.append( " FIELDS");
2142
dynstr_append_checked(&query_string, " FIELDS");
2093
add_load_option(query_string, " TERMINATED BY ", fields_terminated);
2094
add_load_option(query_string, " ENCLOSED BY ", enclosed);
2095
add_load_option(query_string, " OPTIONALLY ENCLOSED BY ", opt_enclosed);
2096
add_load_option(query_string, " ESCAPED BY ", escaped);
2097
add_load_option(query_string, " LINES TERMINATED BY ", lines_terminated);
2144
add_load_option(&query_string, " TERMINATED BY ", fields_terminated);
2145
add_load_option(&query_string, " ENCLOSED BY ", enclosed);
2146
add_load_option(&query_string, " OPTIONALLY ENCLOSED BY ", opt_enclosed);
2147
add_load_option(&query_string, " ESCAPED BY ", escaped);
2148
add_load_option(&query_string, " LINES TERMINATED BY ", lines_terminated);
2099
query_string.append( " FROM ");
2100
query_string.append( result_table);
2150
dynstr_append_checked(&query_string, " FROM ");
2151
dynstr_append_checked(&query_string, result_table);
2104
query_string.append( " WHERE ");
2105
query_string.append( where);
2155
dynstr_append_checked(&query_string, " WHERE ");
2156
dynstr_append_checked(&query_string, where);
2110
query_string.append( " ORDER BY ");
2111
query_string.append( order_by);
2161
dynstr_append_checked(&query_string, " ORDER BY ");
2162
dynstr_append_checked(&query_string, order_by);
2114
if (drizzle_real_query(drizzle, query_string.c_str(), query_string.length()))
2165
if (drizzle_real_query(drizzle, query_string.str, query_string.length))
2116
2167
DB_error(drizzle, "when executing 'SELECT INTO OUTFILE'");
2168
dynstr_free(&query_string);
2147
2199
fprintf(md_result_file, "-- ORDER BY: %s\n", order_by);
2148
2200
check_io(md_result_file);
2150
query_string.append( " ORDER BY ");
2151
query_string.append( order_by);
2202
dynstr_append_checked(&query_string, " ORDER BY ");
2203
dynstr_append_checked(&query_string, order_by);
2154
2206
if (!opt_xml && !opt_compact)
2156
2208
fputs("\n", md_result_file);
2157
2209
check_io(md_result_file);
2159
if (drizzle_query_with_error_report(drizzle, 0, query_string.c_str()))
2211
if (drizzle_query_with_error_report(drizzle, 0, query_string.str))
2161
2213
DB_error(drizzle, "when retrieving data from server");
2196
2248
total_length= opt_net_buffer_length; /* Force row break */
2199
init_length=(uint) insert_pat.length()+4;
2251
init_length=(uint) insert_pat.length+4;
2201
2253
print_xml_tag(md_result_file, "\t", "\n", "table_data", "name=", table,
2203
2255
if (opt_autocommit)
2205
2257
fprintf(md_result_file, "set autocommit=0;\n");
2245
2297
if (extended_insert && !opt_xml)
2300
dynstr_set_checked(&extended_row,"(");
2250
extended_row.append(",");
2302
dynstr_append_checked(&extended_row,",");
2256
if (!(field->type & NUM_FLAG))
2308
if (!IS_NUM_FIELD(field))
2259
2311
"length * 2 + 2" is OK for both HEX and non-HEX modes:
2263
2315
plus 2 bytes for leading and trailing '\'' characters.
2264
2316
Also we need to reserve 1 byte for terminating '\0'.
2266
char * tmp_str= (char *)malloc(length * 2 + 2 + 1);
2267
memset(tmp_str, '\0', length * 2 + 2 + 1);
2318
dynstr_realloc_checked(&extended_row,length * 2 + 2 + 1);
2268
2319
if (opt_hex_blob && is_blob)
2270
extended_row.append("0x");
2271
drizzle_hex_string(tmp_str, row[i], length);
2272
extended_row.append(tmp_str);
2321
dynstr_append_checked(&extended_row, "0x");
2322
extended_row.length+= drizzle_hex_string(extended_row.str +
2323
extended_row.length,
2325
assert(extended_row.length+1 <= extended_row.max_length);
2326
/* drizzle_hex_string() already terminated string by '\0' */
2327
assert(extended_row.str[extended_row.length] == '\0');
2276
extended_row.append("'");
2277
drizzle_escape_string(tmp_str,
2279
extended_row.append(tmp_str);
2280
extended_row.append("'");
2331
dynstr_append_checked(&extended_row,"'");
2332
extended_row.length +=
2333
drizzle_real_escape_string(&drizzle_connection,
2334
&extended_row.str[extended_row.length],
2336
extended_row.str[extended_row.length]='\0';
2337
dynstr_append_checked(&extended_row,"'");
2287
2343
char *ptr= row[i];
2288
2344
if (my_isalpha(charset_info, *ptr) || (*ptr == '-' &&
2289
2345
my_isalpha(charset_info, ptr[1])))
2290
extended_row.append( "NULL");
2346
dynstr_append_checked(&extended_row, "NULL");
2293
extended_row.append( ptr);
2349
dynstr_append_checked(&extended_row, ptr);
2298
extended_row.append("''");
2354
dynstr_append_checked(&extended_row,"''");
2301
extended_row.append("NULL");
2357
dynstr_append_checked(&extended_row,"NULL");
2318
2374
/* Define xsi:type="xs:hexBinary" for hex encoded data */
2319
2375
print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2320
field->name, "xsi:type=", "xs:hexBinary", NULL);
2376
field->name, "xsi:type=", "xs:hexBinary", NullS);
2321
2377
print_blob_as_hex(md_result_file, row[i], length);
2325
2381
print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2382
field->name, NullS);
2327
2383
print_quoted_xml(md_result_file, row[i], length);
2329
2385
fputs("</field>\n", md_result_file);
2377
2433
if (extended_insert)
2379
uint32_t row_length;
2380
extended_row.append(")");
2381
row_length= 2 + extended_row.length();
2436
dynstr_append_checked(&extended_row,")");
2437
row_length= 2 + extended_row.length;
2382
2438
if (total_length + row_length < opt_net_buffer_length)
2384
2440
total_length+= row_length;
2385
2441
fputc(',',md_result_file); /* Always row break */
2386
fputs(extended_row.c_str(),md_result_file);
2442
fputs(extended_row.str,md_result_file);
2391
2447
fputs(";\n", md_result_file);
2392
2448
row_break=1; /* This is first row */
2394
fputs(insert_pat.c_str(),md_result_file);
2395
fputs(extended_row.c_str(),md_result_file);
2450
fputs(insert_pat.str,md_result_file);
2451
fputs(extended_row.str,md_result_file);
2396
2452
total_length= row_length+init_length;
2398
2454
check_io(md_result_file);
2567
2625
static int init_dumping(char *database, int init_func(char*))
2569
2627
if (drizzle_get_server_version(drizzle) >= 50003 &&
2570
!my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
2628
!my_strcasecmp(&my_charset_latin1, database, "information_schema"))
2573
2631
if (drizzle_select_db(drizzle, database))
2600
2658
if (extended_insert)
2601
extended_row.clear();
2659
init_dynamic_string_checked(&extended_row, "", 1024, 1024);
2603
2661
} /* init_dumping */
2606
2664
/* Return 1 if we should copy the table */
2608
static bool include_table(const unsigned char *hash_key, size_t len)
2666
static bool include_table(const uchar *hash_key, size_t len)
2610
2668
return !hash_search(&ignore_table, hash_key, len);
2618
2676
char table_buff[NAME_LEN*2+3];
2619
2677
char hash_key[2*NAME_LEN+2]; /* "db.tablename" */
2620
2678
char *afterdot;
2621
int using_mysql_db= my_strcasecmp(&my_charset_utf8_general_ci, database, "mysql");
2624
afterdot= my_stpcpy(hash_key, database);
2679
int using_mysql_db= my_strcasecmp(&my_charset_latin1, database, "mysql");
2682
afterdot= strmov(hash_key, database);
2625
2683
*afterdot++= '.';
2627
2685
if (init_dumping(database, init_dumping_tables))
2630
print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NULL);
2688
print_xml_tag(md_result_file, "", "\n", "database", "name=", database, NullS);
2631
2689
if (lock_tables)
2634
query= "LOCK TABLES ";
2691
DYNAMIC_STRING query;
2692
init_dynamic_string_checked(&query, "LOCK TABLES ", 256, 1024);
2635
2693
for (numrows= 0 ; (table= getTableName(1)) ; )
2637
char *end= my_stpcpy(afterdot, table);
2638
if (include_table((unsigned char*) hash_key,end - hash_key))
2695
char *end= strmov(afterdot, table);
2696
if (include_table((uchar*) hash_key,end - hash_key))
2641
query.append( quote_name(table, table_buff, 1));
2642
query.append( " READ /*!32311 LOCAL */,");
2699
dynstr_append_checked(&query, quote_name(table, table_buff, 1));
2700
dynstr_append_checked(&query, " READ /*!32311 LOCAL */,");
2645
if (numrows && drizzle_real_query(drizzle, query.c_str(), query.length()-1))
2703
if (numrows && drizzle_real_query(drizzle, query.str, query.length-1))
2646
2704
DB_error(drizzle, "when using LOCK TABLES");
2647
2705
/* We shall continue here, if --force was given */
2706
dynstr_free(&query);
2650
2708
if (flush_logs)
2656
2714
while ((table= getTableName(0)))
2658
char *end= my_stpcpy(afterdot, table);
2659
if (include_table((unsigned char*) hash_key, end - hash_key))
2716
char *end= strmov(afterdot, table);
2717
if (include_table((uchar*) hash_key, end - hash_key))
2661
2719
dump_table(table,database);
2720
my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
2669
2727
check_io(md_result_file);
2671
2729
if (lock_tables)
2672
drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
2730
VOID(drizzle_query_with_error_report(drizzle, 0, "UNLOCK TABLES"));
2673
2731
if (flush_privileges && using_mysql_db == 0)
2675
2733
fprintf(md_result_file,"\n--\n-- Flush Grant Tables \n--\n");
2742
2800
if (!(dump_tables= pos= (char**) alloc_root(&root, tables * sizeof(char *))))
2743
2801
die(EX_EOM, "alloc_root failure.");
2745
lock_tables_query= "LOCK TABLES ";
2803
init_dynamic_string_checked(&lock_tables_query, "LOCK TABLES ", 256, 1024);
2746
2804
for (; tables > 0 ; tables-- , table_names++)
2748
2806
/* the table name passed on commandline may be wrong case */
2751
2809
/* Add found table name to lock_tables_query */
2752
2810
if (lock_tables)
2754
lock_tables_query.append( quote_name(*pos, table_buff, 1));
2755
lock_tables_query.append( " READ /*!32311 LOCAL */,");
2812
dynstr_append_checked(&lock_tables_query, quote_name(*pos, table_buff, 1));
2813
dynstr_append_checked(&lock_tables_query, " READ /*!32311 LOCAL */,");
2771
2830
if (lock_tables)
2773
if (drizzle_real_query(drizzle, lock_tables_query.c_str(),
2774
lock_tables_query.length()-1))
2832
if (drizzle_real_query(drizzle, lock_tables_query.str,
2833
lock_tables_query.length-1))
2776
2835
if (!ignore_errors)
2837
dynstr_free(&lock_tables_query);
2778
2838
free_root(&root, MYF(0));
2780
2840
DB_error(drizzle, "when doing LOCK TABLES");
2781
2841
/* We shall countinue here, if --force was given */
2844
dynstr_free(&lock_tables_query);
2784
2845
if (flush_logs)
2786
2847
if (drizzle_refresh(drizzle, REFRESH_LOG))
2792
2853
/* We shall countinue here, if --force was given */
2795
print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NULL);
2856
print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NullS);
2797
2858
/* Dump each selected table */
2798
2859
for (pos= dump_tables; pos < end; pos++)
2799
2860
dump_table(*pos, db);
2801
2862
free_root(&root, MYF(0));
2863
my_free(order_by, MYF(MY_ALLOW_ZERO_PTR));
3025
3086
static int purge_bin_logs_to(DRIZZLE *drizzle_con, char* log_name)
3028
string str= "PURGE BINARY LOGS TO '";
3029
str.append(log_name);
3031
err = drizzle_query_with_error_report(drizzle_con, 0, str.c_str());
3090
init_dynamic_string_checked(&str, "PURGE BINARY LOGS TO '", 1024, 1024);
3091
dynstr_append_checked(&str, log_name);
3092
dynstr_append_checked(&str, "'");
3093
err = drizzle_query_with_error_report(drizzle_con, 0, str.str);
3086
3149
const char *pos= start;
3089
3152
for (; pos != end && *pos != ','; pos++) ;
3090
var_len= (uint32_t) (pos - start);
3091
strmake(buff, start, min((uint32_t)sizeof(buff), var_len));
3153
var_len= (uint) (pos - start);
3154
strmake(buff, start, min(sizeof(buff), var_len));
3092
3155
find= find_type(buff, lib, var_len);
3293
3356
drizzle_data_seek(res, 0);
3294
3357
row= drizzle_fetch_row(res);
3295
3358
quoted_field= quote_name(row[4], buff, 0);
3296
end= my_stpcpy(result, quoted_field);
3359
end= strmov(result, quoted_field);
3297
3360
while ((row= drizzle_fetch_row(res)) && atoi(row[3]) > 1)
3299
3362
quoted_field= quote_name(row[4], buff, 0);
3300
end= strxmov(end, ",", quoted_field, NULL);
3363
end= strxmov(end, ",", quoted_field, NullS);
3375
The following functions are wrappers for the dynamic string functions
3376
and if they fail, the wrappers will terminate the current process.
3379
#define DYNAMIC_STR_ERROR_MSG "Couldn't perform DYNAMIC_STRING operation"
3381
static void init_dynamic_string_checked(DYNAMIC_STRING *str, const char *init_str,
3382
uint init_alloc, uint alloc_increment)
3384
if (init_dynamic_string(str, init_str, init_alloc, alloc_increment))
3385
die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
3388
static void dynstr_append_checked(DYNAMIC_STRING* dest, const char* src)
3390
if (dynstr_append(dest, src))
3391
die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
3394
static void dynstr_set_checked(DYNAMIC_STRING *str, const char *init_str)
3396
if (dynstr_set(str, init_str))
3397
die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
3400
static void dynstr_append_mem_checked(DYNAMIC_STRING *str, const char *append,
3403
if (dynstr_append_mem(str, append, length))
3404
die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
3407
static void dynstr_realloc_checked(DYNAMIC_STRING *str, ulong additional_size)
3409
if (dynstr_realloc(str, additional_size))
3410
die(EX_DRIZZLEERR, DYNAMIC_STR_ERROR_MSG);
3312
3414
int main(int argc, char **argv)