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;
47
extern boost::unordered_set<std::string> ignore_table;
48
extern void maybe_exit(int error);
56
extern int opt_destination;
58
/* returns true on keep, false on ignore */
59
bool DrizzleDumpDatabase::ignoreTable(std::string tableName)
61
std::string dbTable(databaseName);
63
dbTable.append(tableName);
65
boost::unordered_set<std::string>::iterator iter= ignore_table.find(dbTable);
66
return (iter == ignore_table.end());
69
void DrizzleDumpDatabase::cleanTableName(std::string &tableName)
71
std::string replace("``");
72
std::string find("`");
74
for (;(j = tableName.find(find, j)) != std::string::npos;)
76
tableName.replace(j, find.length(), replace);
82
std::ostream& operator <<(std::ostream &os, const DrizzleDumpForeignKey &obj)
84
os << " CONSTRAINT `" << obj.constraintName << "` FOREIGN KEY ("
85
<< obj.parentColumns << ") REFERENCES `" << obj.childTable << "` ("
86
<< obj.childColumns << ")";
88
if (not obj.deleteRule.empty())
89
os << " ON DELETE " << obj.deleteRule;
91
if (not obj.updateRule.empty())
92
os << " ON UPDATE " << obj.updateRule;
97
std::ostream& operator <<(std::ostream &os, const DrizzleDumpIndex &obj)
101
os << " PRIMARY KEY ";
103
else if (obj.isUnique)
105
os << " UNIQUE KEY `" << obj.indexName << "` ";
109
os << " KEY `" << obj.indexName << "` ";
114
std::vector<std::string>::iterator i;
115
std::vector<std::string> fields = obj.columns;
116
for (i= fields.begin(); i != fields.end(); ++i)
118
if (i != fields.begin())
120
std::string field= *i;
121
os << "`" << field << "`";
123
os << "(" << obj.length << ")";
131
std::ostream& operator <<(std::ostream &os, const DrizzleDumpField &obj)
133
os << " `" << obj.fieldName << "` ";
135
if (((obj.type.compare("VARCHAR") == 0) or
136
(obj.type.compare("VARBINARY") == 0)) and
139
os << "(" << obj.length << ")";
141
else if (((obj.type.compare("DECIMAL") == 0) or
142
(obj.type.compare("DOUBLE") == 0)) and
143
((obj.decimalPrecision + obj.decimalScale) > 0))
145
os << "(" << obj.decimalPrecision << "," << obj.decimalScale << ")";
147
else if (obj.type.compare("ENUM") == 0)
149
os << "(" << obj.enumValues << ")";
157
if ((not obj.collation.empty()) and (obj.collation.compare("binary") != 0))
159
os << " COLLATE " << obj.collation;
162
if (obj.isAutoIncrement)
163
os << " AUTO_INCREMENT";
165
if (not obj.defaultValue.empty())
167
if (obj.defaultValue.compare("CURRENT_TIMESTAMP") != 0)
168
os << " DEFAULT '" << obj.defaultValue << "'";
170
os << " DEFAULT CURRENT_TIMESTAMP";
172
else if ((obj.defaultIsNull))
174
os << " DEFAULT NULL";
180
std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj)
182
if ((opt_destination == DESTINATION_DB) or opt_databases or opt_alldbs)
186
std::cerr << "--" << std::endl
187
<< "-- Current Database: `" << obj.databaseName << "`" << std::endl
188
<< "--" << std::endl << std::endl;
191
/* Love that this variable is the opposite of its name */
192
if (not opt_create_db)
194
if (opt_drop_database)
195
os << "DROP DATABASE IF EXISTS `" << obj.databaseName << "`" << std::endl;
197
os << "CREATE DATABASE IF NOT EXISTS `" << obj.databaseName << "`";
198
if (not obj.collate.empty())
199
os << " COLLATE = " << obj.collate;
201
os << ";" << std::endl << std::endl;
204
os << "USE `" << obj.databaseName << "`;" << std::endl << std::endl;
207
std::vector<DrizzleDumpTable*>::iterator i;
208
std::vector<DrizzleDumpTable*> output_tables = obj.tables;
209
for (i= output_tables.begin(); i != output_tables.end(); ++i)
211
DrizzleDumpTable *table= *i;
212
if (not opt_no_create_info)
216
obj.dcon->setDB(obj.databaseName);
217
DrizzleDumpData *data= table->getData();
220
std::cerr << "Error: Could not get data for table " << table->displayName << std::endl;
221
if (not ignore_errors)
222
maybe_exit(EX_DRIZZLEERR);
235
std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj)
237
bool new_insert= true;
240
size_t byte_counter= 0;
245
std::cerr << _("-- Retrieving data for ") << obj.table->displayName << "..." << std::endl;
247
if (drizzle_result_row_count(obj.result) < 1)
251
std::cerr << "--" << std::endl
252
<< "-- No data to dump for table `" << obj.table->displayName << "`"
253
<< std::endl << "--" << std::endl << std::endl;
259
std::cerr << "--" << std::endl
260
<< "-- Dumping data for table `" << obj.table->displayName << "`"
261
<< std::endl << "--" << std::endl << std::endl;
263
if (opt_disable_keys)
264
os << "ALTER TABLE `" << obj.table->displayName << "` DISABLE KEYS;" << std::endl;
266
/* Another option that does the opposite of its name, makes me sad :( */
268
os << "START TRANSACTION;" << std::endl;
270
std::streampos out_position= os.tellp();
272
while((row= drizzle_row_next(obj.result)))
275
if (verbose and (rownr % show_progress_size) == 0)
277
std::cerr << "-- " << rownr << _(" rows dumped for table ") << obj.table->displayName << std::endl;
280
size_t* row_sizes= drizzle_row_field_sizes(obj.result);
281
for (uint32_t i= 0; i < drizzle_result_column_count(obj.result); i++)
282
byte_counter+= row_sizes[i];
284
if (not first and not new_insert)
289
os << ");" << std::endl;
297
if (opt_replace_into)
305
os << "INTO `" << obj.table->displayName << "` VALUES (";
306
byte_counter+= 28 + obj.table->displayName.length();
310
for (uint32_t i= 0; i < drizzle_result_column_count(obj.result); i++)
316
/* time/date conversion for MySQL connections */
317
else if (obj.table->fields[i]->convertDateTime)
319
os << obj.checkDateTime(row[i], i);
323
if (obj.table->fields[i]->type.compare("INT") != 0)
325
/* Hex blob processing or escape text */
326
if (((obj.table->fields[i]->type.compare("BLOB") == 0) or
327
(obj.table->fields[i]->type.compare("VARBINARY") == 0)))
329
os << obj.convertHex((unsigned char*)row[i], row_sizes[i]);
330
byte_counter+= row_sizes[i];
333
os << "'" << DrizzleDumpData::escape(row[i], row_sizes[i]) << "'";
339
if (i != obj.table->fields.size() - 1)
342
/* Break insert up if it is too long */
343
if (extended_insert and
344
(byte_counter >= DRIZZLE_MAX_LINE_LENGTH))
346
os << ");" << std::endl;
351
os << ");" << std::endl;
354
os << "COMMIT;" << std::endl;
356
if (opt_disable_keys)
357
os << "ALTER TABLE `" << obj.table->tableName << "` ENABLE KEYS;" << std::endl;
364
std::string DrizzleDumpData::convertHex(const unsigned char* from, size_t from_size) const
366
std::ostringstream output;
369
while (from_size > 0)
371
/* Would be nice if std::hex liked uint8_t, ah well */
372
output << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << (unsigned short)(*from);
380
/* Ripped out of libdrizzle, hopefully a little safer */
381
std::string DrizzleDumpData::escape(const char* from, size_t from_size)
385
while (from_size > 0)
392
output.append("\\0");
395
output.append("\\n");
398
output.append("\\r");
401
output.append("\\\\");
404
output.append("\\'");
407
output.append("\\\"");
410
output.append("\\Z");
413
output.push_back(*from);
418
output.push_back(*from);
426
std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj)
430
std::cerr << "--" << std::endl
431
<< "-- Table structure for table `" << obj.displayName << "`" << std::endl
432
<< "--" << std::endl << std::endl;
436
os << "DROP TABLE IF EXISTS `" << obj.displayName << "`;" << std::endl;
438
os << "CREATE TABLE `" << obj.displayName << "` (" << std::endl;
439
std::vector<DrizzleDumpField*>::iterator i;
440
std::vector<DrizzleDumpField*> output_fields = obj.fields;
441
for (i= output_fields.begin(); i != output_fields.end(); ++i)
443
if (i != output_fields.begin())
444
os << "," << std::endl;
445
DrizzleDumpField *field= *i;
449
std::vector<DrizzleDumpIndex*>::iterator j;
450
std::vector<DrizzleDumpIndex*> output_indexes = obj.indexes;
451
for (j= output_indexes.begin(); j != output_indexes.end(); ++j)
453
os << "," << std::endl;
454
DrizzleDumpIndex *index= *j;
458
std::vector<DrizzleDumpForeignKey*>::iterator k;
459
std::vector<DrizzleDumpForeignKey*> output_fkeys = obj.fkeys;
460
for (k= output_fkeys.begin(); k != output_fkeys.end(); ++k)
462
os << "," << std::endl;
463
DrizzleDumpForeignKey *fkey= *k;
468
os << ") ENGINE=" << obj.engineName << " ";
469
if (obj.autoIncrement > 0)
471
os << "AUTO_INCREMENT=" << obj.autoIncrement << " ";
474
os << "COLLATE = " << obj.collate;
476
if (not obj.comment.empty())
478
os << " COMMENT = '" << obj.comment << "'";
481
os << ";" << std::endl << std::endl;
486
DrizzleDumpConnection::DrizzleDumpConnection(std::string &host, uint16_t port,
487
std::string &username, std::string &password, bool drizzle_protocol) :
489
drizzleProtocol(drizzle_protocol)
491
drizzle_return_t ret;
496
std::string protocol= (drizzle_protocol) ? "Drizzle" : "MySQL";
499
std::cerr << _("-- Connecting to ") << host << _(" using protocol ")
500
<< protocol << "..." << std::endl;
502
drizzle_create(&drizzle);
503
drizzle_con_create(&drizzle, &connection);
504
drizzle_con_set_tcp(&connection, (char *)host.c_str(), port);
505
drizzle_con_set_auth(&connection, (char *)username.c_str(),
506
(char *)password.c_str());
507
drizzle_con_add_options(&connection,
508
drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
509
ret= drizzle_con_connect(&connection);
510
if (ret != DRIZZLE_RETURN_OK)
512
errorHandler(NULL, ret, "when trying to connect");
516
boost::match_flag_type flags = boost::match_default;
518
boost::regex mysql_regex("(5\\.[0-9]+\\.[0-9]+)");
519
boost::regex drizzle_regex("(20[0-9]{2}\\.(0[1-9]|1[012])\\.[0-9]+)");
521
std::string version(getServerVersion());
523
if (regex_search(version, mysql_regex, flags))
524
serverType= SERVER_MYSQL_FOUND;
525
else if (regex_search(version, drizzle_regex, flags))
526
serverType= SERVER_DRIZZLE_FOUND;
528
serverType= SERVER_UNKNOWN_FOUND;
531
drizzle_result_st* DrizzleDumpConnection::query(std::string &str_query)
533
drizzle_return_t ret;
534
drizzle_result_st* result= new drizzle_result_st;
535
if (drizzle_query_str(&connection, result, str_query.c_str(), &ret) == NULL ||
536
ret != DRIZZLE_RETURN_OK)
538
if (ret == DRIZZLE_RETURN_ERROR_CODE)
540
std::cerr << _("Error executing query: ") <<
541
drizzle_result_error(result) << std::endl;
542
drizzle_result_free(result);
546
std::cerr << _("Error executing query: ") <<
547
drizzle_con_error(&connection) << std::endl;
552
if (drizzle_result_buffer(result) != DRIZZLE_RETURN_OK)
554
std::cerr << _("Could not buffer result: ") <<
555
drizzle_con_error(&connection) << std::endl;
561
void DrizzleDumpConnection::freeResult(drizzle_result_st* result)
563
drizzle_result_free(result);
567
bool DrizzleDumpConnection::queryNoResult(std::string &str_query)
569
drizzle_return_t ret;
570
drizzle_result_st result;
572
if (drizzle_query_str(&connection, &result, str_query.c_str(), &ret) == NULL ||
573
ret != DRIZZLE_RETURN_OK)
575
if (ret == DRIZZLE_RETURN_ERROR_CODE)
577
std::cerr << _("Error executing query: ") <<
578
drizzle_result_error(&result) << std::endl;
579
drizzle_result_free(&result);
583
std::cerr << _("Error executing query: ") <<
584
drizzle_con_error(&connection) << std::endl;
589
drizzle_result_free(&result);
593
bool DrizzleDumpConnection::setDB(std::string databaseName)
595
drizzle_return_t ret;
596
drizzle_result_st result;
597
if (drizzle_select_db(&connection, &result, databaseName.c_str(), &ret) ==
598
NULL || ret != DRIZZLE_RETURN_OK)
600
std::cerr << _("Error: Could not set db '") << databaseName << "'" << std::endl;
601
if (ret == DRIZZLE_RETURN_ERROR_CODE)
602
drizzle_result_free(&result);
605
drizzle_result_free(&result);
609
void DrizzleDumpConnection::errorHandler(drizzle_result_st *res,
610
drizzle_return_t ret, const char *when)
614
std::cerr << _("Got error: ") << drizzle_con_error(&connection) << " "
615
<< when << std::endl;
617
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
619
std::cerr << _("Got error: ") << drizzle_result_error(res)
620
<< " (" << drizzle_result_error_code(res) << ") " << when << std::endl;
621
drizzle_result_free(res);
625
std::cerr << _("Got error: ") << ret << " " << when << std::endl;
631
DrizzleDumpConnection::~DrizzleDumpConnection()
634
std::cerr << _("-- Disconnecting from ") << hostName << "..." << std::endl;
635
drizzle_con_free(&connection);
636
drizzle_free(&drizzle);