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