~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump_mysql.cc

Merge Stewart - fix bug 587772: READ COMMITTED isolation level doesn't work (at least with InnoDB)

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