~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_derived.cc

  • Committer: Brian Aker
  • Date: 2008-07-03 00:14:39 UTC
  • Revision ID: brian@tangent.org-20080703001439-pit0mcl0wk8elxlq
Cleanup of sql-common and mysqldump

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
 
16
 
16
17
/*
17
18
  Derived tables
18
19
  These were introduced by Sinisa <sinisa@mysql.com>
19
20
*/
20
 
#include <drizzled/server_includes.h>
21
 
#include <drizzled/sql_select.h>
 
21
 
 
22
 
 
23
#include "mysql_priv.h"
 
24
#include "sql_select.h"
 
25
 
 
26
 
22
27
 
23
28
/*
24
29
  Call given derived table processor (preparing or filling tables)
29
34
    processor           procedure of derived table processing
30
35
 
31
36
  RETURN
32
 
    false  OK
33
 
    true   Error
 
37
    FALSE  OK
 
38
    TRUE   Error
34
39
*/
 
40
 
35
41
bool
36
 
mysql_handle_derived(LEX *lex, bool (*processor)(THD*, LEX*, TableList*))
 
42
mysql_handle_derived(LEX *lex, bool (*processor)(THD*, LEX*, TABLE_LIST*))
37
43
{
38
 
  bool res= false;
 
44
  bool res= FALSE;
39
45
  if (lex->derived_tables)
40
46
  {
41
 
    lex->thd->derived_tables_processing= true;
 
47
    lex->thd->derived_tables_processing= TRUE;
42
48
    for (SELECT_LEX *sl= lex->all_selects_list;
43
49
         sl;
44
50
         sl= sl->next_select_in_list())
45
51
    {
46
 
      for (TableList *cursor= sl->get_table_list();
 
52
      for (TABLE_LIST *cursor= sl->get_table_list();
47
53
           cursor;
48
54
           cursor= cursor->next_local)
49
55
      {
62
68
    }
63
69
  }
64
70
out:
65
 
  lex->thd->derived_tables_processing= false;
 
71
  lex->thd->derived_tables_processing= FALSE;
66
72
  return res;
67
73
}
68
74
 
74
80
    mysql_derived_prepare()
75
81
    thd                 Thread handle
76
82
    lex                 LEX for this thread
77
 
    orig_table_list     TableList for the upper SELECT
 
83
    orig_table_list     TABLE_LIST for the upper SELECT
78
84
 
79
85
  IMPLEMENTATION
80
86
    Derived table is resolved with temporary table.
81
87
 
82
 
    After table creation, the above TableList is updated with a new table.
 
88
    After table creation, the above TABLE_LIST is updated with a new table.
83
89
 
84
90
    This function is called before any command containing derived table
85
91
    is executed.
88
94
    close_thread_tables()
89
95
 
90
96
  RETURN
91
 
    false  OK
92
 
    true   Error
 
97
    FALSE  OK
 
98
    TRUE   Error
93
99
*/
94
100
 
95
 
bool mysql_derived_prepare(THD *thd, LEX *lex __attribute__((unused)),
96
 
                           TableList *orig_table_list)
 
101
bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
97
102
{
98
103
  SELECT_LEX_UNIT *unit= orig_table_list->derived;
99
 
  uint64_t create_options;
100
 
  bool res= false;
 
104
  ulonglong create_options;
 
105
  DBUG_ENTER("mysql_derived_prepare");
 
106
  bool res= FALSE;
101
107
  if (unit)
102
108
  {
103
109
    SELECT_LEX *first_select= unit->first_select();
104
 
    Table *table= 0;
 
110
    TABLE *table= 0;
105
111
    select_union *derived_result;
106
112
 
107
113
    /* prevent name resolving out of derived table */
109
115
      sl->context.outer_context= 0;
110
116
 
111
117
    if (!(derived_result= new select_union))
112
 
      return(true); // out of memory
 
118
      DBUG_RETURN(TRUE); // out of memory
113
119
 
114
120
    // st_select_lex_unit::prepare correctly work for single select
115
121
    if ((res= unit->prepare(thd, derived_result, 0)))
121
127
      Temp table is created so that it hounours if UNION without ALL is to be 
122
128
      processed
123
129
 
124
 
      As 'distinct' parameter we always pass false (0), because underlying
 
130
      As 'distinct' parameter we always pass FALSE (0), because underlying
125
131
      query will control distinct condition by itself. Correct test of
126
132
      distinct underlying query will be is_union &&
127
133
      !unit->union_distinct->next_select() (i.e. it is union and last distinct
128
134
      SELECT is last SELECT of UNION).
129
135
    */
130
 
    if ((res= derived_result->create_result_table(thd, &unit->types, false,
 
136
    if ((res= derived_result->create_result_table(thd, &unit->types, FALSE,
131
137
                                                  create_options,
132
138
                                                  orig_table_list->alias,
133
 
                                                  false)))
 
139
                                                  FALSE)))
134
140
      goto exit;
135
141
 
136
142
    table= derived_result->table;
144
150
    if (res)
145
151
    {
146
152
      if (table)
147
 
          table->free_tmp_table(thd);
 
153
        free_tmp_table(thd, table);
148
154
      delete derived_result;
149
155
    }
150
156
    else
170
176
    }
171
177
  }
172
178
 
173
 
  return(res);
 
179
  DBUG_RETURN(res);
174
180
}
175
181
 
176
182
 
182
188
    thd                 Thread handle
183
189
    lex                 LEX for this thread
184
190
    unit                node that contains all SELECT's for derived tables
185
 
    orig_table_list     TableList for the upper SELECT
 
191
    orig_table_list     TABLE_LIST for the upper SELECT
186
192
 
187
193
  IMPLEMENTATION
188
194
    Derived table is resolved with temporary table. It is created based on the
193
199
    Due to evaluation of LIMIT clause it can not be used at prepared stage.
194
200
 
195
201
  RETURN
196
 
    false  OK
197
 
    true   Error
 
202
    FALSE  OK
 
203
    TRUE   Error
198
204
*/
199
205
 
200
 
bool mysql_derived_filling(THD *thd, LEX *lex, TableList *orig_table_list)
 
206
bool mysql_derived_filling(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
201
207
{
202
 
  Table *table= orig_table_list->table;
 
208
  TABLE *table= orig_table_list->table;
203
209
  SELECT_LEX_UNIT *unit= orig_table_list->derived;
204
 
  bool res= false;
 
210
  bool res= FALSE;
205
211
 
206
212
  /*check that table creation pass without problem and it is derived table */
207
213
  if (table && unit)
222
228
 
223
229
      lex->current_select= first_select;
224
230
      res= mysql_select(thd, &first_select->ref_pointer_array,
225
 
                        (TableList*) first_select->table_list.first,
 
231
                        (TABLE_LIST*) first_select->table_list.first,
226
232
                        first_select->with_wild,
227
233
                        first_select->item_list, first_select->where,
228
234
                        (first_select->order_list.elements+
229
235
                         first_select->group_list.elements),
230
 
                        (order_st *) first_select->order_list.first,
231
 
                        (order_st *) first_select->group_list.first,
232
 
                        first_select->having, (order_st*) NULL,
 
236
                        (ORDER *) first_select->order_list.first,
 
237
                        (ORDER *) first_select->group_list.first,
 
238
                        first_select->having, (ORDER*) NULL,
233
239
                        (first_select->options | thd->options |
234
240
                         SELECT_NO_UNLOCK),
235
241
                        derived_result, unit, first_select);
238
244
    if (!res)
239
245
    {
240
246
      /*
241
 
        Here we entirely fix both TableList and list of SELECT's as
 
247
        Here we entirely fix both TABLE_LIST and list of SELECT's as
242
248
        there were no derived tables
243
249
      */
244
250
      if (derived_result->flush())
245
 
        res= true;
 
251
        res= TRUE;
246
252
 
247
253
      if (!lex->describe)
248
254
        unit->cleanup();