42
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='";
44
query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE, AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE != 'VIEW' AND TABLE_SCHEMA='";
45
45
query.append(databaseName);
46
46
query.append("' ORDER BY TABLE_NAME");
69
68
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
70
table->database= this;
77
if ((not table->populateFields()) or (not table->populateIndexes()) or
78
(not table->populateFkeys()))
71
if ((not table->populateFields()) or (not table->populateIndexes()))
81
74
if (not ignore_errors)
168
161
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, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='";
163
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
164
query.append(database->databaseName);
172
165
query.append("' AND TABLE_NAME='");
173
166
query.append(tableName);
184
177
DrizzleDumpFieldMySQL *field = new DrizzleDumpFieldMySQL(fieldName, dcon);
185
178
/* Stop valgrind warning */
186
179
field->convertDateTime= false;
187
field->isNull= (strcmp(row[3], "YES") == 0) ? true : false;
188
180
/* Also sets collation */
189
181
field->setType(row[1], row[8]);
190
if (field->type.compare("ENUM") == 0)
193
if ((row[2]) and (field->type.compare("TEXT") != 0))
195
field->defaultValue= row[2];
196
184
if (field->convertDateTime)
198
field->dateTimeConvert();
186
field->dateTimeConvert(row[2]);
189
field->defaultValue= row[2];
203
field->defaultValue= "";
192
field->defaultValue= "";
194
field->isNull= (strcmp(row[3], "YES") == 0) ? true : false;
206
195
field->isAutoIncrement= (strcmp(row[8], "auto_increment") == 0) ? true : false;
207
196
field->defaultIsNull= field->isNull;
209
/* Seriously MySQL, why is BIT length in NUMERIC_PRECISION? */
210
if ((strncmp(row[1], "bit", 3) == 0) and (row[5] != NULL))
211
field->length= ((boost::lexical_cast<uint32_t>(row[5]) - 1) / 8) + 1;
213
field->length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
215
/* Also, CHAR(0) is valid?? */
216
if (((field->type.compare("VARBINARY") == 0)
217
or (field->type.compare("VARCHAR") == 0))
218
and (field->length == 0))
197
field->length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
223
198
field->decimalPrecision= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
224
199
field->decimalScale= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
225
field->comment= (row[9]) ? row[9] : "";
226
202
fields.push_back(field);
234
void DrizzleDumpFieldMySQL::dateTimeConvert(void)
210
void DrizzleDumpFieldMySQL::dateTimeConvert(const char* oldDefault)
236
212
boost::match_flag_type flags = boost::match_default;
238
if (strcmp(defaultValue.c_str(), "CURRENT_TIMESTAMP") == 0)
214
if (strcmp(oldDefault, "CURRENT_TIMESTAMP") == 0)
216
defaultValue= oldDefault;
241
220
if (type.compare("INT") == 0)
243
222
/* We were a TIME, now we are an INT */
244
std::string ts(defaultValue);
223
std::string ts(oldDefault);
245
224
boost::posix_time::time_duration td(boost::posix_time::duration_from_string(ts));
246
225
defaultValue= boost::lexical_cast<std::string>(td.total_seconds());
250
boost::regex date_regex("(0000|-00)");
229
boost::regex date_regex("([0-9]{3}[1-9]-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))");
252
if (regex_search(defaultValue, date_regex, flags))
231
if (regex_search(oldDefault, date_regex, flags))
233
defaultValue= oldDefault;
254
237
defaultIsNull= true;
307
bool DrizzleDumpTableMySQL::populateFkeys()
309
drizzle_result_st *result;
312
DrizzleDumpForeignKey *fkey;
315
std::cerr << _("-- Retrieving foreign keys for ") << tableName << "..." << std::endl;
317
query= "SHOW CREATE TABLE `";
318
query.append(database->databaseName);
320
query.append(tableName);
322
result= dcon->query(query);
327
if ((row= drizzle_row_next(result)))
329
boost::match_flag_type flags = boost::match_default;
330
boost::regex constraint_regex("CONSTRAINT `(.*?)` FOREIGN KEY \\((.*?)\\) REFERENCES `(.*?)` \\((.*?)\\)( ON (UPDATE|DELETE) (CASCADE|RESTRICT|SET NULL))?( ON (UPDATE|DELETE) (CASCADE|RESTRICT|SET NULL))?");
332
boost::match_results<std::string::const_iterator> constraint_results;
334
std::string search_body(row[1]);
335
std::string::const_iterator start, end;
336
start= search_body.begin();
337
end= search_body.end();
338
while (regex_search(start, end, constraint_results, constraint_regex, flags))
340
fkey= new DrizzleDumpForeignKey(constraint_results[1], dcon);
341
fkey->parentColumns= constraint_results[2];
342
fkey->childTable= constraint_results[3];
343
fkey->childColumns= constraint_results[4];
345
if (constraint_results[5].compare("") != 0)
347
if (constraint_results[6].compare("UPDATE") == 0)
348
fkey->updateRule= constraint_results[7];
349
else if (constraint_results[6].compare("DELETE") == 0)
350
fkey->deleteRule= constraint_results[7];
352
if (constraint_results[8].compare("") != 0)
354
if (constraint_results[9].compare("UPDATE") == 0)
355
fkey->updateRule= constraint_results[10];
356
else if (constraint_results[9].compare("DELETE") == 0)
357
fkey->deleteRule= constraint_results[10];
359
fkey->matchOption= "";
361
fkeys.push_back(fkey);
363
start= constraint_results[0].second;
364
flags |= boost::match_prev_avail;
365
flags |= boost::match_not_bob;
368
dcon->freeResult(result);
372
290
void DrizzleDumpFieldMySQL::setType(const char* raw_type, const char* raw_collation)
374
292
std::string old_type(raw_type);
375
293
std::string extra;
378
if (((pos= old_type.find("(")) != std::string::npos) or
379
((pos= old_type.find(" ")) != std::string::npos))
296
if ((pos= old_type.find("(")) != std::string::npos)
381
298
extra= old_type.substr(pos);
382
299
old_type.erase(pos, std::string::npos);
387
304
(old_type.find("TEXT") != std::string::npos))
388
305
setCollate(raw_collation);
390
if ((old_type.compare("BIGINT") == 0) and
391
((extra.find("unsigned") != std::string::npos)))
396
307
if ((old_type.compare("INT") == 0) and
397
308
((extra.find("unsigned") != std::string::npos)))
573
476
boost::match_flag_type flags = boost::match_default;
574
477
std::string output;
575
boost::regex date_regex("(0000|-00)");
478
boost::regex date_regex("([0-9]{3}[1-9]-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))");
577
if (not regex_search(oldDate, date_regex, flags))
480
if (regex_search(oldDate, date_regex, flags))
579
482
output.push_back('\'');
580
483
output.append(oldDate);
589
std::string DrizzleDumpDataMySQL::checkDateTime(const char* item, uint32_t field) const
492
std::ostream& DrizzleDumpDataMySQL::checkDateTime(std::ostream &os, const char* item, uint32_t field) const
593
494
if (table->fields[field]->convertDateTime)
595
496
if (table->fields[field]->type.compare("INT") == 0)
596
ret= boost::lexical_cast<std::string>(convertTime(item));
497
os << convertTime(item);
598
ret= convertDate(item);
499
os << convertDate(item);