~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/statement_transform.cc

  • Committer: Patrick Crews
  • Date: 2010-09-14 20:21:03 UTC
  • mto: (1771.1.1 pcrews)
  • mto: This revision was merged to the branch mainline in revision 1772.
  • Revision ID: gleebix@gmail.com-20100914202103-1db2n0bshzafep19
Moved transaction_log tests into updated non-publisher-based tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
5
 
 *  Copyright (C) 2010 Jay Pipes
 
4
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *  Copyright (c) 2010 Jay Pipes
6
6
 *
7
7
 *  Authors:
8
8
 *
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())
318
294
      sql_strings.push_back(destination);
319
295
    }
320
296
    break;
321
 
  case Statement::ALTER_SCHEMA:
322
 
    {
323
 
      assert(source.has_alter_schema_statement());
324
 
      string destination;
325
 
      error= transformAlterSchemaStatementToSql(source.alter_schema_statement(),
326
 
                                                destination,
327
 
                                                sql_variant);
328
 
      sql_strings.push_back(destination);
329
 
    }
330
 
    break;
331
297
  case Statement::SET_VARIABLE:
332
298
    {
333
299
      assert(source.has_set_variable_statement());
423
389
    if (should_quote_field_value)
424
390
      destination.push_back('\'');
425
391
 
426
 
    if (record.is_null(x))
 
392
    if (field_metadata.type() == Table::Field::BLOB)
427
393
    {
428
 
      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());
429
402
    }
430
403
    else
431
404
    {
432
 
      if (field_metadata.type() == Table::Field::BLOB)
433
 
      {
434
 
        /*
435
 
         * We do this here because BLOB data is returned
436
 
         * in a string correctly, but calling append()
437
 
         * without a length will result in only the string
438
 
         * up to a \0 being output here.
439
 
         */
440
 
        string raw_data(record.insert_value(x));
441
 
        destination.append(raw_data.c_str(), raw_data.size());
442
 
      }
443
 
      else
444
 
      {
445
 
        string tmp(record.insert_value(x));
446
 
        escapeEmbeddedQuotes(tmp);
447
 
        destination.append(tmp);
448
 
      }
 
405
      if (record.is_null(x))
 
406
      {
 
407
        destination.append("NULL");
 
408
      }
 
409
      else 
 
410
      {
 
411
        destination.append(record.insert_value(x));
 
412
      } 
449
413
    }
450
414
 
451
415
    if (should_quote_field_value)
508
472
      }
509
473
      else
510
474
      {
511
 
        string tmp(data.record(x).insert_value(y));
512
 
        escapeEmbeddedQuotes(tmp);
513
 
        destination.append(tmp);
 
475
        destination.append(data.record(x).insert_value(y));
514
476
      }
515
477
 
516
478
      if (should_quote_field_value)
586
548
    if (should_quote_field_value)
587
549
      destination.push_back('\'');
588
550
 
589
 
    if (record.is_null(x))
 
551
    if (field_metadata.type() == Table::Field::BLOB)
590
552
    {
591
 
      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());
592
561
    }
593
 
    else 
 
562
    else
594
563
    {
595
 
      if (field_metadata.type() == Table::Field::BLOB)
 
564
      if (record.is_null(x))
596
565
      {
597
 
        /*
598
 
         * We do this here because BLOB data is returned
599
 
         * in a string correctly, but calling append()
600
 
         * without a length will result in only the string
601
 
         * up to a \0 being output here.
602
 
         */
603
 
        string raw_data(record.after_value(x));
604
 
        destination.append(raw_data.c_str(), raw_data.size());
 
566
        destination.append("NULL");
605
567
      }
606
 
      else 
 
568
      else
607
569
      {
608
 
        string tmp(record.after_value(x));
609
 
        escapeEmbeddedQuotes(tmp);
610
 
        destination.append(tmp);
 
570
        destination.append(record.after_value(x));
611
571
      }
612
572
    }
613
573
 
