1
/* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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.
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.
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 St, Fifth Floor, Boston, MA 02110-1301 USA */
18
#include "sql_class.h"
19
#include "sql_table.h"
23
Check type of .frm if we are not going to parse it.
25
@param[in] thd The current session.
26
@param[in] path path to FRM file.
27
@param[out] dbt db_type of the table if FRMTYPE_TABLE, otherwise undefined.
29
@retval FRMTYPE_ERROR error
30
@retval FRMTYPE_TABLE table
31
@retval FRMTYPE_VIEW view
34
frm_type_enum dd_frm_type(THD *thd, char *path, enum legacy_db_type *dbt)
37
uchar header[10]; //"TYPE=VIEW\n" it is 10 characters
39
DBUG_ENTER("dd_frm_type");
41
*dbt= DB_TYPE_UNKNOWN;
43
if ((file= mysql_file_open(key_file_frm, path, O_RDONLY | O_SHARE, MYF(0))) < 0)
44
DBUG_RETURN(FRMTYPE_ERROR);
45
error= mysql_file_read(file, (uchar*) header, sizeof(header), MYF(MY_NABP));
46
mysql_file_close(file, MYF(MY_WME));
49
DBUG_RETURN(FRMTYPE_ERROR);
50
if (!strncmp((char*) header, "TYPE=VIEW\n", sizeof(header)))
51
DBUG_RETURN(FRMTYPE_VIEW);
54
This is just a check for DB_TYPE. We'll return default unknown type
55
if the following test is true (arg #3). This should not have effect
56
on return value from this function (default FRMTYPE_TABLE)
58
if (header[0] != (uchar) 254 || header[1] != 1 ||
59
(header[2] != FRM_VER && header[2] != FRM_VER+1 &&
60
(header[2] < FRM_VER+3 || header[2] > FRM_VER+4)))
61
DBUG_RETURN(FRMTYPE_TABLE);
63
*dbt= (enum legacy_db_type) (uint) *(header + 3);
65
/* Probably a table. */
66
DBUG_RETURN(FRMTYPE_TABLE);
71
Given a table name, check type of .frm and legacy table type.
73
@param[in] thd The current session.
74
@param[in] db Table schema.
75
@param[in] table_name Table database.
76
@param[out] table_type handlerton of the table if FRMTYPE_TABLE,
79
@return FALSE if FRMTYPE_TABLE and storage engine found. TRUE otherwise.
82
bool dd_frm_storage_engine(THD *thd, const char *db, const char *table_name,
83
handlerton **table_type)
85
char path[FN_REFLEN + 1];
86
enum legacy_db_type db_type;
87
LEX_STRING db_name = {(char *) db, strlen(db)};
89
/* There should be at least some lock on the table. */
90
DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE, db,
91
table_name, MDL_SHARED));
93
if (check_db_name(&db_name))
95
my_error(ER_WRONG_DB_NAME, MYF(0), db_name.str);
99
if (check_table_name(table_name, strlen(table_name), FALSE))
101
my_error(ER_WRONG_TABLE_NAME, MYF(0), table_name);
105
(void) build_table_filename(path, sizeof(path) - 1, db,
106
table_name, reg_ext, 0);
108
dd_frm_type(thd, path, &db_type);
110
/* Type is unknown if the object is not found or is not a table. */
111
if (db_type == DB_TYPE_UNKNOWN ||
112
!(*table_type= ha_resolve_by_legacy_type(thd, db_type)))
114
my_error(ER_NO_SUCH_TABLE, MYF(0), db, table_name);
123
Given a table name, check if the storage engine for the
124
table referred by this name supports an option 'flag'.
125
Return an error if the table does not exist or is not a
128
@pre Any metadata lock on the table.
130
@param[in] thd The current session.
131
@param[in] db Table schema.
132
@param[in] table_name Table database.
133
@param[in] flag The option to check.
134
@param[out] yes_no The result. Undefined if error.
137
bool dd_check_storage_engine_flag(THD *thd,
138
const char *db, const char *table_name,
139
uint32 flag, bool *yes_no)
141
handlerton *table_type;
143
if (dd_frm_storage_engine(thd, db, table_name, &table_type))
146
*yes_no= ha_check_storage_engine_flag(table_type, flag);
153
Regenerate a metadata locked table.
155
@param thd Thread context.
156
@param db Name of the database to which the table belongs to.
157
@param name Table name.
159
@retval FALSE Success.
163
bool dd_recreate_table(THD *thd, const char *db, const char *table_name)
166
HA_CREATE_INFO create_info;
167
char path[FN_REFLEN + 1];
168
DBUG_ENTER("dd_recreate_table");
170
/* There should be a exclusive metadata lock on the table. */
171
DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE, db, table_name,
174
memset(&create_info, 0, sizeof(create_info));
176
/* Create a path to the table, but without a extension. */
177
build_table_filename(path, sizeof(path) - 1, db, table_name, "", 0);
179
/* Attempt to reconstruct the table. */
180
error= ha_create_table(thd, path, db, table_name, &create_info, TRUE);