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 
00029 namespace drizzled
00030 {
00031 
00032 bool statement::InsertSelect::execute()
00033 {
00034   TableList *first_table= (TableList *) lex().select_lex.table_list.first;
00035   TableList *all_tables= lex().query_tables;
00036   assert(first_table == all_tables && first_table != 0);
00037   Select_Lex *select_lex= &lex().select_lex;
00038   Select_Lex_Unit *unit= &lex().unit;
00039   select_result *sel_result= NULL;
00040   bool res= false;
00041   bool need_start_waiting= false;
00042 
00043   if (insert_precheck(&session(), all_tables))
00044   {
00045     return true;
00046   }
00047 
00048   /* Don't unlock tables until command is written to binary log */
00049   select_lex->options|= SELECT_NO_UNLOCK;
00050 
00051   unit->set_limit(select_lex);
00052 
00053   if (! (need_start_waiting= not session().wait_if_global_read_lock(false, true)))
00054   {
00055     return true;
00056   }
00057 
00058   if (! (res= session().openTablesLock(all_tables)))
00059   {
00060     DRIZZLE_INSERT_SELECT_START(session().getQueryString()->c_str());
00061     /* Skip first table, which is the table we are inserting in */
00062     TableList *second_table= first_table->next_local;
00063     select_lex->table_list.first= (unsigned char*) second_table;
00064     select_lex->context.table_list=
00065       select_lex->context.first_name_resolution_table= second_table;
00066     res= insert_select_prepare(&session());
00067     if (! res && (sel_result= new select_insert(first_table,
00068                                                 first_table->table,
00069                                                 &lex().field_list,
00070                                                 &lex().update_list,
00071                                                 &lex().value_list,
00072                                                 lex().duplicates,
00073                                                 lex().ignore)))
00074     {
00075       res= handle_select(&session(), 
00076                          &lex(), 
00077                          sel_result, 
00078                          OPTION_SETUP_TABLES_DONE);
00079 
00080       /*
00081          Invalidate the table in the query cache if something changed
00082          after unlocking when changes become visible.
00083          TODO: this is a workaround. right way will be move invalidating in
00084          the unlock procedure.
00085        */
00086       if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT && session().lock)
00087       {
00088         /* INSERT ... SELECT should invalidate only the very first table */
00089         TableList *save_table= first_table->next_local;
00090         first_table->next_local= 0;
00091         first_table->next_local= save_table;
00092       }
00093       delete sel_result;
00094     }
00095     /* revert changes for SP */
00096     select_lex->table_list.first= (unsigned char*) first_table;
00097   }
00098 
00099   /*
00100      Release the protection against the global read lock and wake
00101      everyone, who might want to set a global read lock.
00102    */
00103   session().startWaitingGlobalReadLock();
00104 
00105   return res;
00106 }
00107 
00108 } /* namespace drizzled */