~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump_data.cc

  • Committer: David Shrewsbury
  • Date: 2010-10-11 17:20:14 UTC
  • mfrom: (1838 trunk-drizzle)
  • mto: (1838.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1839.
  • Revision ID: shrewsbury.dave@gmail.com-20101011172014-sij1m2doi2fl2duo
Merge from trunk and resolve conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
extern bool opt_drop_database;
44
44
extern bool opt_autocommit;
45
45
extern bool ignore_errors;
46
 
extern std::string opt_destination_database;
47
46
 
48
47
extern boost::unordered_set<std::string> ignore_table;
49
48
extern void maybe_exit(int error);
80
79
 
81
80
}
82
81
 
83
 
std::ostream& operator <<(std::ostream &os, const DrizzleDumpForeignKey &obj)
84
 
{
85
 
  os << "  CONSTRAINT `" << obj.constraintName << "` FOREIGN KEY ("
86
 
    << obj.parentColumns << ") REFERENCES `" << obj.childTable << "` ("
87
 
    << obj.childColumns << ")";
88
 
 
89
 
  if (not obj.deleteRule.empty())
90
 
    os << " ON DELETE " << obj.deleteRule;
91
 
 
92
 
  if (not obj.updateRule.empty())
93
 
    os << " ON UPDATE " << obj.updateRule;
94
 
 
95
 
  return os;
96
 
}
97
 
 
98
82
std::ostream& operator <<(std::ostream &os, const DrizzleDumpIndex &obj)
99
83
{
100
84
  if (obj.isPrimary)
166
150
  if (not obj.defaultValue.empty())
167
151
  {
168
152
    if (obj.defaultValue.compare("CURRENT_TIMESTAMP") != 0)
169
 
    {
170
 
      if (obj.defaultValue.compare(0, 2, "b'") == 0)
171
 
      {
172
 
        os << " DEFAULT " << obj.defaultValue;
173
 
      }
174
 
      else
175
 
      {
176
 
        os << " DEFAULT '" << obj.defaultValue << "'";
177
 
      }
178
 
    }
 
153
     os << " DEFAULT '" << obj.defaultValue << "'";
179
154
    else
180
 
    {
181
155
     os << " DEFAULT CURRENT_TIMESTAMP";
182
 
    }
183
156
  }
184
157
  else if ((obj.defaultIsNull))
185
158
  {
186
159
    os << " DEFAULT NULL";
187
160
  }
188
161
 
189
 
  if (not obj.comment.empty())
190
 
  {
191
 
    os << " COMMENT '" << DrizzleDumpData::escape(obj.comment.c_str(), obj.comment.length()) << "'";
192
 
  }
193
 
 
194
162
  return os;
195
163
}
196
164
 
209
177
    if (not opt_create_db)
210
178
    {
211
179
      if (opt_drop_database)
212
 
      {
213
 
        os << "DROP DATABASE IF EXISTS `"
214
 
          << ((opt_destination_database.empty()) ? obj.databaseName
215
 
          : opt_destination_database) << "`" << std::endl;
216
 
      }
 
180
        os << "DROP DATABASE IF EXISTS `" << obj.databaseName << "`" << std::endl;
217
181
 
218
 
      os << "CREATE DATABASE IF NOT EXISTS `"
219
 
        << ((opt_destination_database.empty()) ? obj.databaseName
220
 
        : opt_destination_database) << "`";
 
182
      os << "CREATE DATABASE IF NOT EXISTS `" << obj.databaseName << "`";
221
183
      if (not obj.collate.empty())
222
184
       os << " COLLATE = " << obj.collate;
223
185
 
224
186
      os << ";" << std::endl << std::endl;
225
187
    }
226
 
    os << "USE `" << ((opt_destination_database.empty()) ? obj.databaseName
227
 
      : opt_destination_database) << "`;" << std::endl << std::endl;
 
188
 
 
189
    os << "USE `" << obj.databaseName << "`;" << std::endl << std::endl;
