916
916
destination.append("TABLE ", 6);
919
destination.push_back(quoted_identifier);
920
destination.append(table.schema());
921
destination.push_back(quoted_identifier);
919
append_escaped_string(&destination, table.schema(), quoted_identifier);
922
920
destination.push_back('.');
924
destination.push_back(quoted_identifier);
925
destination.append(table.name());
926
destination.push_back(quoted_identifier);
922
append_escaped_string(&destination, table.name(), quoted_identifier);
927
923
destination.append(" (\n", 3);
929
925
enum TransformSqlError result= NONE;
982
980
/* Add ENGINE = " clause */
983
981
if (table.has_engine())
985
destination.append("\nENGINE = ", 10);
983
destination.append(" ENGINE=", 8);
986
984
destination.append(table.engine().name());
988
986
size_t num_engine_options= table.engine().options_size();
987
if (num_engine_options > 0)
988
destination.append(" ", 1);
989
989
for (size_t x= 0; x < num_engine_options; ++x)
991
991
const Engine::Option &option= table.engine().options(x);
992
destination.push_back('\n');
993
992
destination.append(option.name());
994
destination.append(" = ", 3);
993
destination.append("='", 2);
995
994
destination.append(option.state());
996
destination.push_back('\n');
995
destination.append("'", 1);
996
if(x != num_engine_options-1)
997
destination.append(", ", 2);
1016
1017
if (options.has_comment())
1018
destination.append("\nCOMMENT = '", 12);
1019
destination.append(options.comment());
1020
destination.push_back('\'');
1019
destination.append(" COMMENT=", 9);
1020
append_escaped_string(&destination, options.comment());
1023
1023
if (options.has_collation())
1025
destination.append("\nCOLLATE = ", 11);
1025
destination.append(" COLLATE = ", 11);
1026
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());
1037
1029
if (options.has_data_file_name())
1039
1031
destination.append("\nDATA_FILE_NAME = '", 19);
1067
if (options.has_auto_increment_value())
1059
if (options.has_user_set_auto_increment_value()
1060
&& options.has_auto_increment_value())
1069
1062
ss << options.auto_increment_value();
1070
destination.append("\nAUTO_INCREMENT = ", 18);
1063
destination.append(" AUTO_INCREMENT=", 16);
1071
1064
destination.append(ss.str());
1100
1093
if (sql_variant == ANSI)
1101
1094
quoted_identifier= '"';
1096
destination.append(" ", 2);
1103
1098
if (index.is_primary())
1104
1099
destination.append("PRIMARY ", 8);
1105
1100
else if (index.is_unique())
1106
1101
destination.append("UNIQUE ", 7);
1108
1103
destination.append("KEY ", 4);
1109
destination.push_back(quoted_identifier);
1110
destination.append(index.name());
1111
destination.push_back(quoted_identifier);
1112
destination.append(" (", 2);
1104
if (! (index.is_primary() && index.name().compare("PRIMARY")==0))
1106
destination.push_back(quoted_identifier);
1107
destination.append(index.name());
1108
destination.push_back(quoted_identifier);
1109
destination.append(" (", 2);
1112
destination.append("(", 1);
1114
1114
size_t num_parts= index.index_part_size();
1115
1115
for (size_t x= 0; x < num_parts; ++x)
1148
1148
destination.push_back(')');
1150
switch (index.type())
1152
case Table::Index::UNKNOWN_INDEX:
1154
case Table::Index::BTREE:
1155
destination.append(" USING BTREE", 12);
1157
case Table::Index::RTREE:
1158
destination.append(" USING RTREE", 12);
1160
case Table::Index::HASH:
1161
destination.append(" USING HASH", 11);
1163
case Table::Index::FULLTEXT:
1164
destination.append(" USING FULLTEXT", 15);
1168
if (index.has_comment())
1170
destination.append(" COMMENT ", 9);
1171
append_escaped_string(&destination, index.comment());
1242
1266
enum TransformSqlVariant sql_variant)
1244
1268
char quoted_identifier= '`';
1269
char quoted_default;
1245
1271
if (sql_variant == ANSI)
1246
1272
quoted_identifier= '"';
1248
destination.push_back(quoted_identifier);
1249
destination.append(field.name());
1250
destination.push_back(quoted_identifier);
1274
if (sql_variant == DRIZZLE)
1275
quoted_default= '\'';
1277
quoted_default= quoted_identifier;
1279
append_escaped_string(&destination, field.name(), quoted_identifier);
1252
1281
Table::Field::FieldType field_type= field.type();
1254
1283
switch (field_type)
1256
1285
case Table::Field::DOUBLE:
1257
destination.append(" DOUBLE", 7);
1286
destination.append(" double", 7);
1287
if (field.has_numeric_options()
1288
&& field.numeric_options().has_precision())
1291
ss << "(" << field.numeric_options().precision() << ",";
1292
ss << field.numeric_options().scale() << ")";
1293
destination.append(ss.str());
1259
1296
case Table::Field::VARCHAR:
1261
destination.append(" VARCHAR(", 9);
1298
if (field.string_options().has_collation()
1299
&& field.string_options().collation().compare("binary") == 0)
1300
destination.append(" varbinary(", 11);
1302
destination.append(" varchar(", 9);
1262
1304
stringstream ss;
1263
1305
ss << field.string_options().length() << ")";
1264
1306
destination.append(ss.str());
1267
1309
case Table::Field::BLOB:
1268
destination.append(" BLOB", 5);
1311
if (field.string_options().has_collation()
1312
&& field.string_options().collation().compare("binary") == 0)
1313
destination.append(" blob", 5);
1315
destination.append(" text", 5);
1270
1318
case Table::Field::ENUM:
1272
1320
size_t num_field_values= field.enumeration_values().field_value_size();
1273
destination.append(" ENUM(", 6);
1321
destination.append(" enum(", 6);
1274
1322
for (size_t x= 0; x < num_field_values; ++x)
1276
1324
const string &type= field.enumeration_values().field_value(x);
1288
1336
case Table::Field::INTEGER:
1289
destination.append(" INT", 4);
1337
destination.append(" int", 4);
1291
1339
case Table::Field::BIGINT:
1292
destination.append(" BIGINT", 7);
1340
destination.append(" bigint", 7);
1294
1342
case Table::Field::DECIMAL:
1296
destination.append(" DECIMAL(", 9);
1344
destination.append(" decimal(", 9);
1297
1345
stringstream ss;
1298
1346
ss << field.numeric_options().precision() << ",";
1299
1347
ss << field.numeric_options().scale() << ")";
1303
1351
case Table::Field::DATE:
1304
destination.append(" DATE", 5);
1352
destination.append(" date", 5);
1306
1354
case Table::Field::TIMESTAMP:
1307
destination.append(" TIMESTAMP", 10);
1355
destination.append(" timestamp", 10);
1309
1357
case Table::Field::DATETIME:
1310
destination.append(" DATETIME", 9);
1358
destination.append(" datetime", 9);
1373
if (field.type() == Table::Field::BLOB ||
1374
field.type() == Table::Field::VARCHAR)
1376
if (field.string_options().has_collation()
1377
&& (field.string_options().collation().compare("binary")
1378
&& field.string_options().collation().compare("utf8_general_ci")))
1380
destination.append(" COLLATE ", 9);
1381
destination.append(field.string_options().collation());
1326
if (! (field.has_constraints() &&
1327
field.constraints().is_nullable()))
1385
if (field.has_constraints() &&
1386
! field.constraints().is_nullable())
1329
destination.append(" NOT", 4);
1388
destination.append(" NOT NULL", 9);
1331
destination.append(" NULL", 5);
1390
else if (field.type() == Table::Field::TIMESTAMP)
1391
destination.append(" NULL", 5);
1333
1393
if (field.type() == Table::Field::INTEGER ||
1334
1394
field.type() == Table::Field::BIGINT)
1344
if (field.type() == Table::Field::BLOB ||
1345
field.type() == Table::Field::VARCHAR)
1347
if (field.string_options().has_collation())
1349
destination.append(" COLLATE ", 9);
1350
destination.append(field.string_options().collation());
1354
1404
if (field.options().has_default_value())
1356
1406
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())
1407
append_escaped_string(&destination, field.options().default_value());
1409
else if (field.options().has_default_expression())
1411
destination.append(" DEFAULT ", 9);
1412
destination.append(field.options().default_expression());
1414
else if (field.options().has_default_bin_value())
1364
1416
const string &v= field.options().default_bin_value();
1365
destination.append(" DEFAULT 0x", 11);
1366
for (size_t x= 0; x < v.length(); x++)
1417
if (v.length() == 0)
1418
destination.append(" DEFAULT ''", 11);
1368
printf("%.2x", *(v.c_str() + x));
1421
destination.append(" DEFAULT 0x", 11);
1422
for (size_t x= 0; x < v.length(); x++)
1425
snprintf(hex, sizeof(hex), "%.2X", *(v.c_str() + x));
1426
destination.append(hex, 2);
1430
else if (field.options().has_default_null()
1431
&& field.options().default_null()
1432
&& field.type() != Table::Field::BLOB)
1434
destination.append(" DEFAULT NULL", 13);
1372
if (field.has_options() && field.options().has_update_value())
1437
if (field.has_options() && field.options().has_update_expression())
1374
1439
destination.append(" ON UPDATE ", 11);
1375
destination.append(field.options().update_value());
1440
destination.append(field.options().update_expression());
1378
1443
if (field.has_comment())
1380
1445
destination.append(" COMMENT ", 9);
1381
destination.push_back(quoted_identifier);
1382
destination.append(field.comment());
1383
destination.push_back(quoted_identifier);
1446
append_escaped_string(&destination, field.comment(), quoted_default);