~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

Viewing changes to sql/datadict.cc

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2013-12-22 10:27:05 UTC
  • Revision ID: package-import@ubuntu.com-20131222102705-mndw7s12mz0szrcn
Tags: upstream-5.5.32
Import upstream version 5.5.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
22
/**
 
23
  Check type of .frm if we are not going to parse it.
 
24
 
 
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.
 
28
 
 
29
  @retval  FRMTYPE_ERROR        error
 
30
  @retval  FRMTYPE_TABLE        table
 
31
  @retval  FRMTYPE_VIEW         view
 
32
*/
 
33
 
 
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
 
 
70
/**
 
71
  Given a table name, check type of .frm and legacy table type.
 
72
 
 
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,
 
77
                            otherwise undefined.
 
78
 
 
79
  @return FALSE if FRMTYPE_TABLE and storage engine found. TRUE otherwise.
 
80
*/
 
81
 
 
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_db_name(&db_name))
 
94
  {
 
95
    my_error(ER_WRONG_DB_NAME, MYF(0), db_name.str);
 
96
    return TRUE;
 
97
  }
 
98
 
 
99
  if (check_table_name(table_name, strlen(table_name), FALSE))
 
100
  {
 
101
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table_name);
 
102
    return TRUE;
 
103
  }
 
104
 
 
105
  (void) build_table_filename(path, sizeof(path) - 1, db,
 
106
                              table_name, reg_ext, 0);
 
107
 
 
108
  dd_frm_type(thd, path, &db_type);
 
109
 
 
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)))
 
113
  {
 
114
    my_error(ER_NO_SUCH_TABLE, MYF(0), db, table_name);
 
115
    return TRUE;
 
116
  }
 
117
 
 
118
  return FALSE;
 
119
}
 
120
 
 
121
 
 
122
/**
 
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
 
126
  base table.
 
127
 
 
128
  @pre Any metadata lock on the table.
 
129
 
 
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.
 
135
*/
 
136
 
 
137
bool dd_check_storage_engine_flag(THD *thd,
 
138
                                  const char *db, const char *table_name,
 
139
                                  uint32 flag, bool *yes_no)
 
140
{
 
141
  handlerton *table_type;
 
142
 
 
143
  if (dd_frm_storage_engine(thd, db, table_name, &table_type))
 
144
    return TRUE;
 
145
 
 
146
  *yes_no= ha_check_storage_engine_flag(table_type, flag);
 
147
 
 
148
  return FALSE;
 
149
}
 
150
 
 
151
 
 
152
/*
 
153
  Regenerate a metadata locked table.
 
154
 
 
155
  @param  thd   Thread context.
 
156
  @param  db    Name of the database to which the table belongs to.
 
157
  @param  name  Table name.
 
158
 
 
159
  @retval  FALSE  Success.
 
160
  @retval  TRUE   Error.
 
161
*/
 
162
 
 
163
bool dd_recreate_table(THD *thd, const char *db, const char *table_name)
 
164
{
 
165
  bool error= TRUE;
 
166
  HA_CREATE_INFO create_info;
 
167
  char path[FN_REFLEN + 1];
 
168
  DBUG_ENTER("dd_recreate_table");
 
169
 
 
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,
 
172
                                             MDL_EXCLUSIVE));
 
173
 
 
174
  memset(&create_info, 0, sizeof(create_info));
 
175
 
 
176
  /* Create a path to the table, but without a extension. */
 
177
  build_table_filename(path, sizeof(path) - 1, db, table_name, "", 0);
 
178
 
 
179
  /* Attempt to reconstruct the table. */
 
180
  error= ha_create_table(thd, path, db, table_name, &create_info, TRUE);
 
181
 
 
182
  DBUG_RETURN(error);
 
183
}
 
184