787
629
* up to a \0 being output here.
789
631
string raw_data(data.record(x).key_value(y));
790
destination.append(raw_data.c_str(), raw_data.size());
632
destination->append(raw_data.c_str(), raw_data.size());
794
string tmp(data.record(x).key_value(y));
795
escapeEmbeddedQuotes(tmp);
796
destination.append(tmp);
636
destination->append(data.record(x).key_value(y));
799
639
if (should_quote_field_value)
800
destination.push_back('\'');
640
destination->push_back('\'');
802
642
if (num_key_fields > 1)
803
destination.push_back(')');
643
destination->push_back(')');
808
enum TransformSqlError
809
transformDropSchemaStatementToSql(const DropSchemaStatement &statement,
811
enum TransformSqlVariant sql_variant)
813
char quoted_identifier= '`';
814
if (sql_variant == ANSI)
815
quoted_identifier= '"';
817
destination.append("DROP SCHEMA ", 12);
818
destination.push_back(quoted_identifier);
819
destination.append(statement.schema_name());
820
destination.push_back(quoted_identifier);
825
enum TransformSqlError
826
transformCreateSchemaStatementToSql(const CreateSchemaStatement &statement,
828
enum TransformSqlVariant sql_variant)
830
char quoted_identifier= '`';
831
if (sql_variant == ANSI)
832
quoted_identifier= '"';
834
const Schema &schema= statement.schema();
836
destination.append("CREATE SCHEMA ", 14);
837
destination.push_back(quoted_identifier);
838
destination.append(schema.name());
839
destination.push_back(quoted_identifier);
841
if (schema.has_collation())
843
destination.append(" COLLATE ", 9);
844
destination.append(schema.collation());
850
enum TransformSqlError
851
transformDropTableStatementToSql(const DropTableStatement &statement,
853
enum TransformSqlVariant sql_variant)
855
char quoted_identifier= '`';
856
if (sql_variant == ANSI)
857
quoted_identifier= '"';
859
const TableMetadata &table_metadata= statement.table_metadata();
861
destination.append("DROP TABLE ", 11);
863
/* Add the IF EXISTS clause if necessary */
864
if (statement.has_if_exists_clause() &&
865
statement.if_exists_clause() == true)
867
destination.append("IF EXISTS ", 10);
870
destination.push_back(quoted_identifier);
871
destination.append(table_metadata.schema_name());
872
destination.push_back(quoted_identifier);
873
destination.push_back('.');
874
destination.push_back(quoted_identifier);
875
destination.append(table_metadata.table_name());
876
destination.push_back(quoted_identifier);
881
enum TransformSqlError
882
transformTruncateTableStatementToSql(const TruncateTableStatement &statement,
884
enum TransformSqlVariant sql_variant)
886
char quoted_identifier= '`';
887
if (sql_variant == ANSI)
888
quoted_identifier= '"';
890
const TableMetadata &table_metadata= statement.table_metadata();
892
destination.append("TRUNCATE TABLE ", 15);
893
destination.push_back(quoted_identifier);
894
destination.append(table_metadata.schema_name());
895
destination.push_back(quoted_identifier);
896
destination.push_back('.');
897
destination.push_back(quoted_identifier);
898
destination.append(table_metadata.table_name());
899
destination.push_back(quoted_identifier);
904
enum TransformSqlError
905
transformSetVariableStatementToSql(const SetVariableStatement &statement,
907
enum TransformSqlVariant sql_variant)
648
enum message::TransformSqlError
649
message::transformTruncateTableStatementToSql(const message::TruncateTableStatement &statement,
650
std::string *destination,
651
enum message::TransformSqlVariant sql_variant)
653
char quoted_identifier= '`';
654
if (sql_variant == ANSI)
655
quoted_identifier= '"';
657
const message::TableMetadata &table_metadata= statement.table_metadata();
659
destination->append("TRUNCATE TABLE ", 15);
660
destination->push_back(quoted_identifier);
661
destination->append(table_metadata.schema_name());
662
destination->push_back(quoted_identifier);
663
destination->push_back('.');
664
destination->push_back(quoted_identifier);
665
destination->append(table_metadata.table_name());
666
destination->push_back(quoted_identifier);
671
enum message::TransformSqlError
672
message::transformSetVariableStatementToSql(const message::SetVariableStatement &statement,
673
std::string *destination,
674
enum message::TransformSqlVariant sql_variant)
909
676
(void) sql_variant;
910
const FieldMetadata &variable_metadata= statement.variable_metadata();
911
bool should_quote_field_value= shouldQuoteFieldValue(variable_metadata.type());
913
destination.append("SET GLOBAL ", 11); /* Only global variables are replicated */
914
destination.append(variable_metadata.name());
915
destination.push_back('=');
917
if (should_quote_field_value)
918
destination.push_back('\'');
920
destination.append(statement.variable_value());
922
if (should_quote_field_value)
923
destination.push_back('\'');
928
enum TransformSqlError
929
transformCreateTableStatementToSql(const CreateTableStatement &statement,
931
enum TransformSqlVariant sql_variant)
933
return transformTableDefinitionToSql(statement.table(), destination, sql_variant);
936
enum TransformSqlError
937
transformTableDefinitionToSql(const Table &table,
939
enum TransformSqlVariant sql_variant, bool with_schema)
941
char quoted_identifier= '`';
942
if (sql_variant == ANSI)
943
quoted_identifier= '"';
945
destination.append("CREATE ", 7);
947
if (table.type() == Table::TEMPORARY)
948
destination.append("TEMPORARY ", 10);
950
destination.append("TABLE ", 6);
953
append_escaped_string(&destination, table.schema(), quoted_identifier);
954
destination.push_back('.');
956
append_escaped_string(&destination, table.name(), quoted_identifier);
957
destination.append(" (\n", 3);
959
enum TransformSqlError result= NONE;
960
size_t num_fields= table.field_size();
961
for (size_t x= 0; x < num_fields; ++x)
963
const Table::Field &field= table.field(x);
966
destination.append(",\n", 2);
968
destination.append(" ");
970
result= transformFieldDefinitionToSql(field, destination, sql_variant);
976
size_t num_indexes= table.indexes_size();
979
destination.append(",\n", 2);
981
for (size_t x= 0; x < num_indexes; ++x)
983
const message::Table::Index &index= table.indexes(x);
986
destination.append(",\n", 2);
988
result= transformIndexDefinitionToSql(index, table, destination, sql_variant);
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
destination.append("\n)", 2);
1014
/* Add ENGINE = " clause */
1015
if (table.has_engine())
1017
destination.append(" ENGINE=", 8);
1018
destination.append(table.engine().name());
1020
size_t num_engine_options= table.engine().options_size();
1021
if (num_engine_options > 0)
1022
destination.append(" ", 1);
1023
for (size_t x= 0; x < num_engine_options; ++x)
1025
const Engine::Option &option= table.engine().options(x);
1026
destination.append(option.name());
1027
destination.append("='", 2);
1028
destination.append(option.state());
1029
destination.append("'", 1);
1030
if(x != num_engine_options-1)
1031
destination.append(", ", 2);
1035
if (table.has_options())
1036
(void) transformTableOptionsToSql(table.options(), destination, sql_variant);
1041
enum TransformSqlError
1042
transformTableOptionsToSql(const Table::TableOptions &options,
1043
string &destination,
1044
enum TransformSqlVariant sql_variant)
1046
if (sql_variant == ANSI)
1047
return NONE; /* ANSI does not support table options... */
1049
if (options.has_comment())
1051
destination.append(" COMMENT=", 9);
1052
append_escaped_string(&destination, options.comment());
1055
if (options.has_collation())
1057
destination.append(" COLLATE = ", 11);
1058
destination.append(options.collation());
1061
if (options.has_data_file_name())
1063
destination.append("\nDATA_FILE_NAME = '", 19);
1064
destination.append(options.data_file_name());
1065
destination.push_back('\'');
1068
if (options.has_index_file_name())
1070
destination.append("\nINDEX_FILE_NAME = '", 20);
1071
destination.append(options.index_file_name());
1072
destination.push_back('\'');
1075
if (options.has_max_rows())
1077
destination.append("\nMAX_ROWS = ", 12);
1078
destination.append(boost::lexical_cast<string>(options.max_rows()));
1081
if (options.has_min_rows())
1083
destination.append("\nMIN_ROWS = ", 12);
1084
destination.append(boost::lexical_cast<string>(options.min_rows()));
1087
if (options.has_user_set_auto_increment_value()
1088
&& options.has_auto_increment_value())
1090
destination.append(" AUTO_INCREMENT=", 16);
1091
destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
1094
if (options.has_avg_row_length())
1096
destination.append("\nAVG_ROW_LENGTH = ", 18);
1097
destination.append(boost::lexical_cast<string>(options.avg_row_length()));
1100
if (options.has_checksum() &&
1102
destination.append("\nCHECKSUM = TRUE", 16);
1103
if (options.has_page_checksum() &&
1104
options.page_checksum())
1105
destination.append("\nPAGE_CHECKSUM = TRUE", 21);
1110
enum TransformSqlError
1111
transformIndexDefinitionToSql(const Table::Index &index,
1113
string &destination,
1114
enum TransformSqlVariant sql_variant)
1116
char quoted_identifier= '`';
1117
if (sql_variant == ANSI)
1118
quoted_identifier= '"';
1120
destination.append(" ", 2);
1122
if (index.is_primary())
1123
destination.append("PRIMARY ", 8);
1124
else if (index.is_unique())
1125
destination.append("UNIQUE ", 7);
1127
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);
1138
size_t num_parts= index.index_part_size();
1139
for (size_t x= 0; x < num_parts; ++x)
1141
const Table::Index::IndexPart &part= index.index_part(x);
1142
const Table::Field &field= table.field(part.fieldnr());
1145
destination.push_back(',');
1147
destination.push_back(quoted_identifier);
1148
destination.append(field.name());
1149
destination.push_back(quoted_identifier);
1152
* If the index part's field type is VARCHAR or TEXT
1153
* then check for a prefix length then is different
1154
* from the field's full length...
1156
if (field.type() == Table::Field::VARCHAR ||
1157
field.type() == Table::Field::BLOB)
1159
if (part.has_compare_length())
1161
if (part.compare_length() != field.string_options().length())
1163
destination.push_back('(');
1164
destination.append(boost::lexical_cast<string>(part.compare_length()));
1165
destination.push_back(')');
1170
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);
1284
enum TransformSqlError
1285
transformFieldDefinitionToSql(const Table::Field &field,
1286
string &destination,
1287
enum TransformSqlVariant sql_variant)
1289
char quoted_identifier= '`';
1290
char quoted_default;
1292
if (sql_variant == ANSI)
1293
quoted_identifier= '"';
1295
if (sql_variant == DRIZZLE)
1296
quoted_default= '\'';
1298
quoted_default= quoted_identifier;
1300
append_escaped_string(&destination, field.name(), quoted_identifier);
1302
Table::Field::FieldType field_type= field.type();
1306
case Table::Field::DOUBLE:
1307
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
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(")");
1329
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);
1338
case Table::Field::ENUM:
1340
size_t num_field_values= field.enumeration_values().field_value_size();
1341
destination.append(" ENUM(", 6);
1342
for (size_t x= 0; x < num_field_values; ++x)
1344
const string &type= field.enumeration_values().field_value(x);
1347
destination.push_back(',');
1349
destination.push_back('\'');
1350
destination.append(type);
1351
destination.push_back('\'');
1353
destination.push_back(')');
1356
case Table::Field::UUID:
1357
destination.append(" UUID", 5);
1359
case Table::Field::INTEGER:
1360
destination.append(" INT", 4);
1362
case Table::Field::BIGINT:
1363
destination.append(" BIGINT", 7);
1365
case Table::Field::DECIMAL:
1367
destination.append(" DECIMAL(", 9);
1369
ss << field.numeric_options().precision() << ",";
1370
ss << field.numeric_options().scale() << ")";
1371
destination.append(ss.str());
1374
case Table::Field::DATE:
1375
destination.append(" DATE", 5);
1377
case Table::Field::TIMESTAMP:
1378
destination.append(" TIMESTAMP", 10);
1380
case Table::Field::DATETIME:
1381
destination.append(" DATETIME", 9);
1385
if (field.type() == Table::Field::INTEGER ||
1386
field.type() == Table::Field::BIGINT)
1388
if (field.has_constraints() &&
1389
field.constraints().has_is_unsigned() &&
1390
field.constraints().is_unsigned())
1392
destination.append(" UNSIGNED", 9);
1396
if (field.type() == Table::Field::BLOB ||
1397
field.type() == Table::Field::VARCHAR)
1399
if (field.string_options().has_collation()
1400
&& field.string_options().collation().compare("binary"))
1402
destination.append(" COLLATE ", 9);
1403
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
if (field.options().has_default_value())
1428
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())
1438
const string &v= field.options().default_bin_value();
1439
if (v.length() == 0)
1440
destination.append(" DEFAULT ''", 11);
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);
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());
1465
if (field.has_comment())
1467
destination.append(" COMMENT ", 9);
1468
append_escaped_string(&destination, field.comment(), quoted_default);
1473
bool shouldQuoteFieldValue(Table::Field::FieldType in_type)
677
const message::FieldMetadata &variable_metadata= statement.variable_metadata();
678
bool should_quote_field_value= message::shouldQuoteFieldValue(variable_metadata.type());
680
destination->append("SET GLOBAL ", 11); /* Only global variables are replicated */
681
destination->append(variable_metadata.name());
682
destination->push_back('=');
684
if (should_quote_field_value)
685
destination->push_back('\'');
687
destination->append(statement.variable_value());
689
if (should_quote_field_value)
690
destination->push_back('\'');
695
bool message::shouldQuoteFieldValue(message::Table::Field::FieldType in_type)
1475
697
switch (in_type)
1477
case Table::Field::DOUBLE:
1478
case Table::Field::DECIMAL:
1479
case Table::Field::INTEGER:
1480
case Table::Field::BIGINT:
699
case message::Table::Field::DOUBLE:
700
case message::Table::Field::DECIMAL:
701
case message::Table::Field::INTEGER:
702
case message::Table::Field::BIGINT:
703
case message::Table::Field::ENUM:
1487
Table::Field::FieldType internalFieldTypeToFieldProtoType(enum enum_field_types type)
710
drizzled::message::Table::Field::FieldType message::internalFieldTypeToFieldProtoType(enum enum_field_types type)
1490
713
case DRIZZLE_TYPE_LONG:
1491
return Table::Field::INTEGER;
714
return message::Table::Field::INTEGER;
1492
715
case DRIZZLE_TYPE_DOUBLE:
1493
return Table::Field::DOUBLE;
716
return message::Table::Field::DOUBLE;
1494
717
case DRIZZLE_TYPE_NULL:
1495
718
assert(false); /* Not a user definable type */
1496
return Table::Field::INTEGER; /* unreachable */
719
return message::Table::Field::INTEGER; /* unreachable */
1497
720
case DRIZZLE_TYPE_TIMESTAMP:
1498
return Table::Field::TIMESTAMP;
721
return message::Table::Field::TIMESTAMP;
1499
722
case DRIZZLE_TYPE_LONGLONG:
1500
return Table::Field::BIGINT;
723
return message::Table::Field::BIGINT;
1501
724
case DRIZZLE_TYPE_DATETIME:
1502
return Table::Field::DATETIME;
725
return message::Table::Field::DATETIME;
1503
726
case DRIZZLE_TYPE_DATE:
1504
return Table::Field::DATE;
727
return message::Table::Field::DATE;
1505
728
case DRIZZLE_TYPE_VARCHAR:
1506
return Table::Field::VARCHAR;
729
return message::Table::Field::VARCHAR;
1507
730
case DRIZZLE_TYPE_DECIMAL:
1508
return Table::Field::DECIMAL;
731
return message::Table::Field::DECIMAL;
1509
732
case DRIZZLE_TYPE_ENUM:
1510
return Table::Field::ENUM;
733
return message::Table::Field::ENUM;
1511
734
case DRIZZLE_TYPE_BLOB:
1512
return Table::Field::BLOB;
1513
case DRIZZLE_TYPE_UUID:
1514
return Table::Field::UUID;
735
return message::Table::Field::BLOB;
1518
return Table::Field::INTEGER; /* unreachable */
1521
bool transactionContainsBulkSegment(const Transaction &transaction)
1523
size_t num_statements= transaction.statement_size();
1524
if (num_statements == 0)
1528
* Only INSERT, UPDATE, and DELETE statements can possibly
1529
* have bulk segments. So, we loop through the statements
1530
* checking for segment_id > 1 in those specific submessages.
1533
for (x= 0; x < num_statements; ++x)
1535
const Statement &statement= transaction.statement(x);
1536
Statement::Type type= statement.type();
1540
case Statement::INSERT:
1541
if (statement.insert_data().segment_id() > 1)
1544
case Statement::UPDATE:
1545
if (statement.update_data().segment_id() > 1)
1548
case Statement::DELETE:
1549
if (statement.delete_data().segment_id() > 1)
1559
} /* namespace message */
739
return message::Table::Field::INTEGER; /* unreachable */
1560
742
} /* namespace drizzled */