~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2002-2003 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/*
17
  Derived tables
18
  These were introduced by Sinisa <sinisa@mysql.com>
19
*/
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
20
#include "drizzled/server_includes.h"
21
#include "drizzled/sql_select.h"
1 by brian
clean slate
22
23
/*
24
  Call given derived table processor (preparing or filling tables)
25
26
  SYNOPSIS
27
    mysql_handle_derived()
28
    lex                 LEX for this thread
29
    processor           procedure of derived table processing
30
31
  RETURN
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
32
    false  OK
33
    true   Error
1 by brian
clean slate
34
*/
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
35
bool mysql_handle_derived(LEX *lex, bool (*processor)(Session*, LEX*, TableList*))
1 by brian
clean slate
36
{
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
37
  bool res= false;
1 by brian
clean slate
38
  if (lex->derived_tables)
39
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
40
    lex->session->derived_tables_processing= true;
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
41
    for (Select_Lex *sl= lex->all_selects_list; sl; sl= sl->next_select_in_list())
1 by brian
clean slate
42
    {
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
43
      for (TableList *cursor= sl->get_table_list(); cursor; cursor= cursor->next_local)
1 by brian
clean slate
44
      {
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
45
        if ((res= (*processor)(lex->session, lex, cursor)))
46
          goto out;
1 by brian
clean slate
47
      }
48
      if (lex->describe)
49
      {
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
50
        /*
51
          Force join->join_tmp creation, because we will use this JOIN
52
          twice for EXPLAIN and we have to have unchanged join for EXPLAINing
53
        */
54
        sl->uncacheable|= UNCACHEABLE_EXPLAIN;
55
        sl->master_unit()->uncacheable|= UNCACHEABLE_EXPLAIN;
1 by brian
clean slate
56
      }
57
    }
58
  }
59
out:
520.1.22 by Brian Aker
Second pass of thd cleanup
60
  lex->session->derived_tables_processing= false;
1 by brian
clean slate
61
  return res;
62
}
63
64
/*
65
  Create temporary table structure (but do not fill it)
66
67
  SYNOPSIS
68
    mysql_derived_prepare()
520.1.22 by Brian Aker
Second pass of thd cleanup
69
    session			Thread handle
1 by brian
clean slate
70
    lex                 LEX for this thread
327.2.4 by Brian Aker
Refactoring table.h
71
    orig_table_list     TableList for the upper SELECT
1 by brian
clean slate
72
73
  IMPLEMENTATION
74
    Derived table is resolved with temporary table.
75
327.2.4 by Brian Aker
Refactoring table.h
76
    After table creation, the above TableList is updated with a new table.
1 by brian
clean slate
77
78
    This function is called before any command containing derived table
79
    is executed.
80
520.1.22 by Brian Aker
Second pass of thd cleanup
81
    Derived tables is stored in session->derived_tables and freed in
1 by brian
clean slate
82
    close_thread_tables()
83
84
  RETURN
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
85
    false  OK
86
    true   Error
1 by brian
clean slate
87
*/
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
88
bool mysql_derived_prepare(Session *session, LEX *, TableList *orig_table_list)
1 by brian
clean slate
89
{
848 by Brian Aker
typdef class removal (just... use the name of the class).
90
  Select_Lex_Unit *unit= orig_table_list->derived;
151 by Brian Aker
Ulonglong to uint64_t
91
  uint64_t create_options;
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
92
  bool res= false;
1 by brian
clean slate
93
  if (unit)
94
  {
846 by Brian Aker
Removing on typedeffed class.
95
    Select_Lex *first_select= unit->first_select();
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
96
    Table *table= 0;
1 by brian
clean slate
97
    select_union *derived_result;
98
99
    /* prevent name resolving out of derived table */
846 by Brian Aker
Removing on typedeffed class.
100
    for (Select_Lex *sl= first_select; sl; sl= sl->next_select())
1 by brian
clean slate
101
      sl->context.outer_context= 0;
102
103
    if (!(derived_result= new select_union))
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
104
      return(true); // out of memory
1 by brian
clean slate
105
848 by Brian Aker
typdef class removal (just... use the name of the class).
106
    // Select_Lex_Unit::prepare correctly work for single select
520.1.22 by Brian Aker
Second pass of thd cleanup
107
    if ((res= unit->prepare(session, derived_result, 0)))
1 by brian
clean slate
108
      goto exit;
109
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
110
    create_options= (first_select->options | session->options | TMP_TABLE_ALL_COLUMNS);
1 by brian
clean slate
111
    /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
112
      Temp table is created so that it hounours if UNION without ALL is to be
1 by brian
clean slate
113
      processed
114
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
115
      As 'distinct' parameter we always pass false (0), because underlying
1 by brian
clean slate
116
      query will control distinct condition by itself. Correct test of
117
      distinct underlying query will be is_union &&
118
      !unit->union_distinct->next_select() (i.e. it is union and last distinct
119
      SELECT is last SELECT of UNION).
120
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
121
    if ((res= derived_result->create_result_table(session, &unit->types, false,
1 by brian
clean slate
122
                                                  create_options,
123
                                                  orig_table_list->alias,
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
124
                                                  false)))
1 by brian
clean slate
125
      goto exit;
126
127
    table= derived_result->table;
128
129
exit:
130
    /*
131
      if it is preparation PS only or commands that need only VIEW structure
132
      then we do not need real data and we can skip execution (and parameters
133
      is not defined, too)
134
    */
135
    if (res)
136
    {
137
      if (table)
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
138
        table->free_tmp_table(session);
1 by brian
clean slate
139
      delete derived_result;
140
    }
141
    else
142
    {
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
143
      if (! session->fill_derived_tables())
1 by brian
clean slate
144
      {
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
145
        delete derived_result;
146
        derived_result= NULL;
1 by brian
clean slate
147
      }
148
      orig_table_list->derived_result= derived_result;
149
      orig_table_list->table= table;
150
      orig_table_list->table_name=        table->s->table_name.str;
151
      orig_table_list->table_name_length= table->s->table_name.length;
152
      table->derived_select_number= first_select->select_number;
153
      table->s->tmp_table= NON_TRANSACTIONAL_TMP_TABLE;
154
      orig_table_list->db= (char *)"";
155
      orig_table_list->db_length= 0;
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
156
      /* Force read of table stats in the optimizer */
1208.3.2 by brian
Update for Cursor renaming.
157
      table->cursor->info(HA_STATUS_VARIABLE);
1 by brian
clean slate
158
      /* Add new temporary table to list of open derived tables */
520.1.22 by Brian Aker
Second pass of thd cleanup
159
      table->next= session->derived_tables;
160
      session->derived_tables= table;
1 by brian
clean slate
161
    }
162
  }
163
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
164
  return(res);
1 by brian
clean slate
165
}
166
167
/*
168
  fill derived table
169
170
  SYNOPSIS
171
    mysql_derived_filling()
520.1.22 by Brian Aker
Second pass of thd cleanup
172
    session			Thread handle
1 by brian
clean slate
173
    lex                 LEX for this thread
174
    unit                node that contains all SELECT's for derived tables
327.2.4 by Brian Aker
Refactoring table.h
175
    orig_table_list     TableList for the upper SELECT
1 by brian
clean slate
176
177
  IMPLEMENTATION
178
    Derived table is resolved with temporary table. It is created based on the
179
    queries defined. After temporary table is filled, if this is not EXPLAIN,
180
    then the entire unit / node is deleted. unit is deleted if UNION is used
181
    for derived table and node is deleted is it is a  simple SELECT.
182
    If you use this function, make sure it's not called at prepare.
183
    Due to evaluation of LIMIT clause it can not be used at prepared stage.
184
185
  RETURN
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
186
    false  OK
187
    true   Error
1 by brian
clean slate
188
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
189
bool mysql_derived_filling(Session *session, LEX *lex, TableList *orig_table_list)
1 by brian
clean slate
190
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
191
  Table *table= orig_table_list->table;
848 by Brian Aker
typdef class removal (just... use the name of the class).
192
  Select_Lex_Unit *unit= orig_table_list->derived;
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
193
  bool res= false;
1 by brian
clean slate
194
195
  /*check that table creation pass without problem and it is derived table */
