~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/statement_transform.cc

  • Committer: Brian Aker
  • Date: 2010-09-09 21:45:53 UTC
  • mto: (1756.1.2 build) (1768.2.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 1757.
  • Revision ID: brian@tangent.org-20100909214553-e687rmf5zk9478on
Force unique to just use memory and let the OS handle paging.

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);
1322
1301
      else
1323
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:
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
1337
    destination.append(" INT", 4);
1361
1338
    break;
1478
1455
  case Table::Field::DECIMAL:
1479
1456
  case Table::Field::INTEGER:
1480
1457
  case Table::Field::BIGINT:
 
1458
  case Table::Field::ENUM:
1481
1459
    return false;
1482
1460
  default:
1483
1461
    return true;
1510
1488
    return Table::Field::ENUM;
1511
1489
  case DRIZZLE_TYPE_BLOB:
1512
1490
    return Table::Field::BLOB;
1513
 
  case DRIZZLE_TYPE_UUID:
1514
 
    return Table::Field::UUID;
1515
1491
  }
1516
1492
 
1517
1493
  assert(false);