~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump_mysql.cc

  • Committer: Brian Aker
  • Date: 2010-10-15 01:23:36 UTC
  • mfrom: (1835.1.7 staging)
  • Revision ID: brian@tangent.org-20101015012336-8w5lox9kj0hkv0a1
MergeĀ inĀ mutable/execute

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= "";
75
75
 
76
76
    table->database= this;
77
 
    if ((not table->populateFields()) or (not table->populateIndexes()) or
78
 
     (not table->populateFkeys()))
 
77
    if ((not table->populateFields()) or (not table->populateIndexes()))
79
78
    {
80
79
      delete table;
81
80
      if (not ignore_errors)
167
166
  if (verbose)
168
167
    std::cerr << _("-- Retrieving fields for ") << tableName << "..." << std::endl;
169
168
 
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='";
 
169
  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
170
  query.append(database->databaseName);
172
171
  query.append("' AND TABLE_NAME='");
173
172
  query.append(tableName);
184
183
    DrizzleDumpFieldMySQL *field = new DrizzleDumpFieldMySQL(fieldName, dcon);
185
184
    /* Stop valgrind warning */
186
185
    field->convertDateTime= false;
187
 
    field->isNull= (strcmp(row[3], "YES") == 0) ? true : false;
188
186
    /* Also sets collation */
189
187
    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))
 
188
    field->isNull= (strcmp(row[3], "YES") == 0) ? true : false;
 
189
    if (row[2])
194
190
    {
195
191
      field->defaultValue= row[2];
196
 
      if (field->convertDateTime)
197
 
      {
198
 
        field->dateTimeConvert();
199
 
      }
200
192
    }
201
193
    else
 
194
     field->defaultValue= "";
 
195
 
 
196
    if (field->convertDateTime)
202
197
    {
203
 
      field->defaultValue= "";
 
198
      field->dateTimeConvert();
204
199
    }
205
200
 
 
201
 
206
202
    field->isAutoIncrement= (strcmp(row[8], "auto_increment") == 0) ? true : false;
207
203
    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
 
 
 
204
    field->length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
223
205
    field->decimalPrecision= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
224
206
    field->decimalScale= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
225
 
    field->comment= (row[9]) ? row[9] : "";
 
207
 
 
208
 
226
209
    fields.push_back(field);
227
210
  }
228
211
 
254
237
    defaultIsNull= true;
255
238
    defaultValue="";
256
239
  }
 
240
  isNull= true;
257
241
}
258
242
 
259
243
 
304
288
  return true;
305
289
}
306
290
 
307
 
bool DrizzleDumpTableMySQL::populateFkeys()
308
 
{
309
 
  drizzle_result_st *result;
310
 
  drizzle_row_t row;
311
 
  std::string query;
312
 
  DrizzleDumpForeignKey *fkey;
313
 
 
314
 
  if (verbose)
315
 
    std::cerr << _("-- Retrieving foreign keys for ") << tableName << "..." << std::endl;
316
 
 
317
 
  query= "SHOW CREATE TABLE `";
318
 
  query.append(database->databaseName);
319
 
  query.append("`.`");
320
 
  query.append(tableName);
321
 
  query.append("`");
322
 
  result= dcon->query(query);
323
 
 
324
 
  if (result == NULL)
325
 
    return false;
326
 
 
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];
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
 
    }
367
 
  }
368
 
  dcon->freeResult(result);
369
 
  return true;
370
 
}
371
 
 
372
291
void DrizzleDumpFieldMySQL::setType(const char* raw_type, const char* raw_collation)
373
292
{
374
293
  std::string old_type(raw_type);
375
294
  std::string extra;
376
295
  size_t pos;
377
296
  
378
 
  if (((pos= old_type.find("(")) != std::string::npos) or
379
 
    ((pos= old_type.find(" ")) != std::string::npos))
 
297
  if ((pos= old_type.find("(")) != std::string::npos)
380
298
  {
381
299
    extra= old_type.substr(pos);
382
300
    old_type.erase(pos, std::string::npos);
387
305
    (old_type.find("TEXT") != std::string::npos))
388
306
    setCollate(raw_collation);
389
307
 
390
 
  if ((old_type.compare("BIGINT") == 0) and
391
 
    ((extra.find("unsigned") != std::string::npos)))
392
 
  {
393
 
    rangeCheck= true;
394
 
  }
395
 
 
396
308
  if ((old_type.compare("INT") == 0) and 
397
309
    ((extra.find("unsigned") != std::string::npos)))
398
310
  {
434
346
  if (old_type.compare("BINARY") == 0)
435
347
  {
436
348
    type= "VARBINARY";
437
 
    
438
349
    return;
439
350
  }
440
351
 
452
363
    /* Intended to catch TIME/DATE/TIMESTAMP/DATETIME 
453
364
       We may have a default TIME/DATE which needs converting */
454
365
    convertDateTime= true;
455
 
    isNull= true;
456
366
  }
457
367
 
458
368
  if ((old_type.compare("TIME") == 0) or (old_type.compare("YEAR") == 0))
467
377
    return;
468
378
  }
469
379
 
470
 
  if (old_type.compare("BIT") == 0)
471
 
  {
472
 
    type= "VARBINARY";
473
 
    return;
474
 
  }
475
 
 
476
380
  type= old_type;
477
381
  return;
478
382
}
479
383
 
480
384
void DrizzleDumpTableMySQL::setEngine(const char* newEngine)
481
385
{
482
 
  if ((strcmp(newEngine, "MyISAM") == 0) || (strcmp(newEngine, "MEMORY") == 0))
 
386
  if (strcmp(newEngine, "MyISAM") == 0)
483
387
    engineName= "InnoDB";
484
388
  else
485
389
    engineName= newEngine; 
551
455
 
552
456
  result= dcon->query(query);
553
457
  if (result == NULL)
554
 
    throw std::exception();
 
458
    throw 1;
555
459
}
556
460
 
557
461
DrizzleDumpDataMySQL::~DrizzleDumpDataMySQL()
558
462
{
559
463
  drizzle_result_free(result);
560
 
  delete result;
 
464
  if (result) delete result;
561
465
}
562
466
 
563
467
long DrizzleDumpDataMySQL::convertTime(const char* oldTime) const