~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/statement_transform.cc

  • Committer: Monty Taylor
  • Date: 2011-02-14 20:25:28 UTC
  • mto: (2168.1.3 build)
  • mto: This revision was merged to the branch mainline in revision 2169.
  • Revision ID: mordred@inaugust.com-20110214202528-wfb2a5h51ntbl5ct
Fix daemonize c++ patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
318
318
      sql_strings.push_back(destination);
319
319
    }
320
320
    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;
321
331
  case Statement::SET_VARIABLE:
322
332
    {
323
333
      assert(source.has_set_variable_statement());
806
816
}
807
817
 
808
818
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
809
850
transformDropSchemaStatementToSql(const DropSchemaStatement &statement,
810
851
                                  string &destination,
811
852
                                  enum TransformSqlVariant sql_variant)
833
874
 
834
875
  const Schema &schema= statement.schema();
835
876
 
836
 
  destination.append("CREATE SCHEMA ", 14);
 
877
  destination.append("CREATE SCHEMA ");
837
878
  destination.push_back(quoted_identifier);
838
879
  destination.append(schema.name());
839
880
  destination.push_back(quoted_identifier);
840
881
 
841
882
  if (schema.has_collation())
842
883
  {
843
 
    destination.append(" COLLATE ", 9);
 
884
    destination.append(" COLLATE ");
844
885
    destination.append(schema.collation());
845
886
  }
846
887
 
858
899
 
859
900
  const TableMetadata &table_metadata= statement.table_metadata();
860
901
 
861
 
  destination.append("DROP TABLE ", 11);
 
902
  destination.append("DROP TABLE ");
862
903
 
863
904
  /* Add the IF EXISTS clause if necessary */
864
905
  if (statement.has_if_exists_clause() &&
865
906
      statement.if_exists_clause() == true)
866
907
  {
867
 
    destination.append("IF EXISTS ", 10);
 
908
    destination.append("IF EXISTS ");
868
909
  }
869
910
 
870
911
  destination.push_back(quoted_identifier);
889
930
 
890
931
  const TableMetadata &table_metadata= statement.table_metadata();
891
932
 
892
 
  destination.append("TRUNCATE TABLE ", 15);
 
933
  destination.append("TRUNCATE TABLE ");
893
934
  destination.push_back(quoted_identifier);
894
935
  destination.append(table_metadata.schema_name());
895
936
  destination.push_back(quoted_identifier);
910
951
  const FieldMetadata &variable_metadata= statement.variable_metadata();
911
952
  bool should_quote_field_value= shouldQuoteFieldValue(variable_metadata.type());
912
953
 
913
 
  destination.append("SET GLOBAL ", 11); /* Only global variables are replicated */
 
954
  destination.append("SET GLOBAL "); /* Only global variables are replicated */
914
955
  destination.append(variable_metadata.name());
915
956
  destination.push_back('=');
916
957
 
942
983
  if (sql_variant == ANSI)
943
984
    quoted_identifier= '"';
944
985
 
945
 
  destination.append("CREATE ", 7);
 
986
  destination.append("CREATE ");
946
987
 
947
988
  if (table.type() == Table::TEMPORARY)
948
 
    destination.append("TEMPORARY ", 10);
 
989
    destination.append("TEMPORARY ");
949
990
  
950
 
  destination.append("TABLE ", 6);
 
991
  destination.append("TABLE ");
951
992
  if (with_schema)
952
993
  {
953
994
    append_escaped_string(&destination, table.schema(), quoted_identifier);
954
995
    destination.push_back('.');
955
996
  }
956
997
  append_escaped_string(&destination, table.name(), quoted_identifier);
957
 
  destination.append(" (\n", 3);
 
998
  destination.append(" (\n");
958
999
 
959
1000
  enum TransformSqlError result= NONE;
960
1001
  size_t num_fields= table.field_size();
963
1004
    const Table::Field &field= table.field(x);
964
1005
 
965
1006
    if (x != 0)
966
 
      destination.append(",\n", 2);
 
1007
      destination.append(",\n");
967
1008
 
968
1009
    destination.append("  ");
969
1010
 
976
1017
  size_t num_indexes= table.indexes_size();
977
1018
  
978
1019
  if (num_indexes > 0)
979
 
    destination.append(",\n", 2);
 
1020
    destination.append(",\n");
980
1021
 
981
1022
  for (size_t x= 0; x < num_indexes; ++x)
