sql_derived.cc 3.47 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* Copyright (C) 2000 MySQL AB

   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
  These were introduced by Monty and Sinisa <sinisa@mysql.com>
*/


#include "mysql_priv.h"
#include "sql_select.h"
#include "sql_acl.h"

static const char *any_db="*any*";	// Special symbol for check_access


31
int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
unknown's avatar
unknown committed
32
{
unknown's avatar
unknown committed
33 34 35 36
  /*
    TODO: make derived tables with union inside (now only 1 SELECT may be
    procesed)
  */
37
  SELECT_LEX *sl= unit->first_select();
unknown's avatar
unknown committed
38 39
  List<Item> item_list;
  TABLE *table;
unknown's avatar
unknown committed
40
  int res= 0;
unknown's avatar
unknown committed
41
  select_union *derived_result;
unknown's avatar
unknown committed
42
  TABLE_LIST *tables= (TABLE_LIST *)sl->table_list.first;
unknown's avatar
unknown committed
43 44 45 46
  TMP_TABLE_PARAM tmp_table_param;
  DBUG_ENTER("mysql_derived");
  
  if (tables)
unknown's avatar
unknown committed
47
    res= check_table_access(thd,SELECT_ACL, tables);
unknown's avatar
unknown committed
48
  else
unknown's avatar
unknown committed
49
    res= check_access(thd, SELECT_ACL, any_db);
unknown's avatar
unknown committed
50 51 52 53 54 55 56 57 58
  if (res)
    DBUG_RETURN(-1);

  Item *item;
  List_iterator<Item> it(sl->item_list);

  while ((item= it++))
    item_list.push_back(item);
    
59
  if (!(res=open_and_lock_tables(thd,tables)))
unknown's avatar
unknown committed
60
  {
unknown's avatar
unknown committed
61
    if (setup_fields(thd,tables,item_list,0,0,1))
unknown's avatar
unknown committed
62 63 64 65 66 67
    {
      res=-1;
      goto exit;
    }
    bzero((char*) &tmp_table_param,sizeof(tmp_table_param));
    tmp_table_param.field_count=item_list.elements;
unknown's avatar
unknown committed
68
    if (!(table=create_tmp_table(thd, &tmp_table_param, item_list,
unknown's avatar
unknown committed
69 70 71 72
			         (ORDER*) 0, 0, 1, 0,
			         (sl->options | thd->options |
				  TMP_TABLE_ALL_COLUMNS),
                                 unit)))
unknown's avatar
unknown committed
73 74 75 76 77 78 79
    {
      res=-1;
      goto exit;
    }
  
    if ((derived_result=new select_union(table)))
    {
80
      derived_result->tmp_table_param=&tmp_table_param;
81 82 83 84 85
      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)
unknown's avatar
unknown committed
86
	sl->options&= ~OPTION_FOUND_ROWS;
87 88 89

      SELECT_LEX_NODE *save_current_select= lex->current_select;
      lex->current_select= sl;
unknown's avatar
unknown committed
90 91 92 93 94 95
      res= mysql_select(thd, tables,  sl->item_list,
			sl->where, (ORDER *) sl->order_list.first,
			(ORDER*) sl->group_list.first,
			sl->having, (ORDER*) NULL,
			sl->options | thd->options | SELECT_NO_UNLOCK,
			derived_result, unit, sl, 0);
96 97
      lex->current_select= save_current_select;

unknown's avatar
unknown committed
98 99 100 101 102 103 104 105 106
      if (!res)
      {
// Here we entirely fix both TABLE_LIST and list of SELECT's as there were no derived tables
	if (derived_result->flush())
	  res=1;
	else
	{
	  t->real_name=table->real_name;
	  t->table=table;
unknown's avatar
unknown committed
107
	  table->derived_select_number= sl->select_number;
unknown's avatar
unknown committed
108
	  table->tmp_table=TMP_TABLE;
109 110
	  if (!lex->describe)
	    sl->exclude();
111
	  t->db=(char *)"";
unknown's avatar
unknown committed
112 113 114 115 116 117 118 119 120 121 122 123
	  t->derived=(SELECT_LEX *)0; // just in case ...
	}
      }
      delete derived_result;
    }
    if (res)
      free_tmp_table(thd,table);
exit:
    close_thread_tables(thd);
  }
  DBUG_RETURN(res);
}