Commit 5045615c authored by Sergei Golubchik's avatar Sergei Golubchik

small cleanup of the SHOW CREATE TABLE code

parent ca2ba229
...@@ -4135,15 +4135,14 @@ select_create::binlog_show_create_table(TABLE **tables, uint count) ...@@ -4135,15 +4135,14 @@ select_create::binlog_show_create_table(TABLE **tables, uint count)
{ {
/* /*
Note 1: In RBR mode, we generate a CREATE TABLE statement for the Note 1: In RBR mode, we generate a CREATE TABLE statement for the
created table by calling store_create_info() (behaves as SHOW created table by calling show_create_table(). In the event of an error,
CREATE TABLE). In the event of an error, nothing should be nothing should be written to the binary log, even if the table is
written to the binary log, even if the table is non-transactional; non-transactional; therefore we pretend that the generated CREATE TABLE
therefore we pretend that the generated CREATE TABLE statement is statement is for a transactional table. The event will then be put in the
for a transactional table. The event will then be put in the transaction cache, and any subsequent events (e.g., table-map events and
transaction cache, and any subsequent events (e.g., table-map binrow events) will also be put there. We can then use
events and binrow events) will also be put there. We can then use ha_autocommit_or_rollback() to either throw away the entire kaboodle of
ha_autocommit_or_rollback() to either throw away the entire events, or write them to the binary log.
kaboodle of events, or write them to the binary log.
We write the CREATE TABLE statement here and not in prepare() We write the CREATE TABLE statement here and not in prepare()
since there potentially are sub-selects or accesses to information since there potentially are sub-selects or accesses to information
...@@ -4162,12 +4161,9 @@ select_create::binlog_show_create_table(TABLE **tables, uint count) ...@@ -4162,12 +4161,9 @@ select_create::binlog_show_create_table(TABLE **tables, uint count)
tmp_table_list.table = *tables; tmp_table_list.table = *tables;
query.length(0); // Have to zero it since constructor doesn't query.length(0); // Have to zero it since constructor doesn't
result= store_create_info(thd, &tmp_table_list, &query, create_info, result= show_create_table(thd, &tmp_table_list, &query, create_info,
/* show_database */ TRUE, WITH_DB_NAME);
MY_TEST(create_info->org_options & DBUG_ASSERT(result == 0); /* show_create_table() always return 0 */
HA_LEX_CREATE_REPLACE) ||
create_info->table_was_deleted);
DBUG_ASSERT(result == 0); /* store_create_info() always return 0 */
if (mysql_bin_log.is_open()) if (mysql_bin_log.is_open())
{ {
......
...@@ -116,8 +116,9 @@ static void get_cs_converted_string_value(THD *thd, ...@@ -116,8 +116,9 @@ static void get_cs_converted_string_value(THD *thd,
bool use_hex); bool use_hex);
#endif #endif
static void static int show_create_view(THD *thd, TABLE_LIST *table, String *buff);
append_algorithm(TABLE_LIST *table, String *buff);
static void append_algorithm(TABLE_LIST *table, String *buff);
static COND * make_cond_for_info_schema(COND *cond, TABLE_LIST *table); static COND * make_cond_for_info_schema(COND *cond, TABLE_LIST *table);
...@@ -1063,9 +1064,8 @@ mysqld_show_create(THD *thd, TABLE_LIST *table_list) ...@@ -1063,9 +1064,8 @@ mysqld_show_create(THD *thd, TABLE_LIST *table_list)
buffer.set_charset(table_list->view_creation_ctx->get_client_cs()); buffer.set_charset(table_list->view_creation_ctx->get_client_cs());
if ((table_list->view ? if ((table_list->view ?
view_store_create_info(thd, table_list, &buffer) : show_create_view(thd, table_list, &buffer) :
store_create_info(thd, table_list, &buffer, NULL, show_create_table(thd, table_list, &buffer, NULL, WITHOUT_DB_NAME)))
FALSE /* show_database */, FALSE)))
goto exit; goto exit;
if (table_list->view) if (table_list->view)
...@@ -1541,7 +1541,7 @@ static void append_create_options(THD *thd, String *packet, ...@@ -1541,7 +1541,7 @@ static void append_create_options(THD *thd, String *packet,
Build a CREATE TABLE statement for a table. Build a CREATE TABLE statement for a table.
SYNOPSIS SYNOPSIS
store_create_info() show_create_table()
thd The thread thd The thread
table_list A list containing one table to write statement table_list A list containing one table to write statement
for. for.
...@@ -1551,8 +1551,7 @@ static void append_create_options(THD *thd, String *packet, ...@@ -1551,8 +1551,7 @@ static void append_create_options(THD *thd, String *packet,
to tailor the format of the statement. Can be to tailor the format of the statement. Can be
NULL, in which case only SQL_MODE is considered NULL, in which case only SQL_MODE is considered
when building the statement. when building the statement.
show_database Add database name to table name with_db_name Add database name to table name
create_or_replace Use CREATE OR REPLACE syntax
NOTE NOTE
Currently always return 0, but might return error code in the Currently always return 0, but might return error code in the
...@@ -1562,9 +1561,9 @@ static void append_create_options(THD *thd, String *packet, ...@@ -1562,9 +1561,9 @@ static void append_create_options(THD *thd, String *packet,
0 OK 0 OK
*/ */
int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, int show_create_table(THD *thd, TABLE_LIST *table_list, String *packet,
HA_CREATE_INFO *create_info_arg, bool show_database, HA_CREATE_INFO *create_info_arg,
bool create_or_replace) enum_with_db_name with_db_name)
{ {
List<Item> field_list; List<Item> field_list;
char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], def_value_buf[MAX_FIELD_WIDTH]; char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], def_value_buf[MAX_FIELD_WIDTH];
...@@ -1578,27 +1577,33 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, ...@@ -1578,27 +1577,33 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet,
handler *file= table->file; handler *file= table->file;
TABLE_SHARE *share= table->s; TABLE_SHARE *share= table->s;
HA_CREATE_INFO create_info; HA_CREATE_INFO create_info;
#ifdef WITH_PARTITION_STORAGE_ENGINE sql_mode_t sql_mode= thd->variables.sql_mode;
bool show_table_options= FALSE; bool foreign_db_mode= sql_mode & (MODE_POSTGRESQL | MODE_ORACLE |
#endif /* WITH_PARTITION_STORAGE_ENGINE */ MODE_MSSQL | MODE_DB2 |
bool foreign_db_mode= (thd->variables.sql_mode & (MODE_POSTGRESQL | MODE_MAXDB | MODE_ANSI);
MODE_ORACLE | bool limited_mysql_mode= sql_mode & (MODE_NO_FIELD_OPTIONS | MODE_MYSQL323 |
MODE_MSSQL | MODE_MYSQL40);
MODE_DB2 | bool show_table_options= !(sql_mode & MODE_NO_TABLE_OPTIONS) &&
MODE_MAXDB | !foreign_db_mode;
MODE_ANSI)) != 0; handlerton *hton;
bool limited_mysql_mode= (thd->variables.sql_mode & (MODE_NO_FIELD_OPTIONS |
MODE_MYSQL323 |
MODE_MYSQL40)) != 0;
my_bitmap_map *old_map; my_bitmap_map *old_map;
int error= 0; int error= 0;
DBUG_ENTER("store_create_info"); DBUG_ENTER("show_create_table");
DBUG_PRINT("enter",("table: %s", table->s->table_name.str)); DBUG_PRINT("enter",("table: %s", table->s->table_name.str));
#ifdef WITH_PARTITION_STORAGE_ENGINE
if (table->part_info)
hton= table->part_info->default_engine_type;
else
#endif
hton= file->ht;
restore_record(table, s->default_values); // Get empty record restore_record(table, s->default_values); // Get empty record
packet->append(STRING_WITH_LEN("CREATE ")); packet->append(STRING_WITH_LEN("CREATE "));
if (create_or_replace) if (create_info_arg &&
(create_info_arg->org_options & HA_LEX_CREATE_REPLACE ||
create_info_arg->table_was_deleted))
packet->append(STRING_WITH_LEN("OR REPLACE ")); packet->append(STRING_WITH_LEN("OR REPLACE "));
if (share->tmp_table) if (share->tmp_table)
packet->append(STRING_WITH_LEN("TEMPORARY ")); packet->append(STRING_WITH_LEN("TEMPORARY "));
...@@ -1625,7 +1630,7 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, ...@@ -1625,7 +1630,7 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet,
avoid having to update gazillions of tests and result files, but avoid having to update gazillions of tests and result files, but
it also saves a few bytes of the binary log. it also saves a few bytes of the binary log.
*/ */
if (show_database) if (with_db_name == WITH_DB_NAME)
{ {
const LEX_STRING *const db= const LEX_STRING *const db=
table_list->schema_table ? &INFORMATION_SCHEMA_NAME : &table->s->db; table_list->schema_table ? &INFORMATION_SCHEMA_NAME : &table->s->db;
...@@ -1664,8 +1669,7 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, ...@@ -1664,8 +1669,7 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet,
field->sql_type(type); field->sql_type(type);
packet->append(type.ptr(), type.length(), system_charset_info); packet->append(type.ptr(), type.length(), system_charset_info);
if (field->has_charset() && if (field->has_charset() && !(sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)))
!(thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)))
{ {
if (field->charset() != share->table_charset) if (field->charset() != share->table_charset)
{ {
...@@ -1722,7 +1726,7 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, ...@@ -1722,7 +1726,7 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet,
if (field->unireg_check == Field::NEXT_NUMBER && if (field->unireg_check == Field::NEXT_NUMBER &&
!(thd->variables.sql_mode & MODE_NO_FIELD_OPTIONS)) !(sql_mode & MODE_NO_FIELD_OPTIONS))
packet->append(STRING_WITH_LEN(" AUTO_INCREMENT")); packet->append(STRING_WITH_LEN(" AUTO_INCREMENT"));
if (field->comment.length) if (field->comment.length)
...@@ -1812,12 +1816,8 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, ...@@ -1812,12 +1816,8 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet,
} }
packet->append(STRING_WITH_LEN("\n)")); packet->append(STRING_WITH_LEN("\n)"));
if (!(thd->variables.sql_mode & MODE_NO_TABLE_OPTIONS) && !foreign_db_mode) if (show_table_options)
{ {
#ifdef WITH_PARTITION_STORAGE_ENGINE
show_table_options= TRUE;
#endif /* WITH_PARTITION_STORAGE_ENGINE */
/* /*
IF check_create_info IF check_create_info
THEN add ENGINE only if it was used when creating the table THEN add ENGINE only if it was used when creating the table
...@@ -1825,19 +1825,11 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, ...@@ -1825,19 +1825,11 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet,
if (!create_info_arg || if (!create_info_arg ||
(create_info_arg->used_fields & HA_CREATE_USED_ENGINE)) (create_info_arg->used_fields & HA_CREATE_USED_ENGINE))
{ {
if (thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) if (sql_mode & (MODE_MYSQL323 | MODE_MYSQL40))
packet->append(STRING_WITH_LEN(" TYPE=")); packet->append(STRING_WITH_LEN(" TYPE="));
else else
packet->append(STRING_WITH_LEN(" ENGINE=")); packet->append(STRING_WITH_LEN(" ENGINE="));
#ifdef WITH_PARTITION_STORAGE_ENGINE packet->append(hton_name(hton));
if (table->part_info)
packet->append(ha_resolve_storage_engine_name(
table->part_info->default_engine_type));
else
packet->append(file->table_type());
#else
packet->append(file->table_type());
#endif
} }
/* /*
...@@ -1859,9 +1851,7 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, ...@@ -1859,9 +1851,7 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet,
packet->append(buff, (uint) (end - buff)); packet->append(buff, (uint) (end - buff));
} }
if (share->table_charset && if (share->table_charset && !(sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)))
!(thd->variables.sql_mode & MODE_MYSQL323) &&
!(thd->variables.sql_mode & MODE_MYSQL40))
{ {
/* /*
IF check_create_info IF check_create_info
...@@ -2114,8 +2104,7 @@ void append_definer(THD *thd, String *buffer, const LEX_STRING *definer_user, ...@@ -2114,8 +2104,7 @@ void append_definer(THD *thd, String *buffer, const LEX_STRING *definer_user,
} }
int static int show_create_view(THD *thd, TABLE_LIST *table, String *buff)
view_store_create_info(THD *thd, TABLE_LIST *table, String *buff)
{ {
my_bool compact_view_name= TRUE; my_bool compact_view_name= TRUE;
my_bool foreign_db_mode= (thd->variables.sql_mode & (MODE_POSTGRESQL | my_bool foreign_db_mode= (thd->variables.sql_mode & (MODE_POSTGRESQL |
......
...@@ -74,10 +74,10 @@ typedef struct system_status_var STATUS_VAR; ...@@ -74,10 +74,10 @@ typedef struct system_status_var STATUS_VAR;
#define IS_FILES_STATUS 36 #define IS_FILES_STATUS 36
#define IS_FILES_EXTRA 37 #define IS_FILES_EXTRA 37
int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, typedef enum { WITHOUT_DB_NAME, WITH_DB_NAME } enum_with_db_name;
HA_CREATE_INFO *create_info_arg, bool show_database, int show_create_table(THD *thd, TABLE_LIST *table_list, String *packet,
bool create_or_replace); HA_CREATE_INFO *create_info_arg,
int view_store_create_info(THD *thd, TABLE_LIST *table, String *buff); enum_with_db_name with_db_name);
int copy_event_to_schema_table(THD *thd, TABLE *sch_table, TABLE *event_table); int copy_event_to_schema_table(THD *thd, TABLE *sch_table, TABLE *event_table);
......
...@@ -5434,7 +5434,7 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, ...@@ -5434,7 +5434,7 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table,
table->open_strategy= TABLE_LIST::OPEN_NORMAL; table->open_strategy= TABLE_LIST::OPEN_NORMAL;
/* /*
In order for store_create_info() to work we need to open In order for show_create_table() to work we need to open
destination table if it is not already open (i.e. if it destination table if it is not already open (i.e. if it
has not existed before). We don't need acquire metadata has not existed before). We don't need acquire metadata
lock in order to do this as we already hold exclusive lock in order to do this as we already hold exclusive
...@@ -5458,13 +5458,9 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, ...@@ -5458,13 +5458,9 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table,
if (!table->view) if (!table->view)
{ {
int result __attribute__((unused))= int result __attribute__((unused))=
store_create_info(thd, table, &query, show_create_table(thd, table, &query, create_info, WITHOUT_DB_NAME);
create_info, FALSE /* show_database */,
MY_TEST(create_info->org_options &
HA_LEX_CREATE_REPLACE) ||
create_info->table_was_deleted);
DBUG_ASSERT(result == 0); // store_create_info() always return 0 DBUG_ASSERT(result == 0); // show_create_table() always return 0
do_logging= FALSE; do_logging= FALSE;
if (write_bin_log(thd, TRUE, query.ptr(), query.length())) if (write_bin_log(thd, TRUE, query.ptr(), query.length()))
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment