00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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
00078
00079
00080
00081
00082 if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT &&
00083 session().lock)
00084 {
00085
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
00093 select_lex->table_list.first= (unsigned char*) first_table;
00094 }
00095
00096
00097
00098
00099
00100 session().startWaitingGlobalReadLock();
00101
00102 return res;
00103 }
00104
00105 }
00106