~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/statement_transform.cc

Merge Joe, plus I updated the tests.

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"
38
 
#include "drizzled/charset.h"
39
 
#include "drizzled/charset_info.h"
40
 
#include "drizzled/global_charset_info.h"
41
37
 
42
38
#include <string>
43
39
#include <vector>
52
48
namespace message
53
49
{
54
50
 
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
 
/* Incredibly similar to append_unescaped() in table.cc, but for std::string */
70
 
static void append_escaped_string(std::string *res, const std::string &input, const char quote='\'')
71
 
{
72
 
  const char *pos= input.c_str();
73
 
  const char *end= input.c_str()+input.length();
74
 
  res->push_back(quote);
75
 
 
76
 
  for (; pos != end ; pos++)
77
 
  {
78
 
    uint32_t mblen;
79
 
    if (use_mb(default_charset_info) &&
80
 
        (mblen= my_ismbchar(default_charset_info, pos, end)))
81
 
    {
82
 
      res->append(pos, mblen);
83
 
      pos+= mblen - 1;
84
 
      if (pos >= end)
85
 
        break;
86
 
      continue;
87
 
    }
88
 
 
89
 
    switch (*pos) {
90
 
    case 0:                             /* Must be escaped for 'mysql' */
91
 
      res->push_back('\\');
92
 
      res->push_back('0');
93
 
      break;
94
 
    case '\n':                          /* Must be escaped for logs */
95
 
      res->push_back('\\');
96
 
      res->push_back('n');
97
 
      break;
98
 
    case '\r':
99
 
      res->push_back('\\');             /* This gives better readability */
100
 
      res->push_back('r');
101
 
      break;
102
 
    case '\\':
103
 
      res->push_back('\\');             /* Because of the sql syntax */
104
 
      res->push_back('\\');
105
 
      break;
106
 
    default:
107
 
      if (*pos == quote) /* SQL syntax for quoting a quote */
108
 
      {
109
 
        res->push_back(quote);
110
 
        res->push_back(quote);
111
 
      }
112
 
      else
113
 
        res->push_back(*pos);
114
 
      break;
115
 
    }
116
 
  }
117
 
  res->push_back(quote);
118
 
}
119
 
 
120
51
enum TransformSqlError
121
52
transformStatementToSql(const Statement &source,
122
53
                        vector<string> &sql_strings,
127
58
 
128
59
  switch (source.type())
129
60
  {
130
 
  case Statement::ROLLBACK_STATEMENT:
131
 
    {
132
 
      break;
133
 
    }
134
 
  case Statement::ROLLBACK:
135
 
    {
136
 
      sql_strings.push_back("ROLLBACK");
137
 
      break;
138
 
    }
139
61
  case Statement::INSERT:
140
62
    {
141
63
      if (! source.has_insert_header())
318
240
      sql_strings.push_back(destination);
319
241
    }
320
242
    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
243
  case Statement::SET_VARIABLE:
332
244
    {
333
245
      assert(source.has_set_variable_statement());
411
323
 
412
324
    const FieldMetadata &field_metadata= header.field_metadata(x);
413
325
 
414
 
    if (record.is_null(x))
415
 
    {
416
 
      should_quote_field_value= false;
417
 
    }
418
 
    else 
419
 
    {
420
 
      should_quote_field_value= shouldQuoteFieldValue(field_metadata.type());
421
 
    }
 
326
    should_quote_field_value= shouldQuoteFieldValue(field_metadata.type());
422
327
 
423
328
    if (should_quote_field_value)
424
329
      destination.push_back('\'');
425
330
 
426
 
    if (record.is_null(x))
 
331
    if (field_metadata.type() == Table::Field::BLOB)
427
332
    {
428
 
      destination.append("NULL");
 
333
      /* 
 
334
        * We do this here because BLOB data is returned
 
335
        * in a string correctly, but calling append()
 
336
        * without a length will result in only the string
 
337
        * up to a \0 being output here.
 
338
        */
 
339
      string raw_data(record.insert_value(x));
 
340
      destination.append(raw_data.c_str(), raw_data.size());
429
341
    }
430
342
    else
431
343
    {
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
 
      }
 
344
      destination.append(record.insert_value(x));
449
345
    }
450
346
 
451
347
    if (should_quote_field_value)
508
404
      }
509
405
      else
510
406
      {
511
 
        string tmp(data.record(x).insert_value(y));
512
 
        escapeEmbeddedQuotes(tmp);
513
 
        destination.append(tmp);
 
407
        destination.append(data.record(x).insert_value(y));
514
408
      }
515
409
 
516
410
      if (should_quote_field_value)
574
468
    destination.push_back(quoted_identifier);
575
469
    destination.push_back('=');
576
470
 
577
 
    if (record.is_null(x))
578
 
    {
579
 
      should_quote_field_value= false;
580
 
    }
581
 
    else 
582
 
    {
583
 
      should_quote_field_value= shouldQuoteFieldValue(field_metadata.type());
584
 
    }    
 
471
    should_quote_field_value= shouldQuoteFieldValue(field_metadata.type());
585
472
 
586
473
    if (should_quote_field_value)
587
474
      destination.push_back('\'');
588
475
 
589
 
    if (record.is_null(x))
 
476
    if (field_metadata.type() == Table::Field::BLOB)
590
477
    {
591
 
      destination.append("NULL");
 
478
      /* 
 
479
       * We do this here because BLOB data is returned
 
480
       * in a string correctly, but calling append()
 
481
       * without a length will result in only the string
 
482
       * up to a \0 being output here.
 
483
       */
 
484
      string raw_data(record.after_value(x));
 
485
      destination.append(raw_data.c_str(), raw_data.size());
592
486
    }
593
 
    else 
 
487
    else
594
488
    {
595
 
      if (field_metadata.type() == Table::Field::BLOB)
596
 
      {
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());
605
 
      }
606
 
      else 
607
 
      {
608
 
        string tmp(record.after_value(x));
609
 
        escapeEmbeddedQuotes(tmp);
610
 
        destination.append(tmp);
611
 
      }
 
489
      destination.append(record.after_value(x));
612
490
    }
613
491
 
614
492
    if (should_quote_field_value)
730
608
    }
