422
389
if (should_quote_field_value)
423
390
destination.push_back('\'');
425
if (record.is_null(x))
392
if (field_metadata.type() == Table::Field::BLOB)
427
destination.append("NULL");
395
* We do this here because BLOB data is returned
396
* in a string correctly, but calling append()
397
* without a length will result in only the string
398
* up to a \0 being output here.
400
string raw_data(record.insert_value(x));
401
destination.append(raw_data.c_str(), raw_data.size());
431
if (field_metadata.type() == Table::Field::BLOB)
434
* We do this here because BLOB data is returned
435
* in a string correctly, but calling append()
436
* without a length will result in only the string
437
* up to a \0 being output here.
439
string raw_data(record.insert_value(x));
440
destination.append(raw_data.c_str(), raw_data.size());
444
string tmp(record.insert_value(x));
445
escapeEmbeddedQuotes(tmp);
446
destination.append(tmp);
405
if (record.is_null(x))
407
destination.append("NULL");
411
destination.append(record.insert_value(x));
450
415
if (should_quote_field_value)
585
548
if (should_quote_field_value)
586
549
destination.push_back('\'');
588
if (record.is_null(x))
551
if (field_metadata.type() == Table::Field::BLOB)
590
destination.append("NULL");
554
* We do this here because BLOB data is returned
555
* in a string correctly, but calling append()
556
* without a length will result in only the string
557
* up to a \0 being output here.
559
string raw_data(record.after_value(x));
560
destination.append(raw_data.c_str(), raw_data.size());
594
if (field_metadata.type() == Table::Field::BLOB)
564
if (record.is_null(x))
597
* We do this here because BLOB data is returned
598
* in a string correctly, but calling append()
599
* without a length will result in only the string
600
* up to a \0 being output here.
602
string raw_data(record.after_value(x));
603
destination.append(raw_data.c_str(), raw_data.size());
566
destination.append("NULL");
607
string tmp(record.after_value(x));
608
escapeEmbeddedQuotes(tmp);
609
destination.append(tmp);
570
destination.append(record.after_value(x));
817
774
enum TransformSqlError
818
transformAlterSchemaStatementToSql(const AlterSchemaStatement &statement,
820
enum TransformSqlVariant sql_variant)
822
const Schema &before= statement.before();
823
const Schema &after= statement.after();
825
/* Make sure we are given the before and after for the same object */
826
if (before.uuid() != after.uuid())
827
return UUID_MISMATCH;
829
char quoted_identifier= '`';
830
if (sql_variant == ANSI)
831
quoted_identifier= '"';
833
destination.append("ALTER SCHEMA ");
834
destination.push_back(quoted_identifier);
835
destination.append(before.name());
836
destination.push_back(quoted_identifier);
839
* Diff our schemas. Currently, only collation can change so a
840
* diff of the two structures is not really necessary.
842
destination.append(" COLLATE = ");
843
destination.append(after.collation());
848
enum TransformSqlError
849
775
transformDropSchemaStatementToSql(const DropSchemaStatement &statement,
850
776
string &destination,
851
777
enum TransformSqlVariant sql_variant)
987
908
if (sql_variant == ANSI)
988
909
quoted_identifier= '"';
990
destination.append("CREATE ");
911
destination.append("CREATE ", 7);
992
913
if (table.type() == Table::TEMPORARY)
993
destination.append("TEMPORARY ");
914
destination.append("TEMPORARY ", 10);
995
destination.append("TABLE ");
916
destination.append("TABLE ", 6);
998
append_escaped_string(&destination, table.schema(), quoted_identifier);
919
destination.push_back(quoted_identifier);
920
destination.append(table.schema());
921
destination.push_back(quoted_identifier);
999
922
destination.push_back('.');
1001
append_escaped_string(&destination, table.name(), quoted_identifier);
1002
destination.append(" (\n");
924
destination.push_back(quoted_identifier);
925
destination.append(table.name());
926
destination.push_back(quoted_identifier);
927
destination.append(" (\n", 3);
1004
929
enum TransformSqlError result= NONE;
1005
930
size_t num_fields= table.field_size();
1057
destination.append("\n)");
980
destination.append("\n)", 2);
1059
982
/* Add ENGINE = " clause */
1060
983
if (table.has_engine())
1062
destination.append(" ENGINE=");
985
destination.append("\nENGINE = ", 10);
1063
986
destination.append(table.engine().name());
1065
988
size_t num_engine_options= table.engine().options_size();
1066
if (num_engine_options > 0)
1067
destination.append(" ", 1);
1068
989
for (size_t x= 0; x < num_engine_options; ++x)
1070
991
const Engine::Option &option= table.engine().options(x);
992
destination.push_back('\n');
1071
993
destination.append(option.name());
1072
destination.append("='");
994
destination.append(" = ", 3);
1073
995
destination.append(option.state());
1074
destination.append("'");
1075
if (x != num_engine_options-1)
1077
destination.append(", ");
996
destination.push_back('\n');
1093
1011
if (sql_variant == ANSI)
1094
1012
return NONE; /* ANSI does not support table options... */
1096
1016
if (options.has_comment())
1098
destination.append(" COMMENT=");
1099
append_escaped_string(&destination, options.comment());
1018
destination.append("\nCOMMENT = '", 12);
1019
destination.append(options.comment());
1020
destination.push_back('\'');
1102
1023
if (options.has_collation())
1104
destination.append(" COLLATE = ");
1025
destination.append("\nCOLLATE = ", 11);
1105
1026
destination.append(options.collation());
1029
if (options.has_auto_increment())
1031
ss << options.auto_increment();
1032
destination.append("\nAUTOINCREMENT_OFFSET = ", 24);
1033
destination.append(ss.str());
1108
1037
if (options.has_data_file_name())
1110
destination.append("\nDATA_FILE_NAME = '");
1039
destination.append("\nDATA_FILE_NAME = '", 19);
1111
1040
destination.append(options.data_file_name());
1112
1041
destination.push_back('\'');
1115
1044
if (options.has_index_file_name())
1117
destination.append("\nINDEX_FILE_NAME = '");
1046
destination.append("\nINDEX_FILE_NAME = '", 20);
1118
1047
destination.append(options.index_file_name());
1119
1048
destination.push_back('\'');
1122
1051
if (options.has_max_rows())
1124
destination.append("\nMAX_ROWS = ");
1125
destination.append(boost::lexical_cast<string>(options.max_rows()));
1053
ss << options.max_rows();
1054
destination.append("\nMAX_ROWS = ", 12);
1055
destination.append(ss.str());
1128
1059
if (options.has_min_rows())
1130
destination.append("\nMIN_ROWS = ");
1131
destination.append(boost::lexical_cast<string>(options.min_rows()));
1061
ss << options.min_rows();
1062
destination.append("\nMIN_ROWS = ", 12);
1063
destination.append(ss.str());
1134
if (options.has_user_set_auto_increment_value()
1135
&& options.has_auto_increment_value())
1067
if (options.has_auto_increment_value())
1137
destination.append(" AUTO_INCREMENT=");
1138
destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
1069
ss << options.auto_increment_value();
1070
destination.append("\nAUTO_INCREMENT = ", 18);
1071
destination.append(ss.str());
1141
1075
if (options.has_avg_row_length())
1143
destination.append("\nAVG_ROW_LENGTH = ");
1144
destination.append(boost::lexical_cast<string>(options.avg_row_length()));
1147
if (options.has_checksum() && options.checksum())
1148
destination.append("\nCHECKSUM = TRUE");
1150
if (options.has_page_checksum() && options.page_checksum())
1151
destination.append("\nPAGE_CHECKSUM = TRUE");
1153
if (options.has_dont_replicate() and options.dont_replicate())
1155
destination.append(" REPLICATION = FALSE");
1077
ss << options.avg_row_length();
1078
destination.append("\nAVG_ROW_LENGTH = ", 18);
1079
destination.append(ss.str());
1083
if (options.has_checksum() &&
1085
destination.append("\nCHECKSUM = TRUE", 16);
1086
if (options.has_page_checksum() &&
1087
options.page_checksum())
1088
destination.append("\nPAGE_CHECKSUM = TRUE", 21);
1168
1100
if (sql_variant == ANSI)
1169
1101
quoted_identifier= '"';
1171
destination.append(" ", 2);
1173
1103
if (index.is_primary())
1174
destination.append("PRIMARY ");
1104
destination.append("PRIMARY ", 8);
1175
1105
else if (index.is_unique())
1176
destination.append("UNIQUE ");
1106
destination.append("UNIQUE ", 7);
1178
1108
destination.append("KEY ", 4);
1179
if (! (index.is_primary() && index.name().compare("PRIMARY")==0))
1181
destination.push_back(quoted_identifier);
1182
destination.append(index.name());
1183
destination.push_back(quoted_identifier);
1184
destination.append(" (", 2);
1187
destination.append("(", 1);
1109
destination.push_back(quoted_identifier);
1110
destination.append(index.name());
1111
destination.push_back(quoted_identifier);
1112
destination.append(" (", 2);
1189
1114
size_t num_parts= index.index_part_size();
1190
1115
for (size_t x= 0; x < num_parts; ++x)
1280
1184
if (sql_variant == ANSI)
1281
1185
quoted_identifier= '"';
1283
destination.append(" ");
1187
destination.append(" ", 2);
1285
1189
if (fkey.has_name())
1287
destination.append("CONSTRAINT ");
1191
destination.append("CONSTRAINT ", 11);
1288
1192
append_escaped_string(&destination, fkey.name(), quoted_identifier);
1289
1193
destination.append(" ", 1);
1292
destination.append("FOREIGN KEY (");
1196
destination.append("FOREIGN KEY (", 13);
1294
1198
for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
1297
destination.append(", ");
1201
destination.push_back(',');
1299
1203
append_escaped_string(&destination, fkey.column_names(x),
1300
1204
quoted_identifier);
1303
destination.append(") REFERENCES ");
1207
destination.append(") REFERENCES ", 13);
1305
1209
append_escaped_string(&destination, fkey.references_table_name(),
1306
1210
quoted_identifier);
1307
destination.append(" (");
1211
destination.append(" (", 2);
1309
1213
for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
1312
destination.append(", ");
1216
destination.push_back(',');
1314
1218
append_escaped_string(&destination, fkey.references_columns(x),
1315
1219
quoted_identifier);
1338
1242
enum TransformSqlVariant sql_variant)
1340
1244
char quoted_identifier= '`';
1341
char quoted_default;
1343
1245
if (sql_variant == ANSI)
1344
1246
quoted_identifier= '"';
1346
if (sql_variant == DRIZZLE)
1347
quoted_default= '\'';
1349
quoted_default= quoted_identifier;
1351
append_escaped_string(&destination, field.name(), quoted_identifier);
1248
destination.push_back(quoted_identifier);
1249
destination.append(field.name());
1250
destination.push_back(quoted_identifier);
1353
1252
Table::Field::FieldType field_type= field.type();
1355
1254
switch (field_type)
1357
1256
case Table::Field::DOUBLE:
1358
destination.append(" DOUBLE");
1359
if (field.has_numeric_options()
1360
&& field.numeric_options().has_precision())
1363
ss << "(" << field.numeric_options().precision() << ",";
1364
ss << field.numeric_options().scale() << ")";
1365
destination.append(ss.str());
1257
destination.append(" DOUBLE", 7);
1368
1259
case Table::Field::VARCHAR:
1370
if (field.string_options().has_collation()
1371
&& field.string_options().collation().compare("binary") == 0)
1372
destination.append(" VARBINARY(");
1374
destination.append(" VARCHAR(");
1376
destination.append(boost::lexical_cast<string>(field.string_options().length()));
1377
destination.append(")");
1261
destination.append(" VARCHAR(", 9);
1263
ss << field.string_options().length() << ")";
1264
destination.append(ss.str());
1380
1267
case Table::Field::BLOB:
1382
if (field.string_options().has_collation()
1383
&& field.string_options().collation().compare("binary") == 0)
1384
destination.append(" BLOB");
1386
destination.append(" TEXT");
1268
destination.append(" BLOB", 5);
1389
1270
case Table::Field::ENUM:
1391
1272
size_t num_field_values= field.enumeration_values().field_value_size();
1392
destination.append(" ENUM(");
1273
destination.append(" ENUM(", 6);
1393
1274
for (size_t x= 0; x < num_field_values; ++x)
1395
1276
const string &type= field.enumeration_values().field_value(x);
1404
1285
destination.push_back(')');
1407
case Table::Field::UUID:
1408
destination.append(" UUID");
1410
case Table::Field::BOOLEAN:
1411
destination.append(" BOOLEAN");
1413
1288
case Table::Field::INTEGER:
1414
destination.append(" INT");
1289
destination.append(" INT", 4);
1416
1291
case Table::Field::BIGINT:
1417
if (field.has_constraints() and
1418
field.constraints().is_unsigned())
1420
destination.append(" BIGINT UNSIGNED");
1424
destination.append(" BIGINT");
1292
destination.append(" BIGINT", 7);
1427
1294
case Table::Field::DECIMAL:
1429
destination.append(" DECIMAL(");
1296
destination.append(" DECIMAL(", 9);
1430
1297
stringstream ss;
1431
1298
ss << field.numeric_options().precision() << ",";
1432
1299
ss << field.numeric_options().scale() << ")";
1436
1303
case Table::Field::DATE:
1437
destination.append(" DATE");
1440
case Table::Field::EPOCH:
1441
if (field.time_options().microseconds())
1443
destination.append(" TIMESTAMP(6)");
1447
destination.append(" TIMESTAMP");
1304
destination.append(" DATE", 5);
1306
case Table::Field::TIMESTAMP:
1307
destination.append(" TIMESTAMP", 10);
1451
1309
case Table::Field::DATETIME:
1452
destination.append(" DATETIME");
1454
case Table::Field::TIME:
1455
destination.append(" TIME");
1310
destination.append(" DATETIME", 9);
1314
if (field.type() == Table::Field::INTEGER ||
1315
field.type() == Table::Field::BIGINT)
1317
if (field.has_constraints() &&
1318
field.constraints().has_is_unsigned() &&
1319
field.constraints().is_unsigned())
1321
destination.append(" UNSIGNED", 9);
1326
if (! (field.has_constraints() &&
1327
field.constraints().is_nullable()))
1329
destination.append(" NOT", 4);
1331
destination.append(" NULL", 5);
1333
if (field.type() == Table::Field::INTEGER ||
1334
field.type() == Table::Field::BIGINT)
1336
/* AUTO_INCREMENT must be after NOT NULL */
1337
if (field.has_numeric_options() &&
1338
field.numeric_options().is_autoincrement())
1340
destination.append(" AUTO_INCREMENT", 15);
1459
1344
if (field.type() == Table::Field::BLOB ||
1460
1345
field.type() == Table::Field::VARCHAR)
1462
if (field.string_options().has_collation()
1463
&& field.string_options().collation().compare("binary"))
1347
if (field.string_options().has_collation())
1465
destination.append(" COLLATE ");
1349
destination.append(" COLLATE ", 9);
1466
1350
destination.append(field.string_options().collation());
1470
if (field.has_constraints() and field.constraints().is_unique())
1472
destination.append(" UNIQUE");
1475
if (field.has_constraints() && field.constraints().is_notnull())
1477
destination.append(" NOT NULL");
1479
else if (field.type() == Table::Field::EPOCH)
1481
destination.append(" NULL");
1484
if (field.type() == Table::Field::INTEGER ||
1485
field.type() == Table::Field::BIGINT)
1487
/* AUTO_INCREMENT must be after NOT NULL */
1488
if (field.has_numeric_options() &&
1489
field.numeric_options().is_autoincrement())
1491
destination.append(" AUTO_INCREMENT");
1495
1354
if (field.options().has_default_value())
1497
destination.append(" DEFAULT ");
1498
append_escaped_string(&destination, field.options().default_value());
1500
else if (field.options().has_default_expression())
1502
destination.append(" DEFAULT ");
1503
destination.append(field.options().default_expression());
1505
else if (field.options().has_default_bin_value())
1356
destination.append(" DEFAULT ", 9);
1357
destination.push_back(quoted_identifier);
1358
destination.append(field.options().default_value());
1359
destination.push_back(quoted_identifier);
1362
if (field.options().has_default_bin_value())
1507
1364
const string &v= field.options().default_bin_value();
1508
if (v.length() == 0)
1510
destination.append(" DEFAULT ''");
1514
destination.append(" DEFAULT 0x");
1515
for (size_t x= 0; x < v.length(); x++)
1518
snprintf(hex, sizeof(hex), "%.2X", *(v.c_str() + x));
1519
destination.append(hex, 2);
1523
else if (field.options().has_default_null()
1524
&& field.options().default_null()
1525
&& field.type() != Table::Field::BLOB)
1527
destination.append(" DEFAULT NULL");
1365
destination.append(" DEFAULT 0x", 11);
1366
for (size_t x= 0; x < v.length(); x++)
1368
printf("%.2x", *(v.c_str() + x));
1530
if (field.has_options() && field.options().has_update_expression())
1372
if (field.has_options() && field.options().has_update_value())
1532
destination.append(" ON UPDATE ");
1533
destination.append(field.options().update_expression());
1374
destination.append(" ON UPDATE ", 11);
1375
destination.append(field.options().update_value());
1536
1378
if (field.has_comment())
1538
destination.append(" COMMENT ");
1539
append_escaped_string(&destination, field.comment(), quoted_default);
1380
destination.append(" COMMENT ", 9);
1381
destination.push_back(quoted_identifier);
1382
destination.append(field.comment());
1383
destination.push_back(quoted_identifier);