~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump_mysql.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
167
167
  if (verbose)
168
168
    std::cerr << _("-- Retrieving fields for ") << tableName << "..." << std::endl;
169
169
 
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='";
 
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='";
171
171
  query.append(database->databaseName);
172
172
  query.append("' AND TABLE_NAME='");
173
173
  query.append(tableName);
190
190
    if (field->type.compare("ENUM") == 0)
191
191
      field->isNull= true;
192
192
 
193
 
    if (row[2])
 
193
    if ((row[2]) and (field->type.compare("TEXT") != 0))
194
194
    {
195
195
      field->defaultValue= row[2];
196
196
      if (field->convertDateTime)
199
199
      }
200
200
    }
201
201
    else
202
 
     field->defaultValue= "";
 
202
    {
 
203
      field->defaultValue= "";
 
204
    }
203
205
 
204
206
    field->isAutoIncrement= (strcmp(row[8], "auto_increment") == 0) ? true : false;
205
207
    field->defaultIsNull= field->isNull;
206
 
    field->length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
207
 
    if ((row[5] != NULL) and (row[6] != NULL))
208
 
    {
209
 
      field->decimalPrecision= boost::lexical_cast<uint32_t>(row[5]);
210
 
      field->decimalScale= boost::lexical_cast<uint32_t>(row[6]);
211
 
    }
 
208
 
 
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;
212
212
    else
 
213
      field->length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
 
214
 
 
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))
213
219
    {
214
 
      field->decimalPrecision= 0;
215
 
      field->decimalScale= 0;
 
220
      field->length= 1;
216
221
    }
217
222
 
 
223
    field->decimalPrecision= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
 
224
    field->decimalScale= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
 
225
    field->comment= (row[9]) ? row[9] : "";
218
226
    fields.push_back(field);
219
227
  }
220
228
 
306
314
  if (verbose)
307
315
    std::cerr << _("-- Retrieving foreign keys for ") << tableName << "..." << std::endl;
308
316
 
309
 
  query= "SHOW TABLES FROM INFORMATION_SCHEMA LIKE 'REFERENTIAL_CONSTRAINTS'";
310
 
 
 
317
  query= "SHOW CREATE TABLE `";
 
318
  query.append(database->databaseName);
 
319
  query.append("`.`");
 
320
  query.append(tableName);
 
321
  query.append("`");
311
322
  result= dcon->query(query);
312
323
 
313
324
  if (result == NULL)
314
325
    return false;
315
326
 
316
 
  uint64_t search_count = drizzle_result_row_count(result);
317
 
 
318
 
  dcon->freeResult(result);
319
 
 
320
 
  /* MySQL 5.0 will be 0 and MySQL 5.1 will be 1 */
321
 
  if (search_count > 0)
 
327
  if ((row= drizzle_row_next(result)))
322
328
  {
323
 
    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='";
324
 
    query.append(database->databaseName);
325
 
    query.append("' and rc.table_name='");
326
 
    query.append(tableName);
327
 
    query.append("' group by rc.constraint_name");
328
 
 
329
 
    result= dcon->query(query);
330
 
 
331
 
    if (result == NULL)
332
 
      return false;
333
 
 
334
 
    while ((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))?");
 
331
 
 
332
    boost::match_results<std::string::const_iterator> constraint_results;
 
333
 
 
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))
335
339
    {
336
 
      fkey= new DrizzleDumpForeignKey(row[0], dcon);
337
 
      fkey->parentColumns= row[6];
338
 
      fkey->childTable= row[1];
339
 
      fkey->childColumns= row[2];
340
 
      fkey->updateRule= (strcmp(row[3], "RESTRICT") != 0) ? row[3] : "";
341
 
      fkey->deleteRule= (strcmp(row[4], "RESTRICT") != 0) ? row[4] : "";
342
 
      fkey->matchOption= (strcmp(row[5], "NONE") != 0) ? row[5] : "";
 
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];
 
344
        
 
345
      if (constraint_results[5].compare("") != 0)
 
346
      {
 
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];
 
351
      }
 
352
      if (constraint_results[8].compare("") != 0)
 
353
      {
 
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];
 
358
      }
 
359
      fkey->matchOption= "";
343
360
 
344
361
      fkeys.push_back(fkey);
345
 
    }
346
 
  }
347
 
  else
