Drizzled Public API Documentation

insert_select.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <drizzled/show.h>
00023 #include <drizzled/lock.h>
00024 #include <drizzled/session.h>
00025 #include <drizzled/probes.h>
00026 #include <drizzled/statement/insert_select.h>
00027 #include <drizzled/select_insert.h>
00028 #include <drizzled/sql_lex.h>
00029 #include <drizzled/open_tables_state.h>
00030 
00031 namespace drizzled {
00032 
00033 bool statement::InsertSelect::execute()
00034 {
00035   TableList *first_table= (TableList *) lex().select_lex.table_list.first;
00036   TableList *all_tables= lex().query_tables;
00037   assert(first_table == all_tables && first_table != 0);
00038   Select_Lex *select_lex= &lex().select_lex;
00039 
00040   if (insert_precheck(&session(), all_tables))
00041   {
00042     return true;
00043   }
00044 
00045   /* Don't unlock tables until command is written to binary log */
00046   select_lex->options|= SELECT_NO_UNLOCK;
00047 
00048   lex().unit.set_limit(select_lex);
00049 
00050   if (session().wait_if_global_read_lock(false, true))
00051   {
00052     return true;
00053   }
00054 
00055   bool res;
00056   if (! (res= session().openTablesLock(all_tables)))
00057   {
00058     DRIZZLE_INSERT_SELECT_START(session().getQueryString()->c_str());
00059     /* Skip first table, which is the table we are inserting in */
00060     TableList *second_table= first_table->next_local;
00061     select_lex->table_list.first= (unsigned char*) second_table;
00062     select_lex->context.table_list= select_lex->context.first_name_resolution_table= second_table;
00063     res= insert_select_prepare(&session());
00064     if (not res)
00065     {
00066       select_insert sel_result(first_table, first_table->table, &lex().field_list, &lex().update_list, &lex().value_list, lex().duplicates, lex().ignore);
00067       res= handle_select(&session(), &lex(), &sel_result, OPTION_SETUP_TABLES_DONE);
00068 
00069       /*
00070          Invalidate the table in the query cache if something changed
00071          after unlocking when changes become visible.
00072          TODO: this is a workaround. right way will be move invalidating in
00073          the unlock procedure.
00074        */
00075       if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT && session().open_tables.lock)
00076       {
00077         /* INSERT ... SELECT should invalidate only the very first table */
00078         TableList *save_table= first_table->next_local;
00079         first_table->next_local= 0;
00080         first_table->next_local= save_table;
00081       }
00082     }
00083     /* revert changes for SP */
00084     select_lex->table_list.first= (unsigned char*) first_table;
00085   }
00086 
00087   /*
00088      Release the protection against the global read lock and wake
00089      everyone, who might want to set a global read lock.
00090    */
00091   session().startWaitingGlobalReadLock();
00092 
00093   return res;
00094 }
00095 
00096 } /* namespace drizzled */