~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/statement_transform.cc

  • Committer: Brian Aker
  • Date: 2011-02-03 20:32:09 UTC
  • mfrom: (2139.1.4 drizzle-build)
  • Revision ID: brian@tangent.org-20110203203209-r1t4knmy15x5n1w2
Rollup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
874
874
 
875
875
  const Schema &schema= statement.schema();
876
876
 
877
 
  destination.append("CREATE SCHEMA ", 14);
 
877
  destination.append("CREATE SCHEMA ");
878
878
  destination.push_back(quoted_identifier);
879
879
  destination.append(schema.name());
880
880
  destination.push_back(quoted_identifier);
881
881
 
882
882
  if (schema.has_collation())
883
883
  {
884
 
    destination.append(" COLLATE ", 9);
 
884
    destination.append(" COLLATE ");
885
885
    destination.append(schema.collation());
886
886
  }
887
887
 
899
899
 
900
900
  const TableMetadata &table_metadata= statement.table_metadata();
901
901
 
902
 
  destination.append("DROP TABLE ", 11);
 
902
  destination.append("DROP TABLE ");
903
903
 
904
904
  /* Add the IF EXISTS clause if necessary */
905
905
  if (statement.has_if_exists_clause() &&
906
906
      statement.if_exists_clause() == true)
907
907
  {
908
 
    destination.append("IF EXISTS ", 10);
 
908
    destination.append("IF EXISTS ");
909
909
  }
910
910
 
911
911
  destination.push_back(quoted_identifier);
930
930
 
931
931
  const TableMetadata &table_metadata= statement.table_metadata();
932
932
 
933
 
  destination.append("TRUNCATE TABLE ", 15);
 
933
  destination.append("TRUNCATE TABLE ");
934
934
  destination.push_back(quoted_identifier);
935
935
  destination.append(table_metadata.schema_name());
936
936
  destination.push_back(quoted_identifier);
951
951
  const FieldMetadata &variable_metadata= statement.variable_metadata();
952
952
  bool should_quote_field_value= shouldQuoteFieldValue(variable_metadata.type());
953
953
 
954
 
  destination.append("SET GLOBAL ", 11); /* Only global variables are replicated */
 
954
  destination.append("SET GLOBAL "); /* Only global variables are replicated */
955
955
  destination.append(variable_metadata.name());
956
956
  destination.push_back('=');
957
957
 
983
983
  if (sql_variant == ANSI)
984
984
    quoted_identifier= '"';
985
985
 
986
 
  destination.append("CREATE ", 7);
 
986
  destination.append("CREATE ");
987
987
 
988
988
  if (table.type() == Table::TEMPORARY)
989
 
    destination.append("TEMPORARY ", 10);
 
989
    destination.append("TEMPORARY ");
990
990
  
991
 
  destination.append("TABLE ", 6);
 
991
  destination.append("TABLE ");
992
992
  if (with_schema)
993
993
  {
994
994
    append_escaped_string(&destination, table.schema(), quoted_identifier);
995
995
    destination.push_back('.');
996
996
  }
997
997
  append_escaped_string(&destination, table.name(), quoted_identifier);
998
 
  destination.append(" (\n", 3);
 
998
  destination.append(" (\n");
999
999
 
1000
1000
  enum TransformSqlError result= NONE;
1001
1001
  size_t num_fields= table.field_size();
1004
1004
    const Table::Field &field= table.field(x);
1005
1005
 
1006
1006
    if (x != 0)
1007
 
      destination.append(",\n", 2);
 
1007
      destination.append(",\n");
1008
1008
 
1009
1009
    destination.append("  ");
1010
1010
 
1017
1017
  size_t num_indexes= table.indexes_size();
1018
1018
  
1019
1019
  if (num_indexes > 0)
1020
 
    destination.append(",\n", 2);
 
1020
    destination.append(",\n");
1021
1021
 
1022
1022
  for (size_t x= 0; x < num_indexes; ++x)
