~drizzle-trunk/drizzle/development

1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2009 Sun Microsystems
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
21
#include "config.h"
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
22
#include <drizzled/show.h>
23
#include <drizzled/lock.h>
24
#include <drizzled/session.h>
25
#include <drizzled/statement/replace_select.h>
26
1130.3.12 by Monty Taylor
using namespace drizzled; to namespace drizzled { in statement/
27
namespace drizzled
28
{
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
29
30
bool statement::ReplaceSelect::execute()
31
{
32
  TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
33
  TableList *all_tables= session->lex->query_tables;
34
  assert(first_table == all_tables && first_table != 0);
35
  Select_Lex *select_lex= &session->lex->select_lex;
36
  Select_Lex_Unit *unit= &session->lex->unit;
37
  select_result *sel_result= NULL;
1238.1.3 by Brian Aker
ICC fixes/fix for solaris.
38
  bool res;
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
39
40
  if (insert_precheck(session, all_tables))
41
  {
42
    return true;
43
  }
44
45
  /* Don't unlock tables until command is written to binary log */
46
  select_lex->options|= SELECT_NO_UNLOCK;
47
48
  unit->set_limit(select_lex);
49
1240.1.1 by Brian Aker
ICC patch.
50
  if (wait_if_global_read_lock(session, false, true))
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
51
  {
52
    return true;
53
  }
54
55
  if (! (res= session->openTablesLock(all_tables)))
56
  {
57
    /* Skip first table, which is the table we are inserting in */
58
    TableList *second_table= first_table->next_local;
59
    select_lex->table_list.first= (unsigned char*) second_table;
60
    select_lex->context.table_list=
61
      select_lex->context.first_name_resolution_table= second_table;
62
    res= mysql_insert_select_prepare(session);
63
    if (! res && (sel_result= new select_insert(first_table,
64
                                                first_table->table,
65
                                                &session->lex->field_list,
66
                                                &session->lex->update_list,
67
                                                &session->lex->value_list,
68
                                                session->lex->duplicates,
69
                                                session->lex->ignore)))
70
    {
1240.1.1 by Brian Aker
ICC patch.
71
      res= handle_select(session,
72
                         session->lex,
73
                         sel_result,
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
74
                         OPTION_SETUP_TABLES_DONE);
75
      /*
76
         Invalidate the table in the query cache if something changed
77
         after unlocking when changes become visible.
78
         TODO: this is a workaround. right way will be move invalidating in
79
         the unlock procedure.
80
       */
81
      if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT &&
82
          session->lock)
83
      {
84
        /* INSERT ... SELECT should invalidate only the very first table */
85
        TableList *save_table= first_table->next_local;
86
        first_table->next_local= 0;
87
        first_table->next_local= save_table;
88
      }
89
      delete sel_result;
90
    }
91
    /* revert changes for SP */
92
    select_lex->table_list.first= (unsigned char*) first_table;
93
  }
94
95
  /*
96
     Release the protection against the global read lock and wake
97
     everyone, who might want to set a global read lock.
98
   */
99
  start_waiting_global_read_lock(session);
100
101
  return res;
102
}
1130.3.12 by Monty Taylor
using namespace drizzled; to namespace drizzled { in statement/
103
104
} /* namespace drizzled */
105