228
190
  }
229
191
 
230
192
  std::vector<DrizzleDumpTable*>::iterator i;
260
222
  bool new_insert= true;
261
223
  bool first= true;
262
224
  uint64_t rownr= 0;
263
 
  size_t byte_counter= 0;
264
225
 
265
226
  drizzle_row_t row;
266
227
 
295
256
  while((row= drizzle_row_next(obj.result)))
296
257
  {
297
258
    rownr++;
298
 
    if (verbose and (rownr % show_progress_size) == 0)
 
259
    if ((rownr % show_progress_size) == 0)
299
260
    {
300
261
      std::cerr << "-- " << rownr << _(" rows dumped for table ") << obj.table->displayName << std::endl;
301
262
    }
302
263
 
303
264
    size_t* row_sizes= drizzle_row_field_sizes(obj.result);
304
 
    for (uint32_t i= 0; i < drizzle_result_column_count(obj.result); i++)
305
 
      byte_counter+= row_sizes[i];
306
 
 
307
 
    if (not first and not new_insert)
 
265
    if (not first)
308
266
    {
309
267
      if (extended_insert)
310
268
        os << "),(";
311
269
      else
312
270
        os << ");" << std::endl;
313
 
      byte_counter+= 3;
314
271
    }
315
272
    else
316
273
      first= false;
326
283
          os << "IGNORE ";
327
284
      }
328
285
      os << "INTO `" << obj.table->displayName << "` VALUES (";
329
 
      byte_counter+= 28 + obj.table->displayName.length();
330
286
      if (extended_insert)
331
287
        new_insert= false;
332
288
    }
335
291
      if (not row[i])
336
292
      {
337
293
        os << "NULL";
338
 
        if (i != obj.table->fields.size() - 1)
339
 
          os << ",";
340
 
        continue;
341
 
      }
342
 
 
343
 
      if ((obj.table->fields[i]->rangeCheck) and
344
 
        (obj.table->fields[i]->type.compare("BIGINT") == 0) and
345
 
        (boost::lexical_cast<uint64_t>(row[i]) > INT64_MAX))
346
 
      {
347
 
        std::cerr << "Error: Data for column " << obj.table->fields[i]->fieldName << " is greater than max BIGINT, cannot migrate automatically" << std::endl;
348
 
        if (not ignore_errors)
349
 
          maybe_exit(EX_DRIZZLEERR);
350
 
        else
351
 
          continue;
352
 
      }
353
 
 
 
294
      }
354
295
      /* time/date conversion for MySQL connections */
355
296
      else if (obj.table->fields[i]->convertDateTime)
356
297
      {
358
299
      }
359
300
      else
360
301
      {
361
 
        if ((obj.table->fields[i]->type.compare("INT") != 0) and
362
 
          (obj.table->fields[i]->type.compare("BIGINT") != 0))
 
302
        if (obj.table->fields[i]->type.compare("INT") != 0)
363
303
        {
364
304
          /* Hex blob processing or escape text */
365
305
          if (((obj.table->fields[i]->type.compare("BLOB") == 0) or
366
306
            (obj.table->fields[i]->type.compare("VARBINARY") == 0)))
367
 
          {
368
307
            os << obj.convertHex((unsigned char*)row[i], row_sizes[i]);
369
 
            byte_counter+= row_sizes[i];
370
 
          }
371
 
          else if ((obj.table->fields[i]->type.compare("ENUM") == 0) and
372
 
            (strcmp(row[i], "") == 0))
373
 
          {
374
 
            os << "NULL";
375
 
          }
376
308
          else
377
309
            os << "'" << DrizzleDumpData::escape(row[i], row_sizes[i]) << "'";
378
 
          byte_counter+= 3;
379
310
        }
380
311
        else
381
312
          os << row[i];
384
315
        os << ",";
385
316
    }
386
317
    /* Break insert up if it is too long */
