1/* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
15
16#include "ma_fulltext.h"
17#include "trnman_public.h"
18
19/**
20 @brief drops (deletes) a table
21
22 @param name table's name
23
24 @return Operation status
25 @retval 0 ok
26 @retval 1 error
27*/
28
29int maria_delete_table(const char *name)
30{
31 MARIA_HA *info;
32 myf sync_dir;
33 DBUG_ENTER("maria_delete_table");
34
35#ifdef EXTRA_DEBUG
36 _ma_check_table_is_closed(name,"delete");
37#endif
38 /** @todo LOCK take X-lock on table */
39 /*
40 We need to know if this table is transactional.
41 Unfortunately it is necessary to open the table just to check this. We use
42 'open_for_repair' to be able to open even a crashed table.
43 */
44 if (!(info= maria_open(name, O_RDONLY, HA_OPEN_FOR_REPAIR)))
45 {
46 sync_dir= 0;
47 }
48 else
49 {
50 sync_dir= (info->s->now_transactional && !info->s->temporary &&
51 !maria_in_recovery) ?
52 MY_SYNC_DIR : 0;
53 /* Remove history for table */
54 _ma_reset_state(info);
55 maria_close(info);
56 }
57
58 if (sync_dir)
59 {
60 /*
61 For this log record to be of any use for Recovery, we need the upper
62 MySQL layer to be crash-safe in DDLs.
63 For now this record can serve when we apply logs to a backup, so we sync
64 it.
65 */
66 LSN lsn;
67 LEX_CUSTRING log_array[TRANSLOG_INTERNAL_PARTS + 1];
68 log_array[TRANSLOG_INTERNAL_PARTS + 0].str= (uchar*)name;
69 log_array[TRANSLOG_INTERNAL_PARTS + 0].length= strlen(name) + 1;
70 if (unlikely(translog_write_record(&lsn, LOGREC_REDO_DROP_TABLE,
71 &dummy_transaction_object, NULL,
72 (translog_size_t)
73 log_array[TRANSLOG_INTERNAL_PARTS +
74 0].length,
75 sizeof(log_array)/sizeof(log_array[0]),
76 log_array, NULL, NULL) ||
77 translog_flush(lsn)))
78 DBUG_RETURN(1);
79 }
80
81 DBUG_RETURN(maria_delete_table_files(name, 0, sync_dir));
82}
83
84
85int maria_delete_table_files(const char *name, my_bool temporary, myf sync_dir)
86{
87 DBUG_ENTER("maria_delete_table_files");
88
89 if (mysql_file_delete_with_symlink(key_file_kfile, name, MARIA_NAME_IEXT, MYF(MY_WME | sync_dir)) ||
90 mysql_file_delete_with_symlink(key_file_dfile, name, MARIA_NAME_DEXT, MYF(MY_WME | sync_dir)))
91 DBUG_RETURN(my_errno);
92
93 if (!temporary)
94 {
95 mysql_file_delete_with_symlink(key_file_dfile, name, ".TMD", MYF(0));
96 mysql_file_delete_with_symlink(key_file_dfile, name, ".OLD", MYF(0));
97 }
98 DBUG_RETURN(0);
99}
100