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 "drizzledump_mysql.h"
22
#include "client_priv.h"
25
#include <boost/regex.hpp>
26
#include <boost/date_time/posix_time/posix_time.hpp>
27
#include <drizzled/gettext.h>
30
extern bool ignore_errors;
32
bool DrizzleDumpDatabaseMySQL::populateTables()
34
drizzle_result_st *result;
38
if (not dcon->setDB(databaseName))
42
std::cerr << _("-- Retrieving table structures for ") << databaseName << "..." << std::endl;
44
query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE, AUTO_INCREMENT, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE != 'VIEW' AND TABLE_SCHEMA='";
45
query.append(databaseName);
46
query.append("' ORDER BY TABLE_NAME");
48
result= dcon->query(query);
53
while ((row= drizzle_row_next(result)))
55
size_t* row_sizes= drizzle_row_field_sizes(result);
56
std::string tableName(row[0]);
57
std::string displayName(tableName);
58
cleanTableName(displayName);
59
if (not ignoreTable(displayName))
62
DrizzleDumpTableMySQL *table = new DrizzleDumpTableMySQL(tableName, dcon);
63
table->displayName= displayName;
64
table->setCollate(row[1]);
65
table->setEngine(row[2]);
67
table->autoIncrement= boost::lexical_cast<uint64_t>(row[3]);
69
table->autoIncrement= 0;
71
if ((row[4]) and (strstr(row[4], "InnoDB free") == NULL))
72
table->comment= DrizzleDumpData::escape(row[4], row_sizes[4]);
76
table->database= this;
77
if ((not table->populateFields()) or (not table->populateIndexes()) or
78
(not table->populateFkeys()))
81
if (not ignore_errors)
86
tables.push_back(table);
89
dcon->freeResult(result);
94
bool DrizzleDumpDatabaseMySQL::populateTables(const std::vector<std::string> &table_names)
96
drizzle_result_st *result;
100
if (not dcon->setDB(databaseName))
104
std::cerr << _("-- Retrieving table structures for ") << databaseName << "..." << std::endl;
105
for (std::vector<std::string>::const_iterator it= table_names.begin(); it != table_names.end(); ++it)
107
std::string tableName= *it;
108
std::string displayName(tableName);
109
cleanTableName(displayName);
110
if (not ignoreTable(displayName))
113
query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE, AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='";
114
query.append(databaseName);
115
query.append("' AND TABLE_NAME = '");
116
query.append(tableName);
119
result= dcon->query(query);
124
if ((row= drizzle_row_next(result)))
126
DrizzleDumpTableMySQL *table = new DrizzleDumpTableMySQL(tableName, dcon);
127
table->displayName= displayName;
128
table->setCollate(row[1]);
129
table->setEngine(row[2]);
131
table->autoIncrement= boost::lexical_cast<uint64_t>(row[3]);
133
table->autoIncrement= 0;
135
table->database= this;
136
if ((not table->populateFields()) or (not table->populateIndexes()))
139
if (not ignore_errors)
144
tables.push_back(table);
145
dcon->freeResult(result);
149
dcon->freeResult(result);
150
if (not ignore_errors)
161
bool DrizzleDumpTableMySQL::populateFields()
163
drizzle_result_st *result;
168
std::cerr << _("-- Retrieving fields for ") << tableName << "..." << std::endl;
170
query="SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_DEFAULT, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLLATION_NAME, EXTRA FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='";
171
query.append(database->databaseName);
172
query.append("' AND TABLE_NAME='");
173
query.append(tableName);
174
query.append("' ORDER BY ORDINAL_POSITION");
176
result= dcon->query(query);
181
while ((row= drizzle_row_next(result)))
183
std::string fieldName(row[0]);
184
DrizzleDumpFieldMySQL *field = new DrizzleDumpFieldMySQL(fieldName, dcon);
185
/* Stop valgrind warning */
186
field->convertDateTime= false;
187
/* Also sets collation */
188
field->setType(row[1], row[8]);
189
if (field->type.compare("ENUM") == 0)
192
field->isNull= (strcmp(row[3], "YES") == 0) ? true : false;
195
field->defaultValue= row[2];
198
field->defaultValue= "";
200
if (field->convertDateTime)
202
field->dateTimeConvert();
206
field->isAutoIncrement= (strcmp(row[8], "auto_increment") == 0) ? true : false;
207
field->defaultIsNull= field->isNull;
208
field->length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
209
if ((row[5] != NULL) and (row[6] != NULL))
211
field->decimalPrecision= boost::lexical_cast<uint32_t>(row[5]);
212
field->decimalScale= boost::lexical_cast<uint32_t>(row[6]);
216
field->decimalPrecision= 0;
217
field->decimalScale= 0;
220
fields.push_back(field);
223
dcon->freeResult(result);
228
void DrizzleDumpFieldMySQL::dateTimeConvert(void)
230
boost::match_flag_type flags = boost::match_default;
232
if (strcmp(defaultValue.c_str(), "CURRENT_TIMESTAMP") == 0)
235
if (type.compare("INT") == 0)
237
/* We were a TIME, now we are an INT */
238
std::string ts(defaultValue);
239
boost::posix_time::time_duration td(boost::posix_time::duration_from_string(ts));
240
defaultValue= boost::lexical_cast<std::string>(td.total_seconds());
244
boost::regex date_regex("(0000|-00)");
246
if (regex_search(defaultValue, date_regex, flags))
255
bool DrizzleDumpTableMySQL::populateIndexes()
257
drizzle_result_st *result;
261
bool firstIndex= true;
262
DrizzleDumpIndex *index;
265
std::cerr << _("-- Retrieving indexes for ") << tableName << "..." << std::endl;
267
query="SHOW INDEXES FROM ";
268
query.append(tableName);
270
result= dcon->query(query);
275
while ((row= drizzle_row_next(result)))
277
std::string indexName(row[2]);
278
if (indexName.compare(lastKey) != 0)
280
if (strcmp(row[10], "FULLTEXT") == 0)
284
indexes.push_back(index);
285
index = new DrizzleDumpIndexMySQL(indexName, dcon);
286
index->isPrimary= (strcmp(row[2], "PRIMARY") == 0);
287
index->isUnique= (strcmp(row[1], "0") == 0);
288
index->isHash= (strcmp(row[10], "HASH") == 0);
289
index->length= (row[7]) ? boost::lexical_cast<uint32_t>(row[7]) : 0;
293
index->columns.push_back(row[4]);
296
indexes.push_back(index);
298
dcon->freeResult(result);
302
bool DrizzleDumpTableMySQL::populateFkeys()
304
drizzle_result_st *result;
307
DrizzleDumpForeignKey *fkey;
310
std::cerr << _("-- Retrieving foreign keys for ") << tableName << "..." << std::endl;
312
query= "SHOW TABLES FROM INFORMATION_SCHEMA LIKE 'REFERENTIAL_CONSTRAINTS'";
314
result= dcon->query(query);
319
uint64_t search_count = drizzle_result_row_count(result);
321
dcon->freeResult(result);
323
/* MySQL 5.0 will be 0 and MySQL 5.1 will be 1 */
324
if (search_count > 0)
326
query= "select rc.constraint_name, rc.referenced_table_name, group_concat(distinct concat('`',kc.column_name,'`')), rc.update_rule, rc.delete_rule, rc.match_option, group_concat(distinct concat('`',kt.column_name,'`')) from information_schema.referential_constraints rc join information_schema.key_column_usage kt on (rc.constraint_schema = kt.constraint_schema and rc.constraint_name = kt.constraint_name) join information_schema.key_column_usage kc on (rc.constraint_schema = kc.constraint_schema and rc.referenced_table_name = kc.table_name and rc.unique_constraint_name = kc.constraint_name) where rc.constraint_schema='";
327
query.append(database->databaseName);
328
query.append("' and rc.table_name='");
329
query.append(tableName);
330
query.append("' group by rc.constraint_name");
332
result= dcon->query(query);
337
while ((row= drizzle_row_next(result)))
339
fkey= new DrizzleDumpForeignKey(row[0], dcon);
340
fkey->parentColumns= row[6];
341
fkey->childTable= row[1];
342
fkey->childColumns= row[2];
343
fkey->updateRule= (strcmp(row[3], "RESTRICT") != 0) ? row[3] : "";
344
fkey->deleteRule= (strcmp(row[4], "RESTRICT") != 0) ? row[4] : "";
345
fkey->matchOption= (strcmp(row[5], "NONE") != 0) ? row[5] : "";
347
fkeys.push_back(fkey);
352
query= "SHOW CREATE TABLE `";
353
query.append(database->databaseName);
355
query.append(tableName);
357
result= dcon->query(query);
362
if ((row= drizzle_row_next(result)))
364
boost::match_flag_type flags = boost::match_default;
365
boost::regex constraint_regex("CONSTRAINT `(.*)` FOREIGN KEY \\((.*)\\) REFERENCES `(.*)` \\((.*)\\)( ON (UPDATE|DELETE) (CASCADE|RESTRICT|SET NULL))?( ON (UPDATE|DELETE) (CASCADE|RESTRICT|SET NULL))?");
367
boost::match_results<std::string::const_iterator> constraint_results;
369
std::string search_body(row[1]);
370
std::string::const_iterator start, end;
371
start= search_body.begin();
372
end= search_body.end();
373
while (regex_search(start, end, constraint_results, constraint_regex, flags))
375
fkey= new DrizzleDumpForeignKey(constraint_results[1], dcon);
376
fkey->parentColumns= constraint_results[2];
377
fkey->childTable= constraint_results[3];
378
fkey->childColumns= constraint_results[4];
380
if (constraint_results[5].compare("") != 0)
382
if (constraint_results[6].compare("UPDATE") == 0)
383
fkey->updateRule= constraint_results[7];
384
else if (constraint_results[6].compare("DELETE") == 0)
385
fkey->deleteRule= constraint_results[7];
387
if (constraint_results[8].compare("") != 0)
389
if (constraint_results[9].compare("UPDATE") == 0)
390
fkey->updateRule= constraint_results[10];
391
else if (constraint_results[9].compare("DELETE") == 0)
392
fkey->deleteRule= constraint_results[10];
394
fkey->matchOption= "";
396
fkeys.push_back(fkey);
398
start= constraint_results[0].second;
399
flags |= boost::match_prev_avail;
400
flags |= boost::match_not_bob;
404
dcon->freeResult(result);
408
void DrizzleDumpFieldMySQL::setType(const char* raw_type, const char* raw_collation)
410
std::string old_type(raw_type);
414
if ((pos= old_type.find("(")) != std::string::npos)
416
extra= old_type.substr(pos);
417
old_type.erase(pos, std::string::npos);
420
std::transform(old_type.begin(), old_type.end(), old_type.begin(), ::toupper);
421
if ((old_type.find("CHAR") != std::string::npos) or
422
(old_type.find("TEXT") != std::string::npos))
423
setCollate(raw_collation);
425
if ((old_type.compare("INT") == 0) and
426
((extra.find("unsigned") != std::string::npos)))
432
if ((old_type.compare("TINYINT") == 0) or
433
(old_type.compare("SMALLINT") == 0) or
434
(old_type.compare("MEDIUMINT") == 0))
440
if ((old_type.compare("TINYBLOB") == 0) or
441
(old_type.compare("MEDIUMBLOB") == 0) or
442
(old_type.compare("LONGBLOB") == 0))
448
if ((old_type.compare("TINYTEXT") == 0) or
449
(old_type.compare("MEDIUMTEXT") == 0) or
450
(old_type.compare("LONGTEXT") == 0) or
451
(old_type.compare("SET") == 0))
457
if (old_type.compare("CHAR") == 0)
463
if (old_type.compare("BINARY") == 0)
469
if (old_type.compare("ENUM") == 0)
472
/* Strip out the braces, we add them again during output */
473
enumValues= extra.substr(1, extra.length()-2);
477
if ((old_type.find("TIME") != std::string::npos) or
478
(old_type.find("DATE") != std::string::npos))
480
/* Intended to catch TIME/DATE/TIMESTAMP/DATETIME
481
We may have a default TIME/DATE which needs converting */
482
convertDateTime= true;
485
if ((old_type.compare("TIME") == 0) or (old_type.compare("YEAR") == 0))
491
if (old_type.compare("FLOAT") == 0)
501
void DrizzleDumpTableMySQL::setEngine(const char* newEngine)
503
if (strcmp(newEngine, "MyISAM") == 0)
504
engineName= "InnoDB";
506
engineName= newEngine;
509
DrizzleDumpData* DrizzleDumpTableMySQL::getData(void)
513
return new DrizzleDumpDataMySQL(this, dcon);
521
void DrizzleDumpDatabaseMySQL::setCollate(const char* newCollate)
525
std::string tmpCollate(newCollate);
526
if (tmpCollate.find("utf8") != std::string::npos)
532
collate= "utf8_general_ci";
535
void DrizzleDumpTableMySQL::setCollate(const char* newCollate)
539
std::string tmpCollate(newCollate);
540
if (tmpCollate.find("utf8") != std::string::npos)
547
collate= "utf8_general_ci";
550
void DrizzleDumpFieldMySQL::setCollate(const char* newCollate)
554
std::string tmpCollate(newCollate);
555
if (tmpCollate.find("utf8") != std::string::npos)
557
collation= tmpCollate;
561
collation= "utf8_general_ci";
564
DrizzleDumpDataMySQL::DrizzleDumpDataMySQL(DrizzleDumpTable *dataTable,
565
DrizzleDumpConnection *connection)
566
: DrizzleDumpData(dataTable, connection)
569
query= "SELECT * FROM `";
570
query.append(table->displayName);
573
result= dcon->query(query);
578
DrizzleDumpDataMySQL::~DrizzleDumpDataMySQL()
580
drizzle_result_free(result);
584
long DrizzleDumpDataMySQL::convertTime(const char* oldTime) const
586
std::string ts(oldTime);
587
boost::posix_time::time_duration td(boost::posix_time::duration_from_string(ts));
588
long seconds= td.total_seconds();
592
std::string DrizzleDumpDataMySQL::convertDate(const char* oldDate) const
594
boost::match_flag_type flags = boost::match_default;
596
boost::regex date_regex("(0000|-00)");
598
if (not regex_search(oldDate, date_regex, flags))
600
output.push_back('\'');
601
output.append(oldDate);
602
output.push_back('\'');
610
std::string DrizzleDumpDataMySQL::checkDateTime(const char* item, uint32_t field) const
614
if (table->fields[field]->convertDateTime)
616
if (table->fields[field]->type.compare("INT") == 0)
617
ret= boost::lexical_cast<std::string>(convertTime(item));
619
ret= convertDate(item);