MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
datadict.cc
1 /* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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 St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "datadict.h"
17 #include "sql_priv.h"
18 #include "sql_class.h"
19 #include "sql_table.h"
20 
21 
34 frm_type_enum dd_frm_type(THD *thd, char *path, enum legacy_db_type *dbt)
35 {
36  File file;
37  uchar header[10]; //"TYPE=VIEW\n" it is 10 characters
38  size_t error;
39  DBUG_ENTER("dd_frm_type");
40 
41  *dbt= DB_TYPE_UNKNOWN;
42 
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));
47 
48  if (error)
49  DBUG_RETURN(FRMTYPE_ERROR);
50  if (!strncmp((char*) header, "TYPE=VIEW\n", sizeof(header)))
51  DBUG_RETURN(FRMTYPE_VIEW);
52 
53  /*
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)
57  */
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);
62 
63  *dbt= (enum legacy_db_type) (uint) *(header + 3);
64 
65  /* Probably a table. */
66  DBUG_RETURN(FRMTYPE_TABLE);
67 }
68 
69 
82 bool dd_frm_storage_engine(THD *thd, const char *db, const char *table_name,
83  handlerton **table_type)
84 {
85  char path[FN_REFLEN + 1];
86  enum legacy_db_type db_type;
87  LEX_STRING db_name = {(char *) db, strlen(db)};
88 
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));
92 
93  if (check_and_convert_db_name(&db_name, FALSE) != IDENT_NAME_OK)
94  return TRUE;
95 
96  enum_ident_name_check ident_check_status=
97  check_table_name(table_name, strlen(table_name), FALSE);
98  if (ident_check_status == IDENT_NAME_WRONG)
99  {
100  my_error(ER_WRONG_TABLE_NAME, MYF(0), table_name);
101  return TRUE;
102  }
103  else if (ident_check_status == IDENT_NAME_TOO_LONG)
104  {
105  my_error(ER_TOO_LONG_IDENT, MYF(0), table_name);
106  return TRUE;
107  }
108 
109  (void) build_table_filename(path, sizeof(path) - 1, db,
110  table_name, reg_ext, 0);
111 
112  dd_frm_type(thd, path, &db_type);
113 
114  /* Type is unknown if the object is not found or is not a table. */
115  if (db_type == DB_TYPE_UNKNOWN ||
116  !(*table_type= ha_resolve_by_legacy_type(thd, db_type)))
117  {
118  my_error(ER_NO_SUCH_TABLE, MYF(0), db, table_name);
119  return TRUE;
120  }
121 
122  return FALSE;
123 }
124 
125 
141 bool dd_check_storage_engine_flag(THD *thd,
142  const char *db, const char *table_name,
143  uint32 flag, bool *yes_no)
144 {
145  handlerton *table_type;
146 
147  if (dd_frm_storage_engine(thd, db, table_name, &table_type))
148  return TRUE;
149 
150  *yes_no= ha_check_storage_engine_flag(table_type, flag);
151 
152  return FALSE;
153 }
154 
155 
156 /*
157  Regenerate a metadata locked table.
158 
159  @param thd Thread context.
160  @param db Name of the database to which the table belongs to.
161  @param name Table name.
162 
163  @retval FALSE Success.
164  @retval TRUE Error.
165 */
166 
167 bool dd_recreate_table(THD *thd, const char *db, const char *table_name)
168 {
169  bool error= TRUE;
170  HA_CREATE_INFO create_info;
171  char path[FN_REFLEN + 1];
172  DBUG_ENTER("dd_recreate_table");
173 
174  /* There should be a exclusive metadata lock on the table. */
175  DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE, db, table_name,
176  MDL_EXCLUSIVE));
177 
178  memset(&create_info, 0, sizeof(create_info));
179 
180  /* Create a path to the table, but without a extension. */
181  build_table_filename(path, sizeof(path) - 1, db, table_name, "", 0);
182 
183  /* Attempt to reconstruct the table. */
184  error= ha_create_table(thd, path, db, table_name, &create_info, TRUE);
185 
186  DBUG_RETURN(error);
187 }
188