730
690
    }
731
691
    else
732
692
    {
733
 
      string tmp(record.key_value(x));
734
 
      escapeEmbeddedQuotes(tmp);
735
 
      destination.append(tmp);
 
693
      destination.append(record.key_value(x));
736
694
    }
737
695
 
738
696
    if (should_quote_field_value)
801
759
      }
802
760
      else
803
761
      {
804
 
        string tmp(data.record(x).key_value(y));
805
 
        escapeEmbeddedQuotes(tmp);
806
 
        destination.append(tmp);
 
762
        destination.append(data.record(x).key_value(y));
807
763
      }
808
764
 
809
765
      if (should_quote_field_value)
816
772
}
817
773
 
818
774
enum TransformSqlError
819
 
transformAlterSchemaStatementToSql(const AlterSchemaStatement &statement,
820
 
                                   string &destination,
821
 
                                   enum TransformSqlVariant sql_variant)
822
 
{
823
 
  const Schema &before= statement.before();
824
 
  const Schema &after= statement.after();
825
 
 
826
 
  /* Make sure we are given the before and after for the same object */
827
 
  if (before.uuid() != after.uuid())
828
 
    return UUID_MISMATCH;
829
 
 
830
 
  char quoted_identifier= '`';
831
 
  if (sql_variant == ANSI)
832
 
    quoted_identifier= '"';
833
 
 
834
 
  destination.append("ALTER SCHEMA ");
835
 
  destination.push_back(quoted_identifier);
836
 
  destination.append(before.name());
837
 
  destination.push_back(quoted_identifier);
838
 
 
839
 
  /*
840
 
   * Diff our schemas. Currently, only collation can change so a
841
 
   * diff of the two structures is not really necessary.
842
 
   */
843
 
  destination.append(" COLLATE = ");
844
 
  destination.append(after.collation());
845
 
 
846
 
  return NONE;
847
 
}
848
 
 
849
 
enum TransformSqlError
850
775
transformDropSchemaStatementToSql(const DropSchemaStatement &statement,
851
776
                                  string &destination,
852
777
                                  enum TransformSqlVariant sql_variant)
874
799
 
875
800
  const Schema &schema= statement.schema();
876
801
 
877
 
  destination.append("CREATE SCHEMA ");
 
802
  destination.append("CREATE SCHEMA ", 14);
878
803
  destination.push_back(quoted_identifier);
879
804
  destination.append(schema.name());
880
805
  destination.push_back(quoted_identifier);
881
806
 
882
807
  if (schema.has_collation())
883
808
  {
884
 
    destination.append(" COLLATE ");
 
809
    destination.append(" COLLATE ", 9);
885
810
    destination.append(schema.collation());
886
811
  }
887
812
 
899
824
 
900
825
  const TableMetadata &table_metadata= statement.table_metadata();
901
826
 
902
 
  destination.append("DROP TABLE ");
 
827
  destination.append("DROP TABLE ", 11);
903
828
 
904
829
  /* Add the IF EXISTS clause if necessary */
905
830
  if (statement.has_if_exists_clause() &&
906
831
      statement.if_exists_clause() == true)
907
832
  {
908
 
    destination.append("IF EXISTS ");
 
833
    destination.append("IF EXISTS ", 10);
909
834
  }
910
835
 
911
836
  destination.push_back(quoted_identifier);
930
855
 
931
856
  const TableMetadata &table_metadata= statement.table_metadata();
932
857
 
933
 
  destination.append("TRUNCATE TABLE ");
 
858
  destination.append("TRUNCATE TABLE ", 15);
934
859
  destination.push_back(quoted_identifier);
935
860
  destination.append(table_metadata.schema_name());
936
861
  destination.push_back(quoted_identifier);
951
876
  const FieldMetadata &variable_metadata= statement.variable_metadata();
952
877
  bool should_quote_field_value= shouldQuoteFieldValue(variable_metadata.type());
953
878
 
