2691
2695
LEX *lex= thd->lex;
2692
2696
const char *wild= lex->wild ? lex->wild->ptr() : NullS;
2693
2699
bzero((char*) lookup_field_values, sizeof(LOOKUP_FIELD_VALUES));
2694
2700
switch (lex->sql_command) {
2695
2701
case SQLCOM_SHOW_DATABASES:
2698
lookup_field_values->db_value.str= (char*) wild;
2699
lookup_field_values->db_value.length= strlen(wild);
2704
thd->make_lex_string(&lookup_field_values->db_value,
2705
wild, strlen(wild), 0);
2700
2706
lookup_field_values->wild_db_value= 1;
2703
2709
case SQLCOM_SHOW_TABLES:
2704
2710
case SQLCOM_SHOW_TABLE_STATUS:
2705
2711
case SQLCOM_SHOW_TRIGGERS:
2706
2712
case SQLCOM_SHOW_EVENTS:
2707
lookup_field_values->db_value.str= lex->select_lex.db;
2708
lookup_field_values->db_value.length=strlen(lex->select_lex.db);
2713
thd->make_lex_string(&lookup_field_values->db_value,
2714
lex->select_lex.db, strlen(lex->select_lex.db), 0);
2711
lookup_field_values->table_value.str= (char*)wild;
2712
lookup_field_values->table_value.length= strlen(wild);
2717
thd->make_lex_string(&lookup_field_values->table_value,
2718
wild, strlen(wild), 0);
2713
2719
lookup_field_values->wild_table_value= 1;
2718
2724
The "default" is for queries over I_S.
2719
2725
All previous cases handle SHOW commands.
2721
return calc_lookup_values_from_cond(thd, cond, tables, lookup_field_values);
2727
rc= calc_lookup_values_from_cond(thd, cond, tables, lookup_field_values);
2731
if (lower_case_table_names && !rc)
2734
We can safely do in-place upgrades here since all of the above cases
2735
are allocating a new memory buffer for these strings.
2737
if (lookup_field_values->db_value.str && lookup_field_values->db_value.str[0])
2738
my_casedn_str(system_charset_info, lookup_field_values->db_value.str);
2739
if (lookup_field_values->table_value.str &&
2740
lookup_field_values->table_value.str[0])
2741
my_casedn_str(system_charset_info, lookup_field_values->table_value.str);
2979
@brief Fill I_S table for SHOW COLUMNS|INDEX commands
2981
@param[in] thd thread handler
2982
@param[in] tables TABLE_LIST for I_S table
2983
@param[in] schema_table pointer to I_S structure
2984
@param[in] open_tables_state_backup pointer to Open_tables_state object
2985
which is used to save|restore original
2986
status of variables related to
2989
@return Operation status
3005
Fill I_S table with data obtained by performing full-blown table open.
3007
@param thd Thread handler.
3008
@param is_show_fields_or_keys Indicates whether it is a legacy SHOW
3009
COLUMNS or SHOW KEYS statement.
3010
@param table TABLE object for I_S table to be filled.
3011
@param schema_table I_S table description structure.
3012
@param orig_db_name Database name.
3013
@param orig_table_name Table name.
3014
@param open_tables_state_backup Open_tables_state object which is used
3015
to save/restore original status of
3016
variables related to open tables state.
3018
@retval FALSE - Success.
3019
@retval TRUE - Failure.
2995
fill_schema_show_cols_or_idxs(THD *thd, TABLE_LIST *tables,
2996
ST_SCHEMA_TABLE *schema_table,
2997
Open_tables_state *open_tables_state_backup)
3023
fill_schema_table_by_open(THD *thd, bool is_show_fields_or_keys,
3024
TABLE *table, ST_SCHEMA_TABLE *schema_table,
3025
LEX_STRING *orig_db_name,
3026
LEX_STRING *orig_table_name,
3027
Open_tables_state *open_tables_state_backup)
3001
LEX_STRING tmp_lex_string, tmp_lex_string1, *db_name, *table_name;
3002
enum_sql_command save_sql_command= lex->sql_command;
3003
TABLE_LIST *show_table_list= tables->schema_select_lex->table_list.first;
3004
TABLE *table= tables->table;
3006
DBUG_ENTER("fill_schema_show");
3008
lex->all_selects_list= tables->schema_select_lex;
3010
Restore thd->temporary_tables to be able to process
3011
temporary tables(only for 'show index' & 'show columns').
3012
This should be changed when processing of temporary tables for
3013
I_S tables will be done.
3015
thd->temporary_tables= open_tables_state_backup->temporary_tables;
3029
Query_arena i_s_arena(thd->mem_root,
3030
Query_arena::CONVENTIONAL_EXECUTION),
3031
backup_arena, *old_arena;
3032
LEX *old_lex= thd->lex, temp_lex, *lex;
3033
LEX_STRING db_name, table_name;
3034
TABLE_LIST *table_list;
3038
When a view is opened its structures are allocated on a permanent
3039
statement arena and linked into the LEX tree for the current statement
3040
(this happens even in cases when view is handled through TEMPTABLE
3043
To prevent this process from unnecessary hogging of memory in the permanent
3044
arena of our I_S query and to avoid damaging its LEX we use temporary
3045
arena and LEX for table/view opening.
3047
Use temporary arena instead of statement permanent arena. Also make
3048
it active arena and save original one for successive restoring.
3050
old_arena= thd->stmt_arena;
3051
thd->stmt_arena= &i_s_arena;
3052
thd->set_n_backup_active_arena(&i_s_arena, &backup_arena);
3054
/* Prepare temporary LEX. */
3055
thd->lex= lex= &temp_lex;
3058
/* Disable constant subquery evaluation as we won't be locking tables. */
3059
lex->context_analysis_only= CONTEXT_ANALYSIS_ONLY_VIEW;
3062
Some of process_table() functions rely on wildcard being passed from
3063
old LEX (or at least being initialized).
3065
lex->wild= old_lex->wild;
3068
Since make_table_list() might change database and table name passed
3069
to it we create copies of orig_db_name and orig_table_name here.
3070
These copies are used for make_table_list() while unaltered values
3071
are passed to process_table() functions.
3073
if (!thd->make_lex_string(&db_name, orig_db_name->str,
3074
orig_db_name->length, FALSE) ||
3075
!thd->make_lex_string(&table_name, orig_table_name->str,
3076
orig_table_name->length, FALSE))
3080
Create table list element for table to be open. Link it with the
3081
temporary LEX. The latter is required to correctly open views and
3082
produce table describing their structure.
3084
if (make_table_list(thd, &lex->select_lex, &db_name, &table_name))
3087
table_list= lex->select_lex.table_list.first;
3089
if (is_show_fields_or_keys)
3092
Restore thd->temporary_tables to be able to process
3093
temporary tables (only for 'show index' & 'show columns').
3094
This should be changed when processing of temporary tables for
3095
I_S tables will be done.
3097
thd->temporary_tables= open_tables_state_backup->temporary_tables;
3102
Apply optimization flags for table opening which are relevant for
3103
this I_S table. We can't do this for SHOW COLUMNS/KEYS because of
3104
backward compatibility.
3106
table_list->i_s_requested_object= schema_table->i_s_requested_object;
3017
3110
Let us set fake sql_command so views won't try to merge
3018
3111
themselves into main statement. If we don't do this,
3019
3112
SELECT * from information_schema.xxxx will cause problems.
3020
SQLCOM_SHOW_FIELDS is used because it satisfies 'only_view_structure()'
3113
SQLCOM_SHOW_FIELDS is used because it satisfies
3114
'only_view_structure()'.
3022
3116
lex->sql_command= SQLCOM_SHOW_FIELDS;
3023
res= open_normal_and_derived_tables(thd, show_table_list,
3024
MYSQL_LOCK_IGNORE_FLUSH);
3025
lex->sql_command= save_sql_command;
3027
get_all_tables() returns 1 on failure and 0 on success thus
3028
return only these and not the result code of ::process_table()
3030
We should use show_table_list->alias instead of
3031
show_table_list->table_name because table_name
3032
could be changed during opening of I_S tables. It's safe
3033
to use alias because alias contains original table name
3034
in this case(this part of code is used only for
3035
'show columns' & 'show statistics' commands).
3037
table_name= thd->make_lex_string(&tmp_lex_string1, show_table_list->alias,
3038
strlen(show_table_list->alias), FALSE);
3039
if (!show_table_list->view)
3040
db_name= thd->make_lex_string(&tmp_lex_string, show_table_list->db,
3041
show_table_list->db_length, FALSE);
3043
db_name= &show_table_list->view_db;
3046
error= test(schema_table->process_table(thd, show_table_list,
3047
table, res, db_name,
3049
thd->temporary_tables= 0;
3050
close_tables_for_reopen(thd, &show_table_list);
3118
result= open_normal_and_derived_tables(thd, table_list,
3119
MYSQL_LOCK_IGNORE_FLUSH);
3122
Restore old value of sql_command back as it is being looked at in
3123
process_table() function.
3125
lex->sql_command= old_lex->sql_command;
3128
XXX: show_table_list has a flag i_is_requested,
3129
and when it's set, open_normal_and_derived_tables()
3130
can return an error without setting an error message
3131
in THD, which is a hack. This is why we have to
3132
check for res, then for thd->is_error() and only then
3133
for thd->main_da.sql_errno().
3135
Again we don't do this for SHOW COLUMNS/KEYS because
3136
of backward compatibility.
3138
if (!is_show_fields_or_keys && result && thd->is_error() &&
3139
thd->main_da.sql_errno() == ER_NO_SUCH_TABLE)
3142
Hide error for a non-existing table.
3143
For example, this error can occur when we use a where condition
3144
with a db name and table, but the table does not exist.
3151
result= schema_table->process_table(thd, table_list,
3158
lex->unit.cleanup();
3160
/* Restore original LEX value, statement's arena and THD arena values. */
3163
if (i_s_arena.free_list)
3164
i_s_arena.free_items();
3167
For safety reset list of open temporary tables before closing
3168
all tables open within this Open_tables_state.
3170
thd->temporary_tables= NULL;
3171
close_thread_tables(thd);
3174
thd->stmt_arena= old_arena;
3175
thd->restore_active_arena(&i_s_arena, &backup_arena);
3436
LEX_STRING tmp_lex_string, orig_db_name;
3438
Set the parent lex of 'sel' because it is needed by
3439
sel.init_query() which is called inside make_table_list.
3441
3563
thd->no_warnings_for_error= 1;
3442
sel.parent_lex= lex;
3443
/* db_name can be changed in make_table_list() func */
3444
if (!thd->make_lex_string(&orig_db_name, db_name->str,
3445
db_name->length, FALSE))
3447
if (make_table_list(thd, &sel, db_name, table_name))
3449
TABLE_LIST *show_table_list= sel.table_list.first;
3450
lex->all_selects_list= &sel;
3451
lex->derived_tables= 0;
3452
lex->sql_command= SQLCOM_SHOW_FIELDS;
3453
show_table_list->i_s_requested_object=
3454
schema_table->i_s_requested_object;
3455
3565
DEBUG_SYNC(thd, "before_open_in_get_all_tables");
3456
res= open_normal_and_derived_tables(thd, show_table_list,
3457
MYSQL_LOCK_IGNORE_FLUSH);
3458
lex->sql_command= save_sql_command;
3460
XXX: show_table_list has a flag i_is_requested,
3461
and when it's set, open_normal_and_derived_tables()
3462
can return an error without setting an error message
3463
in THD, which is a hack. This is why we have to
3464
check for res, then for thd->is_error() only then
3465
for thd->main_da.sql_errno().
3467
if (res && thd->is_error() &&
3468
thd->main_da.sql_errno() == ER_NO_SUCH_TABLE)
3471
Hide error for not existing table.
3472
This error can occur for example when we use
3473
where condition with db name and table name and this
3474
table does not exist.
3482
We should use show_table_list->alias instead of
3483
show_table_list->table_name because table_name
3484
could be changed during opening of I_S tables. It's safe
3485
to use alias because alias contains original table name
3488
thd->make_lex_string(&tmp_lex_string, show_table_list->alias,
3489
strlen(show_table_list->alias), FALSE);
3490
res= schema_table->process_table(thd, show_table_list, table,
3493
close_tables_for_reopen(thd, &show_table_list);
3495
DBUG_ASSERT(!lex->query_tables_own_last);
3567
if (fill_schema_table_by_open(thd, FALSE,
3568
table, schema_table,
3569
db_name, table_name,
3570
&open_tables_state_backup))