94
95
opt_include_master_host_port= false,
95
96
opt_alltspcs= false;
96
97
static bool debug_info_flag= false, debug_check_flag= false;
97
static uint32_t opt_max_allowed_packet, opt_net_buffer_length, show_progress_size= 0;
98
static uint32_t show_progress_size= 0;
98
99
static uint64_t total_rows= 0;
99
static DRIZZLE drizzleclient_connection, *drizzle= 0;
100
static drizzle_st drizzle;
101
static drizzle_con_st dcon;
100
102
static string insert_pat;
101
103
static char *opt_password= NULL, *current_user= NULL,
102
104
*current_host= NULL, *path= NULL, *fields_terminated= NULL,
313
315
"Option automatically turns --lock-tables off.",
314
316
(char**) &opt_master_data, (char**) &opt_master_data, 0,
315
317
GET_UINT, OPT_ARG, 0, 0, DRIZZLE_OPT_MASTER_DATA_COMMENTED_SQL, 0, 0, 0},
316
{"max_allowed_packet", OPT_MAX_ALLOWED_PACKET, "",
317
(char**) &opt_max_allowed_packet, (char**) &opt_max_allowed_packet, 0,
318
GET_UINT32, REQUIRED_ARG, 24*1024*1024, 4096,
319
(int64_t) 2L*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
320
{"net_buffer_length", OPT_NET_BUFFER_LENGTH, "",
321
(char**) &opt_net_buffer_length, (char**) &opt_net_buffer_length, 0,
322
GET_UINT32, REQUIRED_ARG, 1024*1024L-1025, 4096, 16*1024L*1024L,
323
MALLOC_OVERHEAD-1024, 1024, 0},
324
318
{"no-autocommit", OPT_AUTOCOMMIT,
325
319
"Wrap tables with autocommit/commit statements.",
326
320
(char**) &opt_autocommit, (char**) &opt_autocommit, 0, GET_BOOL, NO_ARG,
415
409
static void die(int error, const char* reason, ...);
416
410
static void maybe_die(int error, const char* reason, ...);
417
411
static void write_header(FILE *sql_file, char *db_name);
418
static void print_value(FILE *file, DRIZZLE_RES *result, DRIZZLE_ROW row,
419
const char *prefix,const char *name,
412
static void print_value(FILE *file, drizzle_result_st *result,
413
drizzle_row_t row, const char *prefix, const char *name,
420
414
int string_value);
421
static const char* fetch_named_row(DRIZZLE_RES *result, DRIZZLE_ROW row, const char* name);
415
static const char* fetch_named_row(drizzle_result_st *result, drizzle_row_t row,
422
417
static int dump_selected_tables(char *db, char **table_names, int tables);
423
418
static int dump_all_tables_in_db(char *db);
424
419
static int init_dumping_tables(char *);
469
464
static void print_version(void)
471
466
printf(_("%s Ver %s Distrib %s, for %s (%s)\n"),my_progname,DUMP_VERSION,
472
drizzleclient_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
467
drizzle_version(),SYSTEM_TYPE,MACHINE_TYPE);
473
468
} /* print_version */
523
518
fprintf(sql_file,
524
519
"-- DRIZZLE dump %s Distrib %s, for %s (%s)\n--\n",
525
DUMP_VERSION, drizzleclient_get_client_info(),
526
SYSTEM_TYPE, MACHINE_TYPE);
520
DUMP_VERSION, drizzle_version(), SYSTEM_TYPE, MACHINE_TYPE);
527
521
fprintf(sql_file, "-- Host: %s Database: %s\n",
528
522
current_host ? current_host : "localhost", db_name ? db_name :
530
524
fputs("-- ------------------------------------------------------\n",
532
526
fprintf(sql_file, "-- Server version\t%s\n",
533
drizzleclient_get_server_info(&drizzleclient_connection));
527
drizzle_con_server_version(&dcon));
535
529
if (opt_set_charset)
536
530
fprintf(sql_file,
770
764
static int get_options(int *argc, char ***argv)
773
const DRIZZLE_PARAMETERS *drizzle_params= drizzleclient_get_parameters();
775
opt_max_allowed_packet= *drizzle_params->p_max_allowed_packet;
776
opt_net_buffer_length= *drizzle_params->p_net_buffer_length;
778
768
md_result_file= stdout;
779
769
load_defaults("drizzle",load_default_groups,argc,argv);
858
846
** DB_error -- prints DRIZZLE error message and exits the program.
860
static void DB_error(DRIZZLE *drizzle_arg, const char *when)
848
static void DB_error(drizzle_result_st *res, drizzle_return_t ret,
851
if (ret == DRIZZLE_RETURN_ERROR_CODE)
853
maybe_die(EX_DRIZZLEERR, _("Got error: %s %s"),
854
drizzle_result_error_code(res), when);
855
drizzle_result_free(res);
858
maybe_die(EX_DRIZZLEERR, _("Got error: %s %s"), ret, when);
863
maybe_die(EX_DRIZZLEERR, _("Got error: %d: %s %s"),
864
drizzleclient_errno(drizzle_arg), drizzleclient_error(drizzle_arg), when);
949
static int drizzleclient_query_with_error_report(DRIZZLE *drizzle_con, DRIZZLE_RES **res,
944
static int drizzleclient_query_with_error_report(drizzle_con_st *con,
945
drizzle_result_st *result,
946
const char *query_str,
952
if (drizzleclient_query(drizzle_con, query) ||
953
(res && !((*res)= drizzleclient_store_result(drizzle_con))))
949
drizzle_return_t ret;
951
if (drizzle_query_str(con, result, query_str, &ret) == NULL ||
952
ret != DRIZZLE_RETURN_OK)
954
if (ret == DRIZZLE_RETURN_ERROR_CODE)
956
maybe_die(EX_DRIZZLEERR, _("Couldn't execute '%s': %s (%d)"),
957
query_str, drizzle_result_error(result),
958
drizzle_result_error_code(result));
959
drizzle_result_free(result);
963
maybe_die(EX_DRIZZLEERR, _("Couldn't execute '%s': %s (%d)"),
964
query_str, drizzle_con_error(con), ret);
970
ret= drizzle_column_buffer(result);
972
ret= drizzle_result_buffer(result);
973
if (ret != DRIZZLE_RETURN_OK)
975
drizzle_result_free(result);
955
976
maybe_die(EX_DRIZZLEERR, _("Couldn't execute '%s': %s (%d)"),
956
query, drizzleclient_error(drizzle_con), drizzleclient_errno(drizzle_con));
977
query_str, drizzle_con_error(con), ret);
1014
1036
static int connect_to_db(char *host, char *user,char *passwd)
1038
drizzle_return_t ret;
1016
1040
verbose_msg(_("-- Connecting to %s...\n"), host ? host : "localhost");
1017
drizzleclient_create(&drizzleclient_connection);
1019
drizzleclient_options(&drizzleclient_connection,DRIZZLE_OPT_COMPRESS,NULL);
1020
if (!(drizzle= drizzleclient_connect(&drizzleclient_connection,host,user,passwd,
1021
NULL,opt_drizzle_port, NULL,
1041
drizzle_create(&drizzle);
1042
drizzle_con_create(&drizzle, &dcon);
1043
drizzle_con_set_tcp(&dcon, host, opt_drizzle_port);
1044
drizzle_con_set_auth(&dcon, user, passwd);
1045
ret= drizzle_con_connect(&dcon);
1046
if (ret != DRIZZLE_RETURN_OK)
1024
DB_error(&drizzleclient_connection, "when trying to connect");
1048
DB_error(NULL, ret, "when trying to connect");
1305
1330
static void print_xml_row(FILE *xml_file, const char *row_name,
1306
DRIZZLE_RES *tableRes, DRIZZLE_ROW *row)
1331
drizzle_result_st *tableRes, drizzle_row_t *row)
1309
DRIZZLE_FIELD *field;
1310
uint32_t *lengths= drizzleclient_fetch_lengths(tableRes);
1334
drizzle_column_st *column;
1335
size_t *lengths= drizzle_row_field_sizes(tableRes);
1312
1337
fprintf(xml_file, "\t\t<%s", row_name);
1313
1338
check_io(xml_file);
1314
drizzleclient_field_seek(tableRes, 0);
1315
for (i= 0; (field= drizzleclient_fetch_field(tableRes)); i++)
1339
drizzle_column_seek(tableRes, 0);
1340
for (i= 0; (column= drizzle_column_next(tableRes)); i++)
1319
1344
fputc(' ', xml_file);
1320
print_quoted_xml(xml_file, field->name, field->name_length);
1345
print_quoted_xml(xml_file, drizzle_column_name(column),
1346
strlen(drizzle_column_name(column)));
1321
1347
fputs("=\"", xml_file);
1322
1348
print_quoted_xml(xml_file, (*row)[i], lengths[i]);
1323
1349
fputc('"', xml_file);
1373
1399
bool init=0, delayed, write_data, complete_insert;
1374
1400
char *result_table, *opt_quoted_table;
1375
1401
const char *insert_option;
1376
char name_buff[NAME_LEN+3],table_buff[NAME_LEN*2+3];
1377
char table_buff2[NAME_LEN*2+3], query_buff[QUERY_LENGTH];
1402
char name_buff[DRIZZLE_MAX_COLUMN_NAME_SIZE+3];
1403
char table_buff[DRIZZLE_MAX_COLUMN_NAME_SIZE*2+3];
1404
char table_buff2[DRIZZLE_MAX_TABLE_SIZE*2+3];
1405
char query_buff[QUERY_LENGTH];
1378
1406
FILE *sql_file= md_result_file;
1379
DRIZZLE_RES *result;
1407
drizzle_result_st result;
1382
1410
*ignore_flag= check_if_ignore_table(table, table_type);
1418
1446
/* Make an sql-file, if path was given iow. option -T was given */
1419
1447
char buff[20+FN_REFLEN];
1420
const DRIZZLE_FIELD *field;
1448
const drizzle_column_st *column;
1422
1450
snprintf(buff, sizeof(buff), "show create table %s", result_table);
1424
if (drizzleclient_query_with_error_report(drizzle, &result, buff))
1452
if (drizzleclient_query_with_error_report(&dcon, &result, buff, false))
1429
1457
if (!(sql_file= open_sql_file_for_table(table)))
1459
drizzle_result_free(&result);
1432
1463
write_header(sql_file, db);
1446
1477
check_io(sql_file);
1449
field= drizzleclient_fetch_field_direct(result, 0);
1480
column= drizzle_column_index(&result, 0);
1451
row= drizzleclient_fetch_row(result);
1482
row= drizzle_row_next(&result);
1453
1484
fprintf(sql_file, "%s;\n", row[1]);
1455
1486
check_io(sql_file);
1456
drizzleclient_free_result(result);
1487
drizzle_result_free(&result);
1458
1490
snprintf(query_buff, sizeof(query_buff), "show fields from %s",
1460
if (drizzleclient_query_with_error_report(drizzle, &result, query_buff))
1493
if (drizzleclient_query_with_error_report(&dcon, &result, query_buff, false))
1463
1496
fclose(sql_file);
1503
1536
insert_pat.append(quote_name(row[SHOW_FIELDNAME], name_buff, 0));
1506
*num_fields= drizzleclient_num_rows(result);
1507
drizzleclient_free_result(result);
1539
*num_fields= drizzle_result_row_count(&result);
1540
drizzle_result_free(&result);
1511
1544
verbose_msg(_("%s: Warning: Can't set SQL_QUOTE_SHOW_CREATE option (%s)\n"),
1512
my_progname, drizzleclient_error(drizzle));
1545
my_progname, drizzle_con_error(&dcon));
1514
1547
snprintf(query_buff, sizeof(query_buff), "show fields from %s",
1516
if (drizzleclient_query_with_error_report(drizzle, &result, query_buff))
1549
if (drizzleclient_query_with_error_report(&dcon, &result, query_buff, false))
1519
1552
/* Make an sql-file, if path was given iow. option -T was given */
1601
1637
check_io(sql_file);
1604
*num_fields= drizzleclient_num_rows(result);
1605
drizzleclient_free_result(result);
1640
*num_fields= drizzle_result_row_count(&result);
1641
drizzle_result_free(&result);
1606
1643
if (!opt_no_create_info)
1608
1645
/* Make an sql-file, if path was given iow. option -T was given */
1609
1646
char buff[20+FN_REFLEN];
1610
1647
uint32_t keynr,primary_key;
1611
1648
snprintf(buff, sizeof(buff), "show keys from %s", result_table);
1612
if (drizzleclient_query_with_error_report(drizzle, &result, buff))
1649
if (drizzleclient_query_with_error_report(&dcon, &result, buff, false))
1614
if (drizzleclient_errno(drizzle) == ER_WRONG_OBJECT)
1617
fputs("\t\t<options Comment=\"view\" />\n", sql_file);
1620
fprintf(stderr, _("%s: Can't get keys for table %s (%s)\n"),
1621
my_progname, result_table, drizzleclient_error(drizzle));
1651
fprintf(stderr, _("%s: Can't get keys for table %s\n"),
1652
my_progname, result_table);
1623
1654
fclose(sql_file);
1646
drizzleclient_data_seek(result,0);
1677
drizzle_row_seek(&result,0);
1648
while ((row= drizzleclient_fetch_row(result)))
1679
while ((row= drizzle_row_next(&result)))
1652
print_xml_row(sql_file, "key", result, &row);
1683
print_xml_row(sql_file, "key", &result, &row);
1684
1715
/* Get DRIZZLE specific create options */
1685
1716
if (create_options)
1687
char show_name_buff[NAME_LEN*2+2+24];
1718
char show_name_buff[DRIZZLE_MAX_COLUMN_NAME_SIZE*2+2+24];
1689
1720
/* Check memory for quote_for_like() */
1690
1721
snprintf(buff, sizeof(buff), "show table status like %s",
1691
1722
quote_for_like(table, show_name_buff));
1693
if (drizzleclient_query_with_error_report(drizzle, &result, buff))
1724
if (!drizzleclient_query_with_error_report(&dcon, &result, buff, false))
1695
if (drizzleclient_errno(drizzle) != ER_PARSE_ERROR)
1696
{ /* If old DRIZZLE version */
1697
verbose_msg(_("-- Warning: Couldn't get status information for " \
1698
"table %s (%s)\n"), result_table,drizzleclient_error(drizzle));
1726
if (!(row= drizzle_row_next(&result)))
1729
_("Error: Couldn't read status information for table %s\n"),
1701
else if (!(row= drizzleclient_fetch_row(result)))
1704
_("Error: Couldn't read status information for table %s (%s)\n"),
1705
result_table,drizzleclient_error(drizzle));
1710
print_xml_row(sql_file, "options", result, &row);
1713
fputs("/*!",sql_file);
1714
print_value(sql_file,result,row,"engine=","Engine",0);
1715
print_value(sql_file,result,row,"","Create_options",0);
1716
print_value(sql_file,result,row,"comment=","Comment",1);
1735
print_xml_row(sql_file, "options", &result, &row);
1738
fputs("/*!",sql_file);
1739
print_value(sql_file,&result,row,"engine=","Engine",0);
1740
print_value(sql_file,&result,row,"","Create_options",0);
1741
print_value(sql_file,&result,row,"comment=","Comment",1);
1718
fputs(" */",sql_file);
1743
fputs(" */",sql_file);
1747
drizzle_result_free(&result);
1722
drizzleclient_free_result(result); /* Is always safe to free */
1726
1751
fputs(";\n", sql_file);
1825
1850
static void dump_table(char *table, char *db)
1827
1852
char ignore_flag;
1828
char buf[200], table_buff[NAME_LEN+3];
1853
char table_buff[DRIZZLE_MAX_TABLE_SIZE+3];
1829
1854
string query_string;
1830
char table_type[NAME_LEN];
1831
char *result_table, table_buff2[NAME_LEN*2+3], *opt_quoted_table;
1855
char table_type[DRIZZLE_MAX_TABLE_SIZE];
1856
char *result_table, table_buff2[DRIZZLE_MAX_TABLE_SIZE*2+3], *opt_quoted_table;
1833
1858
uint32_t rownr, row_break, total_length, init_length;
1834
1859
uint64_t num_fields= 0;
1836
DRIZZLE_FIELD *field;
1860
drizzle_return_t ret;
1861
drizzle_result_st result;
1862
drizzle_column_st *column;
1926
1952
query_string.append( order_by);
1929
if (drizzleclient_real_query(drizzle, query_string.c_str(), query_string.length()))
1955
if (drizzle_query(&dcon, &result, query_string.c_str(),
1956
query_string.length(), &ret) == NULL ||
1957
ret != DRIZZLE_RETURN_OK)
1931
DB_error(drizzle, _("when executing 'SELECT INTO OUTFILE'"));
1959
DB_error(&result, ret, _("when executing 'SELECT INTO OUTFILE'"));
1963
drizzle_result_free(&result);
1971
2001
fputs("\n", md_result_file);
1972
2002
check_io(md_result_file);
1974
if (drizzleclient_query_with_error_report(drizzle, 0, query_string.c_str()))
1976
DB_error(drizzle, _("when retrieving data from server"));
1980
res=drizzleclient_use_result(drizzle);
1982
res=drizzleclient_store_result(drizzle);
1985
DB_error(drizzle, _("when retrieving data from server"));
2004
if (drizzleclient_query_with_error_report(&dcon, &result,
2005
query_string.c_str(), quick))
1989
2010
verbose_msg(_("-- Retrieving rows...\n"));
1990
if (drizzleclient_num_fields(res) != num_fields)
2011
if (drizzle_result_column_count(&result) != num_fields)
1992
2013
fprintf(stderr,_("%s: Error in field count for table: %s ! Aborting.\n"),
1993
2014
my_progname, result_table);
1994
2015
error= EX_CONSCHECK;
2016
drizzle_result_free(&result);
2021
2043
check_io(md_result_file);
2024
while ((row= drizzleclient_fetch_row(res)))
2027
uint32_t *lengths= drizzleclient_fetch_lengths(res);
2056
drizzle_row_free(&result, row);
2058
row= drizzle_row_buffer(&result, &ret);
2059
if (ret != DRIZZLE_RETURN_OK)
2062
_("%s: Error reading rows for table: %s (%d:%s) ! Aborting.\n"),
2063
my_progname, result_table, ret, drizzle_con_error(&dcon));
2064
drizzle_result_free(&result);
2069
row= drizzle_row_next(&result);
2074
lengths= drizzle_row_field_sizes(&result);
2029
2077
if ((rownr % show_progress_size) == 0)
2043
2091
check_io(md_result_file);
2046
for (i= 0; i < drizzleclient_num_fields(res); i++)
2094
for (i= 0; i < drizzle_result_column_count(&result); i++)
2049
2097
uint32_t length= lengths[i];
2051
if (!(field= drizzleclient_fetch_field(res)))
2099
if (!(column= drizzle_column_next(&result)))
2052
2100
die(EX_CONSCHECK,
2053
2101
_("Not enough fields from table %s! Aborting.\n"),
2058
2106
we have not a BLOB but a TEXT column.
2059
2107
we'll dump in hex only BLOB columns.
2061
is_blob= (opt_hex_blob && field->charsetnr == 63 &&
2062
(field->type == DRIZZLE_TYPE_VARCHAR ||
2063
field->type == DRIZZLE_TYPE_BLOB)) ? 1 : 0;
2109
is_blob= (opt_hex_blob && drizzle_column_charset(column) == 63 &&
2110
(drizzle_column_type(column) == DRIZZLE_COLUMN_TYPE_VARCHAR ||
2111
drizzle_column_type(column) == DRIZZLE_COLUMN_TYPE_BLOB)) ? 1 : 0;
2064
2112
if (extended_insert && !opt_xml)
2090
2138
if (opt_hex_blob && is_blob)
2092
2140
extended_row.append("0x");
2093
drizzleclient_drizzleclient_octet2hex(tmp_str, row[i], length);
2141
drizzle_hex_string(tmp_str, row[i], length);
2094
2142
extended_row.append(tmp_str);
2098
2146
extended_row.append("'");
2099
drizzleclient_escape_string(tmp_str,
2147
drizzle_escape_string(tmp_str, row[i],length);
2101
2148
extended_row.append(tmp_str);
2102
2149
extended_row.append("'");
2140
2187
/* Define xsi:type="xs:hexBinary" for hex encoded data */
2141
2188
print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2142
field->name, "xsi:type=", "xs:hexBinary", NULL);
2189
drizzle_column_name(column), "xsi:type=", "xs:hexBinary", NULL);
2143
2190
print_blob_as_hex(md_result_file, row[i], length);
2147
2194
print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2195
drizzle_column_name(column), NULL);
2149
2196
print_quoted_xml(md_result_file, row[i], length);
2151
2198
fputs("</field>\n", md_result_file);
2167
2214
print_xml_tag(md_result_file, "\t\t", "", "field", "name=",
2215
drizzle_column_name(column), NULL);
2169
2216
fputs(!my_isalpha(charset_info, *ptr) ? ptr: "NULL",
2170
2217
md_result_file);
2171
2218
fputs("</field>\n", md_result_file);
2201
2248
uint32_t row_length;
2202
2249
extended_row.append(")");
2203
2250
row_length= 2 + extended_row.length();
2204
if (total_length + row_length < opt_net_buffer_length)
2251
if (total_length + row_length < DRIZZLE_MAX_LINE_LENGTH)
2206
2253
total_length+= row_length;
2207
2254
fputc(',',md_result_file); /* Always row break */
2233
2280
fputs(";\n", md_result_file); /* If not empty table */
2234
2281
fflush(md_result_file);
2235
2282
check_io(md_result_file);
2236
if (drizzleclient_errno(drizzle))
2238
snprintf(buf, sizeof(buf),
2239
_("%s: Error %d: %s when dumping table %s at row: %d\n"),
2241
drizzleclient_errno(drizzle),
2242
drizzleclient_error(drizzle),
2246
error= EX_CONSCHECK;
2250
2284
/* Moved enable keys to before unlock per bug 15977 */
2251
2285
if (opt_disable_keys)
2277
2311
static char *getTableName(int reset)
2279
static DRIZZLE_RES *res= NULL;
2313
static drizzle_result_st result;
2314
static bool have_result= false;
2284
if (!(res= drizzleclient_list_tables(drizzle,NULL)))
2319
if (drizzleclient_query_with_error_report(&dcon, &result, "SHOW TABLES", false))
2287
if ((row= drizzleclient_fetch_row(res)))
2288
return((char*) row[0]);
2324
if ((row= drizzle_row_next(&result)))
2291
drizzleclient_data_seek(res,0); /* We want to read again */
2328
drizzle_row_seek(&result, 0);
2294
drizzleclient_free_result(res);
2331
drizzle_result_free(&result);
2298
2335
} /* getTableName */
2301
2338
static int dump_all_databases()
2304
DRIZZLE_RES *tableres;
2341
drizzle_result_st tableres;
2307
if (drizzleclient_query_with_error_report(drizzle, &tableres, "SHOW DATABASES"))
2344
if (drizzleclient_query_with_error_report(&dcon, &tableres, "SHOW DATABASES", false))
2309
while ((row= drizzleclient_fetch_row(tableres)))
2346
while ((row= drizzle_row_next(&tableres)))
2311
2348
if (dump_all_tables_in_db(row[0]))
2351
drizzle_result_free(&tableres);
2316
2354
/* dump_all_databases */
2346
2384
int init_dumping_tables(char *qdatabase)
2350
2386
if (!opt_create_db)
2352
2388
char qbuf[256];
2354
DRIZZLE_RES *dbinfo;
2390
drizzle_result_st result;
2391
drizzle_return_t ret;
2356
2393
snprintf(qbuf, sizeof(qbuf),
2357
2394
"SHOW CREATE DATABASE IF NOT EXISTS %s",
2360
if (drizzleclient_query(drizzle, qbuf) || !(dbinfo = drizzleclient_store_result(drizzle)))
2397
if (drizzle_query_str(&dcon, &result, qbuf, &ret) == NULL ||
2398
ret != DRIZZLE_RETURN_OK)
2400
if (ret == DRIZZLE_RETURN_ERROR_CODE)
2401
drizzle_result_free(&result);
2362
2403
/* Old server version, dump generic CREATE DATABASE */
2363
2404
if (opt_drop_database)
2364
2405
fprintf(md_result_file,
2373
if (opt_drop_database)
2374
fprintf(md_result_file,
2375
"\nDROP DATABASE IF EXISTS %s;\n",
2377
row = drizzleclient_fetch_row(dbinfo);
2414
if (drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
2380
fprintf(md_result_file,"\n%s;\n",row[1]);
2416
if (opt_drop_database)
2417
fprintf(md_result_file,
2418
"\nDROP DATABASE IF EXISTS %s;\n",
2420
row = drizzle_row_next(&result);
2421
if (row != NULL && row[1])
2423
fprintf(md_result_file,"\n%s;\n",row[1]);
2382
drizzleclient_free_result(dbinfo);
2426
drizzle_result_free(&result);
2389
2433
static int init_dumping(char *database, int init_func(char*))
2391
if (drizzleclient_get_server_version(drizzle) >= 50003 &&
2392
!my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
2435
drizzle_result_st result;
2436
drizzle_return_t ret;
2438
if (!my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
2395
if (drizzleclient_select_db(drizzle, database))
2441
if (drizzle_select_db(&dcon, &result, database, &ret) == NULL ||
2442
ret != DRIZZLE_RETURN_OK)
2397
DB_error(drizzle, _("when selecting the database"));
2444
DB_error(&result, ret, _("when executing 'SELECT INTO OUTFILE'"));
2398
2445
return 1; /* If --force */
2447
drizzle_result_free(&result);
2400
2449
if (!path && !opt_xml)
2402
2451
if (opt_databases || opt_alldbs)
2439
2488
uint32_t numrows;
2440
char table_buff[NAME_LEN*2+3];
2441
char hash_key[2*NAME_LEN+2]; /* "db.tablename" */
2489
char table_buff[DRIZZLE_MAX_TABLE_SIZE*2+3];
2490
char hash_key[DRIZZLE_MAX_DB_SIZE+DRIZZLE_MAX_TABLE_SIZE+2]; /* "db.tablename" */
2442
2491
char *afterdot;
2492
drizzle_result_st result;
2493
drizzle_return_t ret;
2444
2495
afterdot= strcpy(hash_key, database) + strlen(database);
2445
2496
*afterdot++= '.';
2461
2512
query.append( " READ LOCAL,");
2464
if (numrows && drizzleclient_real_query(drizzle, query.c_str(), query.length()-1))
2465
DB_error(drizzle, _("when using LOCK TABLES"));
2466
/* We shall continue here, if --force was given */
2517
if (drizzle_query(&dcon, &result, query.c_str(),
2518
query.length()-1, &ret) == NULL ||
2519
ret != DRIZZLE_RETURN_OK)
2521
DB_error(&result, ret, _("when using LOCK TABLES"));
2522
/* We shall continue here, if --force was given */
2525
drizzle_result_free(&result);
2469
2529
if (flush_logs)
2471
if (drizzleclient_real_query(drizzle, "FLUSH LOGS", strlen("FLUSH LOGS")))
2472
DB_error(drizzle, _("when doing refresh"));
2473
/* We shall continue here, if --force was given */
2531
if (drizzle_query_str(&dcon, &result, "FLUSH LOGS", &ret) == NULL ||
2532
ret != DRIZZLE_RETURN_OK)
2534
DB_error(&result, ret, _("when doing refresh"));
2535
/* We shall continue here, if --force was given */
2538
drizzle_result_free(&result);
2475
2540
while ((table= getTableName(0)))
2488
2553
check_io(md_result_file);
2490
2555
if (lock_tables)
2491
drizzleclient_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
2557
if (!drizzleclient_query_with_error_report(&dcon, &result, "UNLOCK TABLES", false))
2558
drizzle_result_free(&result);
2494
2562
} /* dump_all_tables_in_db */
2519
2588
snprintf(query, sizeof(query), "SHOW TABLES LIKE %s",
2520
2589
quote_for_like(old_table_name, show_name_buff));
2522
if (drizzleclient_query_with_error_report(drizzle, 0, query))
2591
if (drizzleclient_query_with_error_report(&dcon, &result, query, false))
2525
if ((table_res= drizzleclient_store_result(drizzle)))
2594
num_rows= drizzle_result_row_count(&result);
2527
uint64_t num_rows= drizzleclient_num_rows(table_res);
2533
TODO: Return all matching rows
2535
row= drizzleclient_fetch_row(table_res);
2536
lengths= drizzleclient_fetch_lengths(table_res);
2537
name= strmake_root(root, row[0], lengths[0]);
2539
drizzleclient_free_result(table_res);
2600
TODO: Return all matching rows
2602
row= drizzle_row_next(&result);
2603
lengths= drizzle_row_field_sizes(&result);
2604
name= strmake_root(root, row[0], lengths[0]);
2606
drizzle_result_free(&result);
2545
2612
static int dump_selected_tables(char *db, char **table_names, int tables)
2547
char table_buff[NAME_LEN*2+3];
2614
char table_buff[DRIZZLE_MAX_TABLE_SIZE*2+3];
2548
2615
string lock_tables_query("LOCK TABLES ");
2550
2617
char **dump_tables, **pos, **end;
2618
drizzle_result_st result;
2619
drizzle_return_t ret;
2553
2622
if (init_dumping(db, init_dumping_tables))
2585
2654
if (lock_tables)
2587
if (drizzleclient_real_query(drizzle, lock_tables_query.c_str(),
2588
lock_tables_query.length()-1))
2656
if (drizzle_query(&dcon, &result, lock_tables_query.c_str(),
2657
lock_tables_query.length()-1, &ret) == NULL ||
2658
ret != DRIZZLE_RETURN_OK)
2590
2660
if (!ignore_errors)
2592
2662
free_root(&root, MYF(0));
2594
DB_error(drizzle, _("when doing LOCK TABLES"));
2595
/* We shall countinue here, if --force was given */
2664
DB_error(&result, ret, _("when doing LOCK TABLES"));
2665
/* We shall countinue here, if --force was given */
2668
drizzle_result_free(&result);
2598
2670
if (flush_logs)
2600
if (drizzleclient_real_query(drizzle, "FLUSH LOGS", strlen("FLUSH LOGS")))
2672
if (drizzle_query_str(&dcon, &result, "FLUSH LOGS", &ret) == NULL ||
2673
ret != DRIZZLE_RETURN_OK)
2602
2675
if (!ignore_errors)
2603
2676
free_root(&root, MYF(0));
2604
DB_error(drizzle, _("when doing refresh"));
2677
DB_error(&result, ret, _("when doing refresh"));
2678
/* We shall countinue here, if --force was given */
2606
/* We shall countinue here, if --force was given */
2681
drizzle_result_free(&result);
2609
2684
print_xml_tag(md_result_file, "", "\n", "database", "name=", db, NULL);
2621
2696
check_io(md_result_file);
2623
2698
if (lock_tables)
2624
drizzleclient_query_with_error_report(drizzle, 0, "UNLOCK TABLES");
2700
if (!(drizzleclient_query_with_error_report(&dcon, &result, "UNLOCK TABLES", false)))
2701
drizzle_result_free(&result);
2626
2704
} /* dump_selected_tables */
2629
static int do_show_master_status(DRIZZLE *drizzle_con)
2707
static int do_show_master_status(drizzle_con_st *drizzle_con)
2632
DRIZZLE_RES *master;
2710
drizzle_result_st master;
2633
2711
const char *comment_prefix=
2634
2712
(opt_master_data == DRIZZLE_OPT_MASTER_DATA_COMMENTED_SQL) ? "-- " : "";
2635
if (drizzleclient_query_with_error_report(drizzle_con, &master, "SHOW MASTER STATUS"))
2713
if (drizzleclient_query_with_error_report(drizzle_con, &master, "SHOW MASTER STATUS", false))
2641
row= drizzleclient_fetch_row(master);
2719
row= drizzle_row_next(&master);
2642
2720
if (row && row[0] && row[1])
2644
2722
/* SHOW MASTER STATUS reports file and position */
2656
2734
/* SHOW MASTER STATUS reports nothing and --force is not enabled */
2657
2735
my_printf_error(0, _("Error: Binlogging on server not active"),
2659
drizzleclient_free_result(master);
2737
drizzle_result_free(&master);
2660
2738
maybe_exit(EX_DRIZZLEERR);
2663
drizzleclient_free_result(master);
2741
drizzle_result_free(&master);
2668
static int do_stop_slave_sql(DRIZZLE *drizzle_con)
2746
static int do_stop_slave_sql(drizzle_con_st *drizzle_con)
2748
drizzle_result_st slave;
2671
2749
/* We need to check if the slave sql is running in the first place */
2672
if (drizzleclient_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS"))
2750
if (drizzleclient_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS", false))
2676
DRIZZLE_ROW row= drizzleclient_fetch_row(slave);
2754
drizzle_row_t row= drizzle_row_next(&slave);
2677
2755
if (row && row[11])
2679
2757
/* if SLAVE SQL is not running, we don't stop it */
2680
2758
if (!strcmp(row[11],"No"))
2682
drizzleclient_free_result(slave);
2760
drizzle_result_free(&slave);
2683
2761
/* Silently assume that they don't have the slave running */
2688
drizzleclient_free_result(slave);
2766
drizzle_result_free(&slave);
2690
2768
/* now, stop slave if running */
2691
if (drizzleclient_query_with_error_report(drizzle_con, 0, "STOP SLAVE SQL_THREAD"))
2769
if (drizzleclient_query_with_error_report(drizzle_con, &slave, "STOP SLAVE SQL_THREAD", false))
2771
drizzle_result_free(&slave);
2715
static int do_show_slave_status(DRIZZLE *drizzle_con)
2794
static int do_show_slave_status(drizzle_con_st *drizzle_con)
2796
drizzle_result_st slave;
2718
2797
const char *comment_prefix=
2719
2798
(opt_slave_data == DRIZZLE_OPT_SLAVE_DATA_COMMENTED_SQL) ? "-- " : "";
2720
if (drizzleclient_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS"))
2799
if (drizzleclient_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS", false))
2722
2801
if (!ignore_errors)
2752
2831
check_io(md_result_file);
2754
drizzleclient_free_result(slave);
2833
drizzle_result_free(&slave);
2759
static int do_start_slave_sql(DRIZZLE *drizzle_con)
2838
static int do_start_slave_sql(drizzle_con_st *drizzle_con)
2840
drizzle_result_st slave;
2762
2841
/* We need to check if the slave sql is stopped in the first place */
2763
if (drizzleclient_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS"))
2842
if (drizzleclient_query_with_error_report(drizzle_con, &slave, "SHOW SLAVE STATUS", false))
2767
DRIZZLE_ROW row= drizzleclient_fetch_row(slave);
2846
drizzle_row_t row= drizzle_row_next(&slave);
2768
2847
if (row && row[11])
2770
2849
/* if SLAVE SQL is not running, we don't start it */
2771
2850
if (!strcmp(row[11],"Yes"))
2773
drizzleclient_free_result(slave);
2852
drizzle_result_free(&slave);
2774
2853
/* Silently assume that they don't have the slave running */
2857
drizzle_result_free(&slave);
2779
drizzleclient_free_result(slave);
2781
2860
/* now, start slave if stopped */
2782
if (drizzleclient_query_with_error_report(drizzle_con, 0, "START SLAVE"))
2861
if (drizzleclient_query_with_error_report(drizzle_con, &slave, "START SLAVE", false))
2784
2863
my_printf_error(0, _("Error: Unable to start slave"), MYF(0));
2866
drizzle_result_free(&slave);
2792
static int do_flush_tables_read_lock(DRIZZLE *drizzle_con)
2872
static int do_flush_tables_read_lock(drizzle_con_st *drizzle_con)
2795
2875
We do first a FLUSH TABLES. If a long update is running, the FLUSH TABLES
2800
2880
update starts between the two FLUSHes, we have that bad stall.
2803
( drizzleclient_query_with_error_report(drizzle_con, 0, "FLUSH TABLES") ||
2883
( drizzleclient_query_with_error_report(drizzle_con, 0, "FLUSH TABLES", false) ||
2804
2884
drizzleclient_query_with_error_report(drizzle_con, 0,
2805
"FLUSH TABLES WITH READ LOCK") );
2885
"FLUSH TABLES WITH READ LOCK", false) );
2809
static int do_unlock_tables(DRIZZLE *drizzle_con)
2889
static int do_unlock_tables(drizzle_con_st *drizzle_con)
2811
return drizzleclient_query_with_error_report(drizzle_con, 0, "UNLOCK TABLES");
2891
return drizzleclient_query_with_error_report(drizzle_con, 0, "UNLOCK TABLES", false);
2814
static int get_bin_log_name(DRIZZLE *drizzle_con,
2894
static int get_bin_log_name(drizzle_con_st *drizzle_con,
2815
2895
char* buff_log_name, uint32_t buff_len)
2897
drizzle_result_st res;
2820
if (drizzleclient_query(drizzle_con, "SHOW MASTER STATUS") ||
2821
!(res= drizzleclient_store_result(drizzle)))
2900
if (drizzleclient_query_with_error_report(drizzle_con, &res, "SHOW MASTER STATUS", false))
2824
if (!(row= drizzleclient_fetch_row(res)))
2903
if (!(row= drizzle_row_next(&res)))
2826
drizzleclient_free_result(res);
2905
drizzle_result_free(&res);
2833
2912
strncpy(buff_log_name, row[0], buff_len - 1);
2835
drizzleclient_free_result(res);
2914
drizzle_result_free(&res);
2839
static int purge_bin_logs_to(DRIZZLE *drizzle_con, char* log_name)
2918
static int purge_bin_logs_to(drizzle_con_st *drizzle_con, char* log_name)
2842
2921
string str= "PURGE BINARY LOGS TO '";
2843
2922
str.append(log_name);
2844
2923
str.append("'");
2845
err = drizzleclient_query_with_error_report(drizzle_con, 0, str.c_str());
2924
drizzle_result_st res;
2925
err= drizzleclient_query_with_error_report(drizzle_con, &res, str.c_str(),
2929
drizzle_result_free(&res);
2850
static int start_transaction(DRIZZLE *drizzle_con)
2934
static int start_transaction(drizzle_con_st *drizzle_con)
2852
2936
return (drizzleclient_query_with_error_report(drizzle_con, 0,
2853
2937
"SET SESSION TRANSACTION ISOLATION "
2854
"LEVEL REPEATABLE READ") ||
2938
"LEVEL REPEATABLE READ", false) ||
2855
2939
drizzleclient_query_with_error_report(drizzle_con, 0,
2856
2940
"START TRANSACTION "
2857
"WITH CONSISTENT SNAPSHOT"));
2941
"WITH CONSISTENT SNAPSHOT", false));
2902
2986
/* Print a value with a prefix on file */
2903
static void print_value(FILE *file, DRIZZLE_RES *result, DRIZZLE_ROW row,
2987
static void print_value(FILE *file, drizzle_result_st *result, drizzle_row_t row,
2904
2988
const char *prefix, const char *name,
2905
2989
int string_value)
2907
DRIZZLE_FIELD *field;
2908
drizzleclient_field_seek(result, 0);
2991
drizzle_column_st *column;
2992
drizzle_column_seek(result, 0);
2910
for ( ; (field= drizzleclient_fetch_field(result)) ; row++)
2994
for ( ; (column= drizzle_column_next(result)) ; row++)
2912
if (!strcmp(field->name,name))
2996
if (!strcmp(drizzle_column_name(column),name))
2914
2998
if (row[0] && row[0][0] && strcmp(row[0],"0")) /* Skip default */
2932
3016
* Returns const char* of the data in that row or NULL if not found
2935
static const char* fetch_named_row(DRIZZLE_RES *result, DRIZZLE_ROW row, const char *name)
3019
static const char* fetch_named_row(drizzle_result_st *result, drizzle_row_t row, const char *name)
2937
DRIZZLE_FIELD *field;
2938
drizzleclient_field_seek(result, 0);
2939
for ( ; (field= drizzleclient_fetch_field(result)) ; row++)
3021
drizzle_column_st *column;
3022
drizzle_column_seek(result, 0);
3024
for ( ; (column= drizzle_column_next(result)) ; row++)
2941
if (!strcmp(field->name,name))
3026
if (!strcmp(drizzle_column_name(column),name))
2943
3028
if (row[0] && row[0][0] && strcmp(row[0],"0")) /* Skip default */
2945
drizzleclient_field_seek(result, 0);
3030
drizzle_column_seek(result, 0);
2950
drizzleclient_field_seek(result, 0);
3035
drizzle_column_seek(result, 0);
2981
3066
char result= IGNORE_NONE;
2982
3067
char buff[FN_REFLEN+80], show_name_buff[FN_REFLEN];
2983
3068
const char *number_of_rows= NULL;
2984
DRIZZLE_RES *res= NULL;
3069
drizzle_result_st res;
2987
3072
/* Check memory for quote_for_like() */
2988
3073
assert(2*sizeof(table_name) < sizeof(show_name_buff));
2989
3074
snprintf(buff, sizeof(buff), "show table status like %s",
2990
3075
quote_for_like(table_name, show_name_buff));
2991
if (drizzleclient_query_with_error_report(drizzle, &res, buff))
3076
if (drizzleclient_query_with_error_report(&dcon, &res, buff, false))
2993
if (drizzleclient_errno(drizzle) != ER_PARSE_ERROR)
2994
{ /* If old DRIZZLE version */
2995
verbose_msg(_("-- Warning: Couldn't get status information for "
2996
"table %s (%s)\n"), table_name, drizzleclient_error(drizzle));
2997
return(result); /* assume table is ok */
3000
if (!(row= drizzleclient_fetch_row(res)))
3080
if (!(row= drizzle_row_next(&res)))
3002
3082
fprintf(stderr,
3003
_("Error: Couldn't read status information for table %s (%s)\n"),
3004
table_name, drizzleclient_error(drizzle));
3005
drizzleclient_free_result(res);
3083
_("Error: Couldn't read status information for table %s\n"),
3085
drizzle_result_free(&res);
3006
3086
return(result); /* assume table is ok */
3010
if ((number_of_rows= fetch_named_row(res, row, "Rows")) != NULL)
3090
if ((number_of_rows= fetch_named_row(&res, row, "Rows")) != NULL)
3012
3092
total_rows= strtoul(number_of_rows, NULL, 10);
3055
3135
static char *primary_key_fields(const char *table_name)
3057
DRIZZLE_RES *res= NULL;
3137
drizzle_result_st res;
3138
drizzle_return_t ret;
3059
3140
/* SHOW KEYS FROM + table name * 2 (escaped) + 2 quotes + \0 */
3060
char show_keys_buff[15 + NAME_LEN * 2 + 3];
3141
char show_keys_buff[15 + DRIZZLE_MAX_TABLE_SIZE * 2 + 3];
3061
3142
uint32_t result_length= 0;
3062
3143
char *result= 0;
3063
char buff[NAME_LEN * 2 + 3];
3144
char buff[DRIZZLE_MAX_TABLE_SIZE * 2 + 3];
3064
3145
char *quoted_field;
3066
3147
snprintf(show_keys_buff, sizeof(show_keys_buff),
3067
3148
"SHOW KEYS FROM %s", table_name);
3068
if (drizzleclient_query(drizzle, show_keys_buff) ||
3069
!(res= drizzleclient_store_result(drizzle)))
3149
if (drizzle_query_str(&dcon, &res, show_keys_buff, &ret) == NULL ||
3150
ret != DRIZZLE_RETURN_OK)
3152
if (ret == DRIZZLE_RETURN_ERROR_CODE)
3154
fprintf(stderr, _("Warning: Couldn't read keys from table %s;"
3155
" records are NOT sorted (%s)\n"),
3156
table_name, drizzle_result_error(&res));
3157
drizzle_result_free(&res);
3161
fprintf(stderr, _("Warning: Couldn't read keys from table %s;"
3162
" records are NOT sorted (%s)\n"),
3163
table_name, drizzle_con_error(&dcon));
3169
if (drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
3071
3171
fprintf(stderr, _("Warning: Couldn't read keys from table %s;"
3072
3172
" records are NOT sorted (%s)\n"),
3073
table_name, drizzleclient_error(drizzle));
3074
/* Don't exit, because it's better to print out unsorted records */
3173
table_name, drizzle_con_error(&dcon));
3081
3180
* row, and UNIQUE keys come before others. So we only need to check
3082
3181
* the first key, not all keys.
3084
if ((row= drizzleclient_fetch_row(res)) && atoi(row[1]) == 0)
3183
if ((row= drizzle_row_next(&res)) && atoi(row[1]) == 0)
3086
3185
/* Key is unique */
3089
3188
quoted_field= quote_name(row[4], buff, 0);
3090
3189
result_length+= strlen(quoted_field) + 1; /* + 1 for ',' or \0 */
3091
} while ((row= drizzleclient_fetch_row(res)) && atoi(row[3]) > 1);
3190
} while ((row= drizzle_row_next(&res)) && atoi(row[3]) > 1);
3094
3193
/* Build the ORDER BY clause result */
3102
3201
fprintf(stderr, _("Error: Not enough memory to store ORDER BY clause\n"));
3202
drizzle_result_free(&res);
3105
drizzleclient_data_seek(res, 0);
3106
row= drizzleclient_fetch_row(res);
3205
drizzle_row_seek(&res, 0);
3206
row= drizzle_row_next(&res);
3107
3207
quoted_field= quote_name(row[4], buff, 0);
3108
3208
end= strcpy(result, quoted_field) + strlen(quoted_field);
3109
while ((row= drizzleclient_fetch_row(res)) && atoi(row[3]) > 1)
3209
while ((row= drizzle_row_next(&res)) && atoi(row[3]) > 1)
3111
3211
quoted_field= quote_name(row[4], buff, 0);
3112
3212
end+= sprintf(end,",%s",quoted_field);
3118
drizzleclient_free_result(res);
3216
drizzle_result_free(&res);
3147
3245
write_header(md_result_file, *argv);
3149
if (opt_slave_data && do_stop_slave_sql(drizzle))
3247
if (opt_slave_data && do_stop_slave_sql(&dcon))
3152
3250
if ((opt_lock_all_tables || opt_master_data) &&
3153
do_flush_tables_read_lock(drizzle))
3251
do_flush_tables_read_lock(&dcon))
3155
if (opt_single_transaction && start_transaction(drizzle))
3253
if (opt_single_transaction && start_transaction(&dcon))
3157
3255
if (opt_delete_master_logs)
3159
if (drizzleclient_real_query(drizzle, "FLUSH LOGS", strlen("FLUSH LOGS")) ||
3160
get_bin_log_name(drizzle, bin_log_name, sizeof(bin_log_name)))
3257
if (drizzleclient_query_with_error_report(&dcon, &result, "FLUSH LOGS", false))
3259
drizzle_result_free(&result);
3260
if (get_bin_log_name(&dcon, bin_log_name, sizeof(bin_log_name)))
3164
3264
if (opt_lock_all_tables || opt_master_data)
3166
if (drizzleclient_real_query(drizzle, "FLUSH LOGS", strlen("FLUSH LOGS")))
3266
if (drizzleclient_query_with_error_report(&dcon, &result, "FLUSH LOGS", false))
3268
drizzle_result_free(&result);
3168
3269
flush_logs= 0; /* not anymore; that would not be sensible */
3170
3271
/* Add 'STOP SLAVE to beginning of dump */
3171
3272
if (opt_slave_apply && add_stop_slave())
3173
if (opt_master_data && do_show_master_status(drizzle))
3175
if (opt_slave_data && do_show_slave_status(drizzle))
3177
if (opt_single_transaction && do_unlock_tables(drizzle)) /* unlock but no commit! */
3274
if (opt_master_data && do_show_master_status(&dcon))
3276
if (opt_slave_data && do_show_slave_status(&dcon))
3278
if (opt_single_transaction && do_unlock_tables(&dcon)) /* unlock but no commit! */
3180
3281
if (opt_alldbs)