| 1 | /* Copyright (c) 2024, MariaDB |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with this program; if not, write to the Free Software |
| 14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA |
| 15 | */ |
| 16 | |
| 17 | #include "my_config.h"cxx . . $ `<file>/build/include/my_config.h`/ |
| 18 | #include "import_util.h"cxx . . $ `<file>/client/import_util.h`/ |
| 19 | |
| 20 | #include <tap.h>cxx . . $ `<file>/unittest/mytap/tap.h`/ |
| 21 | #include <string> |
| 22 | |
| 23 | inline bool operatorcxx . . $ `operator==`(7a35eee654285202).==(const KeyDefinitioncxx . . $ KeyDefinition# &lhs, const KeyDefinitioncxx . . $ KeyDefinition# &rhs) |
| 24 | { |
| 25 | return lhs.definitioncxx . . $ KeyDefinition#definition. ==cxx . . $ std/`operator==`(5ea818b58063a774). rhs.definitioncxx . . $ KeyDefinition#definition. && lhs.namecxx . . $ KeyDefinition#name. ==cxx . . $ std/`operator==`(5ea818b58063a774). rhs.namecxx . . $ KeyDefinition#name.; |
| 26 | } |
| 27 | |
| 28 | /* |
| 29 | Test parsing of CREATE TABLE in mariadb-import utility |
| 30 | */ |
| 31 | static void test_ddl_parsercxx . . $ test_ddl_parser(49f6e7a06ebc5aa8).() |
| 32 | { |
| 33 | stdcxx . . $ std/::stringcxx . . $ std/string# script= R"( |
| 34 | -- Some SQL script |
| 35 | CREATE TABLE `book` ( |
| 36 | `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, |
| 37 | `title` varchar(200) NOT NULL, |
| 38 | `author_id` smallint(5) unsigned NOT NULL, |
| 39 | `publisher_id` smallint(5) unsigned NOT NULL, |
| 40 | `excerpt` text, |
| 41 | PRIMARY KEY (`id`), |
| 42 | KEY `fk_book_author` (`author_id`), |
| 43 | KEY `fk_book_publisher` (`publisher_id`), |
| 44 | UNIQUE KEY `title_author` (`title`,`author`), |
| 45 | FULLTEXT KEY `excerpt` (`excerpt`), |
| 46 | CONSTRAINT `fk_book_author` FOREIGN KEY (`author_id`) REFERENCES `author` (`id`) ON DELETE CASCADE |
| 47 | CONSTRAINT `fk_book_publisher` FOREIGN KEY (`publisher_id`) REFERENCES `publisher` (`id`) ON DELETE CASCADE |
| 48 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci; |
| 49 | )"; |
| 50 | |
| 51 | auto create_table_stmt= extract_first_create_tablecxx . . $ extract_first_create_table(fd626fc03107dd24).(script); |
| 52 | ok(!create_table_stmt.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).(), "CREATE TABLE statement found"); |
| 53 | |
| 54 | TableDDLInfocxx . . $ TableDDLInfo# ddl_infocxx . . $ TableDDLInfo#TableDDLInfo(9c546c94e5fcce4e).(create_table_stmt); |
| 55 | |
| 56 | const stdcxx . . $ std/::stringcxx . . $ std/string#& table_name= ddl_info.table_namecxx . . $ TableDDLInfo#table_name.; |
| 57 | const stdcxx . . $ std/::stringcxx . . $ std/string#& storage_engine= ddl_info.storage_enginecxx . . $ TableDDLInfo#storage_engine.; |
| 58 | |
| 59 | ok(table_name ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "`book`", "Table name is OK"); |
| 60 | ok(storage_engine ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "InnoDB", "Storage engine is OK"); |
| 61 | ok(ddl_info.primary_keycxx . . $ TableDDLInfo#primary_key. ==cxx . . $ `operator==`(7a35eee654285202). KeyDefinitioncxx . . $ KeyDefinition#{"PRIMARY KEY (`id`)", "PRIMARY"}, |
| 62 | "Primary key def is OK"); |
| 63 | |
| 64 | ok(ddl_info.secondary_indexescxx . . $ TableDDLInfo#secondary_indexes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() == 4, "Secondary index size is OK"); |
| 65 | const auto &sec_indexes= ddl_info.secondary_indexescxx . . $ TableDDLInfo#secondary_indexes.; |
| 66 | ok(sec_indexes[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).0] ==cxx . . $ `operator==`(7a35eee654285202). KeyDefinitioncxx . . $ KeyDefinition#{"KEY `fk_book_author` (`author_id`)","`fk_book_author`"}, |
| 67 | "First key is OK"); |
| 68 | ok(sec_indexes[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).1] ==cxx . . $ `operator==`(7a35eee654285202). |
| 69 | KeyDefinitioncxx . . $ KeyDefinition#{"KEY `fk_book_publisher` (`publisher_id`)", |
| 70 | "`fk_book_publisher`"}, |
| 71 | "Second key is OK"); |
| 72 | |
| 73 | ok(ddl_info.constraintscxx . . $ TableDDLInfo#constraints..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() == 2, "Constraints size correct"); |
| 74 | ok(ddl_info.constraintscxx . . $ TableDDLInfo#constraints.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0] ==cxx . . $ `operator==`(7a35eee654285202). |
| 75 | KeyDefinitioncxx . . $ KeyDefinition#{"CONSTRAINT `fk_book_author` FOREIGN KEY (`author_id`) REFERENCES " |
| 76 | "`author` (`id`) ON DELETE CASCADE","`fk_book_author`"}, |
| 77 | "First constraint OK"); |
| 78 | |
| 79 | stdcxx . . $ std/::stringcxx . . $ std/string# drop_constraints= ddl_info.drop_constraints_sqlcxx . . $ TableDDLInfo#drop_constraints_sql(219584694fe2b248).(); |
| 80 | ok(drop_constraints ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). |
| 81 | "ALTER TABLE `book` DROP CONSTRAINT `fk_book_author`, DROP CONSTRAINT `fk_book_publisher`", |
| 82 | "Drop constraints SQL is \"%s\"", drop_constraints.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 83 | stdcxx . . $ std/::stringcxx . . $ std/string# add_constraints= ddl_info.add_constraints_sqlcxx . . $ TableDDLInfo#add_constraints_sql(219584694fe2b248).(); |
| 84 | ok(add_constraints ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). |
| 85 | "ALTER TABLE `book` ADD CONSTRAINT `fk_book_author` FOREIGN KEY (`author_id`) " |
| 86 | "REFERENCES `author` (`id`) ON DELETE CASCADE, " |
| 87 | "ADD CONSTRAINT `fk_book_publisher` FOREIGN KEY (`publisher_id`) " |
| 88 | "REFERENCES `publisher` (`id`) ON DELETE CASCADE", |
| 89 | "Add constraints SQL is \"%s\"",add_constraints.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 90 | |
| 91 | stdcxx . . $ std/::stringcxx . . $ std/string# drop_secondary_indexes= |
| 92 | ddl_info.drop_secondary_indexes_sqlcxx . . $ TableDDLInfo#drop_secondary_indexes_sql(219584694fe2b248).(); |
| 93 | ok(drop_secondary_indexes ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). |
| 94 | "ALTER TABLE `book` " |
| 95 | "DROP INDEX `fk_book_author`, " |
| 96 | "DROP INDEX `fk_book_publisher`, " |
| 97 | "DROP INDEX `title_author`, " |
| 98 | "DROP INDEX `excerpt`", |
| 99 | "Drop secondary indexes SQL is \"%s\"", drop_secondary_indexes.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 100 | stdcxx . . $ std/::stringcxx . . $ std/string# add_secondary_indexes= |
| 101 | ddl_info.add_secondary_indexes_sqlcxx . . $ TableDDLInfo#add_secondary_indexes_sql(219584694fe2b248).(); |
| 102 | ok(add_secondary_indexes ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). |
| 103 | "ALTER TABLE `book` ADD KEY `fk_book_author` (`author_id`), " |
| 104 | "ADD KEY `fk_book_publisher` (`publisher_id`), " |
| 105 | "ADD UNIQUE KEY `title_author` (`title`,`author`), " |
| 106 | "ADD FULLTEXT KEY `excerpt` (`excerpt`)", |
| 107 | "Add secondary indexes SQL is \"%s\"", add_secondary_indexes.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | For Innodb table without PK, and but with Unique key |
| 112 | (which is used for clustering, instead of PK) |
| 113 | this key will not be added and dropped by |
| 114 | the import utility |
| 115 | */ |
| 116 | static void innodb_non_pk_clustering_keycxx . . $ innodb_non_pk_clustering_key(49f6e7a06ebc5aa8).() |
| 117 | { |
| 118 | auto create_table_stmt= R"( |
| 119 | CREATE TABLE `book` ( |
| 120 | `id` mediumint(8), |
| 121 | `uniq` varchar(200), |
| 122 | UNIQUE KEY `id` (`id`), |
| 123 | UNIQUE KEY `uniq` (`uniq`), |
| 124 | KEY `id_uniq` (`id`,`uniq`) |
| 125 | ) ENGINE=InnoDB; |
| 126 | )"; |
| 127 | TableDDLInfocxx . . $ TableDDLInfo# ddl_infocxx . . $ TableDDLInfo#TableDDLInfo(9c546c94e5fcce4e).(create_table_stmtcxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 128 | ok(ddl_info.non_pk_clustering_key_namecxx . . $ TableDDLInfo#non_pk_clustering_key_name. ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "`id`", |
| 129 | "Non-PK clustering key is %s", |
| 130 | ddl_info.non_pk_clustering_key_namecxx . . $ TableDDLInfo#non_pk_clustering_key_name..c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 131 | ok(ddl_info.primary_keycxx . . $ TableDDLInfo#primary_key..definitioncxx . . $ KeyDefinition#definition..emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).(), |
| 132 | "Primary key is %s", ddl_info.primary_keycxx . . $ TableDDLInfo#primary_key..definitioncxx . . $ KeyDefinition#definition..c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 133 | ok(ddl_info.secondary_indexescxx . . $ TableDDLInfo#secondary_indexes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() == 3, |
| 134 | "Secondary indexes size is %zu", |
| 135 | ddl_info.secondary_indexescxx . . $ TableDDLInfo#secondary_indexes..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); |
| 136 | ok(!ddl_info.add_secondary_indexes_sqlcxx . . $ TableDDLInfo#add_secondary_indexes_sql(219584694fe2b248).().emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).(), |
| 137 | "Some secondary indexes to add"); |
| 138 | ok(!ddl_info.drop_secondary_indexes_sqlcxx . . $ TableDDLInfo#drop_secondary_indexes_sql(219584694fe2b248).().emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).(), |
| 139 | "Some secondary indexes to drop"); |
| 140 | } |
| 141 | int maincxx . . $ main(b126dc7c1de90089).() |
| 142 | { |
| 143 | plan(18); |
| 144 | diag("Testing DDL parser"); |
| 145 | |
| 146 | test_ddl_parsercxx . . $ test_ddl_parser(49f6e7a06ebc5aa8).(); |
| 147 | innodb_non_pk_clustering_keycxx . . $ innodb_non_pk_clustering_key(49f6e7a06ebc5aa8).(); |
| 148 | return exit_status(); |
| 149 | } |