413
389
if (should_quote_field_value)
414
390
destination.push_back('\'');
416
if (record.is_null(x))
392
if (field_metadata.type() == Table::Field::BLOB)
418
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());
422
if (field_metadata.type() == Table::Field::BLOB)
425
* We do this here because BLOB data is returned
426
* in a string correctly, but calling append()
427
* without a length will result in only the string
428
* up to a \0 being output here.
430
string raw_data(record.insert_value(x));
431
destination.append(raw_data.c_str(), raw_data.size());
435
string tmp(record.insert_value(x));
436
escapeEmbeddedQuotes(tmp);
437
destination.append(tmp);
405
if (record.is_null(x))
407
destination.append("NULL");
411
destination.append(record.insert_value(x));
441
415
if (should_quote_field_value)
576
548
if (should_quote_field_value)
577
549
destination.push_back('\'');
579
if (record.is_null(x))
551
if (field_metadata.type() == Table::Field::BLOB)
581
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());
585
if (field_metadata.type() == Table::Field::BLOB)
564
if (record.is_null(x))
588
* We do this here because BLOB data is returned
589
* in a string correctly, but calling append()
590
* without a length will result in only the string
591
* up to a \0 being output here.
593
string raw_data(record.after_value(x));
594
destination.append(raw_data.c_str(), raw_data.size());
566
destination.append("NULL");
598
string tmp(record.after_value(x));
599
escapeEmbeddedQuotes(tmp);
600
destination.append(tmp);
570
destination.append(record.after_value(x));
950
916
destination.append("TABLE ", 6);
953
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);
954
922
destination.push_back('.');
956
append_escaped_string(&destination, table.name(), quoted_identifier);
924
destination.push_back(quoted_identifier);
925
destination.append(table.name());
926
destination.push_back(quoted_identifier);
957
927
destination.append(" (\n", 3);
959
929
enum TransformSqlError result= NONE;
1014
982
/* Add ENGINE = " clause */
1015
983
if (table.has_engine())
1017
destination.append(" ENGINE=", 8);
985
destination.append("\nENGINE = ", 10);
1018
986
destination.append(table.engine().name());
1020
988
size_t num_engine_options= table.engine().options_size();
1021
if (num_engine_options > 0)
1022
destination.append(" ", 1);
1023
989
for (size_t x= 0; x < num_engine_options; ++x)
1025
991
const Engine::Option &option= table.engine().options(x);
992
destination.push_back('\n');
1026
993
destination.append(option.name());
1027
destination.append("='", 2);
994
destination.append(" = ", 3);
1028
995
destination.append(option.state());
1029
destination.append("'", 1);
1030
if(x != num_engine_options-1)
1031
destination.append(", ", 2);
996
destination.push_back('\n');
1046
1011
if (sql_variant == ANSI)
1047
1012
return NONE; /* ANSI does not support table options... */
1049
1016
if (options.has_comment())
1051
destination.append(" COMMENT=", 9);
1052
append_escaped_string(&destination, options.comment());
1018
destination.append("\nCOMMENT = '", 12);
1019
destination.append(options.comment());
1020
destination.push_back('\'');
1055
1023
if (options.has_collation())
1057
destination.append(" COLLATE = ", 11);
1025
destination.append("\nCOLLATE = ", 11);
1058
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());
1061
1037
if (options.has_data_file_name())
1063
1039
destination.append("\nDATA_FILE_NAME = '", 19);
1075
1051
if (options.has_max_rows())
1053
ss << options.max_rows();
1077
1054
destination.append("\nMAX_ROWS = ", 12);
1078
destination.append(boost::lexical_cast<string>(options.max_rows()));
1055
destination.append(ss.str());
1081
1059
if (options.has_min_rows())
1061
ss << options.min_rows();
1083
1062
destination.append("\nMIN_ROWS = ", 12);
1084
destination.append(boost::lexical_cast<string>(options.min_rows()));
1063
destination.append(ss.str());
1087
if (options.has_user_set_auto_increment_value()
1088
&& options.has_auto_increment_value())
1067
if (options.has_auto_increment_value())
1090
destination.append(" AUTO_INCREMENT=", 16);
1091
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());
1094
1075
if (options.has_avg_row_length())
1077
ss << options.avg_row_length();
1096
1078
destination.append("\nAVG_ROW_LENGTH = ", 18);
1097
destination.append(boost::lexical_cast<string>(options.avg_row_length()));
1079
destination.append(ss.str());
1100
1083
if (options.has_checksum() &&
1117
1100
if (sql_variant == ANSI)
1118
1101
quoted_identifier= '"';
1120
destination.append(" ", 2);
1122
1103
if (index.is_primary())
1123
1104
destination.append("PRIMARY ", 8);
1124
1105
else if (index.is_unique())
1125
1106
destination.append("UNIQUE ", 7);
1127
1108
destination.append("KEY ", 4);
1128
if (! (index.is_primary() && index.name().compare("PRIMARY")==0))
1130
destination.push_back(quoted_identifier);
1131
destination.append(index.name());
1132
destination.push_back(quoted_identifier);
1133
destination.append(" (", 2);
1136
destination.append("(", 1);
1109
destination.push_back(quoted_identifier);
1110
destination.append(index.name());
1111
destination.push_back(quoted_identifier);
1112
destination.append(" (", 2);
1138
1114
size_t num_parts= index.index_part_size();
1139
1115
for (size_t x= 0; x < num_parts; ++x)
1170
1148
destination.push_back(')');
1172
switch (index.type())
1174
case Table::Index::UNKNOWN_INDEX:
1176
case Table::Index::BTREE:
1177
destination.append(" USING BTREE", 12);
1179
case Table::Index::RTREE:
1180
destination.append(" USING RTREE", 12);
1182
case Table::Index::HASH:
1183
destination.append(" USING HASH", 11);
1185
case Table::Index::FULLTEXT:
1186
destination.append(" USING FULLTEXT", 15);
1190
if (index.has_comment())
1192
destination.append(" COMMENT ", 9);
1193
append_escaped_string(&destination, index.comment());
1267
1222
destination.push_back(')');
1269
if (fkey.has_update_option() and fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1224
if (fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1271
1226
destination.append(" ON UPDATE ", 11);
1272
1227
transformForeignKeyOptionToSql(fkey.update_option(), destination);
1275
if (fkey.has_delete_option() and fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1230
if (fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1277
1232
destination.append(" ON DELETE ", 11);
1278
1233
transformForeignKeyOptionToSql(fkey.delete_option(), destination);
1306
1256
case Table::Field::DOUBLE:
1307
1257
destination.append(" DOUBLE", 7);
1308
if (field.has_numeric_options()
1309
&& field.numeric_options().has_precision())
1312
ss << "(" << field.numeric_options().precision() << ",";
1313
ss << field.numeric_options().scale() << ")";
1314
destination.append(ss.str());
1317
1259
case Table::Field::VARCHAR:
1319
if (field.string_options().has_collation()
1320
&& field.string_options().collation().compare("binary") == 0)
1321
destination.append(" VARBINARY(", 11);
1323
destination.append(" VARCHAR(", 9);
1325
destination.append(boost::lexical_cast<string>(field.string_options().length()));
1326
destination.append(")");
1261
destination.append(" VARCHAR(", 9);
1263
ss << field.string_options().length() << ")";
1264
destination.append(ss.str());
1329
1267
case Table::Field::BLOB:
1331
if (field.string_options().has_collation()
1332
&& field.string_options().collation().compare("binary") == 0)
1333
destination.append(" BLOB", 5);
1335
destination.append(" TEXT", 5);
1268
destination.append(" BLOB", 5);
1338
1270
case Table::Field::ENUM:
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);
1396
1344
if (field.type() == Table::Field::BLOB ||
1397
1345
field.type() == Table::Field::VARCHAR)
1399
if (field.string_options().has_collation()
1400
&& field.string_options().collation().compare("binary"))
1347
if (field.string_options().has_collation())
1402
1349
destination.append(" COLLATE ", 9);
1403
1350
destination.append(field.string_options().collation());
1407
if (field.has_constraints() &&
1408
! field.constraints().is_nullable())
1410
destination.append(" NOT NULL", 9);
1412
else if (field.type() == Table::Field::TIMESTAMP)
1413
destination.append(" NULL", 5);
1415
if (field.type() == Table::Field::INTEGER ||
1416
field.type() == Table::Field::BIGINT)
1418
/* AUTO_INCREMENT must be after NOT NULL */
1419
if (field.has_numeric_options() &&
1420
field.numeric_options().is_autoincrement())
1422
destination.append(" AUTO_INCREMENT", 15);
1426
1354
if (field.options().has_default_value())
1428
1356
destination.append(" DEFAULT ", 9);
1429
append_escaped_string(&destination, field.options().default_value());
1431
else if (field.options().has_default_expression())
1433
destination.append(" DEFAULT ", 9);
1434
destination.append(field.options().default_expression());
1436
else if (field.options().has_default_bin_value())
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())
1438
1364
const string &v= field.options().default_bin_value();
1439
if (v.length() == 0)
1440
destination.append(" DEFAULT ''", 11);
1365
destination.append(" DEFAULT 0x", 11);
1366
for (size_t x= 0; x < v.length(); x++)
1443
destination.append(" DEFAULT 0x", 11);
1444
for (size_t x= 0; x < v.length(); x++)
1447
snprintf(hex, sizeof(hex), "%.2X", *(v.c_str() + x));
1448
destination.append(hex, 2);
1368
printf("%.2x", *(v.c_str() + x));
1452
else if (field.options().has_default_null()
1453
&& field.options().default_null()
1454
&& field.type() != Table::Field::BLOB)
1456
destination.append(" DEFAULT NULL", 13);
1459
if (field.has_options() && field.options().has_update_expression())
1372
if (field.has_options() && field.options().has_update_value())
1461
1374
destination.append(" ON UPDATE ", 11);
1462
destination.append(field.options().update_expression());
1375
destination.append(field.options().update_value());
1465
1378
if (field.has_comment())
1467
1380
destination.append(" COMMENT ", 9);
1468
append_escaped_string(&destination, field.comment(), quoted_default);
1381
destination.push_back(quoted_identifier);
1382
destination.append(field.comment());
1383
destination.push_back(quoted_identifier);