drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
insert_select.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2009 Sun Microsystems, Inc.
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <config.h>
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>
28#include <drizzled/sql_lex.h>
29#include <drizzled/open_tables_state.h>
30
31namespace drizzled {
32
34{
35 TableList *first_table= (TableList *) lex().select_lex.table_list.first;
36 TableList *all_tables= lex().query_tables;
37 assert(first_table == all_tables && first_table != 0);
38 Select_Lex *select_lex= &lex().select_lex;
39
40 if (insert_precheck(&session(), all_tables))
41 {
42 return true;
43 }
44
45 /* Don't unlock tables until command is written to binary log */
46 select_lex->options|= SELECT_NO_UNLOCK;
47
48 lex().unit.set_limit(select_lex);
49
50 if (session().wait_if_global_read_lock(false, true))
51 {
52 return true;
53 }
54
55 bool res;
56 if (! (res= session().openTablesLock(all_tables)))
57 {
58 DRIZZLE_INSERT_SELECT_START(session().getQueryString()->c_str());
59 /* Skip first table, which is the table we are inserting in */
60 TableList *second_table= first_table->next_local;
61 select_lex->table_list.first= (unsigned char*) second_table;
62 select_lex->context.table_list= select_lex->context.first_name_resolution_table= second_table;
63 res= insert_select_prepare(&session());
64 if (not res)
65 {
66 select_insert sel_result(first_table, first_table->table, &lex().field_list, &lex().update_list, &lex().value_list, lex().duplicates, lex().ignore);
67 res= handle_select(&session(), &lex(), &sel_result, OPTION_SETUP_TABLES_DONE);
68
69 /*
70 Invalidate the table in the query cache if something changed
71 after unlocking when changes become visible.
72 TODO: this is a workaround. right way will be move invalidating in
73 the unlock procedure.
74 */
75 if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT && session().open_tables.lock)
76 {
77 /* INSERT ... SELECT should invalidate only the very first table */
78 TableList *save_table= first_table->next_local;
79 first_table->next_local= 0;
80 first_table->next_local= save_table;
81 }
82 }
83 /* revert changes for SP */
84 select_lex->table_list.first= (unsigned char*) first_table;
85 }
86
87 /*
88 Release the protection against the global read lock and wake
89 everyone, who might want to set a global read lock.
90 */
91 session().startWaitingGlobalReadLock();
92
93 return res;
94}
95
96} /* namespace drizzled */
TableList * next_local
Definition table_list.h:107
Table * table
opened table
Definition table_list.h:145
Select_Lex * select_lex
Definition table_list.h:208
bool handle_select(Session *session, LEX *lex, select_result *result, uint64_t setup_tables_done_option)
bool insert_precheck(Session *session, TableList *)
TODO: Rename this file - func.h is stupid.