~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump.cc

  • Committer: Andrew Hutchings
  • Date: 2010-09-17 13:09:55 UTC
  • mto: (1792.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1793.
  • Revision ID: andrew@linuxjedi.co.uk-20100917130955-8z3pljxfztgt76uk
Add DOUBLE/DECIMAL and DATE/TIME types with conversions

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
#include <drizzled/error.h>
46
46
#include <boost/program_options.hpp>
47
47
#include <boost/regex.hpp>
48
 
 
 
48
#include <boost/date_time/posix_time/posix_time.hpp>
49
49
#include "drizzledump.h"
50
50
 
51
51
using namespace std;
243
243
  std::string query;
244
244
 
245
245
  if (connected_server_type == SERVER_MYSQL_FOUND)
246
 
    query="SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_DEFAULT, 'NO', IS_NULLABLE, 'NO', CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLLATION_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='";
 
246
    query="SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_DEFAULT, 'NO', IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLLATION_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='";
247
247
  else
248
 
    query= "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT, COLUMN_DEFAULT_IS_NULL, IS_NULLABLE, IS_USED_IN_PRIMARY, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLLATION_NAME FROM DATA_DICTIONARY.COLUMNS WHERE TABLE_SCHEMA='";
 
248
    query= "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT, COLUMN_DEFAULT_IS_NULL, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLLATION_NAME FROM DATA_DICTIONARY.COLUMNS WHERE TABLE_SCHEMA='";
249
249
  query.append(database->databaseName);
250
250
  query.append("' AND TABLE_NAME='");
251
251
  query.append(tableName);
279
279
    std::string fieldName(row[0]);
280
280
    DrizzleDumpField *field = new DrizzleDumpField(fieldName);
281
281
    /* Also sets collation */
282
 
    field->setType(row[1], row[9]);
283
 
    // Default conversion needed
284
 
    field->defaultValue= (row[2]) ? row[2] : "";
 
282
    field->setType(row[1], row[8]);
 
283
    if (row[2])
 
284
    {
 
285
      if (field->convertDateTime)
 
286
      {
 
287
        field->dateTimeConvert(row[2]);
 
288
      }
 
289
      else
 
290
        field->defaultValue= row[2];
 
291
    }
 
292
    else
 
293
     field->defaultValue= "";
285
294
    if (connected_server_type == SERVER_MYSQL_FOUND)
286
295
      field->defaultIsNull= true;
287
296
    else
288
297
      field->defaultIsNull= (strcmp(row[3], "YES") == 0) ? true : false;
289
298
    field->isNull= (strcmp(row[4], "YES") == 0) ? true : false;
290
 
    field->isPrimary= (strcmp(row[5], "YES") == 0) ? true : false;
291
 
    field->length= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
292
 
    field->decimalPrecision= (row[7]) ? boost::lexical_cast<uint32_t>(row[7]) : 0;
293
 
    field->decimalScale= (row[8]) ? boost::lexical_cast<uint32_t>(row[8]) : 0;
 
299
    field->length= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
 
300
    field->decimalPrecision= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
 
301
    field->decimalScale= (row[7]) ? boost::lexical_cast<uint32_t>(row[7]) : 0;
294
302
 
295
303
    fields.push_back(field);
296
304
  }
299
307
  return true;
300
308
}
301
309
 
 
310
void DrizzleDumpField::dateTimeConvert(const char* oldDefault)
 
311
{
 
312
  boost::match_flag_type flags = boost::match_default;
 
313
 
 
314
  if (strcmp(oldDefault, "CURRENT_TIMESTAMP") == 0)
 
315
  {
 
316
    defaultValue= oldDefault;
 
317
    return;
 
318
  }
 
319
 
 
320
  if (type.compare("INT") == 0)
 
321
  {
 
322
    /* We were a TIME, now we are an INT */
 
323
    std::string ts(oldDefault);
 
324
    boost::posix_time::time_duration td(boost::posix_time::duration_from_string(ts));
 
325
    defaultValue= boost::lexical_cast<string>(td.total_seconds());
 
326
    return;
 
327
  }
 
328
 
 
329
  boost::regex date_regex("([0-9]{3}[1-9]-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))");
 
330
 
 
331
  if (regex_search(oldDefault, date_regex, flags))
 
332
  {
 
333
    defaultValue= oldDefault;
 
334
  }
 
335
  else
 
336
  {
 
337
    defaultIsNull= true;
 
338
    defaultValue="";
 
339
  }
 
340
 
 
341
}
 
342
 
302
343
bool DrizzleDumpTable::populateIndexes(drizzle_con_st &connection)
303
344
{
304
345
  drizzle_result_st result;
463
504
      return;
464
505
    }
465
506
 
 
507
    if ((old_type.find("TIME") != string::npos) or
 
508
      (old_type.find("DATE") != string::npos))
 
509
    {
 
510
      /* Intended to catch TIME/DATE/TIMESTAMP/DATETIME 
 
511
         We may have a default TIME/DATE which needs converting */
 
512
      convertDateTime= true;
 
513
    }
 
514
 
466
515
    if (old_type.compare("TIME") == 0)
467
516
    {
468
517
      type= "INT";
469
518
      return;
470
519
    }
 
520
 
 
521
    if (old_type.compare("FLOAT") == 0)
 
522
    {
 
523
      type= "DOUBLE";
 
524
      return;
 
525
    }
 
526
 
471
527
    type= old_type;
472
528
    return;
473
529
  }
567
623
    os << "(" << obj.length << ")";
568
624
  }
569
625
 
 
626
  if ((obj.type.compare("DECIMAL") == 0) or
 
627
    (obj.type.compare("DOUBLE") == 0))
 
628
  {
 
629
    os << "(" << obj.decimalPrecision << "," << obj.decimalScale << ")";
 
630
  }
 
631
 
570
632
  if (obj.type.compare("ENUM") == 0)
571
633
  {
572
 
    //output fields
 
634
    //output fields, currently broken in Drizzle
573
635
  }
574
636
 
575
637
  if (not obj.isNull)