731
609
    else
732
610
    {
733
 
      string tmp(record.key_value(x));
734
 
      escapeEmbeddedQuotes(tmp);
735
 
      destination.append(tmp);
 
611
      destination.append(record.key_value(x));
736
612
    }
737
613
 
738
614
    if (should_quote_field_value)
801
677
      }
802
678
      else
803
679
      {
804
 
        string tmp(data.record(x).key_value(y));
805
 
        escapeEmbeddedQuotes(tmp);
806
 
        destination.append(tmp);
 
680
        destination.append(data.record(x).key_value(y));
807
681
      }
808
682
 
809
683
      if (should_quote_field_value)
816
690
}
817
691
 
818
692
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
693
transformDropSchemaStatementToSql(const DropSchemaStatement &statement,
851
694
                                  string &destination,
852
695
                                  enum TransformSqlVariant sql_variant)
874
717
 
875
718
  const Schema &schema= statement.schema();
876
719
 
877
 
  destination.append("CREATE SCHEMA ");
 
720
  destination.append("CREATE SCHEMA ", 14);
878
721
  destination.push_back(quoted_identifier);
879
722
  destination.append(schema.name());
880
723
  destination.push_back(quoted_identifier);
881
724
 
882
725
  if (schema.has_collation())
883
726
  {
884
 
    destination.append(" COLLATE ");
 
727
    destination.append(" COLLATE ", 9);
885
728
    destination.append(schema.collation());
886
729
  }
887
730
 
899
742
 
900
743
  const TableMetadata &table_metadata= statement.table_metadata();
901
744
 
902
 
  destination.append("DROP TABLE ");
 
745
  destination.append("DROP TABLE ", 11);
903
746
 
904
747
  /* Add the IF EXISTS clause if necessary */
905
748
  if (statement.has_if_exists_clause() &&
906
749
      statement.if_exists_clause() == true)
907
750
  {
908
 
    destination.append("IF EXISTS ");
 
751
    destination.append("IF EXISTS ", 10);
909
752
  }
910
753
 
911
754
  destination.push_back(quoted_identifier);
930
773
 
931
774
  const TableMetadata &table_metadata= statement.table_metadata();
932
775
 
933
 
  destination.append("TRUNCATE TABLE ");
 
776
  destination.append("TRUNCATE TABLE ", 15);
934
777
  destination.push_back(quoted_identifier);
935
778
  destination.append(table_metadata.schema_name());
936
779
  destination.push_back(quoted_identifier);
951
794
  const FieldMetadata &variable_metadata= statement.variable_metadata();
952
795
  bool should_quote_field_value= shouldQuoteFieldValue(variable_metadata.type());
953
796
 
954
 
  destination.append("SET GLOBAL "); /* Only global variables are replicated */
 