387
 
    if ((extended_insert and
388
 
      (byte_counter >= DRIZZLE_MAX_LINE_LENGTH)) or (not extended_insert))
 
318
    if (extended_insert and
 
319
      ((os.tellp() - out_position) >= DRIZZLE_MAX_LINE_LENGTH))
389
320
    {
390
321
      os << ");" << std::endl;
391
322
      new_insert= true;
392
 
      byte_counter= 0;
 
323
      out_position= os.tellp();
393
324
    }
394
325
  }
395
 
  if (not new_insert)
396
 
    os << ");" << std::endl;
 
326
  os << ");" << std::endl;
397
327
 
398
328
  if (opt_autocommit)
399
329
    os << "COMMIT;" << std::endl;
411
341
  std::ostringstream output;
412
342
  if (from_size > 0)
413
343
    output << "0x";
414
 
  else
415
 
    output << "''";
416
 
 
417
344
  while (from_size > 0)
418
345
  {
419
346
    /* Would be nice if std::hex liked uint8_t, ah well */
437
364
      switch (*from)
438
365
      {
439
366
         case 0:
440
 
           output.append("\\0");
441
 
           break;
442
367
         case '\n':
443
 
           output.append("\\n");
444
 
           break;
445
368
         case '\r':
446
 
           output.append("\\r");
447
 
           break;
448
369
         case '\\':
449
 
           output.append("\\\\");
450
 
           break;
451
370
         case '\'':
452
 
           output.append("\\'");
453
 
           break;
454
371
         case '"':
455
 
           output.append("\\\"");
456
 
           break;
457
372
         case '\032':
458
 
           output.append("\\Z");
459
 
           break;
 
373
           output.push_back('\\');
460
374
         default:
461
 
           output.push_back(*from);
462
375
           break;
463
376
       }
464
377
    }
465
 
    else
466
 
      output.push_back(*from);
 
378
    output.push_back(*from);
467
379
    (void) *from++;
468
380
    from_size--;
469
381
  }
498
410
  std::vector<DrizzleDumpIndex*> output_indexes = obj.indexes;
499
411
  for (j= output_indexes.begin(); j != output_indexes.end(); ++j)
500
412
  {
501
 
    os << "," << std::endl;
 
413
    os << "," << std::endl;;
502
414
    DrizzleDumpIndex *index= *j;
503
415
    os << *index;
504
416
  }
505
 
 
506
 
  std::vector<DrizzleDumpForeignKey*>::iterator k;
507
 
  std::vector<DrizzleDumpForeignKey*> output_fkeys = obj.fkeys;
508
 
  for (k= output_fkeys.begin(); k != output_fkeys.end(); ++k)
509
 
  {
510
 
    os << "," << std::endl;
511
 
    DrizzleDumpForeignKey *fkey= *k;
512
 
    os << *fkey;
513
 
  }
514
 
 
515
417
  os << std::endl;
516
 
  os << ") ENGINE='" << obj.engineName << "' ";
 
418
  os << ") ENGINE=" << obj.engineName << " ";
517
419
  if (obj.autoIncrement > 0)
518
420
  {
519
421
    os << "AUTO_INCREMENT=" << obj.autoIncrement << " ";
520
422
  }
521
423
 
522
 
  os << "COLLATE='" << obj.collate << "'";
 
424
  os << "COLLATE = " << obj.collate;
523
425
 
524
426
  if (not obj.comment.empty())
525
427
  {
526
 
    os << " COMMENT='" << obj.comment << "'";
 
428
    os << " COMMENT = '" << obj.comment << "'";
527
429
  }
528
430
 
529
431
  os << ";" << std::endl << std::endl;
558
460
  if (ret != DRIZZLE_RETURN_OK)
559
461
  {
560
462
    errorHandler(NULL, ret, "when trying to connect");
561
 
    throw std::exception();
 
463
    throw 1;
562
464
  }
563
465
 
564
466
  boost::match_flag_type flags = boost::match_default;