Commit 31379fbe authored by bell@sanja.is.com.ua's avatar bell@sanja.is.com.ua

table lists management during VIEW processing code cleanup

parent bcadf50b
......@@ -643,7 +643,11 @@ typedef struct st_lex
THD *thd;
CHARSET_INFO *charset;
TABLE_LIST *query_tables; /* global list of all tables in this query */
/* last element next_global of previous list */
/*
last element next_global of previous list (used only for list building
during parsing and VIEW processing. This pointer is not valid in
mysql_execute_command
*/
TABLE_LIST **query_tables_last;
TABLE_LIST *proc_table; /* refer to mysql.proc if it was opened by VIEW */
......@@ -765,6 +769,11 @@ typedef struct st_lex
TABLE_LIST *unlink_first_table(bool *link_to_local);
void link_first_table_back(TABLE_LIST *first, bool link_to_local);
void first_lists_tables_same();
inline void add_to_query_tables(TABLE_LIST *table)
{
*(table->prev_global= query_tables_last)= table;
query_tables_last= &table->next_global;
}
bool can_be_merged();
bool can_use_merged();
......
......@@ -5152,8 +5152,7 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
/* Link table in local list (list for current select) */
table_list.link_in_list((byte*) ptr, (byte**) &ptr->next_local);
/* Link table in global list (all used tables) */
*(ptr->prev_global= lex->query_tables_last)= ptr;
lex->query_tables_last= &ptr->next_global;
lex->add_to_query_tables(ptr);
DBUG_RETURN(ptr);
}
......
......@@ -45,6 +45,7 @@ TYPELIB sql_updatable_view_key_typelib=
-1 Error
1 Error and error message given
*/
int mysql_create_view(THD *thd,
enum_view_create_mode mode)
{
......@@ -356,6 +357,7 @@ static LEX_STRING view_file_type[]= {{(char*)"VIEW", 4}};
-1 Error
1 Error and error message given
*/
static int mysql_register_view(THD *thd, TABLE_LIST *view,
enum_view_create_mode mode)
{
......@@ -501,7 +503,6 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view,
my_bool
mysql_make_view(File_parser *parser, TABLE_LIST *table)
{
bool include_proc_table= 0;
DBUG_ENTER("mysql_make_view");
if (table->view)
......@@ -512,7 +513,6 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table)
DBUG_RETURN(0);
}
TABLE_LIST *old_next, *tbl_end, *tbl_next;
SELECT_LEX *end;
THD *thd= current_thd;
LEX *old_lex= thd->lex, *lex;
......@@ -599,11 +599,14 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table)
TABLE_LIST *top_view= (table->belong_to_view ?
table->belong_to_view :
table);
TABLE_LIST *view_tables= lex->query_tables;
TABLE_LIST *view_tables_tail= 0;
if (lex->spfuns.records)
{
/* move SP to main LEX */
sp_merge_funs(old_lex, lex);
/* open mysq.proc for functions which are not in cache */
if (old_lex->proc_table == 0 &&
(old_lex->proc_table=
(TABLE_LIST*)thd->calloc(sizeof(TABLE_LIST))) != 0)
......@@ -614,18 +617,20 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table)
table->real_name= table->alias= (char*)"proc";
table->real_name_length= 4;
table->cacheable_table= 1;
include_proc_table= 1;
old_lex->add_to_query_tables(table);
}
}
/* cleanup LEX */
if (lex->spfuns.array.buffer)
hash_free(&lex->spfuns);
old_next= table->old_next= table->next_global;
if ((table->next_global= lex->query_tables))
table->next_global->prev_global= &table->next_global;
/* mark to avoid temporary table using and put view reference*/
for (TABLE_LIST *tbl= table->next_global; tbl; tbl= tbl->next_global)
/*
mark to avoid temporary table using and put view reference and find
last view table
*/
for (TABLE_LIST *tbl= view_tables;
tbl;
tbl= (view_tables_tail= tbl)->next_global)
{
tbl->skip_temporary= 1;
tbl->belong_to_view= top_view;
......@@ -638,8 +643,8 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table)
if ((old_lex->sql_command == SQLCOM_SELECT && old_lex->describe) ||
old_lex->sql_command == SQLCOM_SHOW_CREATE)
{
if (check_table_access(thd, SELECT_ACL, table->next_global, 1) &&
check_table_access(thd, SHOW_VIEW_ACL, table->next_global, 1))
if (check_table_access(thd, SELECT_ACL, view_tables, 1) &&
check_table_access(thd, SHOW_VIEW_ACL, view_tables, 1))
{
my_error(ER_VIEW_NO_EXPLAIN, MYF(0));
goto err;
......@@ -653,6 +658,29 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table)
if (lex->select_lex.options & OPTION_TO_QUERY_CACHE)
old_lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
/*
Put tables of VIEW after VIEW TABLE_LIST
NOTE: It is important for UPDATE/INSERT/DELETE checks to have this
tables just after VIEW instead of tail of list, to be able check that
table is unique. Also we store old next table for the same purpose.
*/
table->old_next= table->next_global;
if (view_tables)
{
if (table->next_global)
{
table->next_global->prev_global= &view_tables_tail->next_global;
view_tables_tail->next_global= table->old_next;
}
else
{
lex->query_tables_last= &view_tables_tail->next_global;
}
view_tables->prev_global= &table->next_global;
table->next_global= view_tables;
}
/*
check MERGE algorithm ability
- algorithm is not explicit TEMPORARY TABLE
......@@ -667,31 +695,26 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table)
{
/*
TODO: support multi tables substitutions
table->next_global should be the same as
(TABLE_LIST *)lex->select_lex.table_list.first;
*/
TABLE_LIST *view_table= table->next_global;
/* lex should contain at least one table */
DBUG_ASSERT(view_table != 0);
DBUG_ASSERT(view_tables != 0);
table->effective_algorithm= VIEW_ALGORITHM_MERGE;
DBUG_PRINT("info", ("algorithm: MERGE"));
table->updatable= (table->updatable_view != 0);
if (old_next)
{
if ((view_table->next_global= old_next))
old_next->prev_global= &view_table->next_global;
}
table->ancestor= view_table;
// next table should include SELECT_LEX under this table SELECT_LEX
table->ancestor= view_tables;
/*
next table should include SELECT_LEX under this table SELECT_LEX
TODO: ehere should be loop for multi tables substitution
*/
table->ancestor->select_lex= table->select_lex;
/*
move lock type (TODO: should we issue error in case of TMPTABLE
algorithm and non-read locking)?
*/
view_table->lock_type= table->lock_type;
view_tables->lock_type= table->lock_type;
/* Store WHERE clause for postprocessing in setup_ancestor */
table->where= lex->select_lex.where;
......@@ -714,22 +737,6 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table)
lex->unit.include_down(table->select_lex);
lex->unit.slave= &lex->select_lex; // fix include_down initialisation
if (old_next)
{
if ((tbl_end= table->next_global))
{
for (; (tbl_next= tbl_end->next_global); tbl_end= tbl_next)
;
if ((tbl_end->next_global= old_next))
tbl_end->next_global->prev_global= &tbl_end->next_global;
}
else
{
/* VIEW do not contain tables */
table->next_global= old_next;
}
}
table->derived= &lex->unit;
}
else
......@@ -746,17 +753,6 @@ ok:
lex->all_selects_list->link_prev=
(st_select_lex_node**)&old_lex->all_selects_list;
if (include_proc_table)
{
TABLE_LIST *proc= old_lex->proc_table;
if((proc->next_global= table->next_global))
{
table->next_global->prev_global= &proc->next_global;
}
proc->prev_global= &table->next_global;
table->next_global= proc;
}
thd->lex= old_lex;
DBUG_RETURN(0);
......
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