1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
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
318
314
sql_strings.push_back(destination);
321
case Statement::ALTER_SCHEMA:
323
assert(source.has_alter_schema_statement());
325
error= transformAlterSchemaStatementToSql(source.alter_schema_statement(),
328
sql_strings.push_back(destination);
331
317
case Statement::SET_VARIABLE:
333
319
assert(source.has_set_variable_statement());
818
804
enum TransformSqlError
819
transformAlterSchemaStatementToSql(const AlterSchemaStatement &statement,
821
enum TransformSqlVariant sql_variant)
823
const Schema &before= statement.before();
824
const Schema &after= statement.after();
826
/* Make sure we are given the before and after for the same object */
827
if (before.uuid() != after.uuid())
828
return UUID_MISMATCH;
830
char quoted_identifier= '`';
831
if (sql_variant == ANSI)
832
quoted_identifier= '"';
834
destination.append("ALTER SCHEMA ");
835
destination.push_back(quoted_identifier);
836
destination.append(before.name());
837
destination.push_back(quoted_identifier);
840
* Diff our schemas. Currently, only collation can change so a
841
* diff of the two structures is not really necessary.
843
destination.append(" COLLATE = ");
844
destination.append(after.collation());
849
enum TransformSqlError
850
805
transformDropSchemaStatementToSql(const DropSchemaStatement &statement,
851
806
string &destination,
852
807
enum TransformSqlVariant sql_variant)
875
830
const Schema &schema= statement.schema();
877
destination.append("CREATE SCHEMA ");
832
destination.append("CREATE SCHEMA ", 14);
878
833
destination.push_back(quoted_identifier);
879
834
destination.append(schema.name());
880
835
destination.push_back(quoted_identifier);
882
837
if (schema.has_collation())
884
destination.append(" COLLATE ");
839
destination.append(" COLLATE ", 9);
885
840
destination.append(schema.collation());
900
855
const TableMetadata &table_metadata= statement.table_metadata();
902
destination.append("DROP TABLE ");
857
destination.append("DROP TABLE ", 11);
904
859
/* Add the IF EXISTS clause if necessary */
905
860
if (statement.has_if_exists_clause() &&
906
861
statement.if_exists_clause() == true)
908
destination.append("IF EXISTS ");
863
destination.append("IF EXISTS ", 10);
911
866
destination.push_back(quoted_identifier);
931
886
const TableMetadata &table_metadata= statement.table_metadata();
933
destination.append("TRUNCATE TABLE ");
888
destination.append("TRUNCATE TABLE ", 15);
934
889
destination.push_back(quoted_identifier);
935
890
destination.append(table_metadata.schema_name());
936
891
destination.push_back(quoted_identifier);
951
906
const FieldMetadata &variable_metadata= statement.variable_metadata();
952
907
bool should_quote_field_value= shouldQuoteFieldValue(variable_metadata.type());
954
destination.append("SET GLOBAL "); /* Only global variables are replicated */
909
destination.append("SET GLOBAL ", 11); /* Only global variables are replicated */
955
910
destination.append(variable_metadata.name());
956
911
destination.push_back('=');
983
938
if (sql_variant == ANSI)
984
939
quoted_identifier= '"';
986
destination.append("CREATE ");
941
destination.append("CREATE ", 7);
988
943
if (table.type() == Table::TEMPORARY)
989
destination.append("TEMPORARY ");
944
destination.append("TEMPORARY ", 10);
991
destination.append("TABLE ");
946
destination.append("TABLE ", 6);
994
949
append_escaped_string(&destination, table.schema(), quoted_identifier);
995
950
destination.push_back('.');
997
952
append_escaped_string(&destination, table.name(), quoted_identifier);
998
destination.append(" (\n");
953
destination.append(" (\n", 3);
1000
955
enum TransformSqlError result= NONE;
1001
956
size_t num_fields= table.field_size();
1017
972
size_t num_indexes= table.indexes_size();
1019
974
if (num_indexes > 0)
1020
destination.append(",\n");
975
destination.append(",\n", 2);
1022
977
for (size_t x= 0; x < num_indexes; ++x)
1024
979
const message::Table::Index &index= table.indexes(x);
1027
destination.append(",\n");
982
destination.append(",\n", 2);
1029
984
result= transformIndexDefinitionToSql(index, table, destination, sql_variant);
1035
990
size_t num_foreign_keys= table.fk_constraint_size();
1037
992
if (num_foreign_keys > 0)
1038
destination.append(",\n");
993
destination.append(",\n", 2);
1040
995
for (size_t x= 0; x < num_foreign_keys; ++x)
1042
997
const message::Table::ForeignKeyConstraint &fkey= table.fk_constraint(x);
1045
destination.append(",\n");
1000
destination.append(",\n", 2);
1047
1002
result= transformForeignKeyConstraintDefinitionToSql(fkey, table, destination, sql_variant);
1053
destination.append("\n)");
1008
destination.append("\n)", 2);
1055
1010
/* Add ENGINE = " clause */
1056
1011
if (table.has_engine())
1058
destination.append(" ENGINE=");
1013
destination.append(" ENGINE=", 8);
1059
1014
destination.append(table.engine().name());
1061
1016
size_t num_engine_options= table.engine().options_size();
1066
1021
const Engine::Option &option= table.engine().options(x);
1067
1022
destination.append(option.name());
1068
destination.append("='");
1023
destination.append("='", 2);
1069
1024
destination.append(option.state());
1070
destination.append("'");
1071
if (x != num_engine_options-1)
1073
destination.append(", ");
1025
destination.append("'", 1);
1026
if(x != num_engine_options-1)
1027
destination.append(", ", 2);
1092
1045
if (options.has_comment())
1094
destination.append(" COMMENT=");
1047
destination.append(" COMMENT=", 9);
1095
1048
append_escaped_string(&destination, options.comment());
1098
1051
if (options.has_collation())
1100
destination.append(" COLLATE = ");
1053
destination.append(" COLLATE = ", 11);
1101
1054
destination.append(options.collation());
1104
1057
if (options.has_data_file_name())
1106
destination.append("\nDATA_FILE_NAME = '");
1059
destination.append("\nDATA_FILE_NAME = '", 19);
1107
1060
destination.append(options.data_file_name());
1108
1061
destination.push_back('\'');
1111
1064
if (options.has_index_file_name())
1113
destination.append("\nINDEX_FILE_NAME = '");
1066
destination.append("\nINDEX_FILE_NAME = '", 20);
1114
1067
destination.append(options.index_file_name());
1115
1068
destination.push_back('\'');
1118
1071
if (options.has_max_rows())
1120
destination.append("\nMAX_ROWS = ");
1073
destination.append("\nMAX_ROWS = ", 12);
1121
1074
destination.append(boost::lexical_cast<string>(options.max_rows()));
1124
1077
if (options.has_min_rows())
1126
destination.append("\nMIN_ROWS = ");
1079
destination.append("\nMIN_ROWS = ", 12);
1127
1080
destination.append(boost::lexical_cast<string>(options.min_rows()));
1130
1083
if (options.has_user_set_auto_increment_value()
1131
1084
&& options.has_auto_increment_value())
1133
destination.append(" AUTO_INCREMENT=");
1086
destination.append(" AUTO_INCREMENT=", 16);
1134
1087
destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
1137
1090
if (options.has_avg_row_length())
1139
destination.append("\nAVG_ROW_LENGTH = ");
1092
destination.append("\nAVG_ROW_LENGTH = ", 18);
1140
1093
destination.append(boost::lexical_cast<string>(options.avg_row_length()));
1143
1096
if (options.has_checksum() &&
1144
1097
options.checksum())
1145
destination.append("\nCHECKSUM = TRUE");
1098
destination.append("\nCHECKSUM = TRUE", 16);
1146
1099
if (options.has_page_checksum() &&
1147
1100
options.page_checksum())
1148
destination.append("\nPAGE_CHECKSUM = TRUE");
1101
destination.append("\nPAGE_CHECKSUM = TRUE", 21);
1163
1116
destination.append(" ", 2);
1165
1118
if (index.is_primary())
1166
destination.append("PRIMARY ");
1119
destination.append("PRIMARY ", 8);
1167
1120
else if (index.is_unique())
1168
destination.append("UNIQUE ");
1121
destination.append("UNIQUE ", 7);
1170
1123
destination.append("KEY ", 4);
1171
1124
if (! (index.is_primary() && index.name().compare("PRIMARY")==0))
1217
1170
case Table::Index::UNKNOWN_INDEX:
1219
1172
case Table::Index::BTREE:
1220
destination.append(" USING BTREE");
1173
destination.append(" USING BTREE", 12);
1222
1175
case Table::Index::RTREE:
1223
destination.append(" USING RTREE");
1176
destination.append(" USING RTREE", 12);
1225
1178
case Table::Index::HASH:
1226
destination.append(" USING HASH");
1179
destination.append(" USING HASH", 11);
1228
1181
case Table::Index::FULLTEXT:
1229
destination.append(" USING FULLTEXT");
1182
destination.append(" USING FULLTEXT", 15);
1233
1186
if (index.has_comment())
1235
destination.append(" COMMENT ");
1188
destination.append(" COMMENT ", 9);
1236
1189
append_escaped_string(&destination, index.comment());
1272
1225
if (sql_variant == ANSI)
1273
1226
quoted_identifier= '"';
1275
destination.append(" ");
1228
destination.append(" ", 2);
1277
1230
if (fkey.has_name())
1279
destination.append("CONSTRAINT ");
1232
destination.append("CONSTRAINT ", 11);
1280
1233
append_escaped_string(&destination, fkey.name(), quoted_identifier);
1281
1234
destination.append(" ", 1);
1284
destination.append("FOREIGN KEY (");
1237
destination.append("FOREIGN KEY (", 13);
1286
1239
for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
1292
1245
quoted_identifier);
1295
destination.append(") REFERENCES ");
1248
destination.append(") REFERENCES ", 13);
1297
1250
append_escaped_string(&destination, fkey.references_table_name(),
1298
1251
quoted_identifier);
1299
destination.append(" (");
1252
destination.append(" (", 2);
1301
1254
for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
1312
1265
if (fkey.has_update_option() and fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1314
destination.append(" ON UPDATE ");
1267
destination.append(" ON UPDATE ", 11);
1315
1268
transformForeignKeyOptionToSql(fkey.update_option(), destination);
1318
1271
if (fkey.has_delete_option() and fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1320
destination.append(" ON DELETE ");
1273
destination.append(" ON DELETE ", 11);
1321
1274
transformForeignKeyOptionToSql(fkey.delete_option(), destination);
1362
1315
if (field.string_options().has_collation()
1363
1316
&& field.string_options().collation().compare("binary") == 0)
1364
destination.append(" VARBINARY(");
1317
destination.append(" VARBINARY(", 11);
1366
destination.append(" VARCHAR(");
1319
destination.append(" VARCHAR(", 9);
1368
1321
destination.append(boost::lexical_cast<string>(field.string_options().length()));
1369
1322
destination.append(")");
1374
1327
if (field.string_options().has_collation()
1375
1328
&& field.string_options().collation().compare("binary") == 0)
1376
destination.append(" BLOB");
1329
destination.append(" BLOB", 5);
1378
destination.append(" TEXT");
1331
destination.append(" TEXT", 5);
1381
1334
case Table::Field::ENUM:
1383
1336
size_t num_field_values= field.enumeration_values().field_value_size();
1384
destination.append(" ENUM(");
1337
destination.append(" ENUM(", 6);
1385
1338
for (size_t x= 0; x < num_field_values; ++x)
1387
1340
const string &type= field.enumeration_values().field_value(x);
1396
1349
destination.push_back(')');
1399
case Table::Field::UUID:
1400
destination.append(" UUID");
1402
case Table::Field::BOOLEAN:
1403
destination.append(" BOOLEAN");
1405
1352
case Table::Field::INTEGER:
1406
destination.append(" INT");
1353
destination.append(" INT", 4);
1408
1355
case Table::Field::BIGINT:
1409
if (field.has_constraints() and
1410
field.constraints().is_unsigned())
1412
destination.append(" BIGINT UNSIGNED");
1416
destination.append(" BIGINT");
1356
destination.append(" BIGINT", 7);
1419
1358
case Table::Field::DECIMAL:
1421
destination.append(" DECIMAL(");
1360
destination.append(" DECIMAL(", 9);
1422
1361
stringstream ss;
1423
1362
ss << field.numeric_options().precision() << ",";
1424
1363
ss << field.numeric_options().scale() << ")";
1428
1367
case Table::Field::DATE:
1429
destination.append(" DATE");
1432
case Table::Field::EPOCH:
1433
if (field.time_options().microseconds())
1435
destination.append(" TIMESTAMP(6)");
1439
destination.append(" TIMESTAMP");
1368
destination.append(" DATE", 5);
1370
case Table::Field::TIMESTAMP:
1371
destination.append(" TIMESTAMP", 10);
1443
1373
case Table::Field::DATETIME:
1444
destination.append(" DATETIME");
1446
case Table::Field::TIME:
1447
destination.append(" TIME");
1374
destination.append(" DATETIME", 9);
1378
if (field.type() == Table::Field::INTEGER ||
1379
field.type() == Table::Field::BIGINT)
1381
if (field.has_constraints() &&
1382
field.constraints().has_is_unsigned() &&
1383
field.constraints().is_unsigned())
1385
destination.append(" UNSIGNED", 9);
1451
1389
if (field.type() == Table::Field::BLOB ||
1454
1392
if (field.string_options().has_collation()
1455
1393
&& field.string_options().collation().compare("binary"))
1457
destination.append(" COLLATE ");
1395
destination.append(" COLLATE ", 9);
1458
1396
destination.append(field.string_options().collation());
1462
if (field.has_constraints() and field.constraints().is_unique())
1464
destination.append(" UNIQUE");
1467
if (field.has_constraints() && field.constraints().is_notnull())
1469
destination.append(" NOT NULL");
1471
else if (field.type() == Table::Field::EPOCH)
1473
destination.append(" NULL");
1400
if (field.has_constraints() &&
1401
! field.constraints().is_nullable())
1403
destination.append(" NOT NULL", 9);
1405
else if (field.type() == Table::Field::TIMESTAMP)
1406
destination.append(" NULL", 5);
1476
1408
if (field.type() == Table::Field::INTEGER ||
1477
1409
field.type() == Table::Field::BIGINT)
1480
1412
if (field.has_numeric_options() &&
1481
1413
field.numeric_options().is_autoincrement())
1483
destination.append(" AUTO_INCREMENT");
1415
destination.append(" AUTO_INCREMENT", 15);
1487
1419
if (field.options().has_default_value())
1489
destination.append(" DEFAULT ");
1421
destination.append(" DEFAULT ", 9);
1490
1422
append_escaped_string(&destination, field.options().default_value());
1492
1424
else if (field.options().has_default_expression())
1494
destination.append(" DEFAULT ");
1426
destination.append(" DEFAULT ", 9);
1495
1427
destination.append(field.options().default_expression());
1497
1429
else if (field.options().has_default_bin_value())
1499
1431
const string &v= field.options().default_bin_value();
1500
1432
if (v.length() == 0)
1502
destination.append(" DEFAULT ''");
1433
destination.append(" DEFAULT ''", 11);
1506
destination.append(" DEFAULT 0x");
1436
destination.append(" DEFAULT 0x", 11);
1507
1437
for (size_t x= 0; x < v.length(); x++)
1516
1446
&& field.options().default_null()
1517
1447
&& field.type() != Table::Field::BLOB)
1519
destination.append(" DEFAULT NULL");
1449
destination.append(" DEFAULT NULL", 13);
1522
1452
if (field.has_options() && field.options().has_update_expression())
1524
destination.append(" ON UPDATE ");
1454
destination.append(" ON UPDATE ", 11);
1525
1455
destination.append(field.options().update_expression());
1528
1458
if (field.has_comment())
1530
destination.append(" COMMENT ");
1460
destination.append(" COMMENT ", 9);
1531
1461
append_escaped_string(&destination, field.comment(), quoted_default);
1557
1487
case DRIZZLE_TYPE_NULL:
1558
1488
assert(false); /* Not a user definable type */
1559
1489
return Table::Field::INTEGER; /* unreachable */
1560
case DRIZZLE_TYPE_MICROTIME:
1561
1490
case DRIZZLE_TYPE_TIMESTAMP:
1562
return Table::Field::EPOCH;
1491
return Table::Field::TIMESTAMP;
1563
1492
case DRIZZLE_TYPE_LONGLONG:
1564
1493
return Table::Field::BIGINT;
1565
1494
case DRIZZLE_TYPE_DATETIME:
1566
1495
return Table::Field::DATETIME;
1567
case DRIZZLE_TYPE_TIME:
1568
return Table::Field::TIME;
1569
1496
case DRIZZLE_TYPE_DATE:
1570
1497
return Table::Field::DATE;
1571
1498
case DRIZZLE_TYPE_VARCHAR: