~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/statement_transform.cc

  • Committer: Jay Pipes
  • Date: 2010-02-27 23:47:33 UTC
  • mto: (1309.2.14 build)
  • mto: This revision was merged to the branch mainline in revision 1319.
  • Revision ID: jpipes@serialcoder-20100227234733-0e4pq1zxwluxud00
* Adds CREATE TABLE as a specific CreateTableStatement message in the
replication stream.
* Corrects previous incorrect behaviour where non-MyISAM tables were
incorrectly being defined with PACK_RECORD = TRUE.  This affected the
checksumming of the table.
* There is still one remaining test case failure in the create_table.test
case in the transaction log test suite regarding the incorrect setting
of Table::Index::IndexPart::compare_length.

Show diffs side-by-side

added added

removed removed

Lines of Context:
189
189
      }
190
190
    }
191
191
    break;
 
192
  case Statement::CREATE_TABLE:
 
193
    {
 
194
      assert(source.has_create_table_statement());
 
195
      string destination;
 
196
      error= transformCreateTableStatementToSql(source.create_table_statement(),
 
197
                                                destination,
 
198
                                                sql_variant);
 
199
      sql_strings.push_back(destination);
 
200
    }
 
201
    break;
192
202
  case Statement::TRUNCATE_TABLE:
193
203
    {
194
204
      assert(source.has_truncate_table_statement());
199
209
      sql_strings.push_back(destination);
200
210
    }
201
211
    break;
 
212
  case Statement::DROP_TABLE:
 
213
    {
 
214
      assert(source.has_drop_table_statement());
 
215
      string destination;
 
216
      error= transformDropTableStatementToSql(source.drop_table_statement(),
 
217
                                              destination,
 
218
                                              sql_variant);
 
219
      sql_strings.push_back(destination);
 
220
    }
 
221
    break;
202
222
  case Statement::CREATE_SCHEMA:
203
223
    {
204
224
      assert(source.has_create_schema_statement());
219
239
      sql_strings.push_back(destination);
220
240
    }
221
241
    break;
222
 
  case Statement::DROP_TABLE:
223
 
    {
224
 
      assert(source.has_drop_table_statement());
225
 
      string destination;
226
 
      error= transformDropTableStatementToSql(source.drop_table_statement(),
227
 
                                              destination,
228
 
                                              sql_variant);
229
 
      sql_strings.push_back(destination);
230
 
    }
231
 
    break;
232
242
  case Statement::SET_VARIABLE:
233
243
    {
234
244
      assert(source.has_set_variable_statement());
824
834
  destination.push_back(quoted_identifier);
825
835
  destination.append(table.name());
826
836
  destination.push_back(quoted_identifier);
827
 
  destination.append("(\n", 2);
 
837
  destination.append(" (\n", 3);
828
838
 
829
839
  enum TransformSqlError result= NONE;
830
840
  size_t num_fields= table.field_size();
842
852
  }
843
853
 
844
854
  size_t num_indexes= table.indexes_size();
 
855
  
 
856
  if (num_indexes > 0)
 
857
    destination.append(",\n", 2);
 
858
 
845
859
  for (size_t x= 0; x < num_indexes; ++x)
846
860
  {
847
861
    const message::Table::Index &index= table.indexes(x);
854
868
    if (result != NONE)
855
869
      return result;
856
870
  }
857
 
  destination.append("\n)\n", 2);
 
871
  destination.append("\n)", 2);
858
872
 
859
873
  /* Add ENGINE = " clause */
860
874
  if (table.has_engine())
