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