1023
1023
  {
1024
1024
    const message::Table::Index &index= table.indexes(x);
1025
1025
 
1026
1026
    if (x != 0)
1027
 
      destination.append(",\n", 2);
 
1027
      destination.append(",\n");
1028
1028
 
1029
1029
    result= transformIndexDefinitionToSql(index, table, destination, sql_variant);
1030
1030
    
1035
1035
  size_t num_foreign_keys= table.fk_constraint_size();
1036
1036
 
1037
1037
  if (num_foreign_keys > 0)
1038
 
    destination.append(",\n", 2);
 
1038
    destination.append(",\n");
1039
1039
 
1040
1040
  for (size_t x= 0; x < num_foreign_keys; ++x)
1041
1041
  {
1042
1042
    const message::Table::ForeignKeyConstraint &fkey= table.fk_constraint(x);
1043
1043
 
1044
1044
    if (x != 0)
1045
 
      destination.append(",\n", 2);
 
1045
      destination.append(",\n");
1046
1046
 
1047
1047
    result= transformForeignKeyConstraintDefinitionToSql(fkey, table, destination, sql_variant);
1048
1048
 
1050
1050
      return result;
1051
1051
  }
1052
1052
 
1053
 
  destination.append("\n)", 2);
 
1053
  destination.append("\n)");
1054
1054
 
1055
1055
  /* Add ENGINE = " clause */
1056
1056
  if (table.has_engine())
1057
1057
  {
1058
 
    destination.append(" ENGINE=", 8);
 
1058
    destination.append(" ENGINE=");
1059
1059
    destination.append(table.engine().name());
1060
1060
 
1061
1061
    size_t num_engine_options= table.engine().options_size();
1065
1065
    {
1066
1066
      const Engine::Option &option= table.engine().options(x);
1067
1067
      destination.append(option.name());
1068
 
      destination.append("='", 2);
 
1068
      destination.append("='");
1069
1069
      destination.append(option.state());
1070
 
      destination.append("'", 1);
1071
 
      if(x != num_engine_options-1)
1072
 
        destination.append(", ", 2);
 
1070
      destination.append("'");
 
1071
      if (x != num_engine_options-1)
 
1072
      {
 
1073
        destination.append(", ");
 
1074
      }
1073
1075
    }
1074
1076
  }
1075
1077
 
1089
1091
 
1090
1092
  if (options.has_comment())
1091
1093
  {
1092
 
    destination.append(" COMMENT=", 9);
 
1094
    destination.append(" COMMENT=");
1093
1095
    append_escaped_string(&destination, options.comment());
1094
1096
  }
1095
1097
 
1096
1098
  if (options.has_collation())
1097
1099
  {
1098
 
    destination.append(" COLLATE = ", 11);
 
1100
    destination.append(" COLLATE = ");
1099
1101
    destination.append(options.collation());
1100
1102
  }
1101
1103
 
1102
1104
  if (options.has_data_file_name())
1103
1105
  {
1104
 
    destination.append("\nDATA_FILE_NAME = '", 19);
 
1106
    destination.append("\nDATA_FILE_NAME = '");
1105
1107
    destination.append(options.data_file_name());
1106
1108
    destination.push_back('\'');
1107
1109
  }
1108
1110
 
1109
1111
  if (options.has_index_file_name())
1110
1112
  {
1111
 
    destination.append("\nINDEX_FILE_NAME = '", 20);
 
1113
    destination.append("\nINDEX_FILE_NAME = '");
1112
1114
    destination.append(options.index_file_name());
1113
1115
    destination.push_back('\'');
1114
1116
  }
1115
1117
 
1116
1118
  if (options.has_max_rows())
1117
1119
  {
1118
 
    destination.append("\nMAX_ROWS = ", 12);
 
1120
    destination.append("\nMAX_ROWS = ");
1119
1121
    destination.append(boost::lexical_cast<string>(options.max_rows()));
1120
1122
  }
1121
1123
 
1122
1124
  if (options.has_min_rows())
1123
1125
  {
1124
 
    destination.append("\nMIN_ROWS = ", 12);
 
1126
    destination.append("\nMIN_ROWS = ");
1125
1127
    destination.append(boost::lexical_cast<string>(options.min_rows()));
1126
1128
  }
1127
1129
 
1128
1130
  if (options.has_user_set_auto_increment_value()
1129
1131
      && options.has_auto_increment_value())
1130
1132
  {
1131
 
    destination.append(" AUTO_INCREMENT=", 16);
 
1133
    destination.append(" AUTO_INCREMENT=");
1132
1134
    destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
1133
1135
  }
1134
1136
 
1135
1137
  if (options.has_avg_row_length())
1136
1138
  {
1137
 
    destination.append("\nAVG_ROW_LENGTH = ", 18);
 
1139
    destination.append("\nAVG_ROW_LENGTH = ");
1138
1140
    destination.append(boost::lexical_cast<string>(options.avg_row_length()));
1139
1141
  }
1140
1142
 
1141
1143
  if (options.has_checksum() &&
1142
1144
      options.checksum())
1143
 
    destination.append("\nCHECKSUM = TRUE", 16);
 
1145
    destination.append("\nCHECKSUM = TRUE");
1144
1146
  if (options.has_page_checksum() &&
1145
1147
      options.page_checksum())
1146
 
    destination.append("\nPAGE_CHECKSUM = TRUE", 21);
 
1148
    destination.append("\nPAGE_CHECKSUM = TRUE");
1147
1149
 
1148
1150
  return NONE;
1149
1151
}
1161
1163
  destination.append("  ", 2);
