~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>
#include <drizzled/show.h>
#include <drizzled/session.h>
#include <drizzled/lock.h>
#include <drizzled/statement/drop_table.h>
#include <drizzled/sql_table.h>

namespace drizzled
{


/*
 delete (drop) tables.

  SYNOPSIS
   rm_table()
   session			Thread handle
   tables		List of tables to delete
   if_exists		If 1, don't give error if one table doesn't exists

  NOTES
    Will delete all tables that can be deleted and give a compact error
    messages for tables that could not be deleted.
    If a table is in use, we will wait for all users to free the table
    before dropping it

    Wait if global_read_lock (FLUSH TABLES WITH READ LOCK) is set, but
    not if under LOCK TABLES.

  RETURN
    false OK.  In this case ok packet is sent to user
    true  Error

*/

static bool rm_table(Session *session, TableList *tables, bool if_exists, bool drop_temporary)
{
  bool error, need_start_waiting= false;

  /* mark for close and remove all cached entries */

  if (not drop_temporary)
  {
    if (not (need_start_waiting= not session->wait_if_global_read_lock(false, true)))
      return true;
  }

  /*
    Acquire table::Cache::singleton().mutex() after wait_if_global_read_lock(). If we would hold
    table::Cache::singleton().mutex() during wait_if_global_read_lock(), other threads could not
    close their tables. This would make a pretty deadlock.
  */
  error= rm_table_part2(session, tables, if_exists, drop_temporary);

  if (need_start_waiting)
  {
    session->startWaitingGlobalReadLock();
  }

  if (error)
    return true;

  session->my_ok();

  return false;
}

bool statement::DropTable::execute()
{
  TableList *first_table= (TableList *) getSession()->getLex()->select_lex.table_list.first;
  TableList *all_tables= getSession()->getLex()->query_tables;
  assert(first_table == all_tables && first_table != 0);

  if (not drop_temporary)
  {
    if (getSession()->inTransaction())
    {
      my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
      return true;
    }
  }

  /* DDL and binlog write order protected by table::Cache::singleton().mutex() */

  return rm_table(getSession(), first_table, drop_if_exists, drop_temporary);
}

} /* namespace drizzled */