797
  destination.append("SET GLOBAL ", 11); /* Only global variables are replicated */
955
798
  destination.append(variable_metadata.name());
956
799
  destination.push_back('=');
957
800
 
983
826
  if (sql_variant == ANSI)
984
827
    quoted_identifier= '"';
985
828
 
986
 
  destination.append("CREATE ");
 
829
  destination.append("CREATE ", 7);
987
830
 
988
831
  if (table.type() == Table::TEMPORARY)
989
 
    destination.append("TEMPORARY ");
 
832
    destination.append("TEMPORARY ", 10);
990
833
  
991
 
  destination.append("TABLE ");
 
834
  destination.append("TABLE ", 6);
992
835
  if (with_schema)
993
836
  {
994
 
    append_escaped_string(&destination, table.schema(), quoted_identifier);
 
837
    destination.push_back(quoted_identifier);
 
838
    destination.append(table.schema());
 
839
    destination.push_back(quoted_identifier);
995
840
    destination.push_back('.');
996
841
  }
997
 
  append_escaped_string(&destination, table.name(), quoted_identifier);
998
 
  destination.append(" (\n");
 
842
  destination.push_back(quoted_identifier);
 
843
  destination.append(table.name());
 
844
  destination.push_back(quoted_identifier);
 
845
  destination.append(" (\n", 3);
999
846
 
1000
847
  enum TransformSqlError result= NONE;
1001
848
  size_t num_fields= table.field_size();
1004
851
    const Table::Field &field= table.field(x);
1005
852
 
1006
853
    if (x != 0)
1007
 
      destination.append(",\n");
1008
 
 
1009
 
    destination.append("  ");
 
854
      destination.append(",\n", 2);
1010
855
 
1011
856
    result= transformFieldDefinitionToSql(field, destination, sql_variant);
1012
 
 
 
857
    
1013
858
    if (result != NONE)
1014
859
      return result;
1015
860
  }
1017
862
  size_t num_indexes= table.indexes_size();
1018
863
  
1019
864
  if (num_indexes > 0)
1020
 
    destination.append(",\n");
 
865
    destination.append(",\n", 2);
1021
866
 
1022
867
  for (size_t x= 0; x < num_indexes; ++x)
1023
868
  {
1024
869
    const message::Table::Index &index= table.indexes(x);
1025
870
 
1026
871
    if (x != 0)
1027
 
      destination.append(",\n");
 
872
      destination.append(",\n", 2);
1028
873
 
1029
874
    result= transformIndexDefinitionToSql(index, table, destination, sql_variant);
1030
875
    
1031
876
    if (result != NONE)
1032
877
      return result;
1033
878
  }
1034
 
 
1035
 
  size_t num_foreign_keys= table.fk_constraint_size();
1036
 
 
1037
 
  if (num_foreign_keys > 0)
1038
 
    destination.append(",\n");
1039
 
 
1040
 
  for (size_t x= 0; x < num_foreign_keys; ++x)
1041
 
  {
1042
 
    const message::Table::ForeignKeyConstraint &fkey= table.fk_constraint(x);
1043
 
 
1044
 
    if (x != 0)
1045
 
      destination.append(",\n");
1046
 
 
1047
 
    result= transformForeignKeyConstraintDefinitionToSql(fkey, table, destination, sql_variant);
1048
 
 
1049
 
    if (result != NONE)
1050
 
      return result;
1051
 
  }
1052
 
 
1053
 
  destination.append("\n)");
 
879
  destination.append("\n)", 2);
1054
880
 
1055
881
  /* Add ENGINE = " clause */
1056
882
  if (table.has_engine())
1057
883
  {
1058
 
    destination.append(" ENGINE=");
 
884
    destination.append("\nENGINE = ", 10);
1059
885
    destination.append(table.engine().name());
1060
886
 
1061
887
    size_t num_engine_options= table.engine().options_size();
1062
 
    if (num_engine_options > 0)
1063
 
      destination.append(" ", 1);
1064
888
    for (size_t x= 0; x < num_engine_options; ++x)
1065
889
    {
1066
890
      const Engine::Option &option= table.engine().options(x);
 
891
      destination.push_back('\n');
1067
892
      destination.append(option.name());
1068
 
      destination.append("='");
 
893
      destination.append(" = ", 3);
1069
894
      destination.append(option.state());
1070
 
      destination.append("'");
1071
 
      if (x != num_engine_options-1)
1072
 
      {
1073
 
        destination.append(", ");
1074
 
      }
 
895
      destination.push_back('\n');
1075
896
    }
1076
897
  }