954
 
  destination.append("SET GLOBAL "); /* Only global variables are replicated */
 
879
  destination.append("SET GLOBAL ", 11); /* Only global variables are replicated */
955
880
  destination.append(variable_metadata.name());
956
881
  destination.push_back('=');
957
882
 
983
908
  if (sql_variant == ANSI)
984
909
    quoted_identifier= '"';
985
910
 
986
 
  destination.append("CREATE ");
 
911
  destination.append("CREATE ", 7);
987
912
 
988
913
  if (table.type() == Table::TEMPORARY)
989
 
    destination.append("TEMPORARY ");
 
914
    destination.append("TEMPORARY ", 10);
990
915
  
991
 
  destination.append("TABLE ");
 
916
  destination.append("TABLE ", 6);
992
917
  if (with_schema)
993
918
  {
994
919
    append_escaped_string(&destination, table.schema(), quoted_identifier);
995
920
    destination.push_back('.');
996
921
  }
997
922
  append_escaped_string(&destination, table.name(), quoted_identifier);
998
 
  destination.append(" (\n");
 
923
  destination.append(" (\n", 3);
999
924
 
1000
925
  enum TransformSqlError result= NONE;
1001
926
  size_t num_fields= table.field_size();
1004
929
    const Table::Field &field= table.field(x);
1005
930
 
1006
931
    if (x != 0)
1007
 
      destination.append(",\n");
 
932
      destination.append(",\n", 2);
1008
933
 
1009
934
    destination.append("  ");
1010
935
 
1017
942
  size_t num_indexes= table.indexes_size();
1018
943
  
1019
944
  if (num_indexes > 0)
1020
 
    destination.append(",\n");
 
945
    destination.append(",\n", 2);
1021
946
 
1022
947
  for (size_t x= 0; x < num_indexes; ++x)
