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))
377
os << "'" << DrizzleDumpData::escape(row[i], row_sizes[i]) << "'";
383
if (i != obj.table->fields.size() - 1)
386
/* Break insert up if it is too long */
387
if ((extended_insert and
388
(byte_counter >= DRIZZLE_MAX_LINE_LENGTH)) or (not extended_insert))
390
os << ");" << std::endl;
396
os << ");" << std::endl;
399
os << "COMMIT;" << std::endl;
401
if (opt_disable_keys)
402
os << "ALTER TABLE `" << obj.table->tableName << "` ENABLE KEYS;" << std::endl;
409
std::string DrizzleDumpData::convertHex(const unsigned char* from, size_t from_size) const
411
std::ostringstream output;
417
while (from_size > 0)
419
/* Would be nice if std::hex liked uint8_t, ah well */
420
output << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << (unsigned short)(*from);
428
/* Ripped out of libdrizzle, hopefully a little safer */
429
std::string DrizzleDumpData::escape(const char* from, size_t from_size)
433
while (from_size > 0)
440
output.append("\\0");
443
output.append("\\n");
446
output.append("\\r");
449
output.append("\\\\");
452
output.append("\\'");
455
output.append("\\\"");
458
output.append("\\Z");
461
output.push_back(*from);
466
output.push_back(*from);
474
std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj)
478
std::cerr << "--" << std::endl
479
<< "-- Table structure for table `" << obj.displayName << "`" << std::endl
480
<< "--" << std::endl << std::endl;
484
os << "DROP TABLE IF EXISTS `" << obj.displayName << "`;" << std::endl;
486
os << "CREATE TABLE `" << obj.displayName << "` (" << std::endl;
487
std::vector<DrizzleDumpField*>::iterator i;
488
std::vector<DrizzleDumpField*> output_fields = obj.fields;
489
for (i= output_fields.begin(); i != output_fields.end(); ++i)
491
if (i != output_fields.begin())
492
os << "," << std::endl;
493
DrizzleDumpField *field= *i;
497
std::vector<DrizzleDumpIndex*>::iterator j;
498
std::vector<DrizzleDumpIndex*> output_indexes = obj.indexes;
499
for (j= output_indexes.begin(); j != output_indexes.end(); ++j)
501
os << "," << std::endl;
502
DrizzleDumpIndex *index= *j;
506
std::vector<DrizzleDumpForeignKey*>::iterator k;
507
std::vector<DrizzleDumpForeignKey*> output_fkeys = obj.fkeys;
508
for (k= output_fkeys.begin(); k != output_fkeys.end(); ++k)
510
os << "," << std::endl;
511
DrizzleDumpForeignKey *fkey= *k;
516
os << ") ENGINE='" << obj.engineName << "' ";
517
if (obj.autoIncrement > 0)
519
os << "AUTO_INCREMENT=" << obj.autoIncrement << " ";
522
os << "COLLATE='" << obj.collate << "'";
524
if (not obj.comment.empty())
526
os << " COMMENT='" << obj.comment << "'";
529
os << ";" << std::endl << std::endl;
534
DrizzleDumpConnection::DrizzleDumpConnection(std::string &host, uint16_t port,
535
std::string &username, std::string &password, bool drizzle_protocol) :
537
drizzleProtocol(drizzle_protocol)
539
drizzle_return_t ret;
544
std::string protocol= (drizzle_protocol) ? "Drizzle" : "MySQL";
547
std::cerr << _("-- Connecting to ") << host << _(" using protocol ")
548
<< protocol << "..." << std::endl;
550
drizzle_create(&drizzle);
551
drizzle_con_create(&drizzle, &connection);
552
drizzle_con_set_tcp(&connection, (char *)host.c_str(), port);
553
drizzle_con_set_auth(&connection, (char *)username.c_str(),
554
(char *)password.c_str());
555
drizzle_con_add_options(&connection,
556
drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
557
ret= drizzle_con_connect(&connection);
558
if (ret != DRIZZLE_RETURN_OK)
560
errorHandler(NULL, ret, "when trying to connect");
561
throw std::exception();
564
boost::match_flag_type flags = boost::match_default;
566
boost::regex mysql_regex("(5\\.[0-9]+\\.[0-9]+)");
567
boost::regex drizzle_regex("(20[0-9]{2}\\.(0[1-9]|1[012])\\.[0-9]+)");
569
std::string version(getServerVersion());
571
if (regex_search(version, mysql_regex, flags))
572
serverType= SERVER_MYSQL_FOUND;
573
else if (regex_search(version, drizzle_regex, flags))
574
serverType= SERVER_DRIZZLE_FOUND;
576
serverType= SERVER_UNKNOWN_FOUND;
579
drizzle_result_st* DrizzleDumpConnection::query(std::string &str_query)
581
drizzle_return_t ret;
582
drizzle_result_st* result= new drizzle_result_st;
583
if (drizzle_query_str(&connection, result, str_query.c_str(), &ret) == NULL ||
584
ret != DRIZZLE_RETURN_OK)
586
if (ret == DRIZZLE_RETURN_ERROR_CODE)
588
std::cerr << _("Error executing query: ") <<
589
drizzle_result_error(result) << std::endl;
590
drizzle_result_free(result);
594
std::cerr << _("Error executing query: ") <<
595
drizzle_con_error(&connection) << std::endl;
600
if (drizzle_result_buffer(result) != DRIZZLE_RETURN_OK)
602
std::cerr << _("Could not buffer result: ") <<
603
drizzle_con_error(&connection) << std::endl;
609
void DrizzleDumpConnection::freeResult(drizzle_result_st* result)
611
drizzle_result_free(result);
615
bool DrizzleDumpConnection::queryNoResult(std::string &str_query)
617
drizzle_return_t ret;
618
drizzle_result_st result;
620
if (drizzle_query_str(&connection, &result, str_query.c_str(), &ret) == NULL ||
621
ret != DRIZZLE_RETURN_OK)
623
if (ret == DRIZZLE_RETURN_ERROR_CODE)
625
std::cerr << _("Error executing query: ") <<
626
drizzle_result_error(&result) << std::endl;
627
drizzle_result_free(&result);
631
std::cerr << _("Error executing query: ") <<
632
drizzle_con_error(&connection) << std::endl;
637
drizzle_result_free(&result);
641
bool DrizzleDumpConnection::setDB(std::string databaseName)
643
drizzle_return_t ret;
644
drizzle_result_st result;
645
if (drizzle_select_db(&connection, &result, databaseName.c_str(), &ret) ==
646
NULL || ret != DRIZZLE_RETURN_OK)
648
std::cerr << _("Error: Could not set db '") << databaseName << "'" << std::endl;
649
if (ret == DRIZZLE_RETURN_ERROR_CODE)
650
drizzle_result_free(&result);
653
drizzle_result_free(&result);
657
void DrizzleDumpConnection::errorHandler(drizzle_result_st *res,
658
drizzle_return_t ret, const char *when)
662
std::cerr << _("Got error: ") << drizzle_con_error(&connection) << " "
663
<< when << std::endl;
665
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
667
std::cerr << _("Got error: ") << drizzle_result_error(res)
668
<< " (" << drizzle_result_error_code(res) << ") " << when << std::endl;
669
drizzle_result_free(res);
673
std::cerr << _("Got error: ") << ret << " " << when << std::endl;
679
DrizzleDumpConnection::~DrizzleDumpConnection()
682
std::cerr << _("-- Disconnecting from ") << hostName << "..." << std::endl;
683
drizzle_con_free(&connection);
684
drizzle_free(&drizzle);