982
1023
  {
983
1024
    const message::Table::Index &index= table.indexes(x);
984
1025
 
985
1026
    if (x != 0)
986
 
      destination.append(",\n", 2);
 
1027
      destination.append(",\n");
987
1028
 
988
1029
    result= transformIndexDefinitionToSql(index, table, destination, sql_variant);
989
1030
    
994
1035
  size_t num_foreign_keys= table.fk_constraint_size();
995
1036
 
996
1037
  if (num_foreign_keys > 0)
997
 
    destination.append(",\n", 2);
 
1038
    destination.append(",\n");
998
1039
 
999
1040
  for (size_t x= 0; x < num_foreign_keys; ++x)
1000
1041
  {
1001
1042
    const message::Table::ForeignKeyConstraint &fkey= table.fk_constraint(x);
1002
1043
 
1003
1044
    if (x != 0)
1004
 
      destination.append(",\n", 2);
 
1045
      destination.append(",\n");
1005
1046
 
1006
1047
    result= transformForeignKeyConstraintDefinitionToSql(fkey, table, destination, sql_variant);
1007
1048
 
1009
1050
      return result;
1010
1051
  }
1011
1052
 
1012
 
  destination.append("\n)", 2);
 
1053
  destination.append("\n)");
1013
1054
 
1014
1055
  /* Add ENGINE = " clause */
1015
1056
  if (table.has_engine())
1016
1057
  {
1017
 
    destination.append(" ENGINE=", 8);
 
1058
    destination.append(" ENGINE=");
1018
1059
    destination.append(table.engine().name());
1019
1060
 
1020
1061
    size_t num_engine_options= table.engine().options_size();
1024
1065
    {
1025
1066
      const Engine::Option &option= table.engine().options(x);
1026
1067
      destination.append(option.name());
1027
 
      destination.append("='", 2);
 
1068
      destination.append("='");
1028
1069
      destination.append(option.state());
1029
 
      destination.append("'", 1);
1030
 
      if(x != num_engine_options-1)
1031
 
        destination.append(", ", 2);
 
1070
      destination.append("'");
 
1071
      if (x != num_engine_options-1)
 
1072
      {
 
1073
        destination.append(", ");
 
1074
      }
1032
1075
    }
1033
1076
  }
1034
1077
 
1048
1091
 
1049
1092
  if (options.has_comment())
1050
1093
  {
1051
 
    destination.append(" COMMENT=", 9);
 
1094
    destination.append(" COMMENT=");
1052
1095
    append_escaped_string(&destination, options.comment());
1053
1096
  }
1054
1097
 
1055
1098
  if (options.has_collation())
1056
1099
  {
1057
 
    destination.append(" COLLATE = ", 11);
 
1100
    destination.append(" COLLATE = ");
1058
1101
    destination.append(options.collation());
1059
1102
  }
1060
1103
 
1061
1104
  if (options.has_data_file_name())
1062
1105
  {
1063
 
    destination.append("\nDATA_FILE_NAME = '", 19);
 
1106
    destination.append("\nDATA_FILE_NAME = '");
1064
1107
    destination.append(options.data_file_name());
1065
1108
    destination.push_back('\'');
1066
1109
  }
1067
1110
 
1068
1111
  if (options.has_index_file_name())
1069
1112
  {
1070
 
    destination.append("\nINDEX_FILE_NAME = '", 20);
 
1113
    destination.append("\nINDEX_FILE_NAME = '");
1071
1114
    destination.append(options.index_file_name());
1072
1115
    destination.push_back('\'');
1073
1116
  }
1074
1117
 
1075
1118
  if (options.has_max_rows())
1076
1119
  {
1077
 
    destination.append("\nMAX_ROWS = ", 12);
 
1120
    destination.append("\nMAX_ROWS = ");
1078
1121
    destination.append(boost::lexical_cast<string>(options.max_rows()));
1079
1122
  }
1080
1123
 
1081
1124
  if (options.has_min_rows())
1082
1125
  {
1083
 
    destination.append("\nMIN_ROWS = ", 12);
 
1126
    destination.append("\nMIN_ROWS = ");
1084
1127
    destination.append(boost::lexical_cast<string>(options.min_rows()));
1085
1128
  }
1086
1129
 
1087
1130
  if (options.has_user_set_auto_increment_value()
1088
1131
      && options.has_auto_increment_value())
1089
1132
  {
1090
 
    destination.append(" AUTO_INCREMENT=", 16);
 
1133
    destination.append(" AUTO_INCREMENT=");
1091
1134
    destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
1092
1135
  }
1093
1136
 
1094
1137
  if (options.has_avg_row_length())
1095
1138
  {
1096
 
    destination.append("\nAVG_ROW_LENGTH = ", 18);
 
1139
    destination.append("\nAVG_ROW_LENGTH = ");
1097
1140
    destination.append(boost::lexical_cast<string>(options.avg_row_length()));
1098
1141
  }
1099
1142
 
1100
1143
  if (options.has_checksum() &&
1101
1144
      options.checksum())
1102
 
    destination.append("\nCHECKSUM = TRUE", 16);
 
1145
    destination.append("\nCHECKSUM = TRUE");
1103
1146
  if (options.has_page_checksum() &&
1104
1147
      options.page_checksum())
1105
 
    destination.append("\nPAGE_CHECKSUM = TRUE", 21);
 
1148
    destination.append("\nPAGE_CHECKSUM = TRUE");
1106
1149
 
1107
1150
  return NONE;
1108
1151
}
1120
1163
  destination.append("  ", 2);