1077
898
 
1089
910
  if (sql_variant == ANSI)
1090
911
    return NONE; /* ANSI does not support table options... */
1091
912
 
 
913
  stringstream ss;
 
914
 
1092
915
  if (options.has_comment())
1093
916
  {
1094
 
    destination.append(" COMMENT=");
1095
 
    append_escaped_string(&destination, options.comment());
 
917
    destination.append("\nCOMMENT = '", 12);
 
918
    destination.append(options.comment());
 
919
    destination.push_back('\'');
1096
920
  }
1097
921
 
1098
922
  if (options.has_collation())
1099
923
  {
1100
 
    destination.append(" COLLATE = ");
 
924
    destination.append("\nCOLLATE = ", 11);
1101
925
    destination.append(options.collation());
1102
926
  }
1103
927
 
 
928
  if (options.has_auto_increment())
 
929
  {
 
930
    ss << options.auto_increment();
 
931
    destination.append("\nAUTOINCREMENT_OFFSET = ", 24);
 
932
    destination.append(ss.str());
 
933
    ss.clear();
 
934
  }
 
935
  
 
936
  if (options.has_row_type())
 
937
  {
 
938
    ss << options.row_type();
 
939
    destination.append("\nROW_TYPE = ", 12);
 
940
    destination.append(ss.str());
 
941
    ss.clear();
 
942
  }
 
943
 
1104
944
  if (options.has_data_file_name())
1105
945
  {
1106
 
    destination.append("\nDATA_FILE_NAME = '");
 
946
    destination.append("\nDATA_FILE_NAME = '", 19);
1107
947
    destination.append(options.data_file_name());
1108
948
    destination.push_back('\'');
1109
949
  }
1110
950
 
1111
951
  if (options.has_index_file_name())
1112
952
  {
1113
 
    destination.append("\nINDEX_FILE_NAME = '");
 
953
    destination.append("\nINDEX_FILE_NAME = '", 20);
1114
954
    destination.append(options.index_file_name());
1115
955
    destination.push_back('\'');
1116
956
  }
1117
957
 
1118
958
  if (options.has_max_rows())
1119
959
  {
1120
 
    destination.append("\nMAX_ROWS = ");
1121
 
    destination.append(boost::lexical_cast<string>(options.max_rows()));
 
960
    ss << options.max_rows();
 
961
    destination.append("\nMAX_ROWS = ", 12);
 
962
    destination.append(ss.str());
 
963
    ss.clear();
1122
964
  }
1123
965
 
1124
966
  if (options.has_min_rows())
1125
967
  {
1126
 
    destination.append("\nMIN_ROWS = ");
1127
 
    destination.append(boost::lexical_cast<string>(options.min_rows()));
 
968
    ss << options.min_rows();
 
969
    destination.append("\nMIN_ROWS = ", 12);
 
970
    destination.append(ss.str());
 
971
    ss.clear();
1128
972
  }
1129
973
 
1130
 
  if (options.has_user_set_auto_increment_value()
1131
 
      && options.has_auto_increment_value())
 
974
  if (options.has_auto_increment_value())
1132
975
  {
1133
 
    destination.append(" AUTO_INCREMENT=");
1134
 
    destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
 
976
    ss << options.auto_increment_value();
 
977
    destination.append("\nAUTO_INCREMENT = ", 18);
 
978
    destination.append(ss.str());
 
979
    ss.clear();
1135
980
  }
1136
981
 
1137
982
  if (options.has_avg_row_length())
1138
983
  {
1139
 
    destination.append("\nAVG_ROW_LENGTH = ");
1140
 
    destination.append(boost::lexical_cast<string>(options.avg_row_length()));
 
984
    ss << options.avg_row_length();
 
985
    destination.append("\nAVG_ROW_LENGTH = ", 18);
 
986
    destination.append(ss.str());
 
987
    ss.clear();
1141
988
  }
1142
989
 
1143
990
  if (options.has_checksum() &&
1144
991
      options.checksum())
1145
 
    destination.append("\nCHECKSUM = TRUE");
 
992
    destination.append("\nCHECKSUM = TRUE", 16);
1146
993
  if (options.has_page_checksum() &&
1147
994
      options.page_checksum())
1148
 
    destination.append("\nPAGE_CHECKSUM = TRUE");
 
995
    destination.append("\nPAGE_CHECKSUM = TRUE", 21);
1149
996
 
1150
997
  return NONE;
1151
998
}
1160
1007
  if (sql_variant == ANSI)
