~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump_mysql.cc

  • Committer: Brian Aker
  • Date: 2010-10-19 08:17:10 UTC
  • mto: (1864.2.1 merge)
  • mto: This revision was merged to the branch mainline in revision 1864.
  • Revision ID: brian@tangent.org-20101019081710-hw13j03145h13pdg
Merge in a bit more strictness around table type.

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
    else
69
69
      table->autoIncrement= 0;
70
70
 
71
 
    if ((row[4]) and (strstr(row[4], "InnoDB free") == NULL))
 
71
    if (row[4])
72
72
      table->comment= DrizzleDumpData::escape(row[4], row_sizes[4]);
73
73
    else
74
74
      table->comment= "";
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 `";
 
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='";
318
303
  query.append(database->databaseName);
319
 
  query.append("`.`");
 
304
  query.append("' and rc.table_name='");
320
305
  query.append(tableName);
321
 
  query.append("`");
 
306
  query.append("' group by rc.constraint_name");
 
307
 
322
308
  result= dcon->query(query);
323
309
 
324
310
  if (result == NULL)
325
311
    return false;
326
312
 
327
 
  if ((row= drizzle_row_next(result)))
 
313
  while ((row= drizzle_row_next(result)))
328
314
  {
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];
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= "";
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;
366
 
    }
 
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] : "";
 
322
 
 
323
    fkeys.push_back(fkey);
367
324
  }
368
325
  dcon->freeResult(result);
369
326
  return true;
375
332
  std::string extra;
376
333
  size_t pos;
377
334
  
378
 
  if (((pos= old_type.find("(")) != std::string::npos) or
379
 
    ((pos= old_type.find(" ")) != std::string::npos))
 
335
  if ((pos= old_type.find("(")) != std::string::npos)
380
336
  {
381
337
    extra= old_type.substr(pos);
382
338
    old_type.erase(pos, std::string::npos);
387
343
    (old_type.find("TEXT") != std::string::npos))
388
344
    setCollate(raw_collation);
389
345
 
390
 
  if ((old_type.compare("BIGINT") == 0) and
391
 
    ((extra.find("unsigned") != std::string::npos)))
392
 
  {
393
 
    rangeCheck= true;
394
 
  }
395
 
 
396
346
  if ((old_type.compare("INT") == 0) and 
397
347
    ((extra.find("unsigned") != std::string::npos)))
398
348
  {
434
384
  if (old_type.compare("BINARY") == 0)
435
385
  {
436
386
    type= "VARBINARY";
437
 
    
438
387
    return;
439
388
  }
440
389
 
452
401
    /* Intended to catch TIME/DATE/TIMESTAMP/DATETIME 
453
402
       We may have a default TIME/DATE which needs converting */
454
403
    convertDateTime= true;
455
 
    isNull= true;
456
404
  }
457
405
 
458
406
  if ((old_type.compare("TIME") == 0) or (old_type.compare("YEAR") == 0))
467
415
    return;
468
416
  }
469
417
 
470
 
  if (old_type.compare("BIT") == 0)
471
 
  {
472
 
    type= "VARBINARY";
473
 
    return;
474
 
  }
475
 
 
476
418
  type= old_type;
477
419
  return;
478
420
}
479
421
 
480
422
void DrizzleDumpTableMySQL::setEngine(const char* newEngine)
481
423
{
482
 
  if ((strcmp(newEngine, "MyISAM") == 0) || (strcmp(newEngine, "MEMORY") == 0))
 
424
  if (strcmp(newEngine, "MyISAM") == 0)
483
425
    engineName= "InnoDB";
484
426
  else
485
427
    engineName= newEngine; 
551
493
 
552
494
  result= dcon->query(query);
553
495
  if (result == NULL)
554
 
    throw std::exception();
 
496
    throw 1;
555
497
}
556
498
 
557
499
DrizzleDumpDataMySQL::~DrizzleDumpDataMySQL()
558
500
{
559
501
  drizzle_result_free(result);
560
 
  delete result;
 
502
  if (result) delete result;
561
503
}
562
504
 
563
505
long DrizzleDumpDataMySQL::convertTime(const char* oldTime) const