348
 
  {
349
 
    query= "SHOW CREATE TABLE `";
350
 
    query.append(database->databaseName);
351
 
    query.append("`.`");
352
 
    query.append(tableName);
353
 
    query.append("`");
354
 
    result= dcon->query(query);
355
 
 
356
 
    if (result == NULL)
357
 
      return false;
358
 
 
359
 
    if ((row= drizzle_row_next(result)))
360
 
    {
361
 
      boost::match_flag_type flags = boost::match_default;
362
 
      boost::regex constraint_regex("CONSTRAINT `(.*)` FOREIGN KEY \\((.*)\\) REFERENCES `(.*)` \\((.*)\\)( ON (UPDATE|DELETE) (CASCADE|RESTRICT|SET NULL))?( ON (UPDATE|DELETE) (CASCADE|RESTRICT|SET NULL))?");
363
 
 
364
 
      boost::match_results<std::string::const_iterator> constraint_results;
365
 
 
366
 
      std::string search_body(row[1]);
367
 
      std::string::const_iterator start, end;
368
 
      start= search_body.begin();
369
 
      end= search_body.end();
370
 
      while (regex_search(start, end, constraint_results, constraint_regex, flags))
371
 
      {
372
 
        fkey= new DrizzleDumpForeignKey(constraint_results[1], dcon);
373
 
        fkey->parentColumns= constraint_results[2];
374
 
        fkey->childTable= constraint_results[3];
375
 
        fkey->childColumns= constraint_results[4];
376
 
        
377
 
        if (constraint_results[5].compare("") != 0)
378
 
        {
379
 
          if (constraint_results[6].compare("UPDATE") == 0)
380
 
            fkey->updateRule= constraint_results[7];
381
 
          else if (constraint_results[6].compare("DELETE") == 0)
382
 
            fkey->deleteRule= constraint_results[7];
383
 
        }
384
 
        if (constraint_results[8].compare("") != 0)
385
 
        {
386
 
          if (constraint_results[9].compare("UPDATE") == 0)
387
 
            fkey->updateRule= constraint_results[10];
388
 
          else if (constraint_results[9].compare("DELETE") == 0)
389
 
            fkey->deleteRule= constraint_results[10];
390
 
        }
391
 
        fkey->matchOption= "";
392
 
 
393
 
        fkeys.push_back(fkey);
394
 
 
395
 
        start= constraint_results[0].second;
396
 
        flags |= boost::match_prev_avail; 
397
 
        flags |= boost::match_not_bob;
398
 
      }
 
362
 
 
363
      start= constraint_results[0].second;
 
364
      flags |= boost::match_prev_avail; 
 
365
      flags |= boost::match_not_bob;
399
366
    }
400
367
  }
401
368
  dcon->freeResult(result);
408
375
  std::string extra;
409
376
  size_t pos;
410
377
  
411
 
  if ((pos= old_type.find("(")) != std::string::npos)
 
378
  if (((pos= old_type.find("(")) != std::string::npos) or
 
379
    ((pos= old_type.find(" ")) != std::string::npos))
412
380
  {
413
381
    extra= old_type.substr(pos);
414
382
    old_type.erase(pos, std::string::npos);
419
387
    (old_type.find("TEXT") != std::string::npos))
420
388
    setCollate(raw_collation);
421
389
 
 
390
  if ((old_type.compare("BIGINT") == 0) and
 
391
    ((extra.find("unsigned") != std::string::npos)))
 
392
  {
 
393
    rangeCheck= true;
 
394
  }
 
395
 
422
396
  if ((old_type.compare("INT") == 0) and 
423
397
    ((extra.find("unsigned") != std::string::npos)))
424
398
  {
460
434
  if (old_type.compare("BINARY") == 0)
461
435
  {
462
436
    type= "VARBINARY";
 
437
    
463
438
    return;
464
439
  }
465
440
 
492
467
    return;
493
468
  }
494
469
 
 
470
  if (old_type.compare("BIT") == 0)
 
471
  {
 
472
    type= "VARBINARY";
 
473
    return;
 
474
  }
 
475
 
495
476
  type= old_type;
496
477
  return;
497
478
}
498
479
 
499
480
void DrizzleDumpTableMySQL::setEngine(const char* newEngine)
500
481
{
501
 
  if (strcmp(newEngine, "MyISAM") == 0)
 
482
  if ((strcmp(newEngine, "MyISAM") == 0) || (strcmp(newEngine, "MEMORY") == 0))
502
483
    engineName= "InnoDB";
503
484
  else
504
485
    engineName= newEngine; 
570
551
 
571
552
  result= dcon->query(query);
572
553
  if (result == NULL)
573
 
    throw 1;
 
554
    throw std::exception();
574
555
}
575
556
 
576
557
DrizzleDumpDataMySQL::~DrizzleDumpDataMySQL()