861
875
  {
862
876
    const Table::StorageEngine &engine= table.engine();
863
 
    destination.append("ENGINE = ", 9);
 
877
    destination.append("\nENGINE = ", 10);
864
878
    destination.append(engine.name());
865
 
    destination.push_back('\n');
866
879
 
867
880
    size_t num_engine_options= engine.option_size();
868
881
    for (size_t x= 0; x < num_engine_options; ++x)
869
882
    {
870
883
      const Table::StorageEngine::EngineOption &option= engine.option(x);
871
 
      destination.push_back('\t');
 
884
      destination.push_back('\n');
872
885
      destination.append(option.option_name());
873
886
      destination.append(" = ", 3);
874
887
      destination.append(option.option_value());
894
907
 
895
908
  if (options.has_comment())
896
909
  {
897
 
    destination.append(" COMMENT = '", 12);
 
910
    destination.append("\nCOMMENT = '", 12);
898
911
    destination.append(options.comment());
899
 
    destination.append("'\n", 2);
 
912
    destination.push_back('\'');
900
913
  }
901
914
 
902
915
  if (options.has_collation())
903
916
  {
904
 
    destination.append(" COLLATE = '", 12);
 
917
    destination.append("\nCOLLATE = ", 11);
905
918
    destination.append(options.collation());
906
 
    destination.append("'\n", 2);
907
919
  }
908
920
 
909
921
  if (options.has_auto_increment())
910
922
  {
911
923
    ss << options.auto_increment();
912
 
    destination.append(" AUTOINCREMENT_OFFSET = ", 24);
 
924
    destination.append("\nAUTOINCREMENT_OFFSET = ", 24);
913
925
    destination.append(ss.str());
914
 
    destination.push_back('\n');
915
926
    ss.clear();
916
927
  }
917
928
  
918
929
  if (options.has_row_type())
919
930
  {
920
931
    ss << options.row_type();
921
 
    destination.append(" ROW_TYPE = ", 12);
 
932
    destination.append("\nROW_TYPE = ", 12);
922
933
    destination.append(ss.str());
923
 
    destination.push_back('\n');
924
934
    ss.clear();
925
935
  }
926
936
 
927
937
  if (options.has_data_file_name())
928
938
  {
929
 
    destination.append(" DATA_FILE_NAME = '", 19);
 
939
    destination.append("\nDATA_FILE_NAME = '", 19);
930
940
    destination.append(options.data_file_name());
931
 
    destination.append("'\n", 2);
 
941
    destination.push_back('\'');
932
942
  }
933
943
 
934
944
  if (options.has_index_file_name())
935
945
  {
936
 
    destination.append(" INDEX_FILE_NAME = '", 20);
 
946
    destination.append("\nINDEX_FILE_NAME = '", 20);
937
947
    destination.append(options.index_file_name());
938
 
    destination.append("'\n", 2);
 
948
    destination.push_back('\'');
939
949
  }
940
950
 
941
951
  if (options.has_max_rows())
942
952
  {
943
953
    ss << options.max_rows();
944
 
    destination.append(" MAX_ROWS = ", 12);
 
954
    destination.append("\nMAX_ROWS = ", 12);
945
955
    destination.append(ss.str());
946
 
    destination.push_back('\n');
947
956
    ss.clear();
948
957
  }
949
958
 
950
959
  if (options.has_min_rows())
951
960
  {
952
961
    ss << options.min_rows();
953
 
    destination.append(" MIN_ROWS = ", 12);
 
962
    destination.append("\nMIN_ROWS = ", 12);
954
963
    destination.append(ss.str());
955
 
    destination.push_back('\n');
956
964
    ss.clear();
957
965
  }
958
966
 
959
967
  if (options.has_auto_increment_value())
960
968
  {
961
969
    ss << options.auto_increment_value();
962
 
    destination.append(" AUTO_INCREMENT = ", 18);
 
970
    destination.append("\nAUTO_INCREMENT = ", 18);
963
971
    destination.append(ss.str());
964
 
    destination.push_back('\n');
965
972
    ss.clear();
966
973
  }
967
974
 
968
975
  if (options.has_avg_row_length())
969
976
  {
970
977
    ss << options.avg_row_length();
971
 
    destination.append(" AVG_ROW_LENGTH = ", 18);
 
978
    destination.append("\nAVG_ROW_LENGTH = ", 18);
972
979
    destination.append(ss.str());
973
 
    destination.push_back('\n');
974
980
    ss.clear();
975
981
  }
976
982
 
977
983
  if (options.has_key_block_size())
978
984
  {
979
985
    ss << options.key_block_size();
980
 
    destination.append(" KEY_BLOCK_SIZE = ", 18);
 
986
    destination.append("\nKEY_BLOCK_SIZE = ", 18);
981
987
    destination.append(ss.str());
982
 
    destination.push_back('\n');
983
988
    ss.clear();
984
989
  }
985
990
 
986
991
  if (options.has_block_size())
987
992
  {
988
993
    ss << options.block_size();
989
 
    destination.append(" BLOCK_SIZE = ", 14);
 
994
    destination.append("\nBLOCK_SIZE = ", 14);
990
995
    destination.append(ss.str());
991
 
    destination.push_back('\n');
992
996
    ss.clear();
993
997
  }
994
998
 
995
999
  if (options.has_pack_keys() &&
996
1000
      options.pack_keys())
997
 
    destination.append(" PACK_KEYS = TRUE\n", 18);
 
1001
    destination.append("\nPACK_KEYS = TRUE", 17);
998
1002
  if (options.has_pack_record() &&
999
1003
      options.pack_record())
1000
 
    destination.append(" PACK_RECORD = TRUE\n", 20);
 
1004
    destination.append("\nPACK_RECORD = TRUE", 19);
1001
1005
  if (options.has_checksum() &&
1002
1006
      options.checksum())
1003
 
    destination.append(" CHECKSUM = TRUE\n", 17);
 
1007
    destination.append("\nCHECKSUM = TRUE", 16);
1004
1008
  if (options.has_page_checksum() &&
1005
1009
      options.page_checksum())
1006
 
    destination.append(" PAGE_CHECKSUM = TRUE\n", 22);
 
1010
    destination.append("\nPAGE_CHECKSUM = TRUE", 21);
1007
1011
 
1008
1012
  return NONE;
1009
1013
}
1033
1037
  for (size_t x= 0; x < num_parts; ++x)
1034
1038
  {
1035
1039
    const Table::Index::IndexPart &part= index.index_part(x);
 
1040
    const Table::Field &field= table.field(part.fieldnr());
1036
1041
 
1037
1042
    if (x != 0)
1038
1043
      destination.push_back(',');
1039
1044
    
1040
1045
    destination.push_back(quoted_identifier);
1041
 
    destination.append(table.field(part.fieldnr()).name());
 
1046
    destination.append(field.name());
1042
1047
    destination.push_back(quoted_identifier);
1043
1048
 
1044
 
    if (part.has_compare_length())
 
1049
    /* 
 
1050
     * If the index part's field type is VARCHAR or TEXT
 
1051
     * then check for a prefix length
 
1052
     */
 
1053
    if (field.type() == Table::Field::VARCHAR ||
 
1054
        field.type() == Table::Field::BLOB)
1045
1055
    {
1046
 
      stringstream ss;
1047
 
      destination.push_back('(');
1048
 
      ss << part.compare_length();
1049
 
      destination.append(ss.str());
1050
 
      destination.push_back(')');
 
1056
      if (part.has_compare_length() &&
 
1057
          part.compare_length() != field.string_options().length())
 
1058
      {
 
1059
        stringstream ss;
 
1060
        destination.push_back('(');
 
1061
        ss << part.compare_length();
 
1062
        destination.append(ss.str());
 
1063
        destination.push_back(')');
 
1064
      }
1051
1065
    }
1052
1066
  }
1053
1067
  destination.push_back(')');
1073
1087
  switch (field_type)
1074
1088
  {
1075
1089
    case Table::Field::DOUBLE:
1076
 
    destination.append(" DOUBLE ", 8);
 
1090
    destination.append(" DOUBLE", 7);
1077
1091
    break;
1078
1092
  case Table::Field::VARCHAR:
1079
1093
    {
1084
1098
    }
1085
1099
    break;
1086
1100
  case Table::Field::BLOB:
1087
 
    destination.append(" BLOB ", 6);
1088
 
    if (field.string_options().has_collation_id())
1089
 
    {
1090
 
      destination.append("COLLATION=", 10);
1091
 
      destination.append(field.string_options().collation());
1092
 
      destination.push_back(' ');
1093
 
    }
 
1101
    destination.append(" BLOB", 5);
1094
1102
    break;
1095
1103
  case Table::Field::ENUM:
1096
1104
    {
1108
1116
        destination.push_back('\'');
1109
1117
      }
1110
1118
      destination.push_back(')');
1111
 
      destination.push_back(' ');
1112
1119
      break;
1113
1120
    }
1114
1121
  case Table::Field::INTEGER:
1115
 
    destination.append(" INT ", 5);
 
1122
    destination.append(" INT", 4);
1116
1123
    break;
1117
1124
  case Table::Field::BIGINT:
1118
 
    destination.append(" BIGINT ", 8);
 
1125
    destination.append(" BIGINT", 7);
1119
1126
    break;
1120
1127
  case Table::Field::DECIMAL:
1121
1128
    {
1122
1129
      destination.append(" DECIMAL(", 9);
1123
1130
      stringstream ss;
1124
1131
      ss << field.numeric_options().precision() << ",";
1125
 
      ss << field.numeric_options().scale() << ") ";
 
1132
      ss << field.numeric_options().scale() << ")";
1126
1133
      destination.append(ss.str());
1127
1134
    }
1128
1135
    break;
1129
1136
  case Table::Field::DATE:
1130
 
    destination.append(" DATE ", 6);
 
1137
    destination.append(" DATE", 5);
1131
1138
    break;
1132
1139
  case Table::Field::TIMESTAMP:
1133
 
    destination.append(" TIMESTAMP ",  11);
 
1140
    destination.append(" TIMESTAMP",  10);
1134
1141
    break;
1135
1142
  case Table::Field::DATETIME:
1136
 
    destination.append(" DATETIME ",  10);
 
1143
    destination.append(" DATETIME",  9);
1137
1144
    break;
1138
1145
  }