196
  if (table && unit)
197
  {
846 by Brian Aker
Removing on typedeffed class.
198
    Select_Lex *first_select= unit->first_select();
1 by brian
clean slate
199
    select_union *derived_result= orig_table_list->derived_result;
846 by Brian Aker
Removing on typedeffed class.
200
    Select_Lex *save_current_select= lex->current_select;
1 by brian
clean slate
201
    if (unit->is_union())
202
    {
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
203
      /* execute union without clean up */
1 by brian
clean slate
204
      res= unit->exec();
205
    }
206
    else
207
    {
208
      unit->set_limit(first_select);
209
      if (unit->select_limit_cnt == HA_POS_ERROR)
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
210
	      first_select->options&= ~OPTION_FOUND_ROWS;
1 by brian
clean slate
211
212
      lex->current_select= first_select;
520.1.22 by Brian Aker
Second pass of thd cleanup
213
      res= mysql_select(session, &first_select->ref_pointer_array,
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
214
                        (TableList*) first_select->table_list.first,
215
                        first_select->with_wild,
216
                        first_select->item_list, first_select->where,
217
                        (first_select->order_list.elements+
218
                        first_select->group_list.elements),
219
                        (order_st *) first_select->order_list.first,
220
                        (order_st *) first_select->group_list.first,
923.1.10 by Brian Aker
Remove dead code around old procedures.
221
                        first_select->having,
222
                        (first_select->options | session->options | SELECT_NO_UNLOCK),
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
223
                        derived_result, unit, first_select);
1 by brian
clean slate
224
    }
225
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
226
    if (! res)
1 by brian
clean slate
227
    {
228
      /*
327.2.4 by Brian Aker
Refactoring table.h
229
        Here we entirely fix both TableList and list of SELECT's as
1 by brian
clean slate
230
        there were no derived tables
231
      */
232
      if (derived_result->flush())
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
233
        res= true;
1 by brian
clean slate
234
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
235
      if (! lex->describe)
1 by brian
clean slate
236
        unit->cleanup();
237
    }
238
    else
239
      unit->cleanup();
240
    lex->current_select= save_current_select;
241
  }
242
  return res;
243
}