Drizzled Public API Documentation

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