| 1 |
################################## |
| 2 |
# |
| 3 |
# This include file will be used for all CREATE TABLE statements in the suite. |
| 4 |
# If you need to add additional steps or change the logic, copy the file |
| 5 |
# to storage/<engine>/mysql-test/storage_engine/ folder and modify it there. |
| 6 |
# |
| 7 |
################## |
| 8 |
# |
| 9 |
# Parameters: |
| 10 |
# |
| 11 |
# --let $create_definition = <column names, types, indexes) # optional, default a $int_col, b $char_col (based on defaults) |
| 12 |
# --let $table_name = <table name> # optional, default t1 |
| 13 |
# --let $table_options = <table options> # optional, default based on define_engine.inc |
| 14 |
# --let $partition_options = <partitioning definition> # optional, default none |
| 15 |
# --let $as_select = <SELECT statement> # optional, default empty |
| 16 |
# --let $error_codes = <expected error codes, as in --error> # optional, default 0 |
| 17 |
# --let $if_not_exists = [0|1] # optional, default 0 (1 adds IF NOT EXISTS clause) |
| 18 |
# --let $default_engine = [0|1] # optional, default 0 (with 1 will rely on default engine, no ENGINE=) |
| 19 |
# --let $temporary = [0|1] # optional, default 0 (1 adds TEMPORARY) |
| 20 |
# --let $disable_query_log = [0|1] # optional, default 0 (1 disables logging of CREATE) |
| 21 |
# |
| 22 |
# Usage examples: |
| 23 |
# |
| 24 |
# --source create_table.inc -- creates a default table |
| 25 |
# |
| 26 |
# --let $create_definition = a INT NOT NULL, b CHAR(1) PRIMARY KEY, INDEX(a) |
| 27 |
# --let $table_options = AUTO_INCREMENT = 100 |
| 28 |
# --let $partition_options = PARTITION BY HASH(a) PARTITIONS 2 |
| 29 |
# --let $as_select = SELECT 1, 'a' |
| 30 |
# --source create_table.inc |
| 31 |
# |
| 32 |
# Additionally, a test can define $extra_tbl_options. The difference with $table_options |
| 33 |
# is that its value is persistent and will be used until it is unset explicitly, or |
| 34 |
# until the test ends. The purpose of it is to allow one test to call another test, |
| 35 |
# when the called test does not know about specific options the calling test might require, |
| 36 |
# and thus cannot set them on per-create basis. |
| 37 |
|
| 38 |
--let $create_statement = CREATE |
| 39 |
|
| 40 |
if ($temporary) |
| 41 |
{ |
| 42 |
--let $create_statement = $create_statement TEMPORARY |
| 43 |
} |
| 44 |
|
| 45 |
--let $create_statement = $create_statement TABLE |
| 46 |
|
| 47 |
if ($if_not_exists) |
| 48 |
{ |
| 49 |
--let $create_statement = $create_statement IF NOT EXISTS |
| 50 |
} |
| 51 |
|
| 52 |
if (!$table_name) |
| 53 |
{ |
| 54 |
--let $table_name = t1 |
| 55 |
} |
| 56 |
|
| 57 |
# Child statement is a statement that will create an underlying table. |
| 58 |
# From this point, it will deviate from the main statement, that's why |
| 59 |
# we start creating it here in parallel with the main one. |
| 60 |
# For underlying tables, we will create a table in mrg schema, e.g. |
| 61 |
# for table t1 the underlying table will be mrg.t1, etc. |
| 62 |
# Since we will only create one child here, it should be enough. If we want more, |
| 63 |
# we can always add a suffix, e.g. mrg.t1_child1, mrg.t1_child2, etc. |
| 64 |
|
| 65 |
--let $child_statement = $create_statement mrg.$table_name |
| 66 |
--let $create_statement = $create_statement $table_name |
| 67 |
|
| 68 |
if (!$create_definition) |
| 69 |
{ |
| 70 |
# If $create_definition is not defined, and AS SELECT is requested, |
| 71 |
# we should not set $create_definition to the default value, |
| 72 |
# because it might be inconsistent with the SELECT. |
| 73 |
if (!$as_select) |
| 74 |
{ |
| 75 |
--let $create_definition = a $int_col, b $char_col |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if ($create_definition) |
| 80 |
{ |
| 81 |
--let $create_statement = $create_statement ($create_definition) |
| 82 |
# Table definition for the underlying table should be the same |
| 83 |
# as for the MERGE table |
| 84 |
--let $child_statement = $child_statement ($create_definition) |
| 85 |
} |
| 86 |
|
| 87 |
# If $default_engine is set, we will rely on the default storage engine |
| 88 |
|
| 89 |
if (!$default_engine) |
| 90 |
{ |
| 91 |
--let $create_statement = $create_statement ENGINE=$storage_engine |
| 92 |
} |
| 93 |
# Engine for an underlying table differs |
| 94 |
--let $child_statement = $child_statement ENGINE=MyISAM |
| 95 |
|
| 96 |
# Save default table options, we will want to restore them later |
| 97 |
--let $default_tbl_opts_saved = $default_tbl_opts |
| 98 |
--let $default_tbl_opts = $default_tbl_opts UNION(mrg.$table_name) INSERT_METHOD=LAST |
| 99 |
|
| 100 |
# Default table options from define_engine.inc |
| 101 |
--let $create_statement = $create_statement $default_tbl_opts |
| 102 |
|
| 103 |
# The calling script could request additional table options |
| 104 |
if ($table_options) |
| 105 |
{ |
| 106 |
--let $create_statement = $create_statement $table_options |
| 107 |
--let $child_statement = $child_statement $table_options |
| 108 |
} |
| 109 |
|
| 110 |
# The difference between $extra_tbl_opts and $table_options |
| 111 |
# is that its $extra_tbl_opts is persistent -- it will not be unset at the end of this file, |
| 112 |
# and will be used until it is unset explicitly by the calling test, |
| 113 |
# or until the test ends. The purpose of it is to allow one test to call another test, |
| 114 |
# when the called test does not know about specific options the calling test might require, |
| 115 |
# and thus cannot set them on per-create basis. |
| 116 |
|
| 117 |
if ($extra_tbl_opts) |
| 118 |
{ |
| 119 |
--let $create_statement = $create_statement $extra_tbl_opts |
| 120 |
--let $child_statement = $child_statement $extra_tbl_opts |
| 121 |
} |
| 122 |
|
| 123 |
if ($as_select) |
| 124 |
{ |
| 125 |
--let $create_statement = $create_statement AS $as_select |
| 126 |
--let $child_statement = $child_statement AS $as_select |
| 127 |
} |
| 128 |
|
| 129 |
if ($partition_options) |
| 130 |
{ |
| 131 |
--let $create_statement = $create_statement $partition_options |
| 132 |
--let $child_statement = $child_statement $partition_options |
| 133 |
} |
| 134 |
|
| 135 |
# We now have the complete CREATE statement in $create_statement. |
| 136 |
# If your CREATE statement should be composed differently, |
| 137 |
# modify the logic above. |
| 138 |
|
| 139 |
##################### |
| 140 |
# Here you can add logic needed BEFORE the main table creation |
| 141 |
# (e.g. the table needs a base table, a reference table, etc.). |
| 142 |
# Surround it by --disable_query_log/--enable_query_log |
| 143 |
# if you don't want it to appear in the result output. |
| 144 |
##################### |
| 145 |
--disable_warnings |
| 146 |
--disable_query_log |
| 147 |
--disable_result_log |
| 148 |
eval DROP TABLE IF EXISTS mrg.$table_name; |
| 149 |
eval $child_statement; |
| 150 |
--enable_result_log |
| 151 |
--enable_query_log |
| 152 |
--enable_warnings |
| 153 |
|
| 154 |
if ($disable_query_log) |
| 155 |
{ |
| 156 |
--disable_query_log |
| 157 |
} |
| 158 |
|
| 159 |
--source obfuscate.inc |
| 160 |
|
| 161 |
eval $create_statement; |
| 162 |
--source strict_check_errors.inc |
| 163 |
|
| 164 |
# Make sure you don't add any statements between the main CREATE (above) |
| 165 |
# and saving mysql_errno and mysql_errname (below) |
| 166 |
# They are saved in case you want to add more logic after the main CREATE, |
| 167 |
# because we need the result code of the table creation. |
| 168 |
# Also, do not change $create_statement after it is executed! |
| 169 |
|
| 170 |
--let $my_errno = $mysql_errno |
| 171 |
--let $my_errname = $mysql_errname |
| 172 |
|
| 173 |
|
| 174 |
if ($disable_query_log) |
| 175 |
{ |
| 176 |
--enable_query_log |
| 177 |
} |
| 178 |
|
| 179 |
##################### |
| 180 |
# Here you can add logic needed AFTER the main table creation, |
| 181 |
# e.g. triggers creation. |
| 182 |
# Surround it by --disable_query_log/--enable_query_log |
| 183 |
# if you don't want it to appear in the result output. |
| 184 |
##################### |
| 185 |
|
| 186 |
|
| 187 |
# Unset the parameters, we don't want them to be accidentally reused later |
| 188 |
--let $create_definition = |
| 189 |
--let $table_name = t1 |
| 190 |
--let $table_options = |
| 191 |
--let $partition_options = |
| 192 |
--let $as_select = 0 |
| 193 |
--let $error_codes = |
| 194 |
--let $if_not_exists = 0 |
| 195 |
--let $default_engine = 0 |
| 196 |
--let $temporary = 0 |
| 197 |
--let $disable_query_log = 0 |
| 198 |
|
| 199 |
# Restore default table options now |
| 200 |
--let $default_tbl_opts = $default_tbl_opts_saved |
| 201 |
|
| 202 |
|
| 203 |
# Restore the error codes of the main statement |
| 204 |
--let $mysql_errno = $my_errno |
| 205 |
--let $mysql_errname = $my_errname |
| 206 |
# Make sure you don't add any SQL statements after restoring |
| 207 |
# mysql_errno and mysql_errname (above) |
| 208 |
|