~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump_mysql.cc

  • Committer: Brian Aker
  • Date: 2010-10-20 20:26:18 UTC
  • mfrom: (1859.2.13 refactor)
  • Revision ID: brian@tangent.org-20101020202618-9222n39lm329urv5
Merge for Brian 

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, COLUMN_COMMENT 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 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);
184
184
    DrizzleDumpFieldMySQL *field = new DrizzleDumpFieldMySQL(fieldName, dcon);
185
185
    /* Stop valgrind warning */
186
186
    field->convertDateTime= false;
187
 
    field->isNull= (strcmp(row[3], "YES") == 0) ? true : false;
188
187
    /* Also sets collation */
189
188
    field->setType(row[1], row[8]);
190
 
    if (field->type.compare("ENUM") == 0)
191
 
      field->isNull= true;
192
 
 
193
 
    if ((row[2]) and (field->type.compare("TEXT") != 0))
 
189
    field->isNull= (strcmp(row[3], "YES") == 0) ? true : false;
 
190
    if (row[2])
194
191
    {
195
192
      field->defaultValue= row[2];
196
 
      if (field->convertDateTime)
197
 
      {
198
 
        field->dateTimeConvert();
199
 
      }
200
193
    }
201
194
    else
 
195
     field->defaultValue= "";
 
196
 
 
197
    if (field->convertDateTime)
202
198
    {
203
 
      field->defaultValue= "";
 
199
      field->dateTimeConvert();
204
200
    }
205
201
 
 
202
 
206
203
    field->isAutoIncrement= (strcmp(row[8], "auto_increment") == 0) ? true : false;
207
204
    field->defaultIsNull= field->isNull;
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
 
    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))
219
 
    {
220
 
      field->length= 1;
221
 
    }
222
 
 
 
205
    field->length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
223
206
    field->decimalPrecision= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
224
207
    field->decimalScale= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
225
 
    field->comment= (row[9]) ? row[9] : "";
 
208
 
 
209
 
226
210
    fields.push_back(field);
227
211
  }
228
212
 
254
238
    defaultIsNull= true;
255
239
    defaultValue="";
256
240
  }
 
241
  isNull= true;
257
242
}
258
243
 
259
244
 
314
299
  if (verbose)
315
300
    std::cerr << _("-- Retrieving foreign keys for ") << tableName << "..." << std::endl;
316
301
 
317
 
  query= "SHOW CREATE TABLE `";
318
 
  query.append(database->databaseName);
319
 
  query.append("`.`");
320
 
  query.append(tableName);
321
 
  query.append("`");
 
302
  query= "SHOW TABLES FROM INFORMATION_SCHEMA LIKE 'REFERENTIAL_CONSTRAINTS'";
 
303
 
322
304
  result= dcon->query(query);
323
305
 
324
306
  if (result == NULL)
325
307
    return false;
326
308
 
327
 
  if ((row= drizzle_row_next(result)))
328
 
  {
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))
339
 
    {
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];
 
309
  uint64_t search_count = drizzle_result_row_count(result);
 
310
 
 
311
  dcon->freeResult(result);
 
312
 
 
313
  /* MySQL 5.0 will be 0 and MySQL 5.1 will be 1 */
 
314
  if (search_count > 0)
 
315
  {
 
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");
 
321
 
 
322
    result= dcon->query(query);
 
323
 
 
324
    if (result == NULL)
 
325
      return false;
 
326
 
 
327
    while ((row= drizzle_row_next(result)))
 
328
    {
 
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] : "";
 
336
 
 
337
      fkeys.push_back(fkey);
 
338
    }
 
339
  }
 
340
  else
 
341
  {
 
342
    query= "SHOW CREATE TABLE `";
 
343
    query.append(database->databaseName);
 
344
    query.append("`.`");
 
345
    query.append(tableName);
 
346
    query.append("`");
 
347
    result= dcon->query(query);
 
348
 
 
349
    if (result == NULL)
 
350
      return false;
 
351
 
 
352
    if ((row= drizzle_row_next(result)))
 
353
    {
 
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))?");
 
356
 
 
357
      boost::match_results<std::string::const_iterator> constraint_results;
 
358
 
 
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))
 
364
      {
 
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];
344
369
        
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= "";
360
 
 
361
 
      fkeys.push_back(fkey);
362
 
 
363
 
      start= constraint_results[0].second;
364
 
      flags |= boost::match_prev_avail; 
365
 
      flags |= boost::match_not_bob;
 
370
        if (constraint_results[5].compare("") != 0)
 
371
        {
 
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];
 
376
        }
 
377
        if (constraint_results[8].compare("") != 0)
 
378
        {
 
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];
 
383
        }
 
384
        fkey->matchOption= "";
 
385
 
 
386
        fkeys.push_back(fkey);
 
387
 
 
388
        start= constraint_results[0].second;
 
389
        flags |= boost::match_prev_avail; 
 
390
        flags |= boost::match_not_bob;
 
391
      }
366
392
    }
367
393
  }
368
394
  dcon->freeResult(result);
375
401
  std::string extra;
376
402
  size_t pos;
377
403
  
378
 
  if (((pos= old_type.find("(")) != std::string::npos) or
379
 
    ((pos= old_type.find(" ")) != std::string::npos))
 
404
  if ((pos= old_type.find("(")) != std::string::npos)
380
405
  {
381
406
    extra= old_type.substr(pos);
382
407
    old_type.erase(pos, std::string::npos);
387
412
    (old_type.find("TEXT") != std::string::npos))
388
413
    setCollate(raw_collation);
389
414
 
390
 
  if ((old_type.compare("BIGINT") == 0) and
391
 
    ((extra.find("unsigned") != std::string::npos)))
392
 
  {
393
 
    rangeCheck= true;
394
 
  }
395
 
 
396
415
  if ((old_type.compare("INT") == 0) and 
397
416
    ((extra.find("unsigned") != std::string::npos)))
398
417
  {
434
453
  if (old_type.compare("BINARY") == 0)
435
454
  {
436
455
    type= "VARBINARY";
437
 
    
438
456
    return;
439
457
  }
440
458
 
452
470
    /* Intended to catch TIME/DATE/TIMESTAMP/DATETIME 
453
471
       We may have a default TIME/DATE which needs converting */
454
472
    convertDateTime= true;
455
 
    isNull= true;
456
473
  }
457
474
 
458
475
  if ((old_type.compare("TIME") == 0) or (old_type.compare("YEAR") == 0))
467
484
    return;
468
485
  }
469
486
 
470
 
  if (old_type.compare("BIT") == 0)
471
 
  {
472
 
    type= "VARBINARY";
473
 
    return;
474
 
  }
475
 
 
476
487
  type= old_type;
477
488
  return;
478
489
}
479
490
 
480
491
void DrizzleDumpTableMySQL::setEngine(const char* newEngine)
481
492
{
482
 
  if ((strcmp(newEngine, "MyISAM") == 0) || (strcmp(newEngine, "MEMORY") == 0))
 
493
  if (strcmp(newEngine, "MyISAM") == 0)
483
494
    engineName= "InnoDB";
484
495
  else
485
496
    engineName= newEngine; 
551
562
 
552
563
  result= dcon->query(query);
553
564
  if (result == NULL)
554
 
    throw std::exception();
 
565
    throw 1;
555
566
}
556
567
 
557
568
DrizzleDumpDataMySQL::~DrizzleDumpDataMySQL()
558
569
{
559
570
  drizzle_result_free(result);
560
 
  delete result;
 
571
  if (result) delete result;
561
572
}
562
573
 
563
574
long DrizzleDumpDataMySQL::convertTime(const char* oldTime) const