~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/replace_select.cc

  • Committer: Mark Atwood
  • Date: 2009-03-04 01:02:00 UTC
  • mto: (968.2.20 mordred)
  • mto: This revision was merged to the branch mainline in revision 971.
  • Revision ID: me@mark.atwood.name-20090304010200-t1n4xxdoil2yae9a
add gearman logging plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
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, Inc.
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
 
 
21
 
#include <config.h>
22
 
#include <drizzled/show.h>
23
 
#include <drizzled/lock.h>
24
 
#include <drizzled/session.h>
25
 
#include <drizzled/statement/replace_select.h>
26
 
#include <drizzled/select_insert.h>
27
 
 
28
 
namespace drizzled
29
 
{
30
 
 
31
 
bool statement::ReplaceSelect::execute()
32
 
{
33
 
  TableList *first_table= (TableList *) getSession()->getLex()->select_lex.table_list.first;
34
 
  TableList *all_tables= getSession()->getLex()->query_tables;
35
 
  assert(first_table == all_tables && first_table != 0);
36
 
  Select_Lex *select_lex= &getSession()->getLex()->select_lex;
37
 
  Select_Lex_Unit *unit= &getSession()->getLex()->unit;
38
 
  select_result *sel_result= NULL;
39
 
  bool res;
40
 
 
41
 
  if (insert_precheck(getSession(), all_tables))
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
 
 
51
 
  if (getSession()->wait_if_global_read_lock(false, true))
52
 
  {
53
 
    return true;
54
 
  }
55
 
 
56
 
  if (! (res= getSession()->openTablesLock(all_tables)))
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;
63
 
    res= insert_select_prepare(getSession());
64
 
    if (! res && (sel_result= new select_insert(first_table,
65
 
                                                first_table->table,
66
 
                                                &getSession()->getLex()->field_list,
67
 
                                                &getSession()->getLex()->update_list,
68
 
                                                &getSession()->getLex()->value_list,
69
 
                                                getSession()->getLex()->duplicates,
70
 
                                                getSession()->getLex()->ignore)))
71
 
    {
72
 
      res= handle_select(getSession(),
73
 
                         getSession()->getLex(),
74
 
                         sel_result,
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 &&
83
 
          getSession()->lock)
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
 
   */
100
 
  getSession()->startWaitingGlobalReadLock();
101
 
 
102
 
  return res;
103
 
}
104
 
 
105
 
} /* namespace drizzled */
106