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