Skip to content

Commit 8eb66bc

Browse files
committed
cleanup: change dd_frm_type() to return the engine name, not legacy_db_type
this simplifies the code and avoids unnecessary conversions back and forth. and it works even if the engine is not installed.
1 parent 1c8d212 commit 8eb66bc

File tree

4 files changed

+41
-35
lines changed

4 files changed

+41
-35
lines changed

sql/datadict.cc

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,27 @@ static int read_string(File file, uchar**to, size_t length)
3939
/**
4040
Check type of .frm if we are not going to parse it.
4141
42-
@param[in] thd The current session.
43-
@param[in] path path to FRM file.
44-
@param[out] dbt db_type of the table if FRMTYPE_TABLE, otherwise undefined.
42+
@param[in] thd The current session.
43+
@param[in] path path to FRM file.
44+
@param[in/out] engine_name table engine name (length < NAME_CHAR_LEN)
45+
46+
engine_name is a LEX_STRING, where engine_name->str must point to
47+
a buffer of at least NAME_CHAR_LEN+1 bytes.
4548
4649
@retval FRMTYPE_ERROR error
4750
@retval FRMTYPE_TABLE table
4851
@retval FRMTYPE_VIEW view
4952
*/
5053

51-
frm_type_enum dd_frm_type(THD *thd, char *path, enum legacy_db_type *dbt)
54+
frm_type_enum dd_frm_type(THD *thd, char *path, LEX_STRING *engine_name)
5255
{
5356
File file;
5457
uchar header[10]; //"TYPE=VIEW\n" it is 10 characters
5558
size_t error;
5659
frm_type_enum type= FRMTYPE_ERROR;
60+
uchar dbt;
5761
DBUG_ENTER("dd_frm_type");
5862

59-
*dbt= DB_TYPE_UNKNOWN;
60-
6163
if ((file= mysql_file_open(key_file_frm, path, O_RDONLY | O_SHARE, MYF(0))) < 0)
6264
DBUG_RETURN(FRMTYPE_ERROR);
6365
error= mysql_file_read(file, (uchar*) header, sizeof(header), MYF(MY_NABP));
@@ -72,17 +74,24 @@ frm_type_enum dd_frm_type(THD *thd, char *path, enum legacy_db_type *dbt)
7274

7375
type= FRMTYPE_TABLE;
7476

75-
/*
76-
This is just a check for DB_TYPE. We'll return default unknown type
77-
if the following test is true (arg #3). This should not have effect
78-
on return value from this function (default FRMTYPE_TABLE)
79-
*/
80-
if (!is_binary_frm_header(header))
77+
if (!is_binary_frm_header(header) || !engine_name)
8178
goto err;
8279

83-
*dbt= (enum legacy_db_type) (uint) *(header + 3);
80+
engine_name->length= 0;
81+
dbt= header[3];
82+
83+
/* cannot use ha_resolve_by_legacy_type without a THD */
84+
if (thd && dbt < DB_TYPE_FIRST_DYNAMIC)
85+
{
86+
handlerton *ht= ha_resolve_by_legacy_type(thd, (enum legacy_db_type)dbt);
87+
if (ht)
88+
{
89+
*engine_name= hton2plugin[ht->slot]->name;
90+
goto err;
91+
}
92+
}
8493

85-
if (*dbt >= DB_TYPE_FIRST_DYNAMIC) /* read the true engine name */
94+
/* read the true engine name */
8695
{
8796
MY_STAT state;
8897
uchar *frm_image= 0;
@@ -110,15 +119,10 @@ frm_type_enum dd_frm_type(THD *thd, char *path, enum legacy_db_type *dbt)
110119
next_chunk+= connect_string_length + 2;
111120
if (next_chunk + 2 < buff_end)
112121
{
113-
uint str_db_type_length= uint2korr(next_chunk);
114-
LEX_STRING name;
115-
name.str= (char*) next_chunk + 2;
116-
name.length= str_db_type_length;
117-
plugin_ref tmp_plugin= ha_resolve_by_name(thd, &name, false);
118-
if (tmp_plugin)
119-
*dbt= plugin_data(tmp_plugin, handlerton *)->db_type;
120-
else
121-
*dbt= DB_TYPE_UNKNOWN;
122+
uint len= uint2korr(next_chunk);
123+
if (len <= NAME_CHAR_LEN)
124+
strmake(engine_name->str, (char*)next_chunk + 2,
125+
engine_name->length= len);
122126
}
123127
}
124128

sql/datadict.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ enum frm_type_enum
3535
Prefer to use ha_table_exists() instead.
3636
To check whether it's an frm of a view, use dd_frm_is_view().
3737
*/
38-
frm_type_enum dd_frm_type(THD *thd, char *path, enum legacy_db_type *dbt);
38+
frm_type_enum dd_frm_type(THD *thd, char *path, LEX_STRING *engine_name);
3939

4040
static inline bool dd_frm_is_view(THD *thd, char *path)
4141
{
42-
enum legacy_db_type not_used;
43-
return dd_frm_type(thd, path, &not_used) == FRMTYPE_VIEW;
42+
return dd_frm_type(thd, path, NULL) == FRMTYPE_VIEW;
4443
}
4544

4645
bool dd_recreate_table(THD *thd, const char *db, const char *table_name,

sql/handler.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5025,14 +5025,16 @@ bool ha_table_exists(THD *thd, const char *db, const char *table_name,
50255025
bool exists= true;
50265026
if (hton)
50275027
{
5028-
enum legacy_db_type db_type;
5029-
if (dd_frm_type(thd, path, &db_type) != FRMTYPE_VIEW)
5028+
char engine_buf[NAME_CHAR_LEN + 1];
5029+
LEX_STRING engine= { engine_buf, 0 };
5030+
5031+
if (dd_frm_type(thd, path, &engine) != FRMTYPE_VIEW)
50305032
{
5031-
handlerton *ht= ha_resolve_by_legacy_type(thd, db_type);
5032-
if ((*hton= ht))
5033+
plugin_ref p= plugin_lock_by_name(thd, &engine, MYSQL_STORAGE_ENGINE_PLUGIN);
5034+
*hton= p ? plugin_hton(p) : NULL;
5035+
if (*hton)
50335036
// verify that the table really exists
5034-
exists= discover_existence(thd,
5035-
plugin_int_to_ref(hton2plugin[ht->slot]), &args);
5037+
exists= discover_existence(thd, p, &args);
50365038
}
50375039
else
50385040
*hton= view_pseudo_hton;

sql/sql_plugin.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,10 +1646,11 @@ int plugin_init(int *argc, char **argv, int flags)
16461646
{
16471647
char path[FN_REFLEN + 1];
16481648
build_table_filename(path, sizeof(path) - 1, "mysql", "plugin", reg_ext, 0);
1649-
enum legacy_db_type db_type;
1650-
frm_type_enum frm_type= dd_frm_type(NULL, path, &db_type);
1649+
char engine_name_buf[NAME_CHAR_LEN + 1];
1650+
LEX_STRING maybe_myisam= { engine_name_buf, 0 };
1651+
frm_type_enum frm_type= dd_frm_type(NULL, path, &maybe_myisam);
16511652
/* if mysql.plugin table is MyISAM - load it right away */
1652-
if (frm_type == FRMTYPE_TABLE && db_type == DB_TYPE_MYISAM)
1653+
if (frm_type == FRMTYPE_TABLE && !strcasecmp(maybe_myisam.str, "MyISAM"))
16531654
{
16541655
plugin_load(&tmp_root);
16551656
flags|= PLUGIN_INIT_SKIP_PLUGIN_TABLE;

0 commit comments

Comments
 (0)