1162
1164
 
1163
1165
  if (index.is_primary())
1164
 
    destination.append("PRIMARY ", 8);
 
1166
    destination.append("PRIMARY ");
1165
1167
  else if (index.is_unique())
1166
 
    destination.append("UNIQUE ", 7);
 
1168
    destination.append("UNIQUE ");
1167
1169
 
1168
1170
  destination.append("KEY ", 4);
1169
1171
  if (! (index.is_primary() && index.name().compare("PRIMARY")==0))
1215
1217
  case Table::Index::UNKNOWN_INDEX:
1216
1218
    break;
1217
1219
  case Table::Index::BTREE:
1218
 
    destination.append(" USING BTREE", 12);
 
1220
    destination.append(" USING BTREE");
1219
1221
    break;
1220
1222
  case Table::Index::RTREE:
1221
 
    destination.append(" USING RTREE", 12);
 
1223
    destination.append(" USING RTREE");
1222
1224
    break;
1223
1225
  case Table::Index::HASH:
1224
 
    destination.append(" USING HASH", 11);
 
1226
    destination.append(" USING HASH");
1225
1227
    break;
1226
1228
  case Table::Index::FULLTEXT:
1227
 
    destination.append(" USING FULLTEXT", 15);
 
1229
    destination.append(" USING FULLTEXT");
1228
1230
    break;
1229
1231
  }
1230
1232
 
1231
1233
  if (index.has_comment())
1232
1234
  {
1233
 
    destination.append(" COMMENT ", 9);
 
1235
    destination.append(" COMMENT ");
1234
1236
    append_escaped_string(&destination, index.comment());
1235
1237
  }
1236
1238
 
1270
1272
  if (sql_variant == ANSI)
1271
1273
    quoted_identifier= '"';
1272
1274
 
1273
 
  destination.append("  ", 2);
 
1275
  destination.append("  ");
1274
1276
 
1275
1277
  if (fkey.has_name())
1276
1278
  {
1277
 
    destination.append("CONSTRAINT ", 11);
 
1279
    destination.append("CONSTRAINT ");
1278
1280
    append_escaped_string(&destination, fkey.name(), quoted_identifier);
1279
1281
    destination.append(" ", 1);
1280
1282
  }
1281
1283
 
1282
 
  destination.append("FOREIGN KEY (", 13);
 
1284
  destination.append("FOREIGN KEY (");
1283
1285
 
1284
1286
  for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
1285
1287
  {
1290
1292
                          quoted_identifier);
1291
1293
  }
1292
1294
 
1293
 
  destination.append(") REFERENCES ", 13);
 
1295
  destination.append(") REFERENCES ");
1294
1296
 
1295
1297
  append_escaped_string(&destination, fkey.references_table_name(),
1296
1298
                        quoted_identifier);
1297
 
  destination.append(" (", 2);
 
1299
  destination.append(" (");
1298
1300
 
1299
1301
  for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
1300
1302
  {
1309
1311
 
1310
1312
  if (fkey.has_update_option() and fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1311
1313
  {
1312
 
    destination.append(" ON UPDATE ", 11);
 
1314
    destination.append(" ON UPDATE ");
1313
1315
    transformForeignKeyOptionToSql(fkey.update_option(), destination);
1314
1316
  }
1315
1317
 
1316
1318
  if (fkey.has_delete_option() and fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1317
1319
  {
1318
 
    destination.append(" ON DELETE ", 11);
 
1320
    destination.append(" ON DELETE ");
1319
1321
    transformForeignKeyOptionToSql(fkey.delete_option(), destination);
1320
1322
  }
1321
1323
 
1345
1347
  switch (field_type)
1346
1348
  {
1347
1349
    case Table::Field::DOUBLE:
1348
 
    destination.append(" DOUBLE", 7);
 
1350
    destination.append(" DOUBLE");
1349
1351
    if (field.has_numeric_options()
1350
1352
        && field.numeric_options().has_precision())
1351
1353
    {
1359
1361
    {
1360
1362
      if (field.string_options().has_collation()
1361
1363
          && field.string_options().collation().compare("binary") == 0)
1362
 
        destination.append(" VARBINARY(", 11);
 
1364
        destination.append(" VARBINARY(");
1363
1365
      else
1364
 
        destination.append(" VARCHAR(", 9);
 
1366
        destination.append(" VARCHAR(");
1365
1367
 
1366
1368
      destination.append(boost::lexical_cast<string>(field.string_options().length()));
1367
1369
      destination.append(")");
1371
1373
    {
1372
1374
      if (field.string_options().has_collation()
1373
1375
          && field.string_options().collation().compare("binary") == 0)
1374
 
        destination.append(" BLOB", 5);
 
1376
        destination.append(" BLOB");
1375
1377
      else
1376
 
        destination.append(" TEXT", 5);
 
1378
        destination.append(" TEXT");
1377
1379
    }
1378
1380
    break;
1379
1381
  case Table::Field::ENUM:
1380
1382
    {
1381
1383
      size_t num_field_values= field.enumeration_values().field_value_size();
1382
 
      destination.append(" ENUM(", 6);
 
1384
      destination.append(" ENUM(");
1383
1385
      for (size_t x= 0; x < num_field_values; ++x)
1384
1386
      {
1385
1387
        const string &type= field.enumeration_values().field_value(x);
1395
1397
      break;
1396
1398
    }
1397
1399
  case Table::Field::UUID:
1398
 
    destination.append(" UUID", 5);
 
1400
    destination.append(" UUID");
1399
1401
    break;
1400
1402
  case Table::Field::BOOLEAN:
1401
 
    destination.append(" BOOLEAN", 8);
 
1403
    destination.append(" BOOLEAN");
1402
1404
    break;
1403
1405
  case Table::Field::INTEGER:
1404
 
    destination.append(" INT", 4);
 
1406
    destination.append(" INT");
1405
1407
    break;
1406
1408
  case Table::Field::BIGINT:
1407
1409
    if (field.has_constraints() and
1408
1410
        field.constraints().is_unsigned())
1409
1411
    {
1410
 
      destination.append(" BIGINT UNSIGNED", sizeof(" BIGINT UNSIGNED") -1);
 
1412
      destination.append(" BIGINT UNSIGNED");
1411
1413
    }
1412
1414
    else
1413
1415
    {
1414
 
      destination.append(" BIGINT", 7);
 
1416
      destination.append(" BIGINT");
1415
1417
    }
1416
1418
    break;
1417
1419
  case Table::Field::DECIMAL:
1418
1420
    {
1419
 
      destination.append(" DECIMAL(", 9);
 
1421
      destination.append(" DECIMAL(");
1420
1422
      stringstream ss;
1421
1423
      ss << field.numeric_options().precision() << ",";
1422
1424
      ss << field.numeric_options().scale() << ")";
1424
1426
    }
1425
1427
    break;
1426
1428
  case Table::Field::DATE:
1427
 
    destination.append(" DATE", 5);
 
1429
    destination.append(" DATE");
1428
1430
    break;
1429
1431
 
1430
1432
  case Table::Field::EPOCH:
1434
1436
    }
1435
1437
    else
1436
1438
    {
1437
 
      destination.append(" TIMESTAMP",  10);
 
1439
      destination.append(" TIMESTAMP");
1438
1440
    }
1439
1441
    break;
1440
1442
 
1441
1443
  case Table::Field::DATETIME:
1442
 
    destination.append(" DATETIME",  9);
 
1444
    destination.append(" DATETIME");
1443
1445
    break;
1444
1446
  case Table::Field::TIME:
1445
 
    destination.append(" TIME",  5);
 
1447
    destination.append(" TIME");
1446
1448
    break;
1447
1449
  }
1448
1450
 
1452
1454
    if (field.string_options().has_collation()
1453
1455
        && field.string_options().collation().compare("binary"))
1454
1456
    {
1455
 
      destination.append(" COLLATE ", 9);
 
1457
      destination.append(" COLLATE ");
1456
1458
      destination.append(field.string_options().collation());
1457
1459
    }
1458
1460
  }
1459
1461
 
1460
1462
  if (field.has_constraints() and field.constraints().is_unique())
1461
1463
  {
1462
 
    destination.append(" UNIQUE", sizeof(" UNIQUE") -1);
 
1464
    destination.append(" UNIQUE");
1463
1465
  }
1464
1466
 
1465
1467
  if (field.has_constraints() && field.constraints().is_notnull())
1466
1468
  {
1467
 
    destination.append(" NOT NULL", 9);
 
1469
    destination.append(" NOT NULL");
1468
1470
  }
1469
1471
  else if (field.type() == Table::Field::EPOCH)
1470
1472
  {
1471
 
    destination.append(" NULL", 5);
 
1473
    destination.append(" NULL");
1472
1474
  }
1473
1475
 
1474
1476
  if (field.type() == Table::Field::INTEGER || 
1478
1480
    if (field.has_numeric_options() &&
1479
1481
        field.numeric_options().is_autoincrement())
1480
1482
    {
1481
 
      destination.append(" AUTO_INCREMENT", 15);
 
1483
      destination.append(" AUTO_INCREMENT");
1482
1484
    }
1483
1485
  }
1484
1486
 
1485
1487
  if (field.options().has_default_value())
1486
1488
  {
1487
 
    destination.append(" DEFAULT ", 9);
 
1489
    destination.append(" DEFAULT ");
1488
1490
    append_escaped_string(&destination, field.options().default_value());
1489
1491
  }
1490
1492
  else if (field.options().has_default_expression())
1491
1493
  {
1492
 
    destination.append(" DEFAULT ", 9);
 
1494
    destination.append(" DEFAULT ");
1493
1495
    destination.append(field.options().default_expression());
1494
1496
  }
1495
1497
  else if (field.options().has_default_bin_value())
1497
1499
    const string &v= field.options().default_bin_value();
1498
1500
    if (v.length() == 0)
1499
1501
    {
1500
 
      destination.append(" DEFAULT ''", 11);
 
1502
      destination.append(" DEFAULT ''");
1501
1503
    }
1502
1504
    else
1503
1505
    {
1504
 
      destination.append(" DEFAULT 0x", 11);
 
1506
      destination.append(" DEFAULT 0x");
1505
1507
      for (size_t x= 0; x < v.length(); x++)
1506
1508
      {
1507
1509
        char hex[3];
1514
1516
           && field.options().default_null()
1515
1517
           && field.type() != Table::Field::BLOB)
1516
1518
  {
1517
 
    destination.append(" DEFAULT NULL", 13);
 
1519
    destination.append(" DEFAULT NULL");
1518
1520
  }
1519
1521
 
1520
1522
  if (field.has_options() && field.options().has_update_expression())
1521
1523
  {
1522
 
    destination.append(" ON UPDATE ", 11);
 
1524
    destination.append(" ON UPDATE ");
1523
1525
    destination.append(field.options().update_expression());
1524
1526
  }
1525
1527
 
1526
1528
  if (field.has_comment())
1527
1529
  {
1528
 
    destination.append(" COMMENT ", 9);
 
1530
    destination.append(" COMMENT ");
1529
1531
    append_escaped_string(&destination, field.comment(), quoted_default);
1530
1532
  }
1531
1533
  return NONE;