34#include <boost/lexical_cast.hpp>
36#include <drizzled/charset.h>
37#include <drizzled/message.h>
39#include <drizzled/message/transaction.pb.h>
40#include <drizzled/message/access.h>
55static void escapeEmbeddedQuotes(
string &s,
const char quote=
'\'')
59 for (it= s.begin(); it != s.end(); ++it)
63 it= s.insert(it, quote);
70static 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);
103 res->push_back(
'\\');
104 res->push_back(
'\\');
109 res->push_back(quote);
110 res->push_back(quote);
113 res->push_back(*pos);
117 res->push_back(quote);
122 vector<string> &sql_strings,
124 bool already_in_transaction)
128 switch (source.type())
130 case Statement::ROLLBACK_STATEMENT:
134 case Statement::ROLLBACK:
136 sql_strings.push_back(
"ROLLBACK");
139 case Statement::INSERT:
141 if (! source.has_insert_header())
143 error= MISSING_HEADER;
146 if (! source.has_insert_data())
152 const InsertHeader &insert_header= source.insert_header();
153 const InsertData &insert_data= source.insert_data();
154 size_t num_keys= insert_data.record_size();
156 if (num_keys > 1 && ! already_in_transaction)
157 sql_strings.push_back(
"START TRANSACTION");
159 for (
size_t x= 0; x < num_keys; ++x)
164 insert_data.record(x),
170 sql_strings.push_back(destination);
173 if (num_keys > 1 && ! already_in_transaction)
176 sql_strings.push_back(
"COMMIT");
178 sql_strings.push_back(
"ROLLBACK");
182 case Statement::UPDATE:
184 if (! source.has_update_header())
186 error= MISSING_HEADER;
189 if (! source.has_update_data())
195 const UpdateHeader &update_header= source.update_header();
196 const UpdateData &update_data= source.update_data();
197 size_t num_keys= update_data.record_size();
200 if (num_keys > 1 && ! already_in_transaction)
201 sql_strings.push_back(
"START TRANSACTION");
203 for (x= 0; x < num_keys; ++x)
208 update_data.record(x),
214 sql_strings.push_back(destination);
217 if (num_keys > 1 && ! already_in_transaction)
220 sql_strings.push_back(
"COMMIT");
222 sql_strings.push_back(
"ROLLBACK");
226 case Statement::DELETE:
228 if (! source.has_delete_header())
230 error= MISSING_HEADER;
233 if (! source.has_delete_data())
239 const DeleteHeader &delete_header= source.delete_header();
240 const DeleteData &delete_data= source.delete_data();
241 size_t num_keys= delete_data.record_size();
244 if (num_keys > 1 && ! already_in_transaction)
245 sql_strings.push_back(
"START TRANSACTION");
247 for (x= 0; x < num_keys; ++x)
252 delete_data.record(x),
258 sql_strings.push_back(destination);
261 if (num_keys > 1 && ! already_in_transaction)
264 sql_strings.push_back(
"COMMIT");
266 sql_strings.push_back(
"ROLLBACK");
270 case Statement::CREATE_TABLE:
272 assert(source.has_create_table_statement());
277 sql_strings.push_back(destination);
280 case Statement::TRUNCATE_TABLE:
282 assert(source.has_truncate_table_statement());
287 sql_strings.push_back(destination);
290 case Statement::DROP_TABLE:
292 assert(source.has_drop_table_statement());
297 sql_strings.push_back(destination);
300 case Statement::CREATE_SCHEMA:
302 assert(source.has_create_schema_statement());
307 sql_strings.push_back(destination);
310 case Statement::DROP_SCHEMA:
312 assert(source.has_drop_schema_statement());
317 sql_strings.push_back(destination);
320 case Statement::ALTER_SCHEMA:
322 assert(source.has_alter_schema_statement());
327 sql_strings.push_back(destination);
330 case Statement::SET_VARIABLE:
332 assert(source.has_set_variable_statement());
337 sql_strings.push_back(destination);
340 case Statement::RAW_SQL:
342 if (source.has_raw_sql_schema())
344 string destination(
"USE ");
345 destination.append(source.raw_sql_schema());
346 sql_strings.push_back(destination);
348 sql_strings.push_back(source.sql());
352 sql_strings.push_back(source.sql());
358enum TransformSqlError
363 char quoted_identifier=
'`';
364 if (sql_variant == ANSI)
365 quoted_identifier=
'"';
367 destination.assign(
"INSERT INTO ", 12);
368 destination.push_back(quoted_identifier);
369 destination.append(header.table_metadata().schema_name());
370 destination.push_back(quoted_identifier);
371 destination.push_back(
'.');
372 destination.push_back(quoted_identifier);
373 destination.append(header.table_metadata().table_name());
374 destination.push_back(quoted_identifier);
375 destination.append(
" (", 2);
378 size_t num_fields= header.field_metadata_size();
381 for (x= 0; x < num_fields; ++x)
383 const FieldMetadata &field_metadata= header.field_metadata(x);
385 destination.push_back(
',');
387 destination.push_back(quoted_identifier);
388 destination.append(field_metadata.name());
389 destination.push_back(quoted_identifier);
395enum TransformSqlError
397 const InsertRecord &record,
405 destination.append(
") VALUES (");
408 size_t num_fields= header.field_metadata_size();
410 bool should_quote_field_value=
false;
412 for (x= 0; x < num_fields; ++x)
415 destination.push_back(
',');
417 const FieldMetadata &field_metadata= header.field_metadata(x);
419 if (record.is_null(x))
421 should_quote_field_value=
false;
428 if (should_quote_field_value)
429 destination.push_back(
'\'');
431 if (record.is_null(x))
433 destination.append(
"NULL");
437 if (field_metadata.type() == Table::Field::BLOB)
445 string raw_data(record.insert_value(x));
446 destination.append(raw_data.c_str(), raw_data.size());
450 string tmp(record.insert_value(x));
451 escapeEmbeddedQuotes(tmp);
452 destination.append(tmp);
456 if (should_quote_field_value)
457 destination.push_back(
'\'');
459 destination.push_back(
')');
464enum TransformSqlError
466 const InsertData &data,
474 destination.append(
") VALUES (", 10);
477 size_t num_records= data.record_size();
478 size_t num_fields= header.field_metadata_size();
480 bool should_quote_field_value=
false;
482 for (x= 0; x < num_records; ++x)
485 destination.append(
"),(", 3);
487 for (y= 0; y < num_fields; ++y)
490 destination.push_back(
',');
492 const FieldMetadata &field_metadata= header.field_metadata(y);
496 if (should_quote_field_value)
497 destination.push_back(
'\'');
499 if (field_metadata.type() == Table::Field::BLOB)
507 string raw_data(data.record(x).insert_value(y));
508 destination.append(raw_data.c_str(), raw_data.size());
512 string tmp(data.record(x).insert_value(y));
513 escapeEmbeddedQuotes(tmp);
514 destination.append(tmp);
517 if (should_quote_field_value)
518 destination.push_back(
'\'');
521 destination.push_back(
')');
526enum TransformSqlError
531 char quoted_identifier=
'`';
532 if (sql_variant == ANSI)
533 quoted_identifier=
'"';
535 destination.assign(
"UPDATE ", 7);
536 destination.push_back(quoted_identifier);
537 destination.append(header.table_metadata().schema_name());
538 destination.push_back(quoted_identifier);
539 destination.push_back(
'.');
540 destination.push_back(quoted_identifier);
541 destination.append(header.table_metadata().table_name());
542 destination.push_back(quoted_identifier);
543 destination.append(
" SET ", 5);
548enum TransformSqlError
550 const UpdateRecord &record,
558 char quoted_identifier=
'`';
559 if (sql_variant == ANSI)
560 quoted_identifier=
'"';
563 size_t num_set_fields= header.set_field_metadata_size();
565 bool should_quote_field_value=
false;
567 for (x= 0; x < num_set_fields; ++x)
569 const FieldMetadata &field_metadata= header.set_field_metadata(x);
571 destination.push_back(
',');
573 destination.push_back(quoted_identifier);
574 destination.append(field_metadata.name());
575 destination.push_back(quoted_identifier);
576 destination.push_back(
'=');
578 if (record.is_null(x))
580 should_quote_field_value=
false;
587 if (should_quote_field_value)
588 destination.push_back(
'\'');
590 if (record.is_null(x))
592 destination.append(
"NULL");
596 if (field_metadata.type() == Table::Field::BLOB)
604 string raw_data(record.after_value(x));
605 destination.append(raw_data.c_str(), raw_data.size());
609 string tmp(record.after_value(x));
610 escapeEmbeddedQuotes(tmp);
611 destination.append(tmp);
615 if (should_quote_field_value)
616 destination.push_back(
'\'');
619 size_t num_key_fields= header.key_field_metadata_size();
621 destination.append(
" WHERE ", 7);
622 for (x= 0; x < num_key_fields; ++x)
624 const FieldMetadata &field_metadata= header.key_field_metadata(x);
627 destination.append(
" AND ", 5);
629 destination.push_back(quoted_identifier);
630 destination.append(field_metadata.name());
631 destination.push_back(quoted_identifier);
633 destination.push_back(
'=');
637 if (should_quote_field_value)
638 destination.push_back(
'\'');
640 if (field_metadata.type() == Table::Field::BLOB)
648 string raw_data(record.key_value(x));
649 destination.append(raw_data.c_str(), raw_data.size());
653 destination.append(record.key_value(x));
656 if (should_quote_field_value)
657 destination.push_back(
'\'');
663enum TransformSqlError
668 char quoted_identifier=
'`';
669 if (sql_variant == ANSI)
670 quoted_identifier=
'"';
672 destination.assign(
"DELETE FROM ", 12);
673 destination.push_back(quoted_identifier);
674 destination.append(header.table_metadata().schema_name());
675 destination.push_back(quoted_identifier);
676 destination.push_back(
'.');
677 destination.push_back(quoted_identifier);
678 destination.append(header.table_metadata().table_name());
679 destination.push_back(quoted_identifier);
684enum TransformSqlError
686 const DeleteRecord &record,
693 char quoted_identifier=
'`';
694 if (sql_variant == ANSI)
695 quoted_identifier=
'"';
698 uint32_t num_key_fields= header.key_field_metadata_size();
700 bool should_quote_field_value=
false;
702 destination.append(
" WHERE ", 7);
703 for (x= 0; x < num_key_fields; ++x)
705 const FieldMetadata &field_metadata= header.key_field_metadata(x);
708 destination.append(
" AND ", 5);
710 destination.push_back(quoted_identifier);
711 destination.append(field_metadata.name());
712 destination.push_back(quoted_identifier);
714 destination.push_back(
'=');
718 if (should_quote_field_value)
719 destination.push_back(
'\'');
721 if (field_metadata.type() == Table::Field::BLOB)
729 string raw_data(record.key_value(x));
730 destination.append(raw_data.c_str(), raw_data.size());
734 string tmp(record.key_value(x));
735 escapeEmbeddedQuotes(tmp);
736 destination.append(tmp);
739 if (should_quote_field_value)
740 destination.push_back(
'\'');
746enum TransformSqlError
748 const DeleteData &data,
755 char quoted_identifier=
'`';
756 if (sql_variant == ANSI)
757 quoted_identifier=
'"';
760 uint32_t num_key_fields= header.key_field_metadata_size();
761 uint32_t num_key_records= data.record_size();
763 bool should_quote_field_value=
false;
765 destination.append(
" WHERE ", 7);
766 for (x= 0; x < num_key_records; ++x)
769 destination.append(
" OR ", 4);
771 if (num_key_fields > 1)
772 destination.push_back(
'(');
774 for (y= 0; y < num_key_fields; ++y)
776 const FieldMetadata &field_metadata= header.key_field_metadata(y);
779 destination.append(
" AND ", 5);
781 destination.push_back(quoted_identifier);
782 destination.append(field_metadata.name());
783 destination.push_back(quoted_identifier);
785 destination.push_back(
'=');
789 if (should_quote_field_value)
790 destination.push_back(
'\'');
792 if (field_metadata.type() == Table::Field::BLOB)
800 string raw_data(data.record(x).key_value(y));
801 destination.append(raw_data.c_str(), raw_data.size());
805 string tmp(data.record(x).key_value(y));
806 escapeEmbeddedQuotes(tmp);
807 destination.append(tmp);
810 if (should_quote_field_value)
811 destination.push_back(
'\'');
813 if (num_key_fields > 1)
814 destination.push_back(
')');
819enum TransformSqlError
824 const Schema &before= statement.before();
825 const Schema &after= statement.after();
828 if (before.uuid() != after.uuid())
829 return UUID_MISMATCH;
831 char quoted_identifier=
'`';
832 if (sql_variant == ANSI)
833 quoted_identifier=
'"';
835 destination.append(
"ALTER SCHEMA ");
836 destination.push_back(quoted_identifier);
837 destination.append(before.name());
838 destination.push_back(quoted_identifier);
844 destination.append(
" COLLATE = ");
845 destination.append(after.collation());
850enum TransformSqlError
855 char quoted_identifier=
'`';
856 if (sql_variant == ANSI)
857 quoted_identifier=
'"';
859 destination.append(
"DROP SCHEMA ", 12);
860 destination.push_back(quoted_identifier);
861 destination.append(statement.schema_name());
862 destination.push_back(quoted_identifier);
867enum TransformSqlError
872 char quoted_identifier=
'`';
873 if (sql_variant == ANSI)
874 quoted_identifier=
'"';
876 const Schema &schema= statement.schema();
878 destination.append(
"CREATE SCHEMA ");
879 destination.push_back(quoted_identifier);
880 destination.append(schema.name());
881 destination.push_back(quoted_identifier);
883 if (schema.has_collation())
885 destination.append(
" COLLATE ");
886 destination.append(schema.collation());
889 if (not message::is_replicated(schema))
891 destination.append(
" REPLICATE = FALSE");
894 if (message::has_definer(schema))
896 destination.append(
" DEFINER ");
897 destination.push_back(
'\'');
898 destination.append(message::definer(schema));
899 destination.push_back(
'\'');
905enum TransformSqlError
910 char quoted_identifier=
'`';
911 if (sql_variant == ANSI)
912 quoted_identifier=
'"';
914 const TableMetadata &table_metadata= statement.table_metadata();
916 destination.append(
"DROP TABLE ");
919 if (statement.has_if_exists_clause() &&
920 statement.if_exists_clause() ==
true)
922 destination.append(
"IF EXISTS ");
925 destination.push_back(quoted_identifier);
926 destination.append(table_metadata.schema_name());
927 destination.push_back(quoted_identifier);
928 destination.push_back(
'.');
929 destination.push_back(quoted_identifier);
930 destination.append(table_metadata.table_name());
931 destination.push_back(quoted_identifier);
936enum TransformSqlError
941 char quoted_identifier=
'`';
942 if (sql_variant == ANSI)
943 quoted_identifier=
'"';
945 const TableMetadata &table_metadata= statement.table_metadata();
947 destination.append(
"TRUNCATE TABLE ");
948 destination.push_back(quoted_identifier);
949 destination.append(table_metadata.schema_name());
950 destination.push_back(quoted_identifier);
951 destination.push_back(
'.');
952 destination.push_back(quoted_identifier);
953 destination.append(table_metadata.table_name());
954 destination.push_back(quoted_identifier);
959enum TransformSqlError
965 const FieldMetadata &variable_metadata= statement.variable_metadata();
968 destination.append(
"SET GLOBAL ");
969 destination.append(variable_metadata.name());
970 destination.push_back(
'=');
972 if (should_quote_field_value)
973 destination.push_back(
'\'');
975 destination.append(statement.variable_value());
977 if (should_quote_field_value)
978 destination.push_back(
'\'');
983enum TransformSqlError
991enum TransformSqlError
996 char quoted_identifier=
'`';
997 if (sql_variant == ANSI)
998 quoted_identifier=
'"';
1000 destination.append(
"CREATE ");
1002 if (table.type() == Table::TEMPORARY)
1003 destination.append(
"TEMPORARY ");
1005 destination.append(
"TABLE ");
1008 append_escaped_string(&destination, table.schema(), quoted_identifier);
1009 destination.push_back(
'.');
1011 append_escaped_string(&destination, table.name(), quoted_identifier);
1012 destination.append(
" (\n");
1015 size_t num_fields= table.field_size();
1016 for (
size_t x= 0; x < num_fields; ++x)
1018 const Table::Field &field= table.field(x);
1021 destination.append(
",\n");
1023 destination.append(
" ");
1031 size_t num_indexes= table.indexes_size();
1033 if (num_indexes > 0)
1034 destination.append(
",\n");
1036 for (
size_t x= 0; x < num_indexes; ++x)
1038 const message::Table::Index &index= table.indexes(x);
1041 destination.append(
",\n");
1049 size_t num_foreign_keys= table.fk_constraint_size();
1051 if (num_foreign_keys > 0)
1052 destination.append(
",\n");
1054 for (
size_t x= 0; x < num_foreign_keys; ++x)
1056 const message::Table::ForeignKeyConstraint &fkey= table.fk_constraint(x);
1059 destination.append(
",\n");
1067 destination.append(
"\n)");
1070 if (table.has_engine())
1072 destination.append(
" ENGINE=");
1073 destination.append(table.engine().name());
1075 size_t num_engine_options= table.engine().options_size();
1076 if (num_engine_options > 0)
1077 destination.append(
" ", 1);
1078 for (
size_t x= 0; x < num_engine_options; ++x)
1080 const Engine::Option &
option= table.engine().options(x);
1081 destination.append(
option.name());
1082 destination.append(
"='");
1083 destination.append(
option.state());
1084 destination.append(
"'");
1085 if (x != num_engine_options-1)
1087 destination.append(
", ");
1092 if (table.has_options())
1095 if (not message::is_replicated(table))
1097 destination.append(
" REPLICATE = FALSE");
1100 if (message::has_definer(table))
1102 destination.append(
" DEFINER ");
1103 destination.push_back(
'\'');
1104 destination.append(message::definer(table));
1105 destination.push_back(
'\'');
1111enum TransformSqlError
1113 string &destination,
1116 if (sql_variant == ANSI)
1119 if (options.has_comment())
1121 destination.append(
" COMMENT=");
1122 append_escaped_string(&destination, options.comment());
1125 if (options.has_collation())
1127 destination.append(
" COLLATE = ");
1128 destination.append(options.collation());
1131 if (options.has_data_file_name())
1133 destination.append(
"\nDATA_FILE_NAME = '");
1134 destination.append(options.data_file_name());
1135 destination.push_back(
'\'');
1138 if (options.has_index_file_name())
1140 destination.append(
"\nINDEX_FILE_NAME = '");
1141 destination.append(options.index_file_name());
1142 destination.push_back(
'\'');
1145 if (options.has_max_rows())
1147 destination.append(
"\nMAX_ROWS = ");
1148 destination.append(boost::lexical_cast<string>(options.max_rows()));
1151 if (options.has_min_rows())
1153 destination.append(
"\nMIN_ROWS = ");
1154 destination.append(boost::lexical_cast<string>(options.min_rows()));
1157 if (options.has_user_set_auto_increment_value()
1158 && options.has_auto_increment_value())
1160 destination.append(
" AUTO_INCREMENT=");
1161 destination.append(boost::lexical_cast<string>(options.auto_increment_value()));
1164 if (options.has_avg_row_length())
1166 destination.append(
"\nAVG_ROW_LENGTH = ");
1167 destination.append(boost::lexical_cast<string>(options.avg_row_length()));
1170 if (options.has_checksum() && options.checksum())
1171 destination.append(
"\nCHECKSUM = TRUE");
1173 if (options.has_page_checksum() && options.page_checksum())
1174 destination.append(
"\nPAGE_CHECKSUM = TRUE");
1179enum TransformSqlError
1182 string &destination,
1185 char quoted_identifier=
'`';
1186 if (sql_variant == ANSI)
1187 quoted_identifier=
'"';
1189 destination.append(
" ", 2);
1191 if (index.is_primary())
1192 destination.append(
"PRIMARY ");
1193 else if (index.is_unique())
1194 destination.append(
"UNIQUE ");
1196 destination.append(
"KEY ", 4);
1197 if (! (index.is_primary() && index.name().compare(
"PRIMARY")==0))
1199 destination.push_back(quoted_identifier);
1200 destination.append(index.name());
1201 destination.push_back(quoted_identifier);
1202 destination.append(
" (", 2);
1205 destination.append(
"(", 1);
1207 size_t num_parts= index.index_part_size();
1208 for (
size_t x= 0; x < num_parts; ++x)
1210 const Table::Index::IndexPart &part= index.index_part(x);
1211 const Table::Field &field= table.field(part.fieldnr());
1214 destination.push_back(
',');
1216 destination.push_back(quoted_identifier);
1217 destination.append(field.name());
1218 destination.push_back(quoted_identifier);
1225 if (field.type() == Table::Field::VARCHAR ||
1226 field.type() == Table::Field::BLOB)
1228 if (part.has_compare_length())
1230 if (part.compare_length() != field.string_options().length())
1232 destination.push_back(
'(');
1233 destination.append(boost::lexical_cast<string>(part.compare_length()));
1234 destination.push_back(
')');
1239 destination.push_back(
')');
1241 switch (index.type())
1243 case Table::Index::UNKNOWN_INDEX:
1245 case Table::Index::BTREE:
1246 destination.append(
" USING BTREE");
1248 case Table::Index::RTREE:
1249 destination.append(
" USING RTREE");
1251 case Table::Index::HASH:
1252 destination.append(
" USING HASH");
1254 case Table::Index::FULLTEXT:
1255 destination.append(
" USING FULLTEXT");
1257 case Table::Index::LSMTREE:
1258 destination.append(
" USING LSMTREE");
1262 if (index.has_comment())
1264 destination.append(
" COMMENT ");
1265 append_escaped_string(&destination, index.comment());
1271static void transformForeignKeyOptionToSql(Table::ForeignKeyConstraint::ForeignKeyOption opt,
string &destination)
1275 case Table::ForeignKeyConstraint::OPTION_RESTRICT:
1276 destination.append(
"RESTRICT");
1278 case Table::ForeignKeyConstraint::OPTION_CASCADE:
1279 destination.append(
"CASCADE");
1281 case Table::ForeignKeyConstraint::OPTION_SET_NULL:
1282 destination.append(
"SET NULL");
1284 case Table::ForeignKeyConstraint::OPTION_UNDEF:
1285 case Table::ForeignKeyConstraint::OPTION_NO_ACTION:
1286 destination.append(
"NO ACTION");
1288 case Table::ForeignKeyConstraint::OPTION_SET_DEFAULT:
1289 destination.append(
"SET DEFAULT");
1297 string &destination,
1300 char quoted_identifier=
'`';
1301 if (sql_variant == ANSI)
1302 quoted_identifier=
'"';
1304 destination.append(
" ");
1306 if (fkey.has_name())
1308 destination.append(
"CONSTRAINT ");
1309 append_escaped_string(&destination, fkey.name(), quoted_identifier);
1310 destination.append(
" ", 1);
1313 destination.append(
"FOREIGN KEY (");
1315 for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
1318 destination.append(
", ");
1320 append_escaped_string(&destination, fkey.column_names(x),
1324 destination.append(
") REFERENCES ");
1326 append_escaped_string(&destination, fkey.references_table_name(),
1328 destination.append(
" (");
1330 for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
1333 destination.append(
", ");
1335 append_escaped_string(&destination, fkey.references_columns(x),
1339 destination.push_back(
')');
1341 if (fkey.has_update_option() and fkey.update_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1343 destination.append(
" ON UPDATE ");
1344 transformForeignKeyOptionToSql(fkey.update_option(), destination);
1347 if (fkey.has_delete_option() and fkey.delete_option() != Table::ForeignKeyConstraint::OPTION_UNDEF)
1349 destination.append(
" ON DELETE ");
1350 transformForeignKeyOptionToSql(fkey.delete_option(), destination);
1356enum TransformSqlError
1358 string &destination,
1361 char quoted_identifier=
'`';
1362 char quoted_default;
1364 if (sql_variant == ANSI)
1365 quoted_identifier=
'"';
1367 if (sql_variant == DRIZZLE)
1368 quoted_default=
'\'';
1370 quoted_default= quoted_identifier;
1372 append_escaped_string(&destination, field.name(), quoted_identifier);
1374 Table::Field::FieldType field_type= field.type();
1378 case Table::Field::DOUBLE:
1379 destination.append(
" DOUBLE");
1380 if (field.has_numeric_options()
1381 && field.numeric_options().has_precision())
1384 ss <<
"(" << field.numeric_options().precision() <<
",";
1385 ss << field.numeric_options().scale() <<
")";
1386 destination.append(ss.str());
1389 case Table::Field::VARCHAR:
1391 if (field.string_options().has_collation()
1392 && field.string_options().collation().compare(
"binary") == 0)
1393 destination.append(
" VARBINARY(");
1395 destination.append(
" VARCHAR(");
1397 destination.append(boost::lexical_cast<string>(field.string_options().length()));
1398 destination.append(
")");
1401 case Table::Field::BLOB:
1403 if (field.string_options().has_collation()
1404 && field.string_options().collation().compare(
"binary") == 0)
1405 destination.append(
" BLOB");
1407 destination.append(
" TEXT");
1410 case Table::Field::ENUM:
1412 size_t num_field_values= field.enumeration_values().field_value_size();
1413 destination.append(
" ENUM(");
1414 for (
size_t x= 0; x < num_field_values; ++x)
1416 const string &type= field.enumeration_values().field_value(x);
1419 destination.push_back(
',');
1421 destination.push_back(
'\'');
1422 destination.append(type);
1423 destination.push_back(
'\'');
1425 destination.push_back(
')');
1428 case Table::Field::UUID:
1429 destination.append(
" UUID");
1431 case Table::Field::IPV6:
1432 destination.append(
" IPV6");
1434 case Table::Field::BOOLEAN:
1435 destination.append(
" BOOLEAN");
1437 case Table::Field::INTEGER:
1438 destination.append(
" INT");
1440 case Table::Field::BIGINT:
1441 if (field.has_constraints() and
1442 field.constraints().is_unsigned())
1444 destination.append(
" BIGINT UNSIGNED");
1448 destination.append(
" BIGINT");
1451 case Table::Field::DECIMAL:
1453 destination.append(
" DECIMAL(");
1455 ss << field.numeric_options().precision() <<
",";
1456 ss << field.numeric_options().scale() <<
")";
1457 destination.append(ss.str());
1460 case Table::Field::DATE:
1461 destination.append(
" DATE");
1464 case Table::Field::EPOCH:
1465 if (field.time_options().microseconds())
1467 destination.append(
" TIMESTAMP(6)");
1471 destination.append(
" TIMESTAMP");
1475 case Table::Field::DATETIME:
1476 destination.append(
" DATETIME");
1478 case Table::Field::TIME:
1479 destination.append(
" TIME");
1483 if (field.type() == Table::Field::BLOB ||
1484 field.type() == Table::Field::VARCHAR)
1486 if (field.string_options().has_collation()
1487 && field.string_options().collation().compare(
"binary"))
1489 destination.append(
" COLLATE ");
1490 destination.append(field.string_options().collation());
1494 if (field.has_constraints() and field.constraints().is_unique())
1496 destination.append(
" UNIQUE");
1499 if (field.has_constraints() && field.constraints().is_notnull())
1501 destination.append(
" NOT NULL");
1503 else if (field.type() == Table::Field::EPOCH)
1505 destination.append(
" NULL");
1508 if (field.type() == Table::Field::INTEGER ||
1509 field.type() == Table::Field::BIGINT)
1512 if (field.has_numeric_options() &&
1513 field.numeric_options().is_autoincrement())
1515 destination.append(
" AUTO_INCREMENT");
1519 if (field.options().has_default_value())
1521 destination.append(
" DEFAULT ");
1522 append_escaped_string(&destination, field.options().default_value());
1524 else if (field.options().has_default_expression())
1526 destination.append(
" DEFAULT ");
1527 destination.append(field.options().default_expression());
1529 else if (field.options().has_default_bin_value())
1531 const string &v= field.options().default_bin_value();
1532 if (v.length() == 0)
1534 destination.append(
" DEFAULT ''");
1538 destination.append(
" DEFAULT 0x");
1539 for (
size_t x= 0; x < v.length(); x++)
1542 snprintf(hex,
sizeof(hex),
"%.2X", *(v.c_str() + x));
1543 destination.append(hex, 2);
1547 else if (field.options().has_default_null()
1548 && field.options().default_null()
1549 && field.type() != Table::Field::BLOB)
1551 destination.append(
" DEFAULT NULL");
1554 if (field.has_options() && field.options().has_update_expression())
1556 destination.append(
" ON UPDATE ");
1557 destination.append(field.options().update_expression());
1560 if (field.has_comment())
1562 destination.append(
" COMMENT ");
1563 append_escaped_string(&destination, field.comment(), quoted_default);
1572 case Table::Field::DOUBLE:
1573 case Table::Field::DECIMAL:
1574 case Table::Field::INTEGER:
1575 case Table::Field::BIGINT:
1582Table::Field::FieldType internalFieldTypeToFieldProtoType(
enum enum_field_types
type)
1585 case DRIZZLE_TYPE_LONG:
1586 return Table::Field::INTEGER;
1587 case DRIZZLE_TYPE_DOUBLE:
1588 return Table::Field::DOUBLE;
1589 case DRIZZLE_TYPE_NULL:
1591 return Table::Field::INTEGER;
1592 case DRIZZLE_TYPE_MICROTIME:
1593 case DRIZZLE_TYPE_TIMESTAMP:
1594 return Table::Field::EPOCH;
1595 case DRIZZLE_TYPE_LONGLONG:
1596 return Table::Field::BIGINT;
1597 case DRIZZLE_TYPE_DATETIME:
1598 return Table::Field::DATETIME;
1599 case DRIZZLE_TYPE_TIME:
1600 return Table::Field::TIME;
1601 case DRIZZLE_TYPE_DATE:
1602 return Table::Field::DATE;
1603 case DRIZZLE_TYPE_VARCHAR:
1604 return Table::Field::VARCHAR;
1605 case DRIZZLE_TYPE_DECIMAL:
1606 return Table::Field::DECIMAL;
1607 case DRIZZLE_TYPE_ENUM:
1608 return Table::Field::ENUM;
1609 case DRIZZLE_TYPE_BLOB:
1610 return Table::Field::BLOB;
1611 case DRIZZLE_TYPE_UUID:
1612 return Table::Field::UUID;
1613 case DRIZZLE_TYPE_BOOLEAN:
1614 return Table::Field::BOOLEAN;
1615 case DRIZZLE_TYPE_IPV6:
1616 return Table::Field::IPV6;
1620 return Table::Field::INTEGER;
1625 size_t num_statements= transaction.statement_size();
1626 if (num_statements == 0)
1635 for (x= 0; x < num_statements; ++x)
1637 const Statement &statement= transaction.statement(x);
1638 Statement::Type type= statement.type();
1642 case Statement::INSERT:
1643 if (statement.insert_data().segment_id() > 1)
1646 case Statement::UPDATE:
1647 if (statement.update_data().segment_id() > 1)
1650 case Statement::DELETE:
1651 if (statement.delete_data().segment_id() > 1)
Convert from decimal to binary representation.
TODO: Rename this file - func.h is stupid.