1023
948
  {
1024
949
    const message::Table::Index &index= table.indexes(x);
1025
950
 
1026
951
    if (x != 0)
1027
 
      destination.append(",\n");
 
952
      destination.append(",\n", 2);
1028
953
 
1029
954
    result= transformIndexDefinitionToSql(index, table, destination, sql_variant);
1030
955
    
1035
960
  size_t num_foreign_keys= table.fk_constraint_size();
1036
961
 
1037
962
  if (num_foreign_keys > 0)
1038
 
    destination.append(",\n");
 
963
    destination.append(",\n", 2);
1039
964
 
1040
965
  for (size_t x= 0; x < num_foreign_keys; ++x)
1041
966
  {
1042
967
    const message::Table::ForeignKeyConstraint &fkey= table.fk_constraint(x);
1043
968
 
1044
969
    if (x != 0)
1045
 
      destination.append(",\n");
 
970
      destination.append(",\n", 2);
1046
971
 
1047
972
    result= transformForeignKeyConstraintDefinitionToSql(fkey, table, destination, sql_variant);
1048
973
 
1050
975
      return result;
1051
976
  }
1052
977
 
1053
 
  destination.append("\n)");
 
978
  destination.append("\n)", 2);
1054
979
 
1055
980
  /* Add ENGINE = " clause */
1056
981
  if (table.has_engine())
1057
982
  {
1058
 
    destination.append(" ENGINE=");
 
983
    destination.append(" ENGINE=", 8);
1059
984
    destination.append(table.engine().name());
1060
985
 
1061
986
    size_t num_engine_options= table.engine().options_size();
1065
990
    {
1066
991
      const Engine::Option &option= table.engine().options(x);
1067
992
      destination.append(option.name());
1068
 
      destination.append("='");
 
993
      destination.append("='", 2);
1069
994
      destination.append(option.state());
1070
 
      destination.append("'");
1071
 
      if (x != num_engine_options-1)
1072
 
      {
1073
 
        destination.append(", ");
1074
 
      }
 
995
      destination.append("'", 1);
 
996
      if(x != num_engine_options-1)
 
997
        destination.append(", ", 2);
1075
998
    }
1076
999
  }
1077
1000
 
1089
1012
  if (sql_variant == ANSI)
1090
1013
    return NONE; /* ANSI does not support table options... */
1091
1014
 
 
1015
  stringstream ss;
 
1016
 
1092
1017
  if (options.has_comment())
1093
1018
  {
1094
 
    destination.append(" COMMENT=");
 
1019
    destination.append(" COMMENT=", 9);
1095
1020
    append_escaped_string(&destination, options.comment());
1096
1021
  }
1097
1022
 
1098
1023
  if (options.has_collation())
1099
1024
  {
1100
 
    destination.append(" COLLATE = ");
 
1025
    destination.append(" COLLATE = ", 11);
1101
1026
    destination.append(options.collation());
1102
1027
  }
1103
1028
 
1104
1029
  if (options.has_data_file_name())
1105
1030
  {
1106
 
    destination.append("\nDATA_FILE_NAME = '");
 
1031
    destination.append("\nDATA_FILE_NAME = '", 19);
1107
1032
    destination.append(options.data_file_name());
1108
1033
    destination.push_back('\'');
1109
1034
  }
1110
1035
 
1111
1036
  if (options.has_index_file_name())
1112
1037
  {
1113
 
    destination.append("\nINDEX_FILE_NAME = '");
 
1038
    destination.append("\nINDEX_FILE_NAME = '", 20);
1114
1039
    destination.append(options.index_file_name());
1115
1040
    destination.push_back('\'');
1116
1041
  }
1117
1042
 
1118
1043
  if (options.has_max_rows())
1119
1044
  {
1120
 
    destination.append("\nMAX_ROWS = ");
1121
 
    destination.append(boost::lexical_cast<string>(options.max_rows()));
 
1045
    ss << options.max_rows();
 
1046
    destination.append("\nMAX_ROWS = ", 12);
 
1047
    destination.append(ss.str());
 
1048
    ss.clear();
1122
1049
  }
1123
1050
 
1124
1051
  if (options.has_min_rows())
1125
1052
  {
1126
 
    destination.append("\nMIN_ROWS = ");
1127
 
    destination.append(boost::lexical_cast<string>(options.min_rows()));
 
1053
    ss << options.min_rows();
 
1054
    destination.append("\nMIN_ROWS = ", 12);
 
1055
    destination.append(ss.str());
 
1056
    ss.clear();
1128
1057
  }
1129
1058
 
1130
1059
  if (options.has_user_set_auto_increment_value()
1131
1060
      && options.has_auto_increment_value())
1132
1061
  {
1133
 
    destination.append(" AUTO_INCREMENT=");
1134
 
    destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
 
1062
    ss << options.auto_increment_value();
 
1063
    destination.append(" AUTO_INCREMENT=", 16);
 
1064
    destination.append(ss.str());
 
1065
    ss.clear();
1135
1066
  }
1136
1067
 
1137
1068
  if (options.has_avg_row_length())
1138
1069
  {
1139
 
    destination.append("\nAVG_ROW_LENGTH = ");
1140
 
    destination.append(boost::lexical_cast<string>(options.avg_row_length()));
 
1070
    ss << options.avg_row_length();
 
1071
    destination.append("\nAVG_ROW_LENGTH = ", 18);
 
1072
    destination.append(ss.str());
 
1073
    ss.clear();
1141
1074
  }
1142
1075
 
1143
1076
  if (options.has_checksum() &&
1144
1077
      options.checksum())
1145
 
    destination.append("\nCHECKSUM = TRUE");
 
1078
    destination.append("\nCHECKSUM = TRUE", 16);
1146
1079
  if (options.has_page_checksum() &&
1147
1080
      options.page_checksum())
1148
 
    destination.append("\nPAGE_CHECKSUM = TRUE");
 
1081
    destination.append("\nPAGE_CHECKSUM = TRUE", 21);
1149
1082
 
1150
1083
  return NONE;
1151
1084
}
1163
1096
  destination.append("  ", 2);