1161
1008
    quoted_identifier= '"';
1162
1009
 
1163
 
  destination.append("  ", 2);
1164
 
 
1165
1010
  if (index.is_primary())
1166
 
    destination.append("PRIMARY ");
 
1011
    destination.append("PRIMARY ", 8);
1167
1012
  else if (index.is_unique())
1168
 
    destination.append("UNIQUE ");
 
1013
    destination.append("UNIQUE ", 7);
1169
1014
 
1170
1015
  destination.append("KEY ", 4);
1171
 
  if (! (index.is_primary() && index.name().compare("PRIMARY")==0))
1172
 
  {
1173
 
    destination.push_back(quoted_identifier);
1174
 
    destination.append(index.name());
1175
 
    destination.push_back(quoted_identifier);
1176
 
    destination.append(" (", 2);
1177
 
  }
1178
 
  else
1179
 
    destination.append("(", 1);
1180
 
 
 
1016
  destination.push_back(quoted_identifier);
 
1017
  destination.append(index.name());
 
1018
  destination.push_back(quoted_identifier);
 
1019
  destination.append(" (", 2);
 
1020
  
1181
1021
  size_t num_parts= index.index_part_size();
1182
1022
  for (size_t x= 0; x < num_parts; ++x)
1183
1023
  {
1201
1041
    {
1202
1042
      if (part.has_compare_length())
1203
1043
      {
1204
 
        if (part.compare_length() != field.string_options().length())
 
1044
        size_t compare_length_in_chars= part.compare_length();
 
1045
        
 
1046
        /* hack: compare_length() is bytes, not chars, but
 
1047
         * only for VARCHAR. Ass. */
 
1048
        if (field.type() == Table::Field::VARCHAR)
 
1049
          compare_length_in_chars/= 4;
 
1050
 
 
1051
        if (compare_length_in_chars != field.string_options().length())
1205
1052
        {
 
1053
          stringstream ss;
1206
1054
          destination.push_back('(');
1207
 
          destination.append(boost::lexical_cast<string>(part.compare_length()));
 
1055
          ss << compare_length_in_chars;
 
1056
          destination.append(ss.str());
1208
1057
          destination.push_back(')');
1209
1058
        }
1210
1059
      }
1212
1061
  }
1213
1062
  destination.push_back(')');
1214
1063
 
1215
 
  switch (index.type())
1216
 
  {
1217
 
  case Table::Index::UNKNOWN_INDEX:
1218
 
    break;
1219
 
  case Table::Index::BTREE:
1220
 
    destination.append(" USING BTREE");
1221
 
    break;
1222
 
  case Table::Index::RTREE:
1223
 
    destination.append(" USING RTREE");
1224
 
    break;
1225
 
  case Table::Index::HASH:
1226
 
    destination.append(" USING HASH");
1227
 
    break;
1228
 
  case Table::Index::FULLTEXT:
1229
 
    destination.append(" USING FULLTEXT");
1230
 
    break;
1231
 
  }
1232
 
 
1233
 
  if (index.has_comment())
1234
 
  {
1235
 
    destination.append(" COMMENT ");
1236
 
    append_escaped_string(&destination, index.comment());
1237
 
  }
1238
 
 
1239
 
  return NONE;
1240
 
}
1241
 
 
1242
 
static void transformForeignKeyOptionToSql(Table::ForeignKeyConstraint::ForeignKeyOption opt, string &destination)
1243
 
{
1244
 
  switch (opt)
1245
 
  {
1246
 
  case Table::ForeignKeyConstraint::OPTION_RESTRICT:
1247
 
    destination.append("RESTRICT");
1248
 
    break;
1249
 
  case Table::ForeignKeyConstraint::OPTION_CASCADE:
1250
 
    destination.append("CASCADE");
1251
 
    break;
1252
 
  case Table::ForeignKeyConstraint::OPTION_SET_NULL:
1253
 
    destination.append("SET NULL");
1254
 
    break;
1255
 
  case Table::ForeignKeyConstraint::OPTION_UNDEF:
1256
 
  case Table::ForeignKeyConstraint::OPTION_NO_ACTION:
1257
 
    destination.append("NO ACTION");
1258
 
    break;
1259
 
  case Table::ForeignKeyConstraint::OPTION_SET_DEFAULT:
1260
 
    destination.append("SET DEFAULT");
1261
 
    break;
1262
 
  }
1263
 
}
1264
 
 
1265
 
enum TransformSqlError
1266
 
transformForeignKeyConstraintDefinitionToSql(const Table::ForeignKeyConstraint &fkey,
1267
 
                                             const Table &,
1268
 
                                             string &destination,
1269
 
                                             enum TransformSqlVariant sql_variant)
1270
 
{
1271
 
  char quoted_identifier= '`';
1272
 
  if (sql_variant == ANSI)
1273
 
    quoted_identifier= '"';
1274
 
 
1275
 
  destination.append("  ");
1276
 
 
1277
 
  if (fkey.has_name())
1278
 
  {
1279
 
    destination.append("CONSTRAINT ");
1280
 
    append_escaped_string(&destination, fkey.name(), quoted_identifier);
1281
 
    destination.append(" ", 1);
1282
 
  }
1283
 
 
1284
 
  destination.append("FOREIGN KEY (");
1285
 
 
1286
 
  for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
1287
 
  {
1288
 
    if (x != 0)
1289
 
      destination.append(", ");
1290
 
 
1291
 
    append_escaped_string(&destination, fkey.column_names(x),
1292
 
                          quoted_identifier);
1293
 
  }
1294
 
 
1295
 
  destination.append(") REFERENCES ");
1296
 
 
1297
 
  append_escaped_string(&destination, fkey.references_table_name(),
1298
 
                        quoted_identifier);
1299
 
  destination.append(" (");
1300
 
 
1301
 
  for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
1302
 
  {
1303
 
    if (x != 0)
1304
 
      destination.append(", ");
1305
 
 
1306
 
    append_escaped_string(&destination, fkey.references_columns(x),
1307
 
                          quoted_identifier);
1308
 
  }
1309
 
 
1310
 
  destination.push_back(')');
1311
 
 
1312
 
  if (fkey.has_update_option() and fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1313
 
  {
1314
 
    destination.append(" ON UPDATE ");
1315
 
    transformForeignKeyOptionToSql(fkey.update_option(), destination);
1316
 
  }
1317
 
 
1318
 
  if (fkey.has_delete_option() and fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1319
 
  {
1320
 
    destination.append(" ON DELETE ");
1321
 
    transformForeignKeyOptionToSql(fkey.delete_option(), destination);
1322
 
  }
1323
 
 
1324
1064
  return NONE;
1325
1065
}
1326
1066
 
1330
1070
                              enum TransformSqlVariant sql_variant)
1331
1071
{
1332
1072
  char quoted_identifier= '`';
1333
 
  char quoted_default;
1334
 
 
1335
1073
  if (sql_variant == ANSI)
1336
1074
    quoted_identifier= '"';
1337
1075
 
1338
 
  if (sql_variant == DRIZZLE)
1339
 
    quoted_default= '\'';
1340
 
  else
1341
 
    quoted_default= quoted_identifier;
1342
 
 
1343
 
  append_escaped_string(&destination, field.name(), quoted_identifier);
 
1076
  destination.push_back(quoted_identifier);
 
1077
  destination.append(field.name());
 
1078
  destination.push_back(quoted_identifier);
1344
1079
 
1345
1080
  Table::Field::FieldType field_type= field.type();
1346
1081
 
1347
1082
  switch (field_type)
1348
1083
  {
1349
1084
    case Table::Field::DOUBLE:
1350
 
    destination.append(" DOUBLE");
1351
 
    if (field.has_numeric_options()
1352
 
        && field.numeric_options().has_precision())
1353
 
    {
1354
 
      stringstream ss;
1355
 
      ss << "(" << field.numeric_options().precision() << ",";
1356
 
      ss << field.numeric_options().scale() << ")";
1357
 
      destination.append(ss.str());
1358
 
    }
 
1085
    destination.append(" DOUBLE", 7);
1359
1086
    break;
1360
1087
  case Table::Field::VARCHAR:
1361
1088
    {
1362
 
      if (field.string_options().has_collation()
1363
 
          && field.string_options().collation().compare("binary") == 0)
1364
 
        destination.append(" VARBINARY(");
1365
 
      else
1366
 
        destination.append(" VARCHAR(");
1367
 
 
1368
 
      destination.append(boost::lexical_cast<string>(field.string_options().length()));
1369
 
      destination.append(")");
 
1089
      destination.append(" VARCHAR(", 9);
 
1090
      stringstream ss;
 
1091
      ss << field.string_options().length() << ")";
 
1092
      destination.append(ss.str());
1370
1093
    }
1371
1094
    break;
1372
1095
  case Table::Field::BLOB:
1373
 
    {
1374
 
      if (field.string_options().has_collation()
1375
 
          && field.string_options().collation().compare("binary") == 0)
1376
 
        destination.append(" BLOB");
1377
 
      else
1378
 
        destination.append(" TEXT");
1379
 
    }
 
1096
    destination.append(" BLOB", 5);
1380
1097
    break;
1381
1098
  case Table::Field::ENUM:
1382
1099
    {
1383
1100
      size_t num_field_values= field.enumeration_values().field_value_size();
1384
 
      destination.append(" ENUM(");
 
1101
      destination.append(" ENUM(", 6);
1385
1102
      for (size_t x= 0; x < num_field_values; ++x)
1386
1103
      {
1387
1104
        const string &type= field.enumeration_values().field_value(x);
1396
1113
      destination.push_back(')');
1397
1114
      break;
1398
1115
    }
1399
 
  case Table::Field::UUID:
1400
 
    destination.append(" UUID");
1401
 
    break;
1402
 
  case Table::Field::BOOLEAN:
1403
 
    destination.append(" BOOLEAN");
1404
 
    break;
1405
1116
  case Table::Field::INTEGER:
1406
 
    destination.append(" INT");
 
1117
    destination.append(" INT", 4);
1407
1118
    break;
1408
1119
  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
 
    }
 
1120
    destination.append(" BIGINT", 7);
1418
1121
    break;
1419
1122
  case Table::Field::DECIMAL:
1420
1123
    {
1421
 
      destination.append(" DECIMAL(");
 
1124
      destination.append(" DECIMAL(", 9);
1422
1125
      stringstream ss;
1423
1126
      ss << field.numeric_options().precision() << ",";
1424
1127
      ss << field.numeric_options().scale() << ")";
1426
1129
    }
1427
1130
    break;
1428
1131
  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
 
 
 
1132
    destination.append(" DATE", 5);
 
1133
    break;
 
1134
  case Table::Field::TIMESTAMP:
 
1135
    destination.append(" TIMESTAMP",  10);
 
1136
    break;
1443
1137
  case Table::Field::DATETIME:
1444
 
    destination.append(" DATETIME");
1445
 
    break;
1446
 
  case Table::Field::TIME:
1447
 
    destination.append(" TIME");
1448
 
    break;
 
1138
    destination.append(" DATETIME",  9);
 
1139
    break;
 
1140
  }
 
1141
 
 
1142
  if (field.type() == Table::Field::INTEGER || 
 
1143
      field.type() == Table::Field::BIGINT)
 
1144
  {
 
1145
    if (field.has_constraints() &&
 
1146
        field.constraints().has_is_unsigned() &&
 
1147
        field.constraints().is_unsigned())
 
1148
    {
 
1149
      destination.append(" UNSIGNED", 9);
 
1150
    }
 
1151
  }
 
1152
 
 
1153
 
 
1154
  if (! (field.has_constraints() &&
 
1155
         field.constraints().is_nullable()))
 
1156
  {
 
1157
    destination.append(" NOT", 4);
 
1158
  }
 
1159
  destination.append(" NULL", 5);
 
1160
 
 
1161
  if (field.type() == Table::Field::INTEGER || 
 
1162
      field.type() == Table::Field::BIGINT)
 
1163
  {
 
1164
    /* AUTO_INCREMENT must be after NOT NULL */
 
1165
    if (field.has_numeric_options() &&
 
1166
        field.numeric_options().is_autoincrement())
 
1167
    {
 
1168
      destination.append(" AUTO_INCREMENT", 15);
 
1169
    }
1449
1170
  }
1450
1171
 
1451
1172
  if (field.type() == Table::Field::BLOB ||
1452
1173
      field.type() == Table::Field::VARCHAR)
1453
1174
  {
1454
 
    if (field.string_options().has_collation()
1455
 
        && field.string_options().collation().compare("binary"))
 
1175
    if (field.string_options().has_collation())
1456
1176
    {
1457
 
      destination.append(" COLLATE ");
 
1177
      destination.append(" COLLATE ", 9);
1458
1178
      destination.append(field.string_options().collation());
1459
1179
    }
1460
1180
  }
1461
1181
 
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
 
  }
1475
 
 
1476
 
  if (field.type() == Table::Field::INTEGER || 
1477
 
      field.type() == Table::Field::BIGINT)
1478
 
  {
1479
 
    /* AUTO_INCREMENT must be after NOT NULL */
1480
 
    if (field.has_numeric_options() &&
1481
 
        field.numeric_options().is_autoincrement())
1482
 
    {
1483
 
      destination.append(" AUTO_INCREMENT");
1484
 
    }
1485
 
  }
1486
 
 
1487
1182
  if (field.options().has_default_value())
1488
1183
  {
1489
 
    destination.append(" DEFAULT ");
1490
 
    append_escaped_string(&destination, field.options().default_value());
1491
 
  }
1492
 
  else if (field.options().has_default_expression())
1493
 
  {
1494
 
    destination.append(" DEFAULT ");
1495
 
    destination.append(field.options().default_expression());
1496
 
  }
1497
 
  else if (field.options().has_default_bin_value())
 
1184
    destination.append(" DEFAULT ", 9);
 
1185
    destination.push_back(quoted_identifier);
 
1186
    destination.append(field.options().default_value());
 
1187
    destination.push_back(quoted_identifier);
 
1188
  }
 
1189
 
 
1190
  if (field.options().has_default_bin_value())
1498
1191
  {
1499
1192
    const string &v= field.options().default_bin_value();
1500
 
    if (v.length() == 0)
1501
 
    {
1502
 
      destination.append(" DEFAULT ''");
1503
 
    }
1504
 
    else
1505
 
    {
1506
 
      destination.append(" DEFAULT 0x");
1507
 
      for (size_t x= 0; x < v.length(); x++)
1508
 
      {
1509
 
        char hex[3];
1510
 
        snprintf(hex, sizeof(hex), "%.2X", *(v.c_str() + x));
1511
 
        destination.append(hex, 2);
1512
 
      }
1513
 
    }
1514
 
  }
1515
 
  else if (field.options().has_default_null()
1516
 
           && field.options().default_null()
1517
 
           && field.type() != Table::Field::BLOB)
1518
 
  {
1519
 
    destination.append(" DEFAULT NULL");
 
1193
    destination.append(" DEFAULT 0x", 11);
 
1194
    for (size_t x= 0; x < v.length(); x++)
 
1195
    {
 
1196
      printf("%.2x", *(v.c_str() + x));
 
1197
    }
1520
1198
  }
1521
1199
 
1522
 
  if (field.has_options() && field.options().has_update_expression())
