1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <drizzled/show.h>
23
#include <drizzled/lock.h>
24
#include <drizzled/session.h>
25
#include <drizzled/probes.h>
26
#include <drizzled/statement/insert_select.h>
27
#include <drizzled/select_insert.h>
32
bool statement::InsertSelect::execute()
34
TableList *first_table= (TableList *) getSession()->getLex()->select_lex.table_list.first;
35
TableList *all_tables= getSession()->getLex()->query_tables;
36
assert(first_table == all_tables && first_table != 0);
37
Select_Lex *select_lex= &getSession()->getLex()->select_lex;
38
Select_Lex_Unit *unit= &getSession()->getLex()->unit;
39
select_result *sel_result= NULL;
41
bool need_start_waiting= false;
43
if (insert_precheck(getSession(), all_tables))
48
/* Don't unlock tables until command is written to binary log */
49
select_lex->options|= SELECT_NO_UNLOCK;
51
unit->set_limit(select_lex);
53
if (! (need_start_waiting= not getSession()->wait_if_global_read_lock(false, true)))
58
if (! (res= getSession()->openTablesLock(all_tables)))
60
DRIZZLE_INSERT_SELECT_START(getSession()->getQueryString()->c_str());
61
/* Skip first table, which is the table we are inserting in */
62
TableList *second_table= first_table->next_local;
63
select_lex->table_list.first= (unsigned char*) second_table;
64
select_lex->context.table_list=
65
select_lex->context.first_name_resolution_table= second_table;
66
res= insert_select_prepare(getSession());
67
if (! res && (sel_result= new select_insert(first_table,
69
&getSession()->getLex()->field_list,
70
&getSession()->getLex()->update_list,
71
&getSession()->getLex()->value_list,
72
getSession()->getLex()->duplicates,
73
getSession()->getLex()->ignore)))
75
res= handle_select(getSession(),
76
getSession()->getLex(),
78
OPTION_SETUP_TABLES_DONE);
81
Invalidate the table in the query cache if something changed
82
after unlocking when changes become visible.
83
TODO: this is a workaround. right way will be move invalidating in
86
if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT && getSession()->lock)
88
/* INSERT ... SELECT should invalidate only the very first table */
89
TableList *save_table= first_table->next_local;
90
first_table->next_local= 0;
91
first_table->next_local= save_table;
95
/* revert changes for SP */
96
select_lex->table_list.first= (unsigned char*) first_table;
100
Release the protection against the global read lock and wake
101
everyone, who might want to set a global read lock.
103
getSession()->startWaitingGlobalReadLock();
108
} /* namespace drizzled */