1164
1097
 
1165
1098
  if (index.is_primary())
1166
 
    destination.append("PRIMARY ");
 
1099
    destination.append("PRIMARY ", 8);
1167
1100
  else if (index.is_unique())
1168
 
    destination.append("UNIQUE ");
 
1101
    destination.append("UNIQUE ", 7);
1169
1102
 
1170
1103
  destination.append("KEY ", 4);
1171
1104
  if (! (index.is_primary() && index.name().compare("PRIMARY")==0))
1203
1136
      {
1204
1137
        if (part.compare_length() != field.string_options().length())
1205
1138
        {
 
1139
          stringstream ss;
1206
1140
          destination.push_back('(');
1207
 
          destination.append(boost::lexical_cast<string>(part.compare_length()));
 
1141
          ss << part.compare_length();
 
1142
          destination.append(ss.str());
1208
1143
          destination.push_back(')');
1209
1144
        }
1210
1145
      }
1217
1152
  case Table::Index::UNKNOWN_INDEX:
1218
1153
    break;
1219
1154
  case Table::Index::BTREE:
1220
 
    destination.append(" USING BTREE");
 
1155
    destination.append(" USING BTREE", 12);
1221
1156
    break;
1222
1157
  case Table::Index::RTREE:
1223
 
    destination.append(" USING RTREE");
 
1158
    destination.append(" USING RTREE", 12);
1224
1159
    break;
1225
1160
  case Table::Index::HASH:
1226
 
    destination.append(" USING HASH");
 
1161
    destination.append(" USING HASH", 11);
1227
1162
    break;
1228
1163
  case Table::Index::FULLTEXT:
1229
 
    destination.append(" USING FULLTEXT");
 
1164
    destination.append(" USING FULLTEXT", 15);
1230
1165
    break;
1231
1166
  }
1232
1167
 
1233
1168
  if (index.has_comment())
1234
1169
  {
1235
 
    destination.append(" COMMENT ");
 
1170
    destination.append(" COMMENT ", 9);
1236
1171
    append_escaped_string(&destination, index.comment());
1237
1172
  }
1238
1173
 
1243
1178
{
1244
1179
  switch (opt)
1245
1180
  {
 
1181
  case Table::ForeignKeyConstraint::OPTION_UNDEF:
 
1182
    break;
1246
1183
  case Table::ForeignKeyConstraint::OPTION_RESTRICT:
1247
1184
    destination.append("RESTRICT");
1248
1185
    break;
1252
1189
  case Table::ForeignKeyConstraint::OPTION_SET_NULL:
1253
1190
    destination.append("SET NULL");
1254
1191
    break;
1255
 
  case Table::ForeignKeyConstraint::OPTION_UNDEF:
1256
1192
  case Table::ForeignKeyConstraint::OPTION_NO_ACTION:
1257
1193
    destination.append("NO ACTION");
1258
1194
    break;
1259
 
  case Table::ForeignKeyConstraint::OPTION_SET_DEFAULT:
 
1195
  case Table::ForeignKeyConstraint::OPTION_DEFAULT:
1260
1196
    destination.append("SET DEFAULT");
1261
1197
    break;
1262
1198
  }
1272
1208
  if (sql_variant == ANSI)
1273
1209
    quoted_identifier= '"';
1274
1210
 
1275
 
  destination.append("  ");
 
1211
  destination.append("  ", 2);
1276
1212
 
1277
1213
  if (fkey.has_name())
1278
1214
  {
1279
 
    destination.append("CONSTRAINT ");
 
1215
    destination.append("CONSTRAINT ", 11);
1280
1216
    append_escaped_string(&destination, fkey.name(), quoted_identifier);
1281
1217
    destination.append(" ", 1);
1282
1218
  }
1283
1219
 
1284
 
  destination.append("FOREIGN KEY (");
 
1220
  destination.append("FOREIGN KEY (", 13);
1285
1221
 
1286
1222
  for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
1287
1223
  {
1292
1228
                          quoted_identifier);
1293
1229
  }
