1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Andrew Hutchings
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include "drizzledump_data.h"
21
#include "client_priv.h"
22
#include <drizzled/gettext.h>
25
#include <boost/regex.hpp>
26
#include <boost/unordered_set.hpp>
28
#define EX_DRIZZLEERR 2
30
extern bool opt_no_create_info;
31
extern bool opt_no_data;
32
extern bool opt_create_db;
33
extern bool opt_disable_keys;
34
extern bool extended_insert;
35
extern bool opt_replace_into;
38
extern bool opt_databases;
39
extern bool opt_alldbs;
40
extern uint32_t show_progress_size;
41
extern bool opt_ignore;
42
extern bool opt_compress;
43
extern bool opt_drop_database;
44
extern bool opt_autocommit;
45
extern bool ignore_errors;
46
extern std::string opt_destination_database;
48
extern boost::unordered_set<std::string> ignore_table;
49
extern void maybe_exit(int error);
57
extern int opt_destination;
59
/* returns true on keep, false on ignore */
60
bool DrizzleDumpDatabase::ignoreTable(std::string tableName)
62
std::string dbTable(databaseName);
64
dbTable.append(tableName);
66
boost::unordered_set<std::string>::iterator iter= ignore_table.find(dbTable);
67
return (iter == ignore_table.end());
70
void DrizzleDumpDatabase::cleanTableName(std::string &tableName)
72
std::string replace("``");
73
std::string find("`");
75
for (;(j = tableName.find(find, j)) != std::string::npos;)
77
tableName.replace(j, find.length(), replace);
83
std::ostream& operator <<(std::ostream &os, const DrizzleDumpForeignKey &obj)
85
os << " CONSTRAINT `" << obj.constraintName << "` FOREIGN KEY ("
86
<< obj.parentColumns << ") REFERENCES `" << obj.childTable << "` ("
87
<< obj.childColumns << ")";
89
if (not obj.deleteRule.empty())
90
os << " ON DELETE " << obj.deleteRule;
92
if (not obj.updateRule.empty())
93
os << " ON UPDATE " << obj.updateRule;
98
std::ostream& operator <<(std::ostream &os, const DrizzleDumpIndex &obj)
102
os << " PRIMARY KEY ";
104
else if (obj.isUnique)
106
os << " UNIQUE KEY `" << obj.indexName << "` ";
110
os << " KEY `" << obj.indexName << "` ";
115
std::vector<std::string>::iterator i;
116
std::vector<std::string> fields = obj.columns;
117
for (i= fields.begin(); i != fields.end(); ++i)
119
if (i != fields.begin())
121
std::string field= *i;
122
os << "`" << field << "`";
124
os << "(" << obj.length << ")";
132
std::ostream& operator <<(std::ostream &os, const DrizzleDumpField &obj)
134
os << " `" << obj.fieldName << "` ";
136
if (((obj.type.compare("VARCHAR") == 0) or
137
(obj.type.compare("VARBINARY") == 0)) and
140
os << "(" << obj.length << ")";
142
else if (((obj.type.compare("DECIMAL") == 0) or
143
(obj.type.compare("DOUBLE") == 0)) and
144
((obj.decimalPrecision + obj.decimalScale) > 0))
146
os << "(" << obj.decimalPrecision << "," << obj.decimalScale << ")";
148
else if (obj.type.compare("ENUM") == 0)
150
os << "(" << obj.enumValues << ")";
158
if ((not obj.collation.empty()) and (obj.collation.compare("binary") != 0))
160
os << " COLLATE " << obj.collation;
163
if (obj.isAutoIncrement)
164
os << " AUTO_INCREMENT";
166
if (not obj.defaultValue.empty())
168
if (obj.defaultValue.compare("CURRENT_TIMESTAMP") != 0)
170
if (obj.defaultValue.compare(0, 2, "b'") == 0)
172
os << " DEFAULT " << obj.defaultValue;
176
os << " DEFAULT '" << obj.defaultValue << "'";
181
os << " DEFAULT CURRENT_TIMESTAMP";
184
else if ((obj.defaultIsNull))
186
os << " DEFAULT NULL";
189
if (not obj.comment.empty())
191
os << " COMMENT '" << DrizzleDumpData::escape(obj.comment.c_str(), obj.comment.length()) << "'";
197
std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj)
199
if ((opt_destination == DESTINATION_DB) or opt_databases or opt_alldbs)
203
std::cerr << "--" << std::endl
204
<< "-- Current Database: `" << obj.databaseName << "`" << std::endl
205
<< "--" << std::endl << std::endl;
208
/* Love that this variable is the opposite of its name */
209
if (not opt_create_db)
211
if (opt_drop_database)
213
os << "DROP DATABASE IF EXISTS `"
214
<< ((opt_destination_database.empty()) ? obj.databaseName
215
: opt_destination_database) << "`" << std::endl;
218
os << "CREATE DATABASE IF NOT EXISTS `"
219
<< ((opt_destination_database.empty()) ? obj.databaseName
220
: opt_destination_database) << "`";
221
if (not obj.collate.empty())
222
os << " COLLATE = " << obj.collate;
224
os << ";" << std::endl << std::endl;
226
os << "USE `" << ((opt_destination_database.empty()) ? obj.databaseName
227
: opt_destination_database) << "`;" << std::endl << std::endl;
230
std::vector<DrizzleDumpTable*>::iterator i;
231
std::vector<DrizzleDumpTable*> output_tables = obj.tables;
232
for (i= output_tables.begin(); i != output_tables.end(); ++i)
234
DrizzleDumpTable *table= *i;
235
if (not opt_no_create_info)
239
obj.dcon->setDB(obj.databaseName);
240
DrizzleDumpData *data= table->getData();
243
std::cerr << "Error: Could not get data for table " << table->displayName << std::endl;
244
if (not ignore_errors)
245
maybe_exit(EX_DRIZZLEERR);
258
std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj)
260
bool new_insert= true;
263
size_t byte_counter= 0;
268
std::cerr << _("-- Retrieving data for ") << obj.table->displayName << "..." << std::endl;
270
if (drizzle_result_row_count(obj.result) < 1)
274
std::cerr << "--" << std::endl
275
<< "-- No data to dump for table `" << obj.table->displayName << "`"
276
<< std::endl << "--" << std::endl << std::endl;
282
std::cerr << "--" << std::endl
283
<< "-- Dumping data for table `" << obj.table->displayName << "`"
284
<< std::endl << "--" << std::endl << std::endl;
286
if (opt_disable_keys)
287
os << "ALTER TABLE `" << obj.table->displayName << "` DISABLE KEYS;" << std::endl;
289
/* Another option that does the opposite of its name, makes me sad :( */
291
os << "START TRANSACTION;" << std::endl;
293
std::streampos out_position= os.tellp();
295
while((row= drizzle_row_next(obj.result)))
298
if (verbose and (rownr % show_progress_size) == 0)
300
std::cerr << "-- " << rownr << _(" rows dumped for table ") << obj.table->displayName << std::endl;
303
size_t* row_sizes= drizzle_row_field_sizes(obj.result);
304
for (uint32_t i= 0; i < drizzle_result_column_count(obj.result); i++)
305
byte_counter+= row_sizes[i];
307
if (not first and not new_insert)
312
os << ");" << std::endl;
320
if (opt_replace_into)
328
os << "INTO `" << obj.table->displayName << "` VALUES (";
329
byte_counter+= 28 + obj.table->displayName.length();
333
for (uint32_t i= 0; i < drizzle_result_column_count(obj.result); i++)
338
if (i != obj.table->fields.size() - 1)
343
if ((obj.table->fields[i]->rangeCheck) and
344
(obj.table->fields[i]->type.compare("BIGINT") == 0) and
345
(boost::lexical_cast<uint64_t>(row[i]) > INT64_MAX))
347
std::cerr << "Error: Data for column " << obj.table->fields[i]->fieldName << " is greater than max BIGINT, cannot migrate automatically" << std::endl;
348
if (not ignore_errors)
349
maybe_exit(EX_DRIZZLEERR);
354
/* time/date conversion for MySQL connections */
355
else if (obj.table->fields[i]->convertDateTime)
357
os << obj.checkDateTime(row[i], i);
361
if ((obj.table->fields[i]->type.compare("INT") != 0) and
362
(obj.table->fields[i]->type.compare("BIGINT") != 0))
364
/* Hex blob processing or escape text */
365
if (((obj.table->fields[i]->type.compare("BLOB") == 0) or
366
(obj.table->fields[i]->type.compare("VARBINARY") == 0)))
368
os << obj.convertHex((unsigned char*)row[i], row_sizes[i]);
369
byte_counter+= row_sizes[i];
371
else if ((obj.table->fields[i]->type.compare("ENUM") == 0) and
372
(strcmp(row[i], "") == 0))
376
else if (obj.table->fields[i]->type.compare("BOOLEAN") == 0)
378
if (strncmp(row[i], "1", 1) == 0)
384
os << "'" << DrizzleDumpData::escape(row[i], row_sizes[i]) << "'";
390
if (i != obj.table->fields.size() - 1)
393
/* Break insert up if it is too long */
394
if ((extended_insert and
395
(byte_counter >= DRIZZLE_MAX_LINE_LENGTH)) or (not extended_insert))
397
os << ");" << std::endl;
403
os << ");" << std::endl;
406
os << "COMMIT;" << std::endl;
408
if (opt_disable_keys)
409
os << "ALTER TABLE `" << obj.table->tableName << "` ENABLE KEYS;" << std::endl;
416
std::string DrizzleDumpData::convertHex(const unsigned char* from, size_t from_size) const
418
std::ostringstream output;
424
while (from_size > 0)
426
/* Would be nice if std::hex liked uint8_t, ah well */
427
output << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << (unsigned short)(*from);
435
/* Ripped out of libdrizzle, hopefully a little safer */
436
std::string DrizzleDumpData::escape(const char* from, size_t from_size)
440
while (from_size > 0)
447
output.append("\\0");
450
output.append("\\n");
453
output.append("\\r");
456
output.append("\\\\");
459
output.append("\\'");
462
output.append("\\\"");
465
output.append("\\Z");
468
output.push_back(*from);
473
output.push_back(*from);
481
std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj)
485
std::cerr << "--" << std::endl
486
<< "-- Table structure for table `" << obj.displayName << "`" << std::endl
487
<< "--" << std::endl << std::endl;
491
os << "DROP TABLE IF EXISTS `" << obj.displayName << "`;" << std::endl;
493
os << "CREATE TABLE `" << obj.displayName << "` (" << std::endl;
494
std::vector<DrizzleDumpField*>::iterator i;
495
std::vector<DrizzleDumpField*> output_fields = obj.fields;
496
for (i= output_fields.begin(); i != output_fields.end(); ++i)
498
if (i != output_fields.begin())
499
os << "," << std::endl;
500
DrizzleDumpField *field= *i;
504
std::vector<DrizzleDumpIndex*>::iterator j;
505
std::vector<DrizzleDumpIndex*> output_indexes = obj.indexes;
506
for (j= output_indexes.begin(); j != output_indexes.end(); ++j)
508
os << "," << std::endl;
509
DrizzleDumpIndex *index= *j;
513
std::vector<DrizzleDumpForeignKey*>::iterator k;
514
std::vector<DrizzleDumpForeignKey*> output_fkeys = obj.fkeys;
515
for (k= output_fkeys.begin(); k != output_fkeys.end(); ++k)
517
os << "," << std::endl;
518
DrizzleDumpForeignKey *fkey= *k;
523
os << ") ENGINE='" << obj.engineName << "' ";
524
if (obj.autoIncrement > 0)
526
os << "AUTO_INCREMENT=" << obj.autoIncrement << " ";
529
os << "COLLATE='" << obj.collate << "'";
531
if (not obj.comment.empty())
533
os << " COMMENT='" << obj.comment << "'";
536
os << ";" << std::endl << std::endl;
541
DrizzleDumpConnection::DrizzleDumpConnection(std::string &host, uint16_t port,
542
std::string &username, std::string &password, bool drizzle_protocol) :
544
drizzleProtocol(drizzle_protocol)
546
drizzle_return_t ret;
551
std::string protocol= (drizzle_protocol) ? "Drizzle" : "MySQL";
554
std::cerr << _("-- Connecting to ") << host << _(" using protocol ")
555
<< protocol << "..." << std::endl;
557
drizzle_create(&drizzle);
558
drizzle_con_create(&drizzle, &connection);
559
drizzle_con_set_tcp(&connection, (char *)host.c_str(), port);
560
drizzle_con_set_auth(&connection, (char *)username.c_str(),
561
(char *)password.c_str());
562
drizzle_con_add_options(&connection,
563
drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
564
ret= drizzle_con_connect(&connection);
565
if (ret != DRIZZLE_RETURN_OK)
567
errorHandler(NULL, ret, "when trying to connect");
568
throw std::exception();
571
ServerDetect server_detect= ServerDetect(&connection);
573
serverType= server_detect.getServerType();
574
serverVersion= server_detect.getServerVersion();
577
drizzle_result_st* DrizzleDumpConnection::query(std::string &str_query)
579
drizzle_return_t ret;
580
drizzle_result_st* result= new drizzle_result_st;
581
if (drizzle_query_str(&connection, result, str_query.c_str(), &ret) == NULL ||
582
ret != DRIZZLE_RETURN_OK)
584
if (ret == DRIZZLE_RETURN_ERROR_CODE)
586
std::cerr << _("Error executing query: ") <<
587
drizzle_result_error(result) << std::endl;
588
drizzle_result_free(result);
592
std::cerr << _("Error executing query: ") <<
593
drizzle_con_error(&connection) << std::endl;
598
if (drizzle_result_buffer(result) != DRIZZLE_RETURN_OK)
600
std::cerr << _("Could not buffer result: ") <<
601
drizzle_con_error(&connection) << std::endl;
607
void DrizzleDumpConnection::freeResult(drizzle_result_st* result)
609
drizzle_result_free(result);
613
bool DrizzleDumpConnection::queryNoResult(std::string &str_query)
615
drizzle_return_t ret;
616
drizzle_result_st result;
618
if (drizzle_query_str(&connection, &result, str_query.c_str(), &ret) == NULL ||
619
ret != DRIZZLE_RETURN_OK)
621
if (ret == DRIZZLE_RETURN_ERROR_CODE)
623
std::cerr << _("Error executing query: ") <<
624
drizzle_result_error(&result) << std::endl;
625
drizzle_result_free(&result);
629
std::cerr << _("Error executing query: ") <<
630
drizzle_con_error(&connection) << std::endl;
635
drizzle_result_free(&result);
639
bool DrizzleDumpConnection::setDB(std::string databaseName)
641
drizzle_return_t ret;
642
drizzle_result_st result;
643
if (drizzle_select_db(&connection, &result, databaseName.c_str(), &ret) ==
644
NULL || ret != DRIZZLE_RETURN_OK)
646
std::cerr << _("Error: Could not set db '") << databaseName << "'" << std::endl;
647
if (ret == DRIZZLE_RETURN_ERROR_CODE)
648
drizzle_result_free(&result);
651
drizzle_result_free(&result);
655
void DrizzleDumpConnection::errorHandler(drizzle_result_st *res,
656
drizzle_return_t ret, const char *when)
660
std::cerr << _("Got error: ") << drizzle_con_error(&connection) << " "
661
<< when << std::endl;
663
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
665
std::cerr << _("Got error: ") << drizzle_result_error(res)
666
<< " (" << drizzle_result_error_code(res) << ") " << when << std::endl;
667
drizzle_result_free(res);
671
std::cerr << _("Got error: ") << ret << " " << when << std::endl;
677
DrizzleDumpConnection::~DrizzleDumpConnection()
680
std::cerr << _("-- Disconnecting from ") << hostName << "..." << std::endl;
681
drizzle_con_free(&connection);
682
drizzle_free(&drizzle);