300
300
std::cerr << _("-- Retrieving foreign keys for ") << tableName << "..." << std::endl;
302
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='";
303
query.append(database->databaseName);
304
query.append("' and rc.table_name='");
305
query.append(tableName);
306
query.append("' group by rc.constraint_name");
302
query= "SHOW TABLES FROM INFORMATION_SCHEMA LIKE 'REFERENTIAL_CONSTRAINTS'";
308
304
result= dcon->query(query);
310
306
if (result == NULL)
313
while ((row= drizzle_row_next(result)))
315
fkey= new DrizzleDumpForeignKey(row[0], dcon);
316
fkey->parentColumns= row[6];
317
fkey->childTable= row[1];
318
fkey->childColumns= row[2];
319
fkey->updateRule= (strcmp(row[3], "RESTRICT") != 0) ? row[3] : "";
320
fkey->deleteRule= (strcmp(row[4], "RESTRICT") != 0) ? row[4] : "";
321
fkey->matchOption= (strcmp(row[5], "NONE") != 0) ? row[5] : "";
323
fkeys.push_back(fkey);
309
uint64_t search_count = drizzle_result_row_count(result);
311
dcon->freeResult(result);
313
/* MySQL 5.0 will be 0 and MySQL 5.1 will be 1 */
314
if (search_count > 0)
316
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='";
317
query.append(database->databaseName);
318
query.append("' and rc.table_name='");
319
query.append(tableName);
320
query.append("' group by rc.constraint_name");
322
result= dcon->query(query);
327
while ((row= drizzle_row_next(result)))
329
fkey= new DrizzleDumpForeignKey(row[0], dcon);
330
fkey->parentColumns= row[6];
331
fkey->childTable= row[1];
332
fkey->childColumns= row[2];
333
fkey->updateRule= (strcmp(row[3], "RESTRICT") != 0) ? row[3] : "";
334
fkey->deleteRule= (strcmp(row[4], "RESTRICT") != 0) ? row[4] : "";
335
fkey->matchOption= (strcmp(row[5], "NONE") != 0) ? row[5] : "";
337
fkeys.push_back(fkey);
342
query= "SHOW CREATE TABLE `";
343
query.append(database->databaseName);
345
query.append(tableName);
347
result= dcon->query(query);
352
if ((row= drizzle_row_next(result)))
354
boost::match_flag_type flags = boost::match_default;
355
boost::regex constraint_regex("CONSTRAINT `(.*)` FOREIGN KEY \\((.*)\\) REFERENCES `(.*)` \\((.*)\\)( ON (UPDATE|DELETE) (CASCADE|RESTRICT|SET NULL))?( ON (UPDATE|DELETE) (CASCADE|RESTRICT|SET NULL))?");
357
boost::match_results<std::string::const_iterator> constraint_results;
359
std::string search_body(row[1]);
360
std::string::const_iterator start, end;
361
start= search_body.begin();
362
end= search_body.end();
363
while (regex_search(start, end, constraint_results, constraint_regex, flags))
365
fkey= new DrizzleDumpForeignKey(constraint_results[1], dcon);
366
fkey->parentColumns= constraint_results[2];
367
fkey->childTable= constraint_results[3];
368
fkey->childColumns= constraint_results[4];
370
if (constraint_results[5].compare("") != 0)
372
if (constraint_results[6].compare("UPDATE") == 0)
373
fkey->updateRule= constraint_results[7];
374
else if (constraint_results[6].compare("DELETE") == 0)
375
fkey->deleteRule= constraint_results[7];
377
if (constraint_results[8].compare("") != 0)
379
if (constraint_results[9].compare("UPDATE") == 0)
380
fkey->updateRule= constraint_results[10];
381
else if (constraint_results[9].compare("DELETE") == 0)
382
fkey->deleteRule= constraint_results[10];
384
fkey->matchOption= "";
386
fkeys.push_back(fkey);
388
start= constraint_results[0].second;
389
flags |= boost::match_prev_avail;
390
flags |= boost::match_not_bob;
325
394
dcon->freeResult(result);