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>
31
bool DrizzleDumpDatabaseMySQL::populateTables()
33
drizzle_result_st *result;
37
if (not dcon->setDB(databaseName))
41
std::cerr << _("-- Retrieving table structures for ") << databaseName << "..." << std::endl;
43
query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE, AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='";
44
query.append(databaseName);
45
query.append("' ORDER BY TABLE_NAME");
47
result= dcon->query(query);
52
while ((row= drizzle_row_next(result)))
54
std::string tableName(row[0]);
55
std::string displayName(tableName);
56
cleanTableName(displayName);
57
if (not ignoreTable(displayName))
60
DrizzleDumpTableMySQL *table = new DrizzleDumpTableMySQL(tableName, dcon);
61
table->displayName= displayName;
62
table->setCollate(row[1]);
63
table->setEngine(row[2]);
65
table->autoIncrement= boost::lexical_cast<uint64_t>(row[3]);
67
table->autoIncrement= 0;
69
table->database= this;
70
if ((not table->populateFields()) or (not table->populateIndexes()))
75
tables.push_back(table);
78
dcon->freeResult(result);
83
bool DrizzleDumpDatabaseMySQL::populateTables(const std::vector<std::string> &table_names)
85
drizzle_result_st *result;
89
if (not dcon->setDB(databaseName))
93
std::cerr << _("-- Retrieving table structures for ") << databaseName << "..." << std::endl;
94
for (std::vector<std::string>::const_iterator it= table_names.begin(); it != table_names.end(); ++it)
96
std::string tableName= *it;
97
std::string displayName(tableName);
98
cleanTableName(displayName);
99
if (not ignoreTable(displayName))
102
query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE, AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='";
103
query.append(databaseName);
104
query.append("' AND TABLE_NAME = '");
105
query.append(tableName);
108
result= dcon->query(query);
113
if ((row= drizzle_row_next(result)))
115
DrizzleDumpTableMySQL *table = new DrizzleDumpTableMySQL(tableName, dcon);
116
table->displayName= displayName;
117
table->setCollate(row[1]);
118
table->setEngine(row[2]);
120
table->autoIncrement= boost::lexical_cast<uint64_t>(row[3]);
122
table->autoIncrement= 0;
124
table->database= this;
125
if ((not table->populateFields()) or (not table->populateIndexes()))
130
tables.push_back(table);
131
dcon->freeResult(result);
135
dcon->freeResult(result);
144
bool DrizzleDumpTableMySQL::populateFields()
146
drizzle_result_st *result;
151
std::cerr << _("-- Retrieving fields for ") << tableName << "..." << std::endl;
153
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='";
154
query.append(database->databaseName);
155
query.append("' AND TABLE_NAME='");
156
query.append(tableName);
157
query.append("' ORDER BY ORDINAL_POSITION");
159
result= dcon->query(query);
164
while ((row= drizzle_row_next(result)))
166
std::string fieldName(row[0]);
167
DrizzleDumpFieldMySQL *field = new DrizzleDumpFieldMySQL(fieldName, dcon);
168
/* Stop valgrind warning */
169
field->convertDateTime= false;
170
/* Also sets collation */
171
field->setType(row[1], row[8]);
174
if (field->convertDateTime)
176
field->dateTimeConvert(row[2]);
179
field->defaultValue= row[2];
182
field->defaultValue= "";
184
field->isNull= (strcmp(row[3], "YES") == 0) ? true : false;
185
field->isAutoIncrement= (strcmp(row[8], "auto_increment") == 0) ? true : false;
186
field->defaultIsNull= field->isNull;
187
field->length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
188
field->decimalPrecision= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
189
field->decimalScale= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
192
fields.push_back(field);
195
dcon->freeResult(result);
200
void DrizzleDumpFieldMySQL::dateTimeConvert(const char* oldDefault)
202
boost::match_flag_type flags = boost::match_default;
204
if (strcmp(oldDefault, "CURRENT_TIMESTAMP") == 0)
206
defaultValue= oldDefault;
210
if (type.compare("INT") == 0)
212
/* We were a TIME, now we are an INT */
213
std::string ts(oldDefault);
214
boost::posix_time::time_duration td(boost::posix_time::duration_from_string(ts));
215
defaultValue= boost::lexical_cast<std::string>(td.total_seconds());
219
boost::regex date_regex("([0-9]{3}[1-9]-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))");
221
if (regex_search(oldDefault, date_regex, flags))
223
defaultValue= oldDefault;
233
bool DrizzleDumpTableMySQL::populateIndexes()
235
drizzle_result_st *result;
239
bool firstIndex= true;
240
DrizzleDumpIndex *index;
243
std::cerr << _("-- Retrieving indexes for ") << tableName << "..." << std::endl;
245
query="SHOW INDEXES FROM ";
246
query.append(tableName);
248
result= dcon->query(query);
253
while ((row= drizzle_row_next(result)))
255
std::string indexName(row[2]);
256
if (indexName.compare(lastKey) != 0)
258
if (strcmp(row[10], "FULLTEXT") == 0)
262
indexes.push_back(index);
263
index = new DrizzleDumpIndexMySQL(indexName, dcon);
264
index->isPrimary= (strcmp(row[2], "PRIMARY") == 0);
265
index->isUnique= (strcmp(row[1], "0") == 0);
266
index->isHash= (strcmp(row[10], "HASH") == 0);
270
index->columns.push_back(row[4]);
273
indexes.push_back(index);
275
dcon->freeResult(result);
279
void DrizzleDumpFieldMySQL::setType(const char* raw_type, const char* raw_collation)
281
std::string old_type(raw_type);
285
if ((pos= old_type.find("(")) != std::string::npos)
287
extra= old_type.substr(pos);
288
old_type.erase(pos, std::string::npos);
291
std::transform(old_type.begin(), old_type.end(), old_type.begin(), ::toupper);
292
if ((old_type.find("CHAR") != std::string::npos) or
293
(old_type.find("TEXT") != std::string::npos))
294
setCollate(raw_collation);
296
if ((old_type.compare("INT") == 0) and
297
((extra.find("unsigned") != std::string::npos)))
303
if ((old_type.compare("TINYINT") == 0) or
304
(old_type.compare("SMALLINT") == 0) or
305
(old_type.compare("MEDIUMINT") == 0))
311
if ((old_type.compare("TINYBLOB") == 0) or
312
(old_type.compare("MEDIUMBLOB") == 0) or
313
(old_type.compare("LONGBLOB") == 0))
319
if ((old_type.compare("TINYTEXT") == 0) or
320
(old_type.compare("MEDIUMTEXT") == 0) or
321
(old_type.compare("LONGTEXT") == 0) or
322
(old_type.compare("SET") == 0))
328
if (old_type.compare("CHAR") == 0)
334
if (old_type.compare("BINARY") == 0)
340
if (old_type.compare("ENUM") == 0)
343
/* Strip out the braces, we add them again during output */
344
enumValues= extra.substr(1, extra.length()-2);
348
if ((old_type.find("TIME") != std::string::npos) or
349
(old_type.find("DATE") != std::string::npos))
351
/* Intended to catch TIME/DATE/TIMESTAMP/DATETIME
352
We may have a default TIME/DATE which needs converting */
353
convertDateTime= true;
356
if (old_type.compare("TIME") == 0)
362
if (old_type.compare("FLOAT") == 0)
372
void DrizzleDumpTableMySQL::setEngine(const char* newEngine)
374
if (strcmp(newEngine, "MyISAM") == 0)
375
engineName= "InnoDB";
377
engineName= newEngine;
380
DrizzleDumpData* DrizzleDumpTableMySQL::getData(void)
384
return new DrizzleDumpDataMySQL(this, dcon);
392
void DrizzleDumpDatabaseMySQL::setCollate(const char* newCollate)
396
std::string tmpCollate(newCollate);
397
if (tmpCollate.find("utf8") != std::string::npos)
403
collate= "utf8_general_ci";
406
void DrizzleDumpTableMySQL::setCollate(const char* newCollate)
410
std::string tmpCollate(newCollate);
411
if (tmpCollate.find("utf8") != std::string::npos)
418
collate= "utf8_general_ci";
421
void DrizzleDumpFieldMySQL::setCollate(const char* newCollate)
425
std::string tmpCollate(newCollate);
426
if (tmpCollate.find("utf8") != std::string::npos)
428
collation= tmpCollate;
432
collation= "utf8_general_ci";
435
DrizzleDumpDataMySQL::DrizzleDumpDataMySQL(DrizzleDumpTable *dataTable,
436
DrizzleDumpConnection *connection)
437
: DrizzleDumpData(dataTable, connection)
440
query= "SELECT * FROM `";
441
query.append(table->displayName);
444
result= dcon->query(query);
449
DrizzleDumpDataMySQL::~DrizzleDumpDataMySQL()
451
drizzle_result_free(result);
452
if (result) delete result;
455
long DrizzleDumpDataMySQL::convertTime(const char* oldTime) const
457
std::string ts(oldTime);
458
boost::posix_time::time_duration td(boost::posix_time::duration_from_string(ts));
459
long seconds= td.total_seconds();
463
std::string DrizzleDumpDataMySQL::convertDate(const char* oldDate) const
465
boost::match_flag_type flags = boost::match_default;
467
boost::regex date_regex("([0-9]{3}[1-9]-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))");
469
if (regex_search(oldDate, date_regex, flags))
471
output.push_back('\'');
472
output.append(oldDate);
473
output.push_back('\'');
481
std::ostream& DrizzleDumpDataMySQL::checkDateTime(std::ostream &os, const char* item, uint32_t field) const
483
if (table->fields[field]->convertDateTime)
485
if (table->fields[field]->type.compare("INT") == 0)
486
os << convertTime(item);
488
os << convertDate(item);