19 #include "probes_mysql.h"
20 #include "sql_parse.h"
38 SELECT_LEX_UNIT fake_unit;
48 virtual bool is_open()
const {
return table != 0; }
49 virtual int open(
JOIN *join __attribute__((unused)));
50 virtual void fetch(ulong num_rows);
67 select_result *result;
71 :result(result_arg), materialized_cursor(0) {}
94 bool mysql_open_cursor(THD *thd, select_result *result,
97 PSI_statement_locker *parent_locker;
98 select_result *save_result;
105 save_result= lex->result;
107 lex->result= result_materialize;
109 MYSQL_QUERY_EXEC_START(thd->query(),
111 (
char *) (thd->db ? thd->db :
""),
112 &thd->security_ctx->priv_user[0],
113 (
char *) thd->security_ctx->host_or_ip,
115 parent_locker= thd->m_statement_psi;
116 thd->m_statement_psi= NULL;
118 thd->m_statement_psi= parent_locker;
119 MYSQL_QUERY_EXEC_DONE(rc);
121 lex->result= save_result;
136 if (result_materialize->materialized_cursor)
139 result_materialize->abort_result_set();
141 delete result_materialize->materialized_cursor;
147 if (result_materialize->materialized_cursor)
150 result_materialize->materialized_cursor;
158 if ((rc= materialized_cursor->open(0)))
160 delete materialized_cursor;
164 *pcursor= materialized_cursor;
165 thd->stmt_arena->cleanup_stmt();
169 delete result_materialize;
177 Server_side_cursor::~Server_side_cursor()
182 void Server_side_cursor::operator
delete(
void *ptr,
size_t size)
185 MEM_ROOT own_root= *cursor->mem_root;
187 DBUG_ENTER(
"Server_side_cursor::operator delete");
195 free_root(&own_root, MYF(0));
204 Materialized_cursor::Materialized_cursor(select_result *result_arg,
212 fake_unit.init_query();
213 fake_unit.thd=
table->in_use;
227 THD *thd,
List<Item> &send_result_set_metadata)
229 Query_arena backup_arena;
236 thd->set_n_backup_active_arena(
this, &backup_arena);
238 if ((rc= table->fill_item_list(&item_list)))
241 DBUG_ASSERT(send_result_set_metadata.elements == item_list.elements);
250 while ((item_dst= it_dst++, item_org= it_org++))
254 item_org->make_field(&send_field);
256 ident->db_name= thd->strdup(send_field.db_name);
257 ident->table_name= thd->strdup(send_field.table_name);
265 rc=
result->send_result_set_metadata(item_list, Protocol::SEND_NUM_ROWS);
268 thd->restore_active_arena(
this, &backup_arena);
270 return rc || thd->is_error();
274 int Materialized_cursor::open(
JOIN *join __attribute__((unused)))
276 THD *thd= fake_unit.thd;
278 Query_arena backup_arena;
280 thd->set_n_backup_active_arena(
this, &backup_arena);
284 rc=
result->prepare(item_list, &fake_unit);
288 thd->restore_active_arena(
this, &backup_arena);
294 thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
299 result->abort_result_set();
320 THD *thd= table->in_use;
324 for (fetch_limit+= num_rows; fetch_count < fetch_limit; fetch_count++)
326 if ((res= table->file->
ha_rnd_next(table->record[0])))
333 if (
result->send_data(item_list))
339 thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
342 case HA_ERR_END_OF_FILE:
343 thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
355 void Materialized_cursor::close()
366 mem_root= &main_mem_root;
368 free_tmp_table(table->in_use, table);
373 Materialized_cursor::~Materialized_cursor()
384 bool Select_materialize::send_result_set_metadata(
List<Item> &list, uint
flags)
386 DBUG_ASSERT(
table == 0);
391 List<Item> *column_types= (unit->first_select()->parent_lex->proc_analyse ?
392 &list : unit->get_field_list());
393 if (create_result_table(unit->thd, column_types,
395 thd->variables.option_bits | TMP_TABLE_ALL_COLUMNS,
399 materialized_cursor=
new (&
table->mem_root)
402 if (!materialized_cursor)
411 delete materialized_cursor;
413 materialized_cursor= 0;