29
29
* Statement messages to other formats, including SQL strings.
34
34
#include <boost/lexical_cast.hpp>
35
#include <drizzled/message/statement_transform.h>
36
#include <drizzled/message/transaction.pb.h>
37
#include <drizzled/message/table.pb.h>
38
#include <drizzled/charset.h>
39
#include <drizzled/charset_info.h>
40
#include <drizzled/global_charset_info.h>
35
#include "drizzled/message/statement_transform.h"
36
#include "drizzled/message/transaction.pb.h"
37
#include "drizzled/message/table.pb.h"
38
#include "drizzled/charset.h"
39
#include "drizzled/charset_info.h"
40
#include "drizzled/global_charset_info.h"
152
152
const InsertHeader &insert_header= source.insert_header();
153
153
const InsertData &insert_data= source.insert_data();
154
154
size_t num_keys= insert_data.record_size();
156
157
if (num_keys > 1 && ! already_in_transaction)
157
158
sql_strings.push_back("START TRANSACTION");
159
for (size_t x= 0; x < num_keys; ++x)
160
for (x= 0; x < num_keys; ++x)
161
162
string destination;
317
318
sql_strings.push_back(destination);
320
case Statement::ALTER_SCHEMA:
322
assert(source.has_alter_schema_statement());
324
error= transformAlterSchemaStatementToSql(source.alter_schema_statement(),
327
sql_strings.push_back(destination);
330
321
case Statement::SET_VARIABLE:
332
323
assert(source.has_set_variable_statement());
817
808
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
809
transformDropSchemaStatementToSql(const DropSchemaStatement &statement,
850
810
string &destination,
851
811
enum TransformSqlVariant sql_variant)
874
834
const Schema &schema= statement.schema();
876
destination.append("CREATE SCHEMA ");
836
destination.append("CREATE SCHEMA ", 14);
877
837
destination.push_back(quoted_identifier);
878
838
destination.append(schema.name());
879
839
destination.push_back(quoted_identifier);
881
841
if (schema.has_collation())
883
destination.append(" COLLATE ");
843
destination.append(" COLLATE ", 9);
884
844
destination.append(schema.collation());
887
if (schema.has_replication_options() and schema.replication_options().has_dont_replicate() and schema.replication_options().dont_replicate())
889
destination.append(" REPLICATION = FALSE");
904
859
const TableMetadata &table_metadata= statement.table_metadata();
906
destination.append("DROP TABLE ");
861
destination.append("DROP TABLE ", 11);
908
863
/* Add the IF EXISTS clause if necessary */
909
864
if (statement.has_if_exists_clause() &&
910
865
statement.if_exists_clause() == true)
912
destination.append("IF EXISTS ");
867
destination.append("IF EXISTS ", 10);
915
870
destination.push_back(quoted_identifier);
955
910
const FieldMetadata &variable_metadata= statement.variable_metadata();
956
911
bool should_quote_field_value= shouldQuoteFieldValue(variable_metadata.type());
958
destination.append("SET GLOBAL "); /* Only global variables are replicated */
913
destination.append("SET GLOBAL ", 11); /* Only global variables are replicated */
959
914
destination.append(variable_metadata.name());
960
915
destination.push_back('=');
987
942
if (sql_variant == ANSI)
988
943
quoted_identifier= '"';
990
destination.append("CREATE ");
945
destination.append("CREATE ", 7);
992
947
if (table.type() == Table::TEMPORARY)
993
destination.append("TEMPORARY ");
948
destination.append("TEMPORARY ", 10);
995
destination.append("TABLE ");
950
destination.append("TABLE ", 6);
998
953
append_escaped_string(&destination, table.schema(), quoted_identifier);
999
954
destination.push_back('.');
1001
956
append_escaped_string(&destination, table.name(), quoted_identifier);
1002
destination.append(" (\n");
957
destination.append(" (\n", 3);
1004
959
enum TransformSqlError result= NONE;
1005
960
size_t num_fields= table.field_size();
1021
976
size_t num_indexes= table.indexes_size();
1023
978
if (num_indexes > 0)
1024
destination.append(",\n");
979
destination.append(",\n", 2);
1026
981
for (size_t x= 0; x < num_indexes; ++x)
1028
983
const message::Table::Index &index= table.indexes(x);
1031
destination.append(",\n");
986
destination.append(",\n", 2);
1033
988
result= transformIndexDefinitionToSql(index, table, destination, sql_variant);
1039
994
size_t num_foreign_keys= table.fk_constraint_size();
1041
996
if (num_foreign_keys > 0)
1042
destination.append(",\n");
997
destination.append(",\n", 2);
1044
999
for (size_t x= 0; x < num_foreign_keys; ++x)
1046
1001
const message::Table::ForeignKeyConstraint &fkey= table.fk_constraint(x);
1049
destination.append(",\n");
1004
destination.append(",\n", 2);
1051
1006
result= transformForeignKeyConstraintDefinitionToSql(fkey, table, destination, sql_variant);
1057
destination.append("\n)");
1012
destination.append("\n)", 2);
1059
1014
/* Add ENGINE = " clause */
1060
1015
if (table.has_engine())
1062
destination.append(" ENGINE=");
1017
destination.append(" ENGINE=", 8);
1063
1018
destination.append(table.engine().name());
1065
1020
size_t num_engine_options= table.engine().options_size();
1070
1025
const Engine::Option &option= table.engine().options(x);
1071
1026
destination.append(option.name());
1072
destination.append("='");
1027
destination.append("='", 2);
1073
1028
destination.append(option.state());
1074
destination.append("'");
1075
if (x != num_engine_options-1)
1077
destination.append(", ");
1029
destination.append("'", 1);
1030
if(x != num_engine_options-1)
1031
destination.append(", ", 2);
1096
1049
if (options.has_comment())
1098
destination.append(" COMMENT=");
1051
destination.append(" COMMENT=", 9);
1099
1052
append_escaped_string(&destination, options.comment());
1102
1055
if (options.has_collation())
1104
destination.append(" COLLATE = ");
1057
destination.append(" COLLATE = ", 11);
1105
1058
destination.append(options.collation());
1108
1061
if (options.has_data_file_name())
1110
destination.append("\nDATA_FILE_NAME = '");
1063
destination.append("\nDATA_FILE_NAME = '", 19);
1111
1064
destination.append(options.data_file_name());
1112
1065
destination.push_back('\'');
1115
1068
if (options.has_index_file_name())
1117
destination.append("\nINDEX_FILE_NAME = '");
1070
destination.append("\nINDEX_FILE_NAME = '", 20);
1118
1071
destination.append(options.index_file_name());
1119
1072
destination.push_back('\'');
1122
1075
if (options.has_max_rows())
1124
destination.append("\nMAX_ROWS = ");
1077
destination.append("\nMAX_ROWS = ", 12);
1125
1078
destination.append(boost::lexical_cast<string>(options.max_rows()));
1128
1081
if (options.has_min_rows())
1130
destination.append("\nMIN_ROWS = ");
1083
destination.append("\nMIN_ROWS = ", 12);
1131
1084
destination.append(boost::lexical_cast<string>(options.min_rows()));
1134
1087
if (options.has_user_set_auto_increment_value()
1135
1088
&& options.has_auto_increment_value())
1137
destination.append(" AUTO_INCREMENT=");
1090
destination.append(" AUTO_INCREMENT=", 16);
1138
1091
destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
1141
1094
if (options.has_avg_row_length())
1143
destination.append("\nAVG_ROW_LENGTH = ");
1096
destination.append("\nAVG_ROW_LENGTH = ", 18);
1144
1097
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");
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);
1171
1120
destination.append(" ", 2);
1173
1122
if (index.is_primary())
1174
destination.append("PRIMARY ");
1123
destination.append("PRIMARY ", 8);
1175
1124
else if (index.is_unique())
1176
destination.append("UNIQUE ");
1125
destination.append("UNIQUE ", 7);
1178
1127
destination.append("KEY ", 4);
1179
1128
if (! (index.is_primary() && index.name().compare("PRIMARY")==0))
1225
1174
case Table::Index::UNKNOWN_INDEX:
1227
1176
case Table::Index::BTREE:
1228
destination.append(" USING BTREE");
1177
destination.append(" USING BTREE", 12);
1230
1179
case Table::Index::RTREE:
1231
destination.append(" USING RTREE");
1180
destination.append(" USING RTREE", 12);
1233
1182
case Table::Index::HASH:
1234
destination.append(" USING HASH");
1183
destination.append(" USING HASH", 11);
1236
1185
case Table::Index::FULLTEXT:
1237
destination.append(" USING FULLTEXT");
1186
destination.append(" USING FULLTEXT", 15);
1241
1190
if (index.has_comment())
1243
destination.append(" COMMENT ");
1192
destination.append(" COMMENT ", 9);
1244
1193
append_escaped_string(&destination, index.comment());
1280
1229
if (sql_variant == ANSI)
1281
1230
quoted_identifier= '"';
1283
destination.append(" ");
1232
destination.append(" ", 2);
1285
1234
if (fkey.has_name())
1287
destination.append("CONSTRAINT ");
1236
destination.append("CONSTRAINT ", 11);
1288
1237
append_escaped_string(&destination, fkey.name(), quoted_identifier);
1289
1238
destination.append(" ", 1);
1292
destination.append("FOREIGN KEY (");
1241
destination.append("FOREIGN KEY (", 13);
1294
1243
for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
1300
1249
quoted_identifier);
1303
destination.append(") REFERENCES ");
1252
destination.append(") REFERENCES ", 13);
1305
1254
append_escaped_string(&destination, fkey.references_table_name(),
1306
1255
quoted_identifier);
1307
destination.append(" (");
1256
destination.append(" (", 2);
1309
1258
for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
1320
1269
if (fkey.has_update_option() and fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1322
destination.append(" ON UPDATE ");
1271
destination.append(" ON UPDATE ", 11);
1323
1272
transformForeignKeyOptionToSql(fkey.update_option(), destination);
1326
1275
if (fkey.has_delete_option() and fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1328
destination.append(" ON DELETE ");
1277
destination.append(" ON DELETE ", 11);
1329
1278
transformForeignKeyOptionToSql(fkey.delete_option(), destination);
1370
1319
if (field.string_options().has_collation()
1371
1320
&& field.string_options().collation().compare("binary") == 0)
1372
destination.append(" VARBINARY(");
1321
destination.append(" VARBINARY(", 11);
1374
destination.append(" VARCHAR(");
1323
destination.append(" VARCHAR(", 9);
1376
1325
destination.append(boost::lexical_cast<string>(field.string_options().length()));
1377
1326
destination.append(")");
1382
1331
if (field.string_options().has_collation()
1383
1332
&& field.string_options().collation().compare("binary") == 0)
1384
destination.append(" BLOB");
1333
destination.append(" BLOB", 5);
1386
destination.append(" TEXT");
1335
destination.append(" TEXT", 5);
1389
1338
case Table::Field::ENUM:
1391
1340
size_t num_field_values= field.enumeration_values().field_value_size();
1392
destination.append(" ENUM(");
1341
destination.append(" ENUM(", 6);
1393
1342
for (size_t x= 0; x < num_field_values; ++x)
1395
1344
const string &type= field.enumeration_values().field_value(x);
1407
1356
case Table::Field::UUID:
1408
destination.append(" UUID");
1357
destination.append(" UUID", 5);
1410
1359
case Table::Field::BOOLEAN:
1411
destination.append(" BOOLEAN");
1360
destination.append(" BOOLEAN", 8);
1413
1362
case Table::Field::INTEGER:
1414
destination.append(" INT");
1363
destination.append(" INT", 4);
1416
1365
case Table::Field::BIGINT:
1417
if (field.has_constraints() and
1418
field.constraints().is_unsigned())
1420
destination.append(" BIGINT UNSIGNED");
1424
destination.append(" BIGINT");
1366
destination.append(" BIGINT", 7);
1427
1368
case Table::Field::DECIMAL:
1429
destination.append(" DECIMAL(");
1370
destination.append(" DECIMAL(", 9);
1430
1371
stringstream ss;
1431
1372
ss << field.numeric_options().precision() << ",";
1432
1373
ss << field.numeric_options().scale() << ")";
1447
destination.append(" TIMESTAMP");
1388
destination.append(" TIMESTAMP", 10);
1451
1392
case Table::Field::DATETIME:
1452
destination.append(" DATETIME");
1393
destination.append(" DATETIME", 9);
1454
1395
case Table::Field::TIME:
1455
destination.append(" TIME");
1396
destination.append(" TIME", 5);
1400
if (field.type() == Table::Field::INTEGER ||
1401
field.type() == Table::Field::BIGINT)
1403
if (field.has_constraints() &&
1404
field.constraints().has_is_unsigned() &&
1405
field.constraints().is_unsigned())
1407
destination.append(" UNSIGNED", 9);
1459
1411
if (field.type() == Table::Field::BLOB ||
1460
1412
field.type() == Table::Field::VARCHAR)
1462
1414
if (field.string_options().has_collation()
1463
1415
&& field.string_options().collation().compare("binary"))
1465
destination.append(" COLLATE ");
1417
destination.append(" COLLATE ", 9);
1466
1418
destination.append(field.string_options().collation());
1470
if (field.has_constraints() and field.constraints().is_unique())
1472
destination.append(" UNIQUE");
1475
1422
if (field.has_constraints() && field.constraints().is_notnull())
1477
destination.append(" NOT NULL");
1424
destination.append(" NOT NULL", 9);
1479
1426
else if (field.type() == Table::Field::EPOCH)
1481
destination.append(" NULL");
1428
destination.append(" NULL", 5);
1484
1431
if (field.type() == Table::Field::INTEGER ||
1488
1435
if (field.has_numeric_options() &&
1489
1436
field.numeric_options().is_autoincrement())
1491
destination.append(" AUTO_INCREMENT");
1438
destination.append(" AUTO_INCREMENT", 15);
1495
1442
if (field.options().has_default_value())
1497
destination.append(" DEFAULT ");
1444
destination.append(" DEFAULT ", 9);
1498
1445
append_escaped_string(&destination, field.options().default_value());
1500
1447
else if (field.options().has_default_expression())
1502
destination.append(" DEFAULT ");
1449
destination.append(" DEFAULT ", 9);
1503
1450
destination.append(field.options().default_expression());
1505
1452
else if (field.options().has_default_bin_value())
1507
1454
const string &v= field.options().default_bin_value();
1508
1455
if (v.length() == 0)
1510
destination.append(" DEFAULT ''");
1456
destination.append(" DEFAULT ''", 11);
1514
destination.append(" DEFAULT 0x");
1459
destination.append(" DEFAULT 0x", 11);
1515
1460
for (size_t x= 0; x < v.length(); x++)
1524
1469
&& field.options().default_null()
1525
1470
&& field.type() != Table::Field::BLOB)
1527
destination.append(" DEFAULT NULL");
1472
destination.append(" DEFAULT NULL", 13);
1530
1475
if (field.has_options() && field.options().has_update_expression())
1532
destination.append(" ON UPDATE ");
1477
destination.append(" ON UPDATE ", 11);
1533
1478
destination.append(field.options().update_expression());
1536
1481
if (field.has_comment())
1538
destination.append(" COMMENT ");
1483
destination.append(" COMMENT ", 9);
1539
1484
append_escaped_string(&destination, field.comment(), quoted_default);