1121
1164
 
1122
1165
  if (index.is_primary())
1123
 
    destination.append("PRIMARY ", 8);
 
1166
    destination.append("PRIMARY ");
1124
1167
  else if (index.is_unique())
1125
 
    destination.append("UNIQUE ", 7);
 
1168
    destination.append("UNIQUE ");
1126
1169
 
1127
1170
  destination.append("KEY ", 4);
1128
1171
  if (! (index.is_primary() && index.name().compare("PRIMARY")==0))
1174
1217
  case Table::Index::UNKNOWN_INDEX:
1175
1218
    break;
1176
1219
  case Table::Index::BTREE:
1177
 
    destination.append(" USING BTREE", 12);
 
1220
    destination.append(" USING BTREE");
1178
1221
    break;
1179
1222
  case Table::Index::RTREE:
1180
 
    destination.append(" USING RTREE", 12);
 
1223
    destination.append(" USING RTREE");
1181
1224
    break;
1182
1225
  case Table::Index::HASH:
1183
 
    destination.append(" USING HASH", 11);
 
1226
    destination.append(" USING HASH");
1184
1227
    break;
1185
1228
  case Table::Index::FULLTEXT:
1186
 
    destination.append(" USING FULLTEXT", 15);
 
1229
    destination.append(" USING FULLTEXT");
1187
1230
    break;
1188
1231
  }
1189
1232
 
1190
1233
  if (index.has_comment())
1191
1234
  {
1192
 
    destination.append(" COMMENT ", 9);
 
1235
    destination.append(" COMMENT ");
1193
1236
    append_escaped_string(&destination, index.comment());
1194
1237
  }
1195
1238
 
1229
1272
  if (sql_variant == ANSI)
1230
1273
    quoted_identifier= '"';
1231
1274
 
1232
 
  destination.append("  ", 2);
 
1275
  destination.append("  ");
1233
1276
 
1234
1277
  if (fkey.has_name())
1235
1278
  {
1236
 
    destination.append("CONSTRAINT ", 11);
 
1279
    destination.append("CONSTRAINT ");
1237
1280
    append_escaped_string(&destination, fkey.name(), quoted_identifier);
1238
1281
    destination.append(" ", 1);
1239
1282
  }
1240
1283
 
1241
 
  destination.append("FOREIGN KEY (", 13);
 
1284
  destination.append("FOREIGN KEY (");
1242
1285
 
1243
1286
  for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
1244
1287
  {
1249
1292
                          quoted_identifier);
1250
1293
  }
1251
1294
 
1252
 
  destination.append(") REFERENCES ", 13);
 
1295
  destination.append(") REFERENCES ");
1253
1296
 
1254
1297
  append_escaped_string(&destination, fkey.references_table_name(),
1255
1298
                        quoted_identifier);
1256
 
  destination.append(" (", 2);
 
1299
  destination.append(" (");
1257
1300
 
1258
1301
  for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
