~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
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
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>
2154.2.17 by Brian Aker
Additional removal of session
26
#include <drizzled/select_insert.h>
2239.1.10 by Olaf van der Spek
Refactor includes
27
#include <drizzled/sql_lex.h>
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
28
2239.1.10 by Olaf van der Spek
Refactor includes
29
namespace drizzled {
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
30
31
bool statement::ReplaceSelect::execute()
32
{
2224.2.8 by Olaf van der Spek
Statement::lex()
33
  TableList *first_table= (TableList *) lex().select_lex.table_list.first;
34
  TableList *all_tables= lex().query_tables;
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
35
  assert(first_table == all_tables && first_table != 0);
2224.2.8 by Olaf van der Spek
Statement::lex()
36
  Select_Lex *select_lex= &lex().select_lex;
37
  Select_Lex_Unit *unit= &lex().unit;
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
38
  select_result *sel_result= NULL;
1238.1.3 by Brian Aker
ICC fixes/fix for solaris.
39
  bool res;
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
40
2227.4.2 by Olaf van der Spek
Statement::session()
41
  if (insert_precheck(&session(), all_tables))
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
42
  {
43
    return true;
44
  }
45
46
  /* Don't unlock tables until command is written to binary log */
47
  select_lex->options|= SELECT_NO_UNLOCK;
48
49
  unit->set_limit(select_lex);
50
2227.4.1 by Olaf van der Spek
Statement::session()
51
  if (session().wait_if_global_read_lock(false, true))
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
52
  {
53
    return true;
54
  }
55
2227.4.1 by Olaf van der Spek
Statement::session()
56
  if (! (res= session().openTablesLock(all_tables)))
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
57
  {
58
    /* Skip first table, which is the table we are inserting in */
59
    TableList *second_table= first_table->next_local;
60
    select_lex->table_list.first= (unsigned char*) second_table;
61
    select_lex->context.table_list=
62
      select_lex->context.first_name_resolution_table= second_table;
2227.4.2 by Olaf van der Spek
Statement::session()
63
    res= insert_select_prepare(&session());
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
64
    if (! res && (sel_result= new select_insert(first_table,
65
                                                first_table->table,
2224.2.8 by Olaf van der Spek
Statement::lex()
66
                                                &lex().field_list,
67
                                                &lex().update_list,
68
                                                &lex().value_list,
69
                                                lex().duplicates,
70
                                                lex().ignore)))
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
71
    {
2227.4.2 by Olaf van der Spek
Statement::session()
72
      res= handle_select(&session(),
2224.2.8 by Olaf van der Spek
Statement::lex()
73
                         &lex(),
1240.1.1 by Brian Aker
ICC patch.
74
                         sel_result,
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
75
                         OPTION_SETUP_TABLES_DONE);
76
      /*
77
         Invalidate the table in the query cache if something changed
78
         after unlocking when changes become visible.
79
         TODO: this is a workaround. right way will be move invalidating in
80
         the unlock procedure.
81
       */
82
      if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT &&
2227.4.1 by Olaf van der Spek
Statement::session()
83
          session().lock)
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
84
      {
85
        /* INSERT ... SELECT should invalidate only the very first table */
86
        TableList *save_table= first_table->next_local;
87
        first_table->next_local= 0;
88
        first_table->next_local= save_table;
89
      }
90
      delete sel_result;
91
    }
92
    /* revert changes for SP */
93
    select_lex->table_list.first= (unsigned char*) first_table;
94
  }
95
96
  /*
97
     Release the protection against the global read lock and wake
98
     everyone, who might want to set a global read lock.
99
   */
2227.4.1 by Olaf van der Spek
Statement::session()
100
  session().startWaitingGlobalReadLock();
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
101
102
  return res;
103
}
1130.3.12 by Monty Taylor
using namespace drizzled; to namespace drizzled { in statement/
104
105
} /* namespace drizzled */
106