1294
1230
 
1295
 
  destination.append(") REFERENCES ");
 
1231
  destination.append(") REFERENCES ", 13);
1296
1232
 
1297
1233
  append_escaped_string(&destination, fkey.references_table_name(),
1298
1234
                        quoted_identifier);
1299
 
  destination.append(" (");
 
1235
  destination.append(" (", 2);
1300
1236
 
1301
1237
  for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
1302
1238
  {
1309
1245
 
1310
1246
  destination.push_back(')');
1311
1247
 
1312
 
  if (fkey.has_update_option() and fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
 
1248
  if (fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1313
1249
  {
1314
 
    destination.append(" ON UPDATE ");
 
1250
    destination.append(" ON UPDATE ", 11);
1315
1251
    transformForeignKeyOptionToSql(fkey.update_option(), destination);
1316
1252
  }
1317
1253
 
1318
 
  if (fkey.has_delete_option() and fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
 
1254
  if (fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1319
1255
  {
1320
 
    destination.append(" ON DELETE ");
 
1256
    destination.append(" ON DELETE ", 11);
1321
1257
    transformForeignKeyOptionToSql(fkey.delete_option(), destination);
1322
1258
  }
1323
1259
 
1347
1283
  switch (field_type)
1348
1284
  {
1349
1285
    case Table::Field::DOUBLE:
1350
 
    destination.append(" DOUBLE");
 
1286
    destination.append(" DOUBLE", 7);
1351
1287
    if (field.has_numeric_options()
1352
1288
        && field.numeric_options().has_precision())
1353
1289
    {
1361
1297
    {
1362
1298
      if (field.string_options().has_collation()
1363
1299
          && field.string_options().collation().compare("binary") == 0)
1364
 
        destination.append(" VARBINARY(");
 
1300
        destination.append(" VARBINARY(", 11);
1365
1301
      else
1366
 
        destination.append(" VARCHAR(");
 
1302
        destination.append(" VARCHAR(", 9);
1367
1303
 
1368
 
      destination.append(boost::lexical_cast<string>(field.string_options().length()));
1369
 
      destination.append(")");
 
1304
      stringstream ss;
 
1305
      ss << field.string_options().length() << ")";
 
1306
      destination.append(ss.str());
1370
1307
    }
1371
1308
    break;
1372
1309
  case Table::Field::BLOB:
1373
1310
    {
1374
1311
      if (field.string_options().has_collation()
1375
1312
          && field.string_options().collation().compare("binary") == 0)
1376
 
        destination.append(" BLOB");
 
1313
        destination.append(" BLOB", 5);
1377
1314
      else
1378
 
        destination.append(" TEXT");
 
1315
        destination.append(" TEXT", 5);
1379
1316
    }
1380
1317
    break;
1381
1318
  case Table::Field::ENUM:
1382
1319
    {
1383
1320
      size_t num_field_values= field.enumeration_values().field_value_size();
1384
 
      destination.append(" ENUM(");
 
1321
      destination.append(" ENUM(", 6);
1385
1322
      for (size_t x= 0; x < num_field_values; ++x)
1386
1323
      {
1387
1324
        const string &type= field.enumeration_values().field_value(x);
1396
1333
      destination.push_back(')');
1397
1334
      break;
1398
1335
    }
1399
 
  case Table::Field::UUID:
1400
 
    destination.append(" UUID");
1401
 
    break;
1402
 
  case Table::Field::BOOLEAN:
1403
 
    destination.append(" BOOLEAN");
1404
 
    break;
1405
1336
  case Table::Field::INTEGER:
1406
 
    destination.append(" INT");
 
1337
    destination.append(" INT", 4);
1407
1338
    break;
1408
1339
  case Table::Field::BIGINT:
1409
 
    if (field.has_constraints() and
1410
 
        field.constraints().is_unsigned())
1411
 
    {
1412
 
      destination.append(" BIGINT UNSIGNED");
1413
 
    }
1414
 
    else
1415
 
    {
1416
 
      destination.append(" BIGINT");
1417
 
    }
 
1340
    destination.append(" BIGINT", 7);
1418
1341
    break;
1419
1342
  case Table::Field::DECIMAL:
1420
1343
    {
1421
 
      destination.append(" DECIMAL(");
 
1344
      destination.append(" DECIMAL(", 9);
1422
1345
      stringstream ss;
1423
1346
      ss << field.numeric_options().precision() << ",";
1424
1347
      ss << field.numeric_options().scale() << ")";
1426
1349
    }
1427
1350
    break;
1428
1351
  case Table::Field::DATE:
1429
 
    destination.append(" DATE");
1430
 
    break;
1431
 
 
1432
 
  case Table::Field::EPOCH:
1433
 
    if (field.time_options().microseconds())
1434
 
    {
1435
 
      destination.append(" TIMESTAMP(6)");
1436
 
    }
1437
 
    else
1438
 
    {
1439
 
      destination.append(" TIMESTAMP");
1440
 
    }
1441
 
    break;
1442
 
 
 
1352
    destination.append(" DATE", 5);
 
1353
    break;
 
1354
  case Table::Field::TIMESTAMP:
 
1355
    destination.append(" TIMESTAMP",  10);
 
1356
    break;
1443
1357
  case Table::Field::DATETIME:
1444
 
    destination.append(" DATETIME");
1445
 
    break;
1446
 
  case Table::Field::TIME:
1447
 
    destination.append(" TIME");
1448
 
    break;
 
1358
    destination.append(" DATETIME",  9);
 
1359
    break;
 
1360
  }
 
1361
 
 
1362
  if (field.type() == Table::Field::INTEGER || 
 
1363
      field.type() == Table::Field::BIGINT)
 
1364
  {
 
1365
    if (field.has_constraints() &&
 
1366
        field.constraints().has_is_unsigned() &&
 
1367
        field.constraints().is_unsigned())
 
1368
    {
 
1369
      destination.append(" UNSIGNED", 9);
 
1370
    }
1449
1371
  }
1450
1372
 
1451
1373
  if (field.type() == Table::Field::BLOB ||
1454
1376
    if (field.string_options().has_collation()
1455
1377
        && field.string_options().collation().compare("binary"))
1456
1378
    {
1457
 
      destination.append(" COLLATE ");
 
1379
      destination.append(" COLLATE ", 9);
1458
1380
      destination.append(field.string_options().collation());
1459
1381
    }
1460
1382
  }
1461
1383
 
1462
 
  if (field.has_constraints() and field.constraints().is_unique())
1463
 
  {
1464
 
    destination.append(" UNIQUE");
1465
 
  }
1466
 
 
1467
 
  if (field.has_constraints() && field.constraints().is_notnull())
1468
 
  {
1469
 
    destination.append(" NOT NULL");
1470
 
  }
1471
 
  else if (field.type() == Table::Field::EPOCH)
1472
 
  {
1473
 
    destination.append(" NULL");
1474
 
  }
 
1384
  if (field.has_constraints() &&
 
1385
      ! field.constraints().is_nullable())
 
1386
  {
 
1387
    destination.append(" NOT NULL", 9);
 
1388
  }
 
1389
  else if (field.type() == Table::Field::TIMESTAMP)
 
1390
    destination.append(" NULL", 5);
1475
1391
 
1476
1392
  if (field.type() == Table::Field::INTEGER || 
1477
1393
      field.type() == Table::Field::BIGINT)
1480
1396
    if (field.has_numeric_options() &&
1481
1397
        field.numeric_options().is_autoincrement())
1482
1398
    {
1483
 
      destination.append(" AUTO_INCREMENT");
 
1399
      destination.append(" AUTO_INCREMENT", 15);
1484
1400
    }
1485
1401
  }
1486
1402
 
1487
1403
  if (field.options().has_default_value())
1488
1404
  {
1489
 
    destination.append(" DEFAULT ");
 
1405
    destination.append(" DEFAULT ", 9);
1490
1406
    append_escaped_string(&destination, field.options().default_value());
1491
1407
  }
1492
1408
  else if (field.options().has_default_expression())
1493
1409
  {
1494
 
    destination.append(" DEFAULT ");
 
1410
    destination.append(" DEFAULT ", 9);
1495
1411
    destination.append(field.options().default_expression());
1496
1412
  }
1497
1413
  else if (field.options().has_default_bin_value())
1498
1414
  {
1499
1415
    const string &v= field.options().default_bin_value();
1500
1416
    if (v.length() == 0)
1501
 
    {
1502
 
      destination.append(" DEFAULT ''");
1503
 
    }
 
1417
      destination.append(" DEFAULT ''", 11);
1504
1418
    else
1505
1419
    {
1506
 
      destination.append(" DEFAULT 0x");
 
1420
      destination.append(" DEFAULT 0x", 11);
1507
1421
      for (size_t x= 0; x < v.length(); x++)
1508
1422
      {
1509
1423
        char hex[3];
1516
1430
           && field.options().default_null()
1517
1431
           && field.type() != Table::Field::BLOB)
1518
1432
  {
1519
 
    destination.append(" DEFAULT NULL");
 
1433
    destination.append(" DEFAULT NULL", 13);
1520
1434
  }
1521
1435
 
1522
1436
  if (field.has_options() && field.options().has_update_expression())
1523
1437
  {
1524
 
    destination.append(" ON UPDATE ");
 
1438
    destination.append(" ON UPDATE ", 11);
1525
1439
    destination.append(field.options().update_expression());
1526
1440
  }
1527
1441
 
1528
1442
  if (field.has_comment())
1529
1443
  {
1530
 
    destination.append(" COMMENT ");
 
1444
    destination.append(" COMMENT ", 9);
1531
1445
    append_escaped_string(&destination, field.comment(), quoted_default);
1532
1446
  }
1533
1447
  return NONE;
1541
1455
  case Table::Field::DECIMAL:
1542
1456
  case Table::Field::INTEGER:
1543
1457
  case Table::Field::BIGINT:
 
1458
  case Table::Field::ENUM:
1544
1459
    return false;
1545
1460
  default:
1546
1461
    return true;
1557
1472
  case DRIZZLE_TYPE_NULL:
1558
1473
    assert(false); /* Not a user definable type */
1559
1474
    return Table::Field::INTEGER; /* unreachable */
1560
 
  case DRIZZLE_TYPE_MICROTIME:
1561
1475
  case DRIZZLE_TYPE_TIMESTAMP:
1562
 
    return Table::Field::EPOCH;
 
1476
    return Table::Field::TIMESTAMP;
1563
1477
  case DRIZZLE_TYPE_LONGLONG:
1564
1478
    return Table::Field::BIGINT;
1565
1479
  case DRIZZLE_TYPE_DATETIME:
1566
1480
    return Table::Field::DATETIME;
1567
 
  case DRIZZLE_TYPE_TIME:
1568
 
    return Table::Field::TIME;
1569
1481
  case DRIZZLE_TYPE_DATE:
1570
1482
    return Table::Field::DATE;
1571
1483
  case DRIZZLE_TYPE_VARCHAR:
1576
1488
    return Table::Field::ENUM;
1577
1489
  case DRIZZLE_TYPE_BLOB:
1578
1490
    return Table::Field::BLOB;
1579
 
  case DRIZZLE_TYPE_UUID:
1580
 
    return Table::Field::UUID;
1581
 
  case DRIZZLE_TYPE_BOOLEAN:
1582
 
    return Table::Field::BOOLEAN;
1583
1491
  }
1584
1492
 
1585
1493
  assert(false);