1259
1302
  {
1268
1311
 
1269
1312
  if (fkey.has_update_option() and fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1270
1313
  {
1271
 
    destination.append(" ON UPDATE ", 11);
 
1314
    destination.append(" ON UPDATE ");
1272
1315
    transformForeignKeyOptionToSql(fkey.update_option(), destination);
1273
1316
  }
1274
1317
 
1275
1318
  if (fkey.has_delete_option() and fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1276
1319
  {
1277
 
    destination.append(" ON DELETE ", 11);
 
1320
    destination.append(" ON DELETE ");
1278
1321
    transformForeignKeyOptionToSql(fkey.delete_option(), destination);
1279
1322
  }
1280
1323
 
1304
1347
  switch (field_type)
1305
1348
  {
1306
1349
    case Table::Field::DOUBLE:
1307
 
    destination.append(" DOUBLE", 7);
 
1350
    destination.append(" DOUBLE");
1308
1351
    if (field.has_numeric_options()
1309
1352
        && field.numeric_options().has_precision())
1310
1353
    {
1318
1361
    {
1319
1362
      if (field.string_options().has_collation()
1320
1363
          && field.string_options().collation().compare("binary") == 0)
1321
 
        destination.append(" VARBINARY(", 11);
 
1364
        destination.append(" VARBINARY(");
1322
1365
      else
1323
 
        destination.append(" VARCHAR(", 9);
 
1366
        destination.append(" VARCHAR(");
1324
1367
 
1325
1368
      destination.append(boost::lexical_cast<string>(field.string_options().length()));
1326
1369
      destination.append(")");
1330
1373
    {
1331
1374
      if (field.string_options().has_collation()
1332
1375
          && field.string_options().collation().compare("binary") == 0)
1333
 
        destination.append(" BLOB", 5);
 
1376
        destination.append(" BLOB");
1334
1377
      else
1335
 
        destination.append(" TEXT", 5);
 
1378
        destination.append(" TEXT");
1336
1379
    }
1337
1380
    break;
1338
1381
  case Table::Field::ENUM:
1339
1382
    {
1340
1383
      size_t num_field_values= field.enumeration_values().field_value_size();
1341
 
      destination.append(" ENUM(", 6);
 
1384
      destination.append(" ENUM(");
1342
1385
      for (size_t x= 0; x < num_field_values; ++x)
1343
1386
      {
1344
1387
        const string &type= field.enumeration_values().field_value(x);
1354
1397
      break;
1355
1398
    }
1356
1399
  case Table::Field::UUID:
1357
 
    destination.append(" UUID", 5);
 
1400
    destination.append(" UUID");
 
1401
    break;
 
1402
  case Table::Field::BOOLEAN:
 
1403
    destination.append(" BOOLEAN");
1358
1404
    break;
1359
1405
  case Table::Field::INTEGER:
1360
 
    destination.append(" INT", 4);
 
1406
    destination.append(" INT");
1361
1407
    break;
1362
1408
  case Table::Field::BIGINT:
1363
 
    destination.append(" BIGINT", 7);
 
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
    }
1364
1418
    break;
1365
1419
  case Table::Field::DECIMAL:
1366
1420
    {
1367
 
      destination.append(" DECIMAL(", 9);
 
1421
      destination.append(" DECIMAL(");
1368
1422
      stringstream ss;
1369
1423
      ss << field.numeric_options().precision() << ",";
1370
1424
      ss << field.numeric_options().scale() << ")";
1372
1426
    }
1373
1427
    break;
1374
1428
  case Table::Field::DATE:
1375
 
    destination.append(" DATE", 5);
1376
 
    break;
1377
 
  case Table::Field::TIMESTAMP:
1378
 
    destination.append(" TIMESTAMP",  10);
1379
 
    break;
 
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
 
1380
1443
  case Table::Field::DATETIME:
1381
 
    destination.append(" DATETIME",  9);
1382
 
    break;
1383
 
  }
1384
 
 
1385
 
  if (field.type() == Table::Field::INTEGER || 
1386
 
      field.type() == Table::Field::BIGINT)
1387
 
  {
1388
 
    if (field.has_constraints() &&
1389
 
        field.constraints().has_is_unsigned() &&
1390
 
        field.constraints().is_unsigned())
1391
 
    {
1392
 
      destination.append(" UNSIGNED", 9);
1393
 
    }
 
1444
    destination.append(" DATETIME");
 
1445
    break;
 
1446
  case Table::Field::TIME:
 
1447
    destination.append(" TIME");
 
1448
    break;
1394
1449
  }
1395
1450
 
1396
1451
  if (field.type() == Table::Field::BLOB ||
1399
1454
    if (field.string_options().has_collation()
1400
1455
        && field.string_options().collation().compare("binary"))
1401
1456
    {
1402
 
      destination.append(" COLLATE ", 9);
 
1457
      destination.append(" COLLATE ");
1403
1458
      destination.append(field.string_options().collation());
1404
1459
    }
1405
1460
  }
1406
1461
 
1407
 
  if (field.has_constraints() &&
1408
 
      ! field.constraints().is_nullable())
1409
 
  {
1410
 
    destination.append(" NOT NULL", 9);
1411
 
  }
1412
 
  else if (field.type() == Table::Field::TIMESTAMP)
1413
 
    destination.append(" NULL", 5);
 
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
  }
1414
1475
 
1415
1476
  if (field.type() == Table::Field::INTEGER || 
1416
1477
      field.type() == Table::Field::BIGINT)
1419
1480
    if (field.has_numeric_options() &&
1420
1481
        field.numeric_options().is_autoincrement())
1421
1482
    {
1422
 
      destination.append(" AUTO_INCREMENT", 15);
 
1483
      destination.append(" AUTO_INCREMENT");
1423
1484
    }
1424
1485
  }
1425
1486
 
1426
1487
  if (field.options().has_default_value())
1427
1488
  {
1428
 
    destination.append(" DEFAULT ", 9);
 
1489
    destination.append(" DEFAULT ");
1429
1490
    append_escaped_string(&destination, field.options().default_value());
1430
1491
  }
1431
1492
  else if (field.options().has_default_expression())
1432
1493
  {
1433
 
    destination.append(" DEFAULT ", 9);
 
1494
    destination.append(" DEFAULT ");
1434
1495
    destination.append(field.options().default_expression());
1435
1496
  }
1436
1497
  else if (field.options().has_default_bin_value())
1437
1498
  {
1438
1499
    const string &v= field.options().default_bin_value();
1439
1500
    if (v.length() == 0)
1440
 
      destination.append(" DEFAULT ''", 11);
 
1501
    {
 
1502
      destination.append(" DEFAULT ''");
 
1503
    }
1441
1504
    else
1442
1505
    {
1443
 
      destination.append(" DEFAULT 0x", 11);
 
1506
      destination.append(" DEFAULT 0x");
1444
1507
      for (size_t x= 0; x < v.length(); x++)
1445
1508
      {
1446
1509
        char hex[3];
1453
1516
           && field.options().default_null()
1454
1517
           && field.type() != Table::Field::BLOB)
1455
1518
  {
1456
 
    destination.append(" DEFAULT NULL", 13);
 
1519
    destination.append(" DEFAULT NULL");
1457
1520
  }
1458
1521
 
1459
1522
  if (field.has_options() && field.options().has_update_expression())
1460
1523
  {
1461
 
    destination.append(" ON UPDATE ", 11);
 
1524
    destination.append(" ON UPDATE ");
1462
1525
    destination.append(field.options().update_expression());
1463
1526
  }
1464
1527
 
1465
1528
  if (field.has_comment())
1466
1529
  {
1467
 
    destination.append(" COMMENT ", 9);
 
1530
    destination.append(" COMMENT ");
1468
1531
    append_escaped_string(&destination, field.comment(), quoted_default);
1469
1532
  }
1470
1533
  return NONE;
1494
1557
  case DRIZZLE_TYPE_NULL:
1495
1558
    assert(false); /* Not a user definable type */
1496
1559
    return Table::Field::INTEGER; /* unreachable */
 
1560
  case DRIZZLE_TYPE_MICROTIME:
1497
1561
  case DRIZZLE_TYPE_TIMESTAMP:
1498
 
    return Table::Field::TIMESTAMP;
 
1562
    return Table::Field::EPOCH;
1499
1563
  case DRIZZLE_TYPE_LONGLONG:
1500
1564
    return Table::Field::BIGINT;
1501
1565
  case DRIZZLE_TYPE_DATETIME:
1502
1566
    return Table::Field::DATETIME;
 
1567
  case DRIZZLE_TYPE_TIME:
 
1568
    return Table::Field::TIME;
1503
1569
  case DRIZZLE_TYPE_DATE:
1504
1570
    return Table::Field::DATE;
1505
1571
  case DRIZZLE_TYPE_VARCHAR:
1512
1578
    return Table::Field::BLOB;
1513
1579
  case DRIZZLE_TYPE_UUID:
1514
1580
    return Table::Field::UUID;
 
1581
  case DRIZZLE_TYPE_BOOLEAN:
 
1582
    return Table::Field::BOOLEAN;
1515
1583
  }
1516
1584
 
1517
1585
  assert(false);