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/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
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
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
00082
00083
00084
00085
00086 if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT && session().lock)
00087 {
00088
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
00096 select_lex->table_list.first= (unsigned char*) first_table;
00097 }
00098
00099
00100
00101
00102
00103 session().startWaitingGlobalReadLock();
00104
00105 return res;
00106 }
00107
00108 }