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
00027 namespace drizzled
00028 {
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 if (!(derived_result= new select_union))
00111 return(true);
00112
00113
00114 if ((res= unit->prepare(session, derived_result, 0)))
00115 goto exit;
00116
00117 create_options= (first_select->options | session->options | TMP_TABLE_ALL_COLUMNS);
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 if ((res= derived_result->create_result_table(session, &unit->types, false,
00129 create_options,
00130 orig_table_list->alias)))
00131 goto exit;
00132
00133 table= derived_result->table;
00134
00135 exit:
00136
00137
00138
00139
00140
00141 if (res)
00142 {
00143 if (table)
00144 {
00145 table= 0;
00146 }
00147 delete derived_result;
00148 }
00149 else
00150 {
00151 orig_table_list->derived_result= derived_result;
00152 orig_table_list->table= table;
00153 orig_table_list->setTableName(const_cast<char *>(table->getShare()->getTableName()));
00154 orig_table_list->table_name_length= table->getShare()->getTableNameSize();
00155 table->derived_select_number= first_select->select_number;
00156 orig_table_list->setSchemaName((char *)"");
00157 orig_table_list->db_length= 0;
00158
00159 table->cursor->info(HA_STATUS_VARIABLE);
00160
00161 table->setNext(session->getDerivedTables());
00162 session->setDerivedTables(table);
00163 }
00164 }
00165
00166 return(res);
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 bool derived_filling(Session *session, LEX *lex, TableList *orig_table_list)
00192 {
00193 Table *table= orig_table_list->table;
00194 Select_Lex_Unit *unit= orig_table_list->derived;
00195 bool res= false;
00196
00197
00198 if (table && unit)
00199 {
00200 Select_Lex *first_select= unit->first_select();
00201 select_union *derived_result= orig_table_list->derived_result;
00202 Select_Lex *save_current_select= lex->current_select;
00203 if (unit->is_union())
00204 {
00205
00206 res= unit->exec();
00207 }
00208 else
00209 {
00210 unit->set_limit(first_select);
00211 if (unit->select_limit_cnt == HA_POS_ERROR)
00212 first_select->options&= ~OPTION_FOUND_ROWS;
00213
00214 lex->current_select= first_select;
00215 res= select_query(session, &first_select->ref_pointer_array,
00216 (TableList*) first_select->table_list.first,
00217 first_select->with_wild,
00218 first_select->item_list, first_select->where,
00219 (first_select->order_list.elements+
00220 first_select->group_list.elements),
00221 (Order *) first_select->order_list.first,
00222 (Order *) first_select->group_list.first,
00223 first_select->having,
00224 (first_select->options | session->options | SELECT_NO_UNLOCK),
00225 derived_result, unit, first_select);
00226 }
00227
00228 if (! res)
00229 {
00230
00231
00232
00233
00234 if (derived_result->flush())
00235 res= true;
00236
00237 if (! lex->describe)
00238 unit->cleanup();
00239 }
00240 else
00241 unit->cleanup();
00242 lex->current_select= save_current_select;
00243 }
00244 return res;
00245 }
00246
00247 }