00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <drizzled/sql_lex.h>
00023 #include <drizzled/select_union.h>
00024 #include <drizzled/sql_select.h>
00025 #include <drizzled/session.h>
00026 #include <drizzled/open_tables_state.h>
00027
00028 namespace drizzled {
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 bool handle_derived(LEX *lex, bool (*processor)(Session*, LEX*, TableList*))
00043 {
00044 bool res= false;
00045 if (lex->derived_tables)
00046 {
00047 lex->session->derived_tables_processing= true;
00048 for (Select_Lex *sl= lex->all_selects_list; sl; sl= sl->next_select_in_list())
00049 {
00050 for (TableList *cursor= sl->get_table_list(); cursor; cursor= cursor->next_local)
00051 {
00052 if ((res= (*processor)(lex->session, lex, cursor)))
00053 goto out;
00054 }
00055 if (lex->describe)
00056 {
00057
00058
00059
00060
00061 sl->uncacheable.set(UNCACHEABLE_EXPLAIN);
00062 sl->master_unit()->uncacheable.set(UNCACHEABLE_EXPLAIN);
00063 }
00064 }
00065 }
00066 out:
00067 lex->session->derived_tables_processing= false;
00068 return res;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 bool derived_prepare(Session *session, LEX *, TableList *orig_table_list)
00096 {
00097 Select_Lex_Unit *unit= orig_table_list->derived;
00098 uint64_t create_options;
00099 bool res= false;
00100 if (unit)
00101 {
00102 Select_Lex *first_select= unit->first_select();
00103 Table *table= 0;
00104 select_union *derived_result;
00105
00106
00107 for (Select_Lex *sl= first_select; sl; sl= sl->next_select())
00108 sl->context.outer_context= 0;
00109
00110 derived_result= new select_union;
00111
00112
00113 if ((res= unit->prepare(session, derived_result, 0)))
00114 goto exit;
00115
00116 create_options= (first_select->options | session->options | TMP_TABLE_ALL_COLUMNS);
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 if ((res= derived_result->create_result_table(session, &unit->types, false,
00128 create_options,
00129 orig_table_list->alias)))
00130 goto exit;
00131
00132 table= derived_result->table;
00133
00134 exit:
00135
00136
00137
00138
00139
00140 if (res)
00141 {
00142 table= 0;
00143 delete derived_result;
00144 }
00145 else
00146 {
00147 orig_table_list->derived_result= derived_result;
00148 orig_table_list->table= table;
00149 orig_table_list->setTableName(table->getShare()->getTableName());
00150 table->derived_select_number= first_select->select_number;
00151 orig_table_list->setSchemaName("");
00152
00153 table->cursor->info(HA_STATUS_VARIABLE);
00154
00155 table->setNext(session->open_tables.getDerivedTables());
00156 session->open_tables.setDerivedTables(table);
00157 }
00158 }
00159
00160 return res;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 bool derived_filling(Session *session, LEX *lex, TableList *orig_table_list)
00186 {
00187 Table *table= orig_table_list->table;
00188 Select_Lex_Unit *unit= orig_table_list->derived;
00189 bool res= false;
00190
00191
00192 if (table && unit)
00193 {
00194 Select_Lex *first_select= unit->first_select();
00195 select_union *derived_result= orig_table_list->derived_result;
00196 Select_Lex *save_current_select= lex->current_select;
00197 if (unit->is_union())
00198 {
00199
00200 res= unit->exec();
00201 }
00202 else
00203 {
00204 unit->set_limit(first_select);
00205 if (unit->select_limit_cnt == HA_POS_ERROR)
00206 first_select->options&= ~OPTION_FOUND_ROWS;
00207
00208 lex->current_select= first_select;
00209 res= select_query(session, &first_select->ref_pointer_array,
00210 (TableList*) first_select->table_list.first,
00211 first_select->with_wild,
00212 first_select->item_list, first_select->where,
00213 (first_select->order_list.elements+
00214 first_select->group_list.elements),
00215 (Order *) first_select->order_list.first,
00216 (Order *) first_select->group_list.first,
00217 first_select->having,
00218 (first_select->options | session->options | SELECT_NO_UNLOCK),
00219 derived_result, unit, first_select);
00220 }
00221
00222 if (! res)
00223 {
00224
00225
00226
00227
00228 if (derived_result->flush())
00229 res= true;
00230
00231 if (! lex->describe)
00232 unit->cleanup();
00233 }
00234 else
00235 unit->cleanup();
00236 lex->current_select= save_current_select;
00237 }
00238 return res;
00239 }
00240
00241 }