55
static void escapeEmbeddedQuotes(string &s, const char quote='\'')
59
for (it= s.begin(); it != s.end(); ++it)
63
it= s.insert(it, quote);
64
++it; // advance back to the quote
69
/* Incredibly similar to append_unescaped() in table.cc, but for std::string */
70
static void append_escaped_string(std::string *res, const std::string &input, const char quote='\'')
72
const char *pos= input.c_str();
73
const char *end= input.c_str()+input.length();
74
res->push_back(quote);
76
for (; pos != end ; pos++)
79
if (use_mb(default_charset_info) &&
80
(mblen= my_ismbchar(default_charset_info, pos, end)))
82
res->append(pos, mblen);
90
case 0: /* Must be escaped for 'mysql' */
94
case '\n': /* Must be escaped for logs */
99
res->push_back('\\'); /* This gives better readability */
103
res->push_back('\\'); /* Because of the sql syntax */
104
res->push_back('\\');
107
if (*pos == quote) /* SQL syntax for quoting a quote */
109
res->push_back(quote);
110
res->push_back(quote);
113
res->push_back(*pos);
117
res->push_back(quote);
120
51
enum TransformSqlError
121
52
transformStatementToSql(const Statement &source,
122
53
vector<string> &sql_strings,
402
324
const FieldMetadata &field_metadata= header.field_metadata(x);
404
if (record.is_null(x))
406
should_quote_field_value= false;
410
should_quote_field_value= shouldQuoteFieldValue(field_metadata.type());
326
should_quote_field_value= shouldQuoteFieldValue(field_metadata.type());
413
328
if (should_quote_field_value)
414
329
destination.push_back('\'');
416
if (record.is_null(x))
331
if (field_metadata.type() == Table::Field::BLOB)
418
destination.append("NULL");
334
* We do this here because BLOB data is returned
335
* in a string correctly, but calling append()
336
* without a length will result in only the string
337
* up to a \0 being output here.
339
string raw_data(record.insert_value(x));
340
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);
344
destination.append(record.insert_value(x));
441
347
if (should_quote_field_value)
564
468
destination.push_back(quoted_identifier);
565
469
destination.push_back('=');
567
if (record.is_null(x))
569
should_quote_field_value= false;
573
should_quote_field_value= shouldQuoteFieldValue(field_metadata.type());
471
should_quote_field_value= shouldQuoteFieldValue(field_metadata.type());
576
473
if (should_quote_field_value)
577
474
destination.push_back('\'');
579
if (record.is_null(x))
476
if (field_metadata.type() == Table::Field::BLOB)
581
destination.append("NULL");
479
* We do this here because BLOB data is returned
480
* in a string correctly, but calling append()
481
* without a length will result in only the string
482
* up to a \0 being output here.
484
string raw_data(record.after_value(x));
485
destination.append(raw_data.c_str(), raw_data.size());
585
if (field_metadata.type() == Table::Field::BLOB)
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());
598
string tmp(record.after_value(x));
599
escapeEmbeddedQuotes(tmp);
600
destination.append(tmp);
489
destination.append(record.after_value(x));
604
492
if (should_quote_field_value)
950
834
destination.append("TABLE ", 6);
953
append_escaped_string(&destination, table.schema(), quoted_identifier);
837
destination.push_back(quoted_identifier);
838
destination.append(table.schema());
839
destination.push_back(quoted_identifier);
954
840
destination.push_back('.');
956
append_escaped_string(&destination, table.name(), quoted_identifier);
842
destination.push_back(quoted_identifier);
843
destination.append(table.name());
844
destination.push_back(quoted_identifier);
957
845
destination.append(" (\n", 3);
959
847
enum TransformSqlError result= NONE;
990
876
if (result != NONE)
994
size_t num_foreign_keys= table.fk_constraint_size();
996
if (num_foreign_keys > 0)
997
destination.append(",\n", 2);
999
for (size_t x= 0; x < num_foreign_keys; ++x)
1001
const message::Table::ForeignKeyConstraint &fkey= table.fk_constraint(x);
1004
destination.append(",\n", 2);
1006
result= transformForeignKeyConstraintDefinitionToSql(fkey, table, destination, sql_variant);
1012
879
destination.append("\n)", 2);
1014
881
/* Add ENGINE = " clause */
1015
882
if (table.has_engine())
1017
destination.append(" ENGINE=", 8);
884
destination.append("\nENGINE = ", 10);
1018
885
destination.append(table.engine().name());
1020
887
size_t num_engine_options= table.engine().options_size();
1021
if (num_engine_options > 0)
1022
destination.append(" ", 1);
1023
888
for (size_t x= 0; x < num_engine_options; ++x)
1025
890
const Engine::Option &option= table.engine().options(x);
891
destination.push_back('\n');
1026
892
destination.append(option.name());
1027
destination.append("='", 2);
893
destination.append(" = ", 3);
1028
894
destination.append(option.state());
1029
destination.append("'", 1);
1030
if(x != num_engine_options-1)
1031
destination.append(", ", 2);
895
destination.push_back('\n');
1046
910
if (sql_variant == ANSI)
1047
911
return NONE; /* ANSI does not support table options... */
1049
915
if (options.has_comment())
1051
destination.append(" COMMENT=", 9);
1052
append_escaped_string(&destination, options.comment());
917
destination.append("\nCOMMENT = '", 12);
918
destination.append(options.comment());
919
destination.push_back('\'');
1055
922
if (options.has_collation())
1057
destination.append(" COLLATE = ", 11);
924
destination.append("\nCOLLATE = ", 11);
1058
925
destination.append(options.collation());
928
if (options.has_auto_increment())
930
ss << options.auto_increment();
931
destination.append("\nAUTOINCREMENT_OFFSET = ", 24);
932
destination.append(ss.str());
936
if (options.has_row_type())
938
ss << options.row_type();
939
destination.append("\nROW_TYPE = ", 12);
940
destination.append(ss.str());
1061
944
if (options.has_data_file_name())
1063
946
destination.append("\nDATA_FILE_NAME = '", 19);
1075
958
if (options.has_max_rows())
960
ss << options.max_rows();
1077
961
destination.append("\nMAX_ROWS = ", 12);
1078
destination.append(boost::lexical_cast<string>(options.max_rows()));
962
destination.append(ss.str());
1081
966
if (options.has_min_rows())
968
ss << options.min_rows();
1083
969
destination.append("\nMIN_ROWS = ", 12);
1084
destination.append(boost::lexical_cast<string>(options.min_rows()));
970
destination.append(ss.str());
1087
if (options.has_user_set_auto_increment_value()
1088
&& options.has_auto_increment_value())
974
if (options.has_auto_increment_value())
1090
destination.append(" AUTO_INCREMENT=", 16);
1091
destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
976
ss << options.auto_increment_value();
977
destination.append("\nAUTO_INCREMENT = ", 18);
978
destination.append(ss.str());
1094
982
if (options.has_avg_row_length())
984
ss << options.avg_row_length();
1096
985
destination.append("\nAVG_ROW_LENGTH = ", 18);
1097
destination.append(boost::lexical_cast<string>(options.avg_row_length()));
986
destination.append(ss.str());
1100
990
if (options.has_checksum() &&
1117
1007
if (sql_variant == ANSI)
1118
1008
quoted_identifier= '"';
1120
destination.append(" ", 2);
1122
1010
if (index.is_primary())
1123
1011
destination.append("PRIMARY ", 8);
1124
1012
else if (index.is_unique())
1125
1013
destination.append("UNIQUE ", 7);
1127
1015
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);
1016
destination.push_back(quoted_identifier);
1017
destination.append(index.name());
1018
destination.push_back(quoted_identifier);
1019
destination.append(" (", 2);
1138
1021
size_t num_parts= index.index_part_size();
1139
1022
for (size_t x= 0; x < num_parts; ++x)
1159
1042
if (part.has_compare_length())
1161
if (part.compare_length() != field.string_options().length())
1044
size_t compare_length_in_chars= part.compare_length();
1046
/* hack: compare_length() is bytes, not chars, but
1047
* only for VARCHAR. Ass. */
1048
if (field.type() == Table::Field::VARCHAR)
1049
compare_length_in_chars/= 4;
1051
if (compare_length_in_chars != field.string_options().length())
1163
1054
destination.push_back('(');
1164
destination.append(boost::lexical_cast<string>(part.compare_length()));
1055
ss << compare_length_in_chars;
1056
destination.append(ss.str());
1165
1057
destination.push_back(')');
1170
1062
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());
1199
static void transformForeignKeyOptionToSql(Table::ForeignKeyConstraint::ForeignKeyOption opt, string &destination)
1203
case Table::ForeignKeyConstraint::OPTION_RESTRICT:
1204
destination.append("RESTRICT");
1206
case Table::ForeignKeyConstraint::OPTION_CASCADE:
1207
destination.append("CASCADE");
1209
case Table::ForeignKeyConstraint::OPTION_SET_NULL:
1210
destination.append("SET NULL");
1212
case Table::ForeignKeyConstraint::OPTION_UNDEF:
1213
case Table::ForeignKeyConstraint::OPTION_NO_ACTION:
1214
destination.append("NO ACTION");
1216
case Table::ForeignKeyConstraint::OPTION_SET_DEFAULT:
1217
destination.append("SET DEFAULT");
1222
enum TransformSqlError
1223
transformForeignKeyConstraintDefinitionToSql(const Table::ForeignKeyConstraint &fkey,
1225
string &destination,
1226
enum TransformSqlVariant sql_variant)
1228
char quoted_identifier= '`';
1229
if (sql_variant == ANSI)
1230
quoted_identifier= '"';
1232
destination.append(" ", 2);
1234
if (fkey.has_name())
1236
destination.append("CONSTRAINT ", 11);
1237
append_escaped_string(&destination, fkey.name(), quoted_identifier);
1238
destination.append(" ", 1);
1241
destination.append("FOREIGN KEY (", 13);
1243
for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
1246
destination.append(", ");
1248
append_escaped_string(&destination, fkey.column_names(x),
1252
destination.append(") REFERENCES ", 13);
1254
append_escaped_string(&destination, fkey.references_table_name(),
1256
destination.append(" (", 2);
1258
for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
1261
destination.append(", ");
1263
append_escaped_string(&destination, fkey.references_columns(x),
1267
destination.push_back(')');
1269
if (fkey.has_update_option() and fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1271
destination.append(" ON UPDATE ", 11);
1272
transformForeignKeyOptionToSql(fkey.update_option(), destination);
1275
if (fkey.has_delete_option() and fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1277
destination.append(" ON DELETE ", 11);
1278
transformForeignKeyOptionToSql(fkey.delete_option(), destination);
1306
1084
case Table::Field::DOUBLE:
1307
1085
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
1087
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(")");
1089
destination.append(" VARCHAR(", 9);
1091
ss << field.string_options().length() << ")";
1092
destination.append(ss.str());
1329
1095
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);
1096
destination.append(" BLOB", 5);
1338
1098
case Table::Field::ENUM:
1154
if (! (field.has_constraints() &&
1155
field.constraints().is_nullable()))
1157
destination.append(" NOT", 4);
1159
destination.append(" NULL", 5);
1161
if (field.type() == Table::Field::INTEGER ||
1162
field.type() == Table::Field::BIGINT)
1164
/* AUTO_INCREMENT must be after NOT NULL */
1165
if (field.has_numeric_options() &&
1166
field.numeric_options().is_autoincrement())
1168
destination.append(" AUTO_INCREMENT", 15);
1396
1172
if (field.type() == Table::Field::BLOB ||
1397
1173
field.type() == Table::Field::VARCHAR)
1399
if (field.string_options().has_collation()
1400
&& field.string_options().collation().compare("binary"))
1175
if (field.string_options().has_collation())
1402
1177
destination.append(" COLLATE ", 9);
1403
1178
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
1182
if (field.options().has_default_value())
1428
1184
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())
1185
destination.push_back(quoted_identifier);
1186
destination.append(field.options().default_value());
1187
destination.push_back(quoted_identifier);
1190
if (field.options().has_default_bin_value())
1438
1192
const string &v= field.options().default_bin_value();
1439
if (v.length() == 0)
1440
destination.append(" DEFAULT ''", 11);
1193
destination.append(" DEFAULT 0x", 11);
1194
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);
1196
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())
1461
destination.append(" ON UPDATE ", 11);
1462
destination.append(field.options().update_expression());
1200
if (field.type() == Table::Field::TIMESTAMP)
1201
if (field.timestamp_options().has_auto_updates() &&
1202
field.timestamp_options().auto_updates())
1203
destination.append(" ON UPDATE CURRENT_TIMESTAMP", 28);
1465
1205
if (field.has_comment())
1467
1207
destination.append(" COMMENT ", 9);
1468
append_escaped_string(&destination, field.comment(), quoted_default);
1208
destination.push_back(quoted_identifier);
1209
destination.append(field.comment());
1210
destination.push_back(quoted_identifier);