2003-01-07 10:45:06 +01:00
|
|
|
/* Copyright (C) 2002-2003 MySQL AB
|
2002-03-26 14:06:05 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Derived tables
|
2003-01-07 10:45:06 +01:00
|
|
|
These were introduced by Sinisa <sinisa@mysql.com>
|
2002-03-26 14:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "mysql_priv.h"
|
|
|
|
#include "sql_select.h"
|
|
|
|
#include "sql_acl.h"
|
|
|
|
|
2003-01-21 13:50:55 +01:00
|
|
|
extern const char *any_db; // Special symbol for check_access
|
2002-03-26 14:06:05 +01:00
|
|
|
|
2002-12-14 14:13:25 +01:00
|
|
|
/*
|
|
|
|
Resolve derived tables in all queries
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
|
|
|
|
thd Thread handle
|
|
|
|
lex LEX for this thread
|
|
|
|
unit node that contains all SELECT's for derived tables
|
|
|
|
t TABLE_LIST for the upper SELECT
|
|
|
|
|
|
|
|
IMPLEMENTATION
|
2003-01-07 10:45:06 +01:00
|
|
|
Derived table is resolved with temporary table. It is created based on the
|
|
|
|
queries defined. After temporary table is created, if this is not EXPLAIN,
|
|
|
|
then the entire unit / node is deleted. unit is deleted if UNION is used
|
|
|
|
for derived table and node is deleted is it is a simple SELECT.
|
|
|
|
|
|
|
|
After table creation, the above TABLE_LIST is updated with a new table.
|
2002-12-14 14:13:25 +01:00
|
|
|
|
2003-01-07 10:45:06 +01:00
|
|
|
This function is called before any command containing derived table
|
|
|
|
is executed.
|
2002-12-14 14:13:25 +01:00
|
|
|
|
2003-01-07 10:45:06 +01:00
|
|
|
Derived tables is stored in thd->derived_tables and freed in
|
|
|
|
close_thread_tables()
|
2002-12-14 14:13:25 +01:00
|
|
|
|
2003-01-07 10:45:06 +01:00
|
|
|
TODO
|
|
|
|
Move creation of derived tables in open_and_lock_tables()
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 ok
|
|
|
|
1 Error
|
|
|
|
-1 Error and error message given
|
2002-12-14 14:13:25 +01:00
|
|
|
*/
|
|
|
|
|
2002-03-26 14:06:05 +01:00
|
|
|
|
2002-11-09 14:40:46 +01:00
|
|
|
int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2002-05-27 19:52:54 +02:00
|
|
|
SELECT_LEX *sl= unit->first_select();
|
2002-03-26 14:06:05 +01:00
|
|
|
List<Item> item_list;
|
|
|
|
TABLE *table;
|
2003-01-07 10:45:06 +01:00
|
|
|
int res;
|
2002-03-26 14:06:05 +01:00
|
|
|
select_union *derived_result;
|
2002-05-06 23:04:16 +02:00
|
|
|
TABLE_LIST *tables= (TABLE_LIST *)sl->table_list.first;
|
2002-03-26 14:06:05 +01:00
|
|
|
TMP_TABLE_PARAM tmp_table_param;
|
2003-01-26 20:30:35 +01:00
|
|
|
bool is_union= sl->next_select() && sl->next_select()->linkage == UNION_TYPE;
|
|
|
|
bool is_subsel= sl->first_inner_unit();
|
2002-12-26 00:28:59 +01:00
|
|
|
SELECT_LEX_NODE *save_current_select= lex->current_select;
|
2003-01-07 10:45:06 +01:00
|
|
|
DBUG_ENTER("mysql_derived");
|
2002-03-26 14:06:05 +01:00
|
|
|
|
2003-01-07 10:45:06 +01:00
|
|
|
/*
|
|
|
|
In create_total_list, derived tables have to be treated in case of
|
|
|
|
EXPLAIN, This is because unit/node is not deleted in that
|
|
|
|
case. Current code in this function has to be improved to
|
|
|
|
recognize better when this function is called from derived tables
|
|
|
|
and when from other functions.
|
|
|
|
*/
|
2003-01-26 20:30:35 +01:00
|
|
|
if ((is_union || is_subsel) && unit->create_total_list(thd, lex, &tables, 1))
|
2002-12-12 15:09:06 +01:00
|
|
|
DBUG_RETURN(-1);
|
|
|
|
|
2002-03-26 14:06:05 +01:00
|
|
|
if (tables)
|
2002-05-06 23:04:16 +02:00
|
|
|
res= check_table_access(thd,SELECT_ACL, tables);
|
2002-03-26 14:06:05 +01:00
|
|
|
else
|
2002-05-06 23:04:16 +02:00
|
|
|
res= check_access(thd, SELECT_ACL, any_db);
|
2002-03-26 14:06:05 +01:00
|
|
|
if (res)
|
|
|
|
DBUG_RETURN(-1);
|
|
|
|
|
2002-11-09 14:40:46 +01:00
|
|
|
if (!(res=open_and_lock_tables(thd,tables)))
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2003-01-26 20:30:35 +01:00
|
|
|
if (is_union || is_subsel)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
The following code is a re-do of fix_tables_pointers() found
|
|
|
|
in sql_select.cc for UNION's within derived tables. The only
|
|
|
|
difference is in navigation, as in derived tables we care for
|
|
|
|
this level only.
|
|
|
|
|
|
|
|
*/
|
|
|
|
fix_tables_pointers(unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
lex->current_select= sl;
|
|
|
|
TABLE_LIST *first_table= (TABLE_LIST*) sl->table_list.first;
|
2003-01-29 18:42:39 +01:00
|
|
|
if (setup_tables(first_table) ||
|
|
|
|
setup_wild(thd, first_table, sl->item_list, 0, sl->with_wild))
|
|
|
|
{
|
|
|
|
res= -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
item_list= sl->item_list;
|
|
|
|
sl->with_wild= 0;
|
|
|
|
if (setup_ref_array(thd, &sl->ref_pointer_array,
|
|
|
|
(item_list.elements + sl->with_sum_func +
|
|
|
|
sl->order_list.elements + sl->group_list.elements)) ||
|
|
|
|
setup_fields(thd, sl->ref_pointer_array, first_table, item_list,
|
|
|
|
0, 0, 1))
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2003-01-07 10:45:06 +01:00
|
|
|
res= -1;
|
2002-03-26 14:06:05 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
bzero((char*) &tmp_table_param,sizeof(tmp_table_param));
|
2003-01-26 20:30:35 +01:00
|
|
|
tmp_table_param.field_count= item_list.elements;
|
|
|
|
if (!(table= create_tmp_table(thd, &tmp_table_param, item_list,
|
|
|
|
(ORDER*) 0,
|
|
|
|
is_union && !unit->union_option, 1,
|
|
|
|
(sl->options | thd->options |
|
|
|
|
TMP_TABLE_ALL_COLUMNS),
|
|
|
|
HA_POS_ERROR)))
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2003-01-07 10:45:06 +01:00
|
|
|
res= -1;
|
2002-03-26 14:06:05 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((derived_result=new select_union(table)))
|
|
|
|
{
|
2002-11-15 18:58:07 +01:00
|
|
|
derived_result->tmp_table_param=&tmp_table_param;
|
2002-05-08 22:14:40 +02:00
|
|
|
unit->offset_limit_cnt= sl->offset_limit;
|
|
|
|
unit->select_limit_cnt= sl->select_limit+sl->offset_limit;
|
|
|
|
if (unit->select_limit_cnt < sl->select_limit)
|
|
|
|
unit->select_limit_cnt= HA_POS_ERROR;
|
|
|
|
if (unit->select_limit_cnt == HA_POS_ERROR)
|
2002-03-26 14:06:05 +01:00
|
|
|
sl->options&= ~OPTION_FOUND_ROWS;
|
2002-11-11 09:49:41 +01:00
|
|
|
|
2002-12-12 15:09:06 +01:00
|
|
|
if (is_union)
|
2003-01-29 18:42:39 +01:00
|
|
|
res= mysql_union(thd, lex, derived_result, unit, 1);
|
2002-12-12 15:09:06 +01:00
|
|
|
else
|
2003-01-26 20:30:35 +01:00
|
|
|
res= mysql_select(thd, &sl->ref_pointer_array,
|
|
|
|
(TABLE_LIST*) sl->table_list.first,
|
|
|
|
sl->with_wild,
|
2003-01-25 12:19:46 +01:00
|
|
|
sl->item_list, sl->where,
|
|
|
|
sl->order_list.elements+sl->group_list.elements,
|
|
|
|
(ORDER *) sl->order_list.first,
|
|
|
|
(ORDER *) sl->group_list.first,
|
|
|
|
sl->having, (ORDER*) NULL,
|
|
|
|
sl->options | thd->options | SELECT_NO_UNLOCK,
|
2003-01-29 18:42:39 +01:00
|
|
|
derived_result, unit, sl, 0, 1);
|
2002-11-11 09:49:41 +01:00
|
|
|
|
2002-03-26 14:06:05 +01:00
|
|
|
if (!res)
|
|
|
|
{
|
2003-01-07 10:45:06 +01:00
|
|
|
/*
|
|
|
|
Here we entirely fix both TABLE_LIST and list of SELECT's as
|
|
|
|
there were no derived tables
|
|
|
|
*/
|
2002-03-26 14:06:05 +01:00
|
|
|
if (derived_result->flush())
|
2003-01-07 10:45:06 +01:00
|
|
|
res= 1;
|
2002-03-26 14:06:05 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
t->real_name=table->real_name;
|
|
|
|
t->table=table;
|
2002-09-26 22:08:22 +02:00
|
|
|
table->derived_select_number= sl->select_number;
|
2002-11-11 13:04:50 +01:00
|
|
|
table->tmp_table=TMP_TABLE;
|
2002-11-29 13:49:07 +01:00
|
|
|
if (lex->describe)
|
2002-11-30 18:53:31 +01:00
|
|
|
{
|
2003-01-07 10:45:06 +01:00
|
|
|
// to fix a problem in EXPLAIN
|
2002-11-30 18:53:31 +01:00
|
|
|
if (tables)
|
2003-01-07 10:45:06 +01:00
|
|
|
tables->table_list->table=tables->table;
|
2002-11-30 18:53:31 +01:00
|
|
|
}
|
2002-11-30 17:46:24 +01:00
|
|
|
else
|
2002-12-24 12:58:07 +01:00
|
|
|
unit->exclude();
|
2003-01-07 10:45:06 +01:00
|
|
|
t->db= (char *)"";
|
|
|
|
t->derived=(SELECT_LEX *) 1; // just in case ...
|
2002-11-30 17:46:24 +01:00
|
|
|
table->file->info(HA_STATUS_VARIABLE);
|
2002-03-26 14:06:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
delete derived_result;
|
|
|
|
}
|
|
|
|
if (res)
|
2003-01-26 20:30:35 +01:00
|
|
|
free_tmp_table(thd, table);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
table->next= thd->derived_tables;
|
|
|
|
thd->derived_tables= table;
|
|
|
|
}
|
2003-01-25 12:19:46 +01:00
|
|
|
|
2002-03-26 14:06:05 +01:00
|
|
|
exit:
|
2002-12-26 00:28:59 +01:00
|
|
|
lex->current_select= save_current_select;
|
2003-01-07 10:45:06 +01:00
|
|
|
close_thread_tables(thd, 0, 1);
|
2002-03-26 14:06:05 +01:00
|
|
|
}
|
|
|
|
DBUG_RETURN(res);
|
|
|
|
}
|