1139
1146
 
1146
1153
    {
1147
1154
      destination.append(" UNSIGNED", 9);
1148
1155
    }
1149
 
 
 
1156
  }
 
1157
 
 
1158
 
 
1159
  if (! (field.has_constraints() &&
 
1160
         field.constraints().is_nullable()))
 
1161
  {
 
1162
    destination.append(" NOT", 4);
 
1163
  }
 
1164
  destination.append(" NULL", 5);
 
1165
 
 
1166
  if (field.type() == Table::Field::INTEGER || 
 
1167
      field.type() == Table::Field::BIGINT)
 
1168
  {
 
1169
    /* AUTO_INCREMENT must be after NOT NULL */
1150
1170
    if (field.has_numeric_options() &&
1151
1171
        field.numeric_options().is_autoincrement())
1152
1172
    {
1153
 
      destination.append(" AUTOINCREMENT ", 15);
 
1173
      destination.append(" AUTO_INCREMENT", 15);
1154
1174
    }
1155
1175
  }
1156
1176
 
1157
 
 
1158
 
  if (! (field.has_constraints() &&
1159
 
         field.constraints().is_nullable()))
1160
 
  {
1161
 
    destination.append(" NOT NULL ", 10);
1162
 
  }
1163
 
 
1164
1177
  if (field.type() == Table::Field::BLOB ||
1165
1178
      field.type() == Table::Field::VARCHAR)
1166
1179
  {
1177
1190
    destination.push_back(quoted_identifier);
1178
1191
    destination.append(field.options().default_value());
1179
1192
    destination.push_back(quoted_identifier);
1180
 
    destination.push_back(' ');
1181
1193
  }
1182
1194
 
1183
1195
  if (field.options().has_default_bin_value())
1201
1213
    destination.push_back(quoted_identifier);
1202
1214
    destination.append(field.comment());
1203
1215
    destination.push_back(quoted_identifier);
1204
 
    destination.push_back(' ');
1205
1216
  }
1206
1217
  return NONE;
1207
1218
}