~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/statement_transform.cc

  • Committer: Monty Taylor
  • Date: 2010-08-21 02:55:13 UTC
  • mfrom: (1711.6.12 staging)
  • Revision ID: mordred@inaugust.com-20100821025513-3xyidjf8dodeh4gy
Remove btree heap index.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
#include "config.h"
33
33
 
34
 
#include <boost/lexical_cast.hpp>
35
34
#include "drizzled/message/statement_transform.h"
36
35
#include "drizzled/message/transaction.pb.h"
37
36
#include "drizzled/message/table.pb.h"
52
51
namespace message
53
52
{
54
53
 
55
 
static void escapeEmbeddedQuotes(string &s, const char quote='\'')
56
 
{
57
 
  string::iterator it;
58
 
 
59
 
  for (it= s.begin(); it != s.end(); ++it)
60
 
  {
61
 
    if (*it == quote)
62
 
    {
63
 
      it= s.insert(it, quote);
64
 
      ++it;  // advance back to the quote
65
 
    }
66
 
  }
67
 
}
68
 
 
69
54
/* Incredibly similar to append_unescaped() in table.cc, but for std::string */
70
55
static void append_escaped_string(std::string *res, const std::string &input, const char quote='\'')
71
56
{
127
112
 
128
113
  switch (source.type())
129
114
  {
130
 
  case Statement::ROLLBACK_STATEMENT:
131
 
    {
132
 
      break;
133
 
    }
134
 
  case Statement::ROLLBACK:
135
 
    {
136
 
      sql_strings.push_back("ROLLBACK");
137
 
      break;
138
 
    }
139
115
  case Statement::INSERT:
140
116
    {
141
117
      if (! source.has_insert_header())
413
389
    if (should_quote_field_value)
414
390
      destination.push_back('\'');
415
391
 
416
 
    if (record.is_null(x))
 
392
    if (field_metadata.type() == Table::Field::BLOB)
417
393
    {
418
 
      destination.append("NULL");
 
394
      /* 
 
395
        * We do this here because BLOB data is returned
 
396
        * in a string correctly, but calling append()
 
397
        * without a length will result in only the string
 
398
        * up to a \0 being output here.
 
399
        */
 
400
      string raw_data(record.insert_value(x));
 
401
      destination.append(raw_data.c_str(), raw_data.size());
419
402
    }
420
403
    else
421
404
    {
422
 
      if (field_metadata.type() == Table::Field::BLOB)
423
 
      {
424
 
        /*
425
 
         * We do this here because BLOB data is returned
426
 
         * in a string correctly, but calling append()
427
 
         * without a length will result in only the string
428
 
         * up to a \0 being output here.
429
 
         */
430
 
        string raw_data(record.insert_value(x));
431
 
        destination.append(raw_data.c_str(), raw_data.size());
432
 
      }
433
 
      else
434
 
      {
435
 
        string tmp(record.insert_value(x));
436
 
        escapeEmbeddedQuotes(tmp);
437
 
        destination.append(tmp);
438
 
      }
 
405
      if (record.is_null(x))
 
406
      {
 
407
        destination.append("NULL");
 
408
      }
 
409
      else 
 
410
      {
 
411
        destination.append(record.insert_value(x));
 
412
      } 
439
413
    }
440
414
 
441
415
    if (should_quote_field_value)
498
472
      }
499
473
      else
500
474
      {
501
 
        string tmp(data.record(x).insert_value(y));
502
 
        escapeEmbeddedQuotes(tmp);
503
 
        destination.append(tmp);
 
475
        destination.append(data.record(x).insert_value(y));
504
476
      }
505
477
 
506
478
      if (should_quote_field_value)
576
548
    if (should_quote_field_value)
577
549
      destination.push_back('\'');
578
550
 
579
 
    if (record.is_null(x))
 
551
    if (field_metadata.type() == Table::Field::BLOB)
580
552
    {
581
 
      destination.append("NULL");
 
553
      /* 
 
554
       * We do this here because BLOB data is returned
 
555
       * in a string correctly, but calling append()
 
556
       * without a length will result in only the string
 
557
       * up to a \0 being output here.
 
558
       */
 
559
      string raw_data(record.after_value(x));
 
560
      destination.append(raw_data.c_str(), raw_data.size());
582
561
    }
583
 
    else 
 
562
    else
584
563
    {
585
 
      if (field_metadata.type() == Table::Field::BLOB)
 
564
      if (record.is_null(x))
586
565
      {
587
 
        /*
588
 
         * We do this here because BLOB data is returned
589
 
         * in a string correctly, but calling append()
590
 
         * without a length will result in only the string
591
 
         * up to a \0 being output here.
592
 
         */
593
 
        string raw_data(record.after_value(x));
594
 
        destination.append(raw_data.c_str(), raw_data.size());
 
566
        destination.append("NULL");
595
567
      }
596
 
      else 
 
568
      else
597
569
      {
598
 
        string tmp(record.after_value(x));
599
 
        escapeEmbeddedQuotes(tmp);
600
 
        destination.append(tmp);
 
570
        destination.append(record.after_value(x));
601
571
      }
602
572
    }
603
573
 
720
690
    }
721
691
    else
722
692
    {
723
 
      string tmp(record.key_value(x));
724
 
      escapeEmbeddedQuotes(tmp);
725
 
      destination.append(tmp);
 
693
      destination.append(record.key_value(x));
726
694
    }
727
695
 
728
696
    if (should_quote_field_value)
791
759
      }
792
760
      else
793
761
      {
794
 
        string tmp(data.record(x).key_value(y));
795
 
        escapeEmbeddedQuotes(tmp);
796
 
        destination.append(tmp);
 
762
        destination.append(data.record(x).key_value(y));
797
763
      }
798
764
 
799
765
      if (should_quote_field_value)
1046
1012
  if (sql_variant == ANSI)
1047
1013
    return NONE; /* ANSI does not support table options... */
1048
1014
 
 
1015
  stringstream ss;
 
1016
 
1049
1017
  if (options.has_comment())
1050
1018
  {
1051
1019
    destination.append(" COMMENT=", 9);
1074
1042
 
1075
1043
  if (options.has_max_rows())
1076
1044
  {
 
1045
    ss << options.max_rows();
1077
1046
    destination.append("\nMAX_ROWS = ", 12);
1078
 
    destination.append(boost::lexical_cast<string>(options.max_rows()));
 
1047
    destination.append(ss.str());
 
1048
    ss.clear();
1079
1049
  }
1080
1050
 
1081
1051
  if (options.has_min_rows())
1082
1052
  {
 
1053
    ss << options.min_rows();
1083
1054
    destination.append("\nMIN_ROWS = ", 12);
1084
 
    destination.append(boost::lexical_cast<string>(options.min_rows()));
 
1055
    destination.append(ss.str());
 
1056
    ss.clear();
1085
1057
  }
1086
1058
 
1087
1059
  if (options.has_user_set_auto_increment_value()
1088
1060
      && options.has_auto_increment_value())
1089
1061
  {
 
1062
    ss << options.auto_increment_value();
1090
1063
    destination.append(" AUTO_INCREMENT=", 16);
1091
 
    destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
 
1064
    destination.append(ss.str());
 
1065
    ss.clear();
1092
1066
  }
1093
1067
 
1094
1068
  if (options.has_avg_row_length())
1095
1069
  {
 
1070
    ss << options.avg_row_length();
1096
1071
    destination.append("\nAVG_ROW_LENGTH = ", 18);
1097
 
    destination.append(boost::lexical_cast<string>(options.avg_row_length()));
 
1072
    destination.append(ss.str());
 
1073
    ss.clear();
1098
1074
  }
1099
1075
 
1100
1076
  if (options.has_checksum() &&
1160
1136
      {
1161
1137
        if (part.compare_length() != field.string_options().length())
1162
1138
        {
 
1139
          stringstream ss;
1163
1140
          destination.push_back('(');
1164
 
          destination.append(boost::lexical_cast<string>(part.compare_length()));
 
1141
          ss << part.compare_length();
 
1142
          destination.append(ss.str());
1165
1143
          destination.push_back(')');
1166
1144
        }
1167
1145
      }
1200
1178
{
1201
1179
  switch (opt)
1202
1180
  {
 
1181
  case Table::ForeignKeyConstraint::OPTION_UNDEF:
 
1182
    break;
1203
1183
  case Table::ForeignKeyConstraint::OPTION_RESTRICT:
1204
1184
    destination.append("RESTRICT");
1205
1185
    break;
1209
1189
  case Table::ForeignKeyConstraint::OPTION_SET_NULL:
1210
1190
    destination.append("SET NULL");
1211
1191
    break;
1212
 
  case Table::ForeignKeyConstraint::OPTION_UNDEF:
1213
1192
  case Table::ForeignKeyConstraint::OPTION_NO_ACTION:
1214
1193
    destination.append("NO ACTION");
1215
1194
    break;
1216
 
  case Table::ForeignKeyConstraint::OPTION_SET_DEFAULT:
 
1195
  case Table::ForeignKeyConstraint::OPTION_DEFAULT:
1217
1196
    destination.append("SET DEFAULT");
1218
1197
    break;
1219
1198
  }
1266
1245
 
1267
1246
  destination.push_back(')');
1268
1247
 
1269
 
  if (fkey.has_update_option() and fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
 
1248
  if (fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1270
1249
  {
1271
1250
    destination.append(" ON UPDATE ", 11);
1272
1251
    transformForeignKeyOptionToSql(fkey.update_option(), destination);
1273
1252
  }
1274
1253
 
1275
 
  if (fkey.has_delete_option() and fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
 
1254
  if (fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1276
1255
  {
1277
1256
    destination.append(" ON DELETE ", 11);
1278
1257
    transformForeignKeyOptionToSql(fkey.delete_option(), destination);
1304
1283
  switch (field_type)
1305
1284
  {
1306
1285
    case Table::Field::DOUBLE:
1307
 
    destination.append(" DOUBLE", 7);
 
1286
    destination.append(" double", 7);
1308
1287
    if (field.has_numeric_options()
1309
1288
        && field.numeric_options().has_precision())
1310
1289
    {
1318
1297
    {
1319
1298
      if (field.string_options().has_collation()
1320
1299
          && field.string_options().collation().compare("binary") == 0)
1321
 
        destination.append(" VARBINARY(", 11);
 
1300
        destination.append(" varbinary(", 11);
1322
1301
      else
1323
 
        destination.append(" VARCHAR(", 9);
 
1302
        destination.append(" varchar(", 9);
1324
1303
 
1325
 
      destination.append(boost::lexical_cast<string>(field.string_options().length()));
1326
 
      destination.append(")");
 
1304
      stringstream ss;
 
1305
      ss << field.string_options().length() << ")";
 
1306
      destination.append(ss.str());
1327
1307
    }
1328
1308
    break;
1329
1309
  case Table::Field::BLOB:
1330
1310
    {
1331
1311
      if (field.string_options().has_collation()
1332
1312
          && field.string_options().collation().compare("binary") == 0)
1333
 
        destination.append(" BLOB", 5);
 
1313
        destination.append(" blob", 5);
1334
1314
      else
1335
 
        destination.append(" TEXT", 5);
 
1315
        destination.append(" text", 5);
1336
1316
    }
1337
1317
    break;
1338
1318
  case Table::Field::ENUM:
1339
1319
    {
1340
1320
      size_t num_field_values= field.enumeration_values().field_value_size();
1341
 
      destination.append(" ENUM(", 6);
 
1321
      destination.append(" enum(", 6);
1342
1322
      for (size_t x= 0; x < num_field_values; ++x)
1343
1323
      {
1344
1324
        const string &type= field.enumeration_values().field_value(x);
1353
1333
      destination.push_back(')');
1354
1334
      break;
1355
1335
    }
1356
 
  case Table::Field::UUID:
1357
 
    destination.append(" UUID", 5);
1358
 
    break;
1359
1336
  case Table::Field::INTEGER:
1360
 
    destination.append(" INT", 4);
 
1337
    destination.append(" int", 4);
1361
1338
    break;
1362
1339
  case Table::Field::BIGINT:
1363
 
    destination.append(" BIGINT", 7);
 
1340
    destination.append(" bigint", 7);
1364
1341
    break;
1365
1342
  case Table::Field::DECIMAL:
1366
1343
    {
1367
 
      destination.append(" DECIMAL(", 9);
 
1344
      destination.append(" decimal(", 9);
1368
1345
      stringstream ss;
1369
1346
      ss << field.numeric_options().precision() << ",";
1370
1347
      ss << field.numeric_options().scale() << ")";
1372
1349
    }
1373
1350
    break;
1374
1351
  case Table::Field::DATE:
1375
 
    destination.append(" DATE", 5);
 
1352
    destination.append(" date", 5);
1376
1353
    break;
1377
1354
  case Table::Field::TIMESTAMP:
1378
 
    destination.append(" TIMESTAMP",  10);
 
1355
    destination.append(" timestamp",  10);
1379
1356
    break;
1380
1357
  case Table::Field::DATETIME:
1381
 
    destination.append(" DATETIME",  9);
 
1358
    destination.append(" datetime",  9);
1382
1359
    break;
1383
1360
  }
1384
1361
 
1397
1374
      field.type() == Table::Field::VARCHAR)
1398
1375
  {
1399
1376
    if (field.string_options().has_collation()
1400
 
        && field.string_options().collation().compare("binary"))
 
1377
        && (field.string_options().collation().compare("binary")
 
1378
            && field.string_options().collation().compare("utf8_general_ci")))
1401
1379
    {
1402
1380
      destination.append(" COLLATE ", 9);
1403
1381
      destination.append(field.string_options().collation());
1478
1456
  case Table::Field::DECIMAL:
1479
1457
  case Table::Field::INTEGER:
1480
1458
  case Table::Field::BIGINT:
 
1459
  case Table::Field::ENUM:
1481
1460
    return false;
1482
1461
  default:
1483
1462
    return true;
1510
1489
    return Table::Field::ENUM;
1511
1490
  case DRIZZLE_TYPE_BLOB:
1512
1491
    return Table::Field::BLOB;
1513
 
  case DRIZZLE_TYPE_UUID:
1514
 
    return Table::Field::UUID;
1515
1492
  }
1516
1493
 
1517
1494
  assert(false);