1523
 
  {
1524
 
    destination.append(" ON UPDATE ");
1525
 
    destination.append(field.options().update_expression());
1526
 
  }
 
1200
  if (field.type() == Table::Field::TIMESTAMP)
 
1201
    if (field.timestamp_options().has_auto_updates() &&
 
1202
        field.timestamp_options().auto_updates())
 
1203
      destination.append(" ON UPDATE CURRENT_TIMESTAMP", 28);
1527
1204
 
1528
1205
  if (field.has_comment())
1529
1206
  {
1530
 
    destination.append(" COMMENT ");
1531
 
    append_escaped_string(&destination, field.comment(), quoted_default);
 
1207
    destination.append(" COMMENT ", 9);
 
1208
    destination.push_back(quoted_identifier);
 
1209
    destination.append(field.comment());
 
1210
    destination.push_back(quoted_identifier);
1532
1211
  }
1533
1212
  return NONE;
1534
1213
}
1541
1220
  case Table::Field::DECIMAL:
1542
1221
  case Table::Field::INTEGER:
1543
1222
  case Table::Field::BIGINT:
 
1223
  case Table::Field::ENUM:
1544
1224
    return false;
1545
1225
  default:
1546
1226
    return true;
1557
1237
  case DRIZZLE_TYPE_NULL:
1558
1238
    assert(false); /* Not a user definable type */
1559
1239
    return Table::Field::INTEGER; /* unreachable */
1560
 
  case DRIZZLE_TYPE_MICROTIME:
1561
1240
  case DRIZZLE_TYPE_TIMESTAMP:
1562
 
    return Table::Field::EPOCH;
 
1241
    return Table::Field::TIMESTAMP;
1563
1242
  case DRIZZLE_TYPE_LONGLONG:
1564
1243
    return Table::Field::BIGINT;
1565
1244
  case DRIZZLE_TYPE_DATETIME:
1566
1245
    return Table::Field::DATETIME;
1567
 
  case DRIZZLE_TYPE_TIME:
1568
 
    return Table::Field::TIME;
1569
1246
  case DRIZZLE_TYPE_DATE:
1570
1247
    return Table::Field::DATE;
1571
1248
  case DRIZZLE_TYPE_VARCHAR:
1576
1253
    return Table::Field::ENUM;
1577
1254
  case DRIZZLE_TYPE_BLOB:
1578
1255
    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
1256
  }
1584
1